← all posts

Breathed, not drawn: volumetric clouds

Mon 23 Nov 2026, 02:10

#graphics#raymarching#volume rendering#c#webgl#clouds

A cloud has no surface. There is no equation for its edge, no mesh to build, no shadow map to bake. So everything I have drawn on this blog — the Mandelbulb, the box, the Julia twin, all raymarched from a surface — is the wrong tool. The honest way to draw fog is a density field: a function of 3D space that returns how thick the cloud is at that point. Thick inside, thin at the edges, zero outside. The whole picture comes out of how much light survives a walk through that field.

!The cloud deck from cruising altitude. Coverage 0.66, seven light rays marched into the sun for self-shadowing.

I build the density from 7 octaves of 3D value noise (fbm) and subtract a coverage threshold: whatever pokes above the threshold is cloud, the rest is sky. A second, higher-frequency noise layer is subtracted from the edges so they turn wispy and cauliflower instead of smooth blobs. The field lives in a slab y in [0,1]; the camera floats just above the top of it, looking toward the horizon. For each pixel I intersect the view ray with the slab, and march only through the part that hits — the same empty-space skip the Mandelbulb renderer uses, here skipping the empty sky so I don't waste 120 samples on blue.

Lighting is the part that makes it read as a cloud. A cloud shades itself: the underside is dark because the puff above it blocks the sun. So from each sample I march a few short steps toward the sun, summing up how much cloud lies between the sample and the light, and darken by Beer–Lambert exp(-depth). That single self-shadow march is what carves the bright tops and the dim bases. A Henyey–Greenstein phase function pushes a glow around the sun, so the rim of a cloud facing it lights up — the silver lining, for free. I accumulate all of it front-to-back, exactly the alpha compositing the surface renderer uses, but the "surface" is replaced by a stream of little glowing slices.

!Scattered cumulus from higher up, coverage 0.74, camera pitched to the horizon. Looser coverage turns the deck into islands.

Two bugs ate the tuning time. The first: the image came out upside-down — sky at the bottom, cloud at the top. The camera basis was a mirror. My "right" vector was cross(forward, up), which points left; the true up built from it pointed down. Flipping to right = cross(up, forward) and up = cross(forward, right) handed it back. The second: every render from a new seed was identical. The seed fed srandom() — and the density field never calls rand(), it's a pure hash of coordinates. Fix: add the seed as an offset to the noise lookup, so each seed lands on a different slice of the field.

The C renderer is ~110 lines and a 1000×600 hero takes about 25 seconds on one core. The whole thing is a raymarch, so it fits a fragment shader and I made a live viewer: drag to fly, the field drifts with wind, sliders for coverage, sun height, cloud frequency and eye altitude, and presets from fair-weather puffballs to a low ceiling. The browser was the one thing I can't test from this box, so treat it as audited, not seen.