Advanced shaders · lesson 9 of 21 · Advanced · about 7 min
Clouds 4: a cloud deck for a 3D world
You will build: Draw a cloud deck over a ground horizon: find where each upward ray crosses the slab (y 1.5 to 3.0), march 48 steps, light every sample with a 5-step march towards the sun, add the forward-scattering glow, and fade distant clouds into the horizon haze.
Clouds 1 to 3 showed the idea. Now the real thing: the sky of a 3D game world. Stand on the ground, look at the horizon, and see a cloud deck: a thick, flat slab of cloud that stretches far away, with flat dark bases, bright billowing tops and a horizon that melts into haze. Games and demos like Sebastian Lague's Clouds project render exactly this, by ray marching a slab of 3D noise.
1. The cloud lives in a slab, not in the whole sky
The clouds exist only between two heights: here y = 1.5 and y = 3.0. A ray from the camera (which is below the slab) enters through the bottom plane and leaves through the top plane. Where? A plane is easy: the ray reaches height y0 after travelling (y0 - ro.y) / rd.y.
float a = (y0 - ro.y) / rd.y; // distance to the bottom of the slab float b = (y1 - ro.y) / rd.y; // distance to the top of the slab float tin = min(a, b), tout = max(a, b);
Looking straight up, the ray crosses the slab quickly. Looking near the horizon, rd.y is tiny and the ray crosses the slab very far away: a long way through cloud, which is what makes clouds look huge and flat near the horizon. We limit the distance (min(tout, 60.0)) and march a fixed 48 steps between the two ends, so near rays get fine steps and distant rays get coarse ones. Only rays that go upwards are drawn: below the horizon it is ground.
2. Shape: flat bottom, round top
Inside the slab, density is 3D noise (fbm3) cut off at a threshold, so only the high parts of the noise become cloud. Multiply by a height profile: 0 at the bottom edge, quickly up to 1, then fading out over the top half. That gives a flat base (the typical cumulus look) and softly rounded tops.
3. Light: a short march towards the sun
From every sample inside the cloud, march a second short ray towards the sun (5 steps), add up the density on the way, and use Beer's law: the light that gets through is exp(-optical depth). Keep a floor so the shadows are never black: light bounces around inside a cloud.
float od = 0.0;
float3 lp = pos;
for (int j = 0; j < 5; j++) { lp += sun * 0.35; od += cdens(lp); }
float light = 0.25 + 0.75 * exp(-od * 1.2);
4. The silver lining
Droplets scatter light mostly forward. The Henyey-Greenstein function hg(cosTheta, g) models this: cosTheta = dot(rd, sun) is 1 when you look straight at the sun. Put the sun in front of you, low over the horizon, and the thin edges of the clouds around it light up with a bright rim.
5. Atmosphere: haze and the horizon
The air itself scatters light, so far away things fade into a pale blue-white. Blend the cloud's colour towards the horizon color by 1 - exp(-distance * 0.03). Do the same for the ground. A sky colour that goes from pale at the horizon to deep blue overhead, plus a glow around the sun, finishes the picture: now it is a place, not a texture.
The numbers you need
Everything the picture depends on is given, so the challenge is the code, not guessing.
- Camera
p = (uv - 0.5) * float2(aspect, 1.0) * 2.0, ro = float3(0.0, 0.0, time * 0.6), rd = normalize(float3(p.x, p.y + 0.18, 1.2))- Slab
ry = max(rd.y, 0.015); hit = layerHit(ro, float3(rd.x, ry, rd.z), 1.5, 3.0); t0 = max(hit.x, 0.0); t1 = min(hit.y, 60.0); stepLength = max(t1 - t0, 0.0) / 48.0 (48 steps, sample at t0 + (i + 0.5) * stepLength, using the same ray with ry)- Density
cdens(q): h = (q.y - 1.5) / 1.5; shape = smoothstep(0.0, 0.15, h) * (1.0 - smoothstep(0.55, 1.0, h)); n = fbm3(float3(q.x * 0.28 + time * 0.05, q.y * 0.5, q.z * 0.28)); result saturate((n - 0.44) * 4.0) * shape- Sun
sun = normalize(float3(0.4, 0.35, 0.85))- Light march
inside the cloud (d > 0.01): 5 steps of 0.35 towards the sun, od = sum of cdens; light = 0.25 + 0.75 * exp(-od * 1.2)- Color
lit = lerp(float3(0.5, 0.57, 0.72), float3(1.0, 0.95, 0.85), light); phase = 0.5 + 0.1 * hg(dot(rd, sun), 0.6)- Step
a = 1.0 - exp(-d * stepLength * 2.0); L += T * a * lit * phase; T *= 1.0 - a (L starts at 0, T at 1)- Haze
haze = float3(0.78, 0.86, 0.95); fog = 1.0 - exp(-t0 * 0.03); L = lerp(L, haze * (1.0 - T), fog)- Sky
lerp(haze, float3(0.25, 0.48, 0.85), saturate(pow(ry, 0.6))) + float3(1.0, 0.85, 0.6) * pow(saturate(dot(rd, sun)), 64.0) * 0.35; final sky * T + L- Ground
below the horizon (rd.y <= 0) draw lerp(haze, float3(0.22, 0.3, 0.2), saturate(-rd.y * 6.0)) instead of all of the above
Try it in your own language
The lesson is written once and shown in every language. Pick yours: Unity (Built-in), Unity (URP), Unreal, Shadertoy / WebGL, OpenGL (LWJGL, raylib), three.js, GameMaker, LÖVE (Lua), Godot, Apple (iOS, macOS), WebGPU / Bevy.
Start coding