// The ending's Earth: a scorched, dried-out ball that turns green from wherever the seeds come down. Each seed is a cap on // the sphere (its centre in the ball's own space, and how far it has spread, in radians) with a ragged, glowing front; // clouds and the sea's shine come back with the green. Lit by its own sun, so it reads the same wherever it is put. Shader "GrassCutter/EarthGlobe" { Properties { _DryTex ("Dry", 2D) = "white" {} _GreenTex ("Green", 2D) = "white" {} _SunDir ("Sun (world)", Vector) = (0, 0, 1, 0) _AirColor ("Air", Color) = (0.8, 0.55, 0.3, 1) _Life ("Life", Range(0, 1)) = 0 _SeedCount ("Seeds", Float) = 0 } SubShader { Tags { "RenderType" = "Opaque" "Queue" = "Geometry" "RenderPipeline" = "UniversalPipeline" } Pass { Name "Earth" Tags { "LightMode" = "UniversalForward" } HLSLPROGRAM #pragma vertex vert #pragma fragment frag #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl" TEXTURE2D(_DryTex); SAMPLER(sampler_DryTex); TEXTURE2D(_GreenTex); SAMPLER(sampler_GreenTex); CBUFFER_START(UnityPerMaterial) float4 _DryTex_ST, _GreenTex_ST; float4 _SunDir; float4 _AirColor; float _Life; float _SeedCount; CBUFFER_END // xyz: the seed's centre on the ball (object space, unit), w: how far it has spread (radians) float4 _Seeds[12]; // x: how bright the seed's front still is (it goes out once the patch has done spreading) float4 _SeedFx[12]; struct Attributes { float4 positionOS : POSITION; float3 normalOS : NORMAL; }; struct Varyings { float4 positionCS : SV_POSITION; float3 dirOS : TEXCOORD0; float3 normalWS : TEXCOORD1; float3 positionWS : TEXCOORD2; }; Varyings vert(Attributes v) { Varyings o; o.positionWS = TransformObjectToWorld(v.positionOS.xyz); o.positionCS = TransformWorldToHClip(o.positionWS); o.normalWS = TransformObjectToWorldNormal(v.normalOS); o.dirOS = v.positionOS.xyz; return o; } float hash(float3 p) { p = frac(p * 0.3183099 + 0.1); p *= 17.0; return frac(p.x * p.y * p.z * (p.x + p.y + p.z)); } float vnoise(float3 x) { float3 i = floor(x); float3 f = frac(x); f = f * f * (3.0 - 2.0 * f); return lerp(lerp(lerp(hash(i + float3(0, 0, 0)), hash(i + float3(1, 0, 0)), f.x), lerp(hash(i + float3(0, 1, 0)), hash(i + float3(1, 1, 0)), f.x), f.y), lerp(lerp(hash(i + float3(0, 0, 1)), hash(i + float3(1, 0, 1)), f.x), lerp(hash(i + float3(0, 1, 1)), hash(i + float3(1, 1, 1)), f.x), f.y), f.z); } float fbm(float3 p) { float s = 0, a = 0.5; for (int k = 0; k < 5; k++) { s += a * vnoise(p); p = p * 2.03 + 17.1; a *= 0.5; } return s; } // the map is equirectangular; the derivatives come from whichever of two seams is further away, so the // line where the longitude wraps never shows float3 SampleMap(TEXTURE2D_PARAM(tex, smp), float2 uv, float2 dx, float2 dy) { return SAMPLE_TEXTURE2D_GRAD(tex, smp, uv, dx, dy).rgb; } float4 frag(Varyings i) : SV_Target { float3 d = normalize(i.dirOS); float lon = atan2(d.z, d.x); float lat = asin(clamp(d.y, -1, 1)); float2 uv = float2(lon / (2 * PI) + 0.5, lat / PI + 0.5); float2 uv2 = float2(frac(uv.x + 0.5) - 0.5, uv.y); float2 dx = ddx(uv), dy = ddy(uv), dx2 = ddx(uv2), dy2 = ddy(uv2); if (abs(dx2.x) + abs(dy2.x) < abs(dx.x) + abs(dy.x)) { dx = dx2; dy = dy2; } float3 dry = SampleMap(TEXTURE2D_ARGS(_DryTex, sampler_DryTex), uv, dx, dy); float3 green = SampleMap(TEXTURE2D_ARGS(_GreenTex, sampler_GreenTex), uv, dx, dy); // how green this spot is, and how close it is to a front that is still moving float wob = fbm(d * 7.0) - 0.5; float wob2 = fbm(d * 23.0 + 5.3) - 0.5; float g = 0, front = 0; for (int s = 0; s < 12; s++) { if (s >= (int)_SeedCount) break; float4 sd = _Seeds[s]; if (sd.w <= 0) continue; float ang = acos(clamp(dot(d, sd.xyz), -1, 1)); // ragged at the front; a seed spread past 4.7 has covered the whole ball whatever the wobble float reach = sd.w * (1 + wob * 0.55 + wob2 * 0.18); float e = reach - ang; g = max(g, smoothstep(0, 0.035, e)); front = max(front, exp(-abs(e) / 0.011) * _SeedFx[s].x * smoothstep(0, 0.02, sd.w)); } float water = saturate((green.b - max(green.r, green.g * 0.8)) * 4.0); float3 albedo = lerp(dry, green, g); float3 n = normalize(i.normalWS); float3 sun = normalize(_SunDir.xyz); float3 view = normalize(GetCameraPositionWS() - i.positionWS); float ndl = dot(n, sun); float light = smoothstep(-0.12, 0.55, ndl) * 1.05 + 0.035; // clouds come back over the green; the dead ball only has thin streaks of dust float3 cd = d; float cs = _Time.y * 0.004; cd = float3(cd.x * cos(cs) - cd.z * sin(cs), cd.y, cd.x * sin(cs) + cd.z * cos(cs)); float cn = fbm(cd * 4.5 + float3(0, cd.x * 2.0, 0)); float cloud = smoothstep(0.56, 0.78, cn) * (0.15 + 0.85 * g) * _Life * 0.85; float dust = smoothstep(0.55, 0.8, fbm(cd * float3(3, 9, 3) + 11.0)) * (1 - g) * 0.25; float3 col = albedo * light; // the sea's shine float3 h = normalize(sun + view); float spec = pow(saturate(dot(n, h)), 60) * water * g * saturate(ndl * 4); col += spec * float3(1, 0.95, 0.85) * 0.6; col = lerp(col, float3(0.6, 0.43, 0.28) * light, dust); col = lerp(col, float3(1, 1, 1) * (light * 0.95 + 0.03), cloud); // the air seen edge on float rim = pow(1 - saturate(dot(n, view)), 3.0); col += _AirColor.rgb * rim * saturate(ndl * 0.8 + 0.45) * 0.9; // the moving front glows, day side or night; softer out at sea col += float3(0.55, 1.0, 0.3) * front * (0.35 + 0.65 * (1 - water)) * 1.1; return float4(col, 1); } ENDHLSL } } }