Procedural Planets

Integrated Sebastian Lague's Planet Generator
Created Tour function in SectorRenderer which makes the camera follow each terrestrial planet in turn
This commit is contained in:
Reinier Vens 2020-12-05 23:43:02 +00:00
commit 53a91351b7
332 changed files with 11904 additions and 78 deletions

View file

@ -0,0 +1,191 @@
Shader "Hidden/Atmosphere"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
}
SubShader
{
// No culling or depth
Cull Off ZWrite Off ZTest Always
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
#include "../Includes/Math.cginc"
//
struct appdata {
float4 vertex : POSITION;
float4 uv : TEXCOORD0;
};
struct v2f {
float4 pos : SV_POSITION;
float2 uv : TEXCOORD0;
float3 viewVector : TEXCOORD1;
};
v2f vert (appdata v) {
v2f output;
output.pos = UnityObjectToClipPos(v.vertex);
output.uv = v.uv;
// Camera space matches OpenGL convention where cam forward is -z. In unity forward is positive z.
// (https://docs.unity3d.com/ScriptReference/Camera-cameraToWorldMatrix.html)
float3 viewVector = mul(unity_CameraInvProjection, float4(v.uv.xy * 2 - 1, 0, -1));
output.viewVector = mul(unity_CameraToWorld, float4(viewVector,0));
return output;
}
float2 squareUV(float2 uv) {
float width = _ScreenParams.x;
float height =_ScreenParams.y;
//float minDim = min(width, height);
float scale = 1000;
float x = uv.x * width;
float y = uv.y * height;
return float2 (x/scale, y/scale);
}
sampler2D _BlueNoise;
sampler2D _MainTex;
sampler2D _BakedOpticalDepth;
sampler2D _CameraDepthTexture;
float4 params;
float3 dirToSun;
float3 planetCentre;
float atmosphereRadius;
float oceanRadius;
float planetRadius;
// Paramaters
int numInScatteringPoints;
int numOpticalDepthPoints;
float intensity;
float4 scatteringCoefficients;
float ditherStrength;
float ditherScale;
float densityFalloff;
float densityAtPoint(float3 densitySamplePoint) {
float heightAboveSurface = length(densitySamplePoint - planetCentre) - planetRadius;
float height01 = heightAboveSurface / (atmosphereRadius - planetRadius);
float localDensity = exp(-height01 * densityFalloff) * (1 - height01);
return localDensity;
}
float opticalDepth(float3 rayOrigin, float3 rayDir, float rayLength) {
float3 densitySamplePoint = rayOrigin;
float stepSize = rayLength / (numOpticalDepthPoints - 1);
float opticalDepth = 0;
for (int i = 0; i < numOpticalDepthPoints; i ++) {
float localDensity = densityAtPoint(densitySamplePoint);
opticalDepth += localDensity * stepSize;
densitySamplePoint += rayDir * stepSize;
}
return opticalDepth;
}
float opticalDepthBaked(float3 rayOrigin, float3 rayDir) {
float height = length(rayOrigin - planetCentre) - planetRadius;
float height01 = saturate(height / (atmosphereRadius - planetRadius));
float uvX = 1 - (dot(normalize(rayOrigin - planetCentre), rayDir) * .5 + .5);
return tex2Dlod(_BakedOpticalDepth, float4(uvX, height01,0,0));
}
float opticalDepthBaked2(float3 rayOrigin, float3 rayDir, float rayLength) {
float3 endPoint = rayOrigin + rayDir * rayLength;
float d = dot(rayDir, normalize(rayOrigin-planetCentre));
float opticalDepth = 0;
const float blendStrength = 1.5;
float w = saturate(d * blendStrength + .5);
float d1 = opticalDepthBaked(rayOrigin, rayDir) - opticalDepthBaked(endPoint, rayDir);
float d2 = opticalDepthBaked(endPoint, -rayDir) - opticalDepthBaked(rayOrigin, -rayDir);
opticalDepth = lerp(d2, d1, w);
return opticalDepth;
}
float3 calculateLight(float3 rayOrigin, float3 rayDir, float rayLength, float3 originalCol, float2 uv) {
float blueNoise = tex2Dlod(_BlueNoise, float4(squareUV(uv) * ditherScale,0,0));
blueNoise = (blueNoise - 0.5) * ditherStrength;
float3 inScatterPoint = rayOrigin;
float stepSize = rayLength / (numInScatteringPoints - 1);
float3 inScatteredLight = 0;
float viewRayOpticalDepth = 0;
for (int i = 0; i < numInScatteringPoints; i ++) {
float sunRayLength = raySphere(planetCentre, atmosphereRadius, inScatterPoint, dirToSun).y;
float sunRayOpticalDepth = opticalDepthBaked(inScatterPoint + dirToSun * ditherStrength, dirToSun);
float localDensity = densityAtPoint(inScatterPoint);
viewRayOpticalDepth = opticalDepthBaked2(rayOrigin, rayDir, stepSize * i);
float3 transmittance = exp(-(sunRayOpticalDepth + viewRayOpticalDepth) * scatteringCoefficients);
inScatteredLight += localDensity * transmittance;
inScatterPoint += rayDir * stepSize;
}
inScatteredLight *= scatteringCoefficients * intensity * stepSize / planetRadius;
inScatteredLight += blueNoise * 0.01;
// Attenuate brightness of original col (i.e light reflected from planet surfaces)
// This is a hacky mess, TODO: figure out a proper way to do this
const float brightnessAdaptionStrength = 0.15;
const float reflectedLightOutScatterStrength = 3;
float brightnessAdaption = dot (inScatteredLight,1) * brightnessAdaptionStrength;
float brightnessSum = viewRayOpticalDepth * intensity * reflectedLightOutScatterStrength + brightnessAdaption;
float reflectedLightStrength = exp(-brightnessSum);
float hdrStrength = saturate(dot(originalCol,1)/3-1);
reflectedLightStrength = lerp(reflectedLightStrength, 1, hdrStrength);
float3 reflectedLight = originalCol * reflectedLightStrength;
float3 finalCol = reflectedLight + inScatteredLight;
return finalCol;
}
float4 frag (v2f i) : SV_Target
{
float4 originalCol = tex2D(_MainTex, i.uv);
float sceneDepthNonLinear = SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, i.uv);
float sceneDepth = LinearEyeDepth(sceneDepthNonLinear) * length(i.viewVector);
float3 rayOrigin = _WorldSpaceCameraPos;
float3 rayDir = normalize(i.viewVector);
float dstToOcean = raySphere(planetCentre, oceanRadius, rayOrigin, rayDir);
float dstToSurface = min(sceneDepth, dstToOcean);
float2 hitInfo = raySphere(planetCentre, atmosphereRadius, rayOrigin, rayDir);
float dstToAtmosphere = hitInfo.x;
float dstThroughAtmosphere = min(hitInfo.y, dstToSurface - dstToAtmosphere);
if (dstThroughAtmosphere > 0) {
const float epsilon = 0.0001;
float3 pointInAtmosphere = rayOrigin + rayDir * (dstToAtmosphere + epsilon);
float3 light = calculateLight(pointInAtmosphere, rayDir, dstThroughAtmosphere - epsilon * 2, originalCol, i.uv);
return float4(light, 1);
}
return originalCol;
}
ENDCG
}
}
}

View file

@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: e5c9adc680af74e8fb0b8d63fb518c0a
ShaderImporter:
externalObjects: {}
defaultTextures: []
nonModifiableTextures: []
userData:
assetBundleName:
assetBundleVariant:

Binary file not shown.

View file

@ -0,0 +1,104 @@
fileFormatVersion: 2
guid: 8886a14b14a1b44089f789c0b8c05643
TextureImporter:
internalIDToNameTable: []
externalObjects: {}
serializedVersion: 11
mipmaps:
mipMapMode: 0
enableMipMap: 0
sRGBTexture: 1
linearTexture: 0
fadeOut: 0
borderMipMap: 0
mipMapsPreserveCoverage: 0
alphaTestReferenceValue: 0.5
mipMapFadeDistanceStart: 1
mipMapFadeDistanceEnd: 3
bumpmap:
convertToNormalMap: 0
externalNormalMap: 0
heightScale: 0.25
normalMapFilter: 0
isReadable: 0
streamingMipmaps: 0
streamingMipmapsPriority: 0
grayScaleToAlpha: 0
generateCubemap: 6
cubemapConvolution: 0
seamlessCubemap: 0
textureFormat: 1
maxTextureSize: 2048
textureSettings:
serializedVersion: 2
filterMode: -1
aniso: -1
mipBias: -100
wrapU: -1
wrapV: -1
wrapW: -1
nPOTScale: 1
lightmap: 0
compressionQuality: 50
spriteMode: 0
spriteExtrude: 1
spriteMeshType: 1
alignment: 0
spritePivot: {x: 0.5, y: 0.5}
spritePixelsToUnits: 100
spriteBorder: {x: 0, y: 0, z: 0, w: 0}
spriteGenerateFallbackPhysicsShape: 1
alphaUsage: 0
alphaIsTransparency: 0
spriteTessellationDetail: -1
textureType: 0
textureShape: 1
singleChannelComponent: 0
maxTextureSizeSet: 0
compressionQualitySet: 0
textureFormatSet: 0
applyGammaDecoding: 0
platformSettings:
- serializedVersion: 3
buildTarget: DefaultTexturePlatform
maxTextureSize: 2048
resizeAlgorithm: 0
textureFormat: -1
textureCompression: 1
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
androidETC2FallbackOverride: 0
forceMaximumCompressionQuality_BC6H_BC7: 0
- serializedVersion: 3
buildTarget: Standalone
maxTextureSize: 2048
resizeAlgorithm: 0
textureFormat: -1
textureCompression: 1
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
androidETC2FallbackOverride: 0
forceMaximumCompressionQuality_BC6H_BC7: 0
spriteSheet:
serializedVersion: 2
sprites: []
outline: []
physicsShape: []
bones: []
spriteID:
internalID: 0
vertices: []
indices:
edges: []
weights: []
secondaryTextures: []
spritePackingTag:
pSDRemoveMatte: 0
pSDShowRemoveMatteOption: 0
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,124 @@
Shader "Hidden/Ocean"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
}
SubShader
{
// No culling or depth
Cull Off ZWrite Off ZTest Always
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
#include "../Includes/Math.cginc"
#include "../Includes/Triplanar.cginc"
struct appdata {
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f {
float4 pos : SV_POSITION;
float2 uv : TEXCOORD0;
float3 viewVector : TEXCOORD1;
};
v2f vert (appdata v) {
v2f output;
output.pos = UnityObjectToClipPos(v.vertex);
output.uv = v.uv;
// Camera space matches OpenGL convention where cam forward is -z. In unity forward is positive z.
// (https://docs.unity3d.com/ScriptReference/Camera-cameraToWorldMatrix.html)
float3 viewVector = mul(unity_CameraInvProjection, float4(v.uv * 2 - 1, 0, -1));
output.viewVector = mul(unity_CameraToWorld, float4(viewVector,0));
return output;
}
float4 colA;
float4 colB;
float4 specularCol;
float depthMultiplier;
float alphaMultiplier;
float smoothness;
sampler2D waveNormalA;
sampler2D waveNormalB;
float waveStrength;
float waveNormalScale;
float waveSpeed;
sampler2D _MainTex;
sampler2D _CameraDepthTexture;
float4 params;
float planetScale;
float3 oceanCentre;
float oceanRadius;
float3 dirToSun;
fixed4 frag (v2f i) : SV_Target
{
fixed4 originalCol = tex2D(_MainTex, i.uv);
float3 rayPos = _WorldSpaceCameraPos;
float viewLength = length(i.viewVector);
float3 rayDir = i.viewVector / viewLength;
float nonlin_depth = SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, i.uv);
float sceneDepth = LinearEyeDepth(nonlin_depth) * viewLength;
float2 hitInfo = raySphere(oceanCentre, oceanRadius, rayPos, rayDir);
float dstToOcean = hitInfo.x;
float dstThroughOcean = hitInfo.y;
float3 rayOceanIntersectPos = rayPos + rayDir * dstToOcean - oceanCentre;
// dst that view ray travels through ocean (before hitting terrain / exiting ocean)
float oceanViewDepth = min(dstThroughOcean, sceneDepth - dstToOcean);
if (oceanViewDepth > 0) {
float3 clipPlanePos = rayPos + i.viewVector * _ProjectionParams.y;
float dstAboveWater = length(clipPlanePos - oceanCentre) - oceanRadius;
float t = 1 - exp(-oceanViewDepth / planetScale * depthMultiplier);
float alpha = 1-exp(-oceanViewDepth / planetScale * alphaMultiplier);
float4 oceanCol = lerp(colA, colB, t);
float3 oceanSphereNormal = normalize(rayOceanIntersectPos);
float2 waveOffsetA = float2(_Time.x * waveSpeed, _Time.x * waveSpeed * 0.8);
float2 waveOffsetB = float2(_Time.x * waveSpeed * - 0.8, _Time.x * waveSpeed * -0.3);
float3 waveNormal = triplanarNormal(rayOceanIntersectPos, oceanSphereNormal, waveNormalScale / planetScale, waveOffsetA, waveNormalA);
waveNormal = triplanarNormal(rayOceanIntersectPos, waveNormal, waveNormalScale / planetScale, waveOffsetB, waveNormalB);
waveNormal = normalize(lerp(oceanSphereNormal, waveNormal, waveStrength));
//return float4(oceanNormal * .5 + .5,1);
float diffuseLighting = saturate(dot(oceanSphereNormal, dirToSun));
float specularAngle = acos(dot(normalize(dirToSun - rayDir), waveNormal));
float specularExponent = specularAngle / (1 - smoothness);
float specularHighlight = exp(-specularExponent * specularExponent);
oceanCol *= diffuseLighting;
oceanCol += specularHighlight * (dstAboveWater > 0) * specularCol;
//return float4(oceanSphereNormal,1);
float4 finalCol = originalCol * (1-alpha) + oceanCol * alpha;
return float4(finalCol.xyz, params.x);
}
return originalCol;
}
ENDCG
}
}
}

View file

@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: d03ae1df4c5324c888d341830e3620ad
ShaderImporter:
externalObjects: {}
defaultTextures: []
nonModifiableTextures: []
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,53 @@
Shader "Hidden/Test"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
}
SubShader
{
// No culling or depth
Cull Off ZWrite Off ZTest Always
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
};
struct v2f
{
float4 vertex : SV_POSITION;
};
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
return o;
}
float myMultiplier;
float myValue;
sampler2D _MainTex;
fixed4 frag (v2f i) : SV_Target
{
float mySum = 0;
for (int i = 0; i < 10; i ++) {
mySum += myValue * myMultiplier;
}
return mySum;
}
ENDCG
}
}
}

View file

@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: 416fbda6abed2402d93abace9deb725c
ShaderImporter:
externalObjects: {}
defaultTextures: []
nonModifiableTextures: []
userData:
assetBundleName:
assetBundleVariant: