garage44.eu/jeroen/cartopolis

logclone: git clone https://garage44.eu/jeroen/cartopolis.git

.. /docs/notes/cloud-lighting.md

raw


triggers:


Cloud lighting: why the deck is a scattering model, not a shaded surface

The clouds were rewritten on 2026-08-08 from a Lambert-shaded noise field to a single-scattering model with a phase function. The geometry did not change — it is still one static camera-centred shell projecting view rays onto flat planes — and neither did the noise. All of the difference is in the lighting, and this note is why, because the shader can only say what it does.

The governing fact: don't reach for a raymarch

The prior art worth studying here is the generation of real-time skies that were admired specifically for how their clouds caught the light. Reading their shipping shaders, the striking thing is how little of it was cloud geometry: several were projected 2D noise planes, architecturally the same thing we have, and at least one shipped that way after its authors had working volumetrics in hand.

Everything that makes a cloud read as cloud is affordable in a fragment shader. A projected plane with a scattering model beats a volumetric with a surface model, at a fraction of the cost. That is the load-bearing conclusion of the whole exercise, and it is the reason to be suspicious of any future proposal that starts by adding a march through a 3D density field.

But a plane has no sides, and that was the limit (2026-10-17). With the lighting done, the deck still looked like brush strokes: toward the horizon every cloud foreshortened to a sliver, the far deck was a stack of stripes, and nothing had a top to light. The shell now draws the low deck in relief — the same 2D field read as a height map, a flat base with a dome over it, which the view ray climbs in eight steps and two secant refinements (see "The low deck in relief" below). That is a march, but through a height field, not a density field: one coarse 3-octave fbm a step, stopping at the first cloud, lit once. On the M5 Pro node at 1920×1080 the phone tier went 5.8 → 6.0 ms with clouds on; the desktop tier was inside the run-to-run noise. The conclusion above stands for the lighting; it was never an argument for flat geometry.

The five mechanisms, and why each is here

  1. Cloud colour must be an output of the sky model. If the deck carries its own palette it will drift from the sky, and ours did: update_clouds used to hand-author lit_color / shadow_color as CPU-side lerps sitting next to Bevy's physical atmosphere. They now come from utils::sun, which is also what sky::sun_light and free_camera::distance_fog are built from, and the haze colour is read straight off the camera's own DistanceFog.

    "Built the same way as" is not the same as "is", and the gap was visible (2026-08-16). The ambient term survived that clean-up as its own constant — sky_day = (0.38, 0.44, 0.55), assembled out of the right ingredients, in the right shape, next to a comment saying it therefore agreed with the fog. It did not: the fog colour is ~(0.46, 0.52, 0.65) linear, and after the shader's SKY_FLOOR an opaque core landed at ~0.15 — under a third of the horizon it was supposed to recede into. On a fully overcast day that is the entire sky, so the phone drew a mid-grey lid butted against a near-white horizon band, with a hard step between them. Both values live in the same post-exposure linear space (bevy's apply_fog mixes toward DistanceFog::color after exposure, and horizonhaze.wesl paints it un-exposed to match), so there was no unit mismatch to explain it — just two different greys. The deck's ambient is now the camera's fog colour itself, floored at a night value (see below).

    The one place cloud and fog must not agree is night: a cloud base over a city catches its glow from below, and matching the fog's near-black night colour makes the deck darker than the sky behind it — black holes in the star field. CLOUD_NIGHT_FLOOR is that exception, and now the only one.

    The sky floor has to ride coverage. A lone cumulus has a dark base: it is optically deep, lit from above, over ground that bounces little back. A solid overcast has a bright one — it is thinner, and it is the sky, with no brighter sky beside it to be dark against. One constant cannot be both, and the old single 0.35 was the cumulus answer applied to overcast. cloud_sky_floor ramps it over the top of the coverage range (smoothstep(0.55, 1.0)), so 30 % cover looks like 10 % cover and only a genuinely closed sky changes character. It is decided on the Rust side and shipped in the previously-unused alpha of haze_color: it is a lighting decision, and the shader is where shape lives.

  2. The sun must be extincted to the deck's altitude, not the ground's. utils::sun::sun_transmittance_at applies the barometric factor exp(-h/H), H = 8500 m. This is why a sunset deck catches pink and gold while the city under it has gone blue — and it is a colour difference more than a brightness one, because Rayleigh strips blue first and there is less of it left to strip up there. Unit-tested (the_sun_is_brighter_and_bluer_above_the_haze). The usual real-time shortcut is a flat "clouds are above half the air" fudge factor; the barometric term costs the same and is right.

    With bevy's atmosphere running, the colour is the atmosphere's own (2026-09-29, cloud_field::deck_sunlight): its transmittance table, read at the deck's altitude toward the sun, times utils::sun's disc occlusion and the gain (params3.x). Before, a Full-sky sunset put a utils::sun-coloured deck in front of bevy's differently-coloured sky. utils::sun is still the answer wherever there is no atmosphere (the dome sky, CARTO_NO_ATMOSPHERE), and the radii are read through the binding a field at a time — the Adreno 840 misreads a by-value copy of that struct (adreno-atmosphere-struct-copy.md).

  3. The silver lining is a view-dependent phase function. A Henyey-Greenstein dual lobe (g = 0.8 forward, -0.3 back). This is the biggest single contributor to the look. The old shader had no view-dependent term at all; dot(gradient_normal, sun) is a Lambertian surface, and a Lambertian surface is precisely what a cloud is not.

  4. Self-shadowing is a march toward the sun, along sun.xz / sun.y × deck thickness. The cheap version of this — one tap offset by the sun's horizontal direction alone — has a trap worth not inheriting: the offset, and with it all self-shadowing, goes to zero as the sun approaches the zenith. Dividing by sun.y makes the shadow lengthen as the sun drops, which is the behaviour that sells a low sun.

  5. Two decks, at different heights and different wind speeds, so they slide past each other both when the camera translates and when nothing moves at all. One field can never have parallax.

The deck moves with the weather's wind (2026-09-29)

It used to drift at a fixed 126 m/s from one direction, whatever the weather said. It now takes MET's 10 m wind — speed and direction — scaled and veered to the deck's height (sky::deck_wind: twice the speed, 20° clockwise) and sped up four times (CLOUD_TIME_LAPSE), because at its true speed a deck 2.5 km up barely visibly moves. The drift is integrated on the CPU (CloudField) and handed to the shader in metres: velocity × clock in the shader would jump the whole field every time the wind changed. The velocity eases over 8 s, so a new sample turns the clouds rather than snapping them. The grass and the waves read the same surface wind (weather::surface_wind), so all three move one way; without a sample it is a 3 m/s south-westerly.

The water mirrors this deck

water.wesl and the flat map's water past the ring (ground_detail.wesl) find the deck along their reflected ray, through the same cloud_field.wesl the shell draws from, reading its uniforms out of one shared buffer. That is why the field, its octave counts, its sun march and its lighting live in the shared module rather than in clouds.wesl: two copies would drift, and a lake would mirror clouds that are not overhead. water-surface.md has the rest.

Plus two terms from the volumetric-cloud literature, both ALU-only:

Diffusion through the column (2026-10-17)

The octaves above are the first few bounces. Light that has scattered dozens of times diffuses through a cloud, and diffusion falls off as 1/(1 + (1−g)τ), not exponentially. Without it the deck's cores lived on the ambient floor alone: grey bases with a hard step behind a lit rim, and at sunset a muddy brown. Frozen Fractal (Around the World 34: A pretty sky, 2026) hit the same thing and measured it with a Monte Carlo tracer: real multiple scattering makes cloud bottoms brighter and the lighting smoother, and the old look was "metallic".

cloud_field::low_deck_light (shared by the shell and the mirror) adds the diffuse part of the Eddington two-stream transmittance of a conservative slab — everything but what still travels near the incoming direction, which the phase octaves already draw — for two sources:

The column's depth for diffusion is DIFFUSE_DEPTH_LOW = 30 per unit of coverage — a real cumulus is 30–100 — and not OPTICAL_DEPTH_LOW (7), which is tuned as the view ray's opacity and the march's shadow. With that light in, the sky floors came down (0.35 → 0.2 scattered, 0.85 → 0.55 overcast), because part of what they stood in for is now modelled. The sun's diffused part is weighted by sun.y, as a beam spread over a slanted top is: without it a closed deck under a 16 h sun glowed orange. At noon an opaque cumulus core lands at 0.63× the horizon haze and a closed overcast at 0.98× (print_noon_core_bases, an ignored test, prints both; the two tests beside it mirror the shader's constants and must move with them).

On its own, on the old flat deck, this was a small change — the person steering said they could barely see it. It earns its place in the relief, where above (the cloud over the point seen) differs from column (the cloud's thickness): a crown has little above it and is lit, a base has all of it and is grey.

After sunset the base is the lit face. The deck's horizon dips √(2h/R) — 1.6° at 2.5 km — below the street's, so it keeps the sun for minutes after the city has lost it (utils::sun::sun_disc_at), and then the sun is under it: what a viewer below sees is what the column reflects (1 − T of the same slab). That is the red glow on a deck's underside just after sunset.

The deck's sunlight follows eye adaptation (2026-10-17)

The deck's sun term is written in the screen's units at the day exposure, and the camera opens up toward dusk and night by up to 4.5 stops (sky::eye_adaptation_stops: the night and twilight lifts). The sky and the fog colour followed; the deck's lit side did not, so a sunset cloud's lit side was that many stops too dark — the muddy brown decks of the evening. update_clouds now multiplies the sun term by 2^stops. There was an overcast exposure lift beside it, left out of the deck on purpose; since 2026-11-11 there is none (sky.rs, beside NIGHT_EXPOSURE_LIFT): on a screen a full overcast drew the city brighter than a clear noon, so the deck lost the 1.6 stops the lift gave the whole frame.

And it follows the light the sky is drawn with (sky_light_dimming, 2026-10-18). The atmosphere is lit by sun_light_cut's illuminance, which falls with the softened air mass (SUN_EXTINCTION_SOFTNESS); the deck took the 2^stops and not that, so at sunset its lit side was about 4× the sky it hung in and clipped to flat neon orange.

The low deck in relief (2026-10-17)

cloud_field.wesl's relief section and the shell's march. In short:

Before/after sets for all of this were rendered on the M5 Pro node (Groningen, 40 m up, headings S, N and WNW, 12–17.4 h, covers 0.25–1.0).

And aerial perspective: Bevy composites its own over opaque geometry only, so an alpha-blended shell never receives any. The deck now extincts toward the camera's fog colour — the same colour the horizon-haze shell paints. Before, it kept full contrast to the last visible pixel and then had its alpha faded to nothing, which reads as the sky ending.

Dutch light: what the deck is judged against (2026-10-18)

The person steering judges this deck against Dutch Golden Age paintings and photographs of Dutch skies, not against what is physically defensible — "skies are never that red", "clouds never that solid dark", "too pink". The references used, all public domain on Wikimedia Commons: Cuyp, The Maas at Dordrecht (evening light); van der Neer, River Landscape at Sunset and Moonlit Landscape … Kostverloren (dusk, night); Ruisdael, View of Haarlem with Bleaching Grounds and van Goyen, View of Dordrecht 1651 (day); and the photographs Sunset over Groningen (again), A Dutch polder and Lutjegast Doezum. What they agree on:

What each finding changed, and why:

Still wrong, and not the clouds' doing: the sky bevy draws is greyer than the references — at noon sRGB ~(87, 110, 143) at the top of the frame against a photograph's deep blue, and at sunset ~(123, 129, 143) against (94, 127, 195). Tried and set aside: the tonemapper (AcesFitted is bluer by day than TonyMcMapface, no help at sunset), a smaller TWILIGHT_EXPOSURE_LIFT (1.5: the sky only got darker, not bluer), and ozone (ScatteringMedium::default() has it). The next place to look is the medium and the light's illuminance curve, not the cloud shader.

The tone curve is Khronos PBR Neutral since 2026-11-06 (quality::TONEMAPPING), and that moved the sky most of the way. Under Tony the noon zenith was greyed, the shade lifted and the brick paled. Under Khronos the sky is bluer and the frame's contrast is close to a sunny photograph's (luma standard deviation 0.21 against 0.19; Tony's was 0.135). Its toe is darker, though: dusk and night needed a toe lift (since folded into the light meter, below). Into a 10° sun the frame is brighter than before (mean luma 0.79 against 0.69), because Khronos holds highlights where Tony rolled them off. At 17.6 h the zenith now reads faintly violet, worth watching against the "no mauve" finding above.

After sunset the deck is grey, not black (2026-11-07). Once the atmosphere's sun stopped being dimmed twice (the-atmosphere-from-the-street-to-orbit.md), the sky after sunset came out a stop brighter and lavender, and every cloud in it black: the deck's fill is the fog colour, which faded by e every 1.5° below the horizon, and the blue sky above it faded with daylight. Both were tuned to the old, darker sky. The fog now fades by e every 6° (free_camera::DUSK_FADE_Y) and the sky above does not fade; at 17.6 h toward the set sun a cloud measured sRGB (112, 116, 133) against the sky's (92, 85, 103), blue-grey and a little brighter, as in the photograph Sunset over Groningen (again) ((117, 143, 197) against (93, 126, 192)). At night CLOUD_NIGHT_FLOOR went up tenfold, since the night is exposed about four stops brighter: at the old value every cloud was a black hole in a navy sky.

The exposure has been a light meter since 2026-11-07 (exposure-and-adaptation.md): the swap to Khronos had left the city a quarter stop dark by day and half as bright at night, and the time-of-day lifts it was tuned with are gone. Still open: at 17.6 h the cloud undersides go near-black under Khronos where Tony greyed them. Frame time did not move with the curve, on the desktop or the phone rung (perf.toml street, city, earth).

The white flicker is unconfirmed. Suspected: sun_rays.wesl scaled every beam by the plain mean of eight taps round the sun, and a crisp, forward-lit cloud edge crossing one tap moved the whole frame. It now takes their geometric mean. The shot harness could not show it either way: a scripted capture holds the scene clock, so ten captures in a row are identical, and --flicker's pair differs by whole drifted clouds.

docs/cloud-shots.toml is the sweep all of this was judged on: Groningen 40 m up, toward and away from the sun, noon to 22 h at 50 % cover. On the Deck (midas take gpu) it takes about five minutes; use CARTO_DATA_DIR set to an empty directory there, or the captures inherit the bot's settings.

Rejected, with reasons

Constraints that shape the code

Verifying a change here

cargo shots / --shot with --time is the A/B: the whole point of the model is that it responds to sun elevation and azimuth, so sweep --time 6 / 12 / 18 at a fixed --clouds, and sweep the heading too — the phase function means looking toward and away from the sun should not look the same.

Two limits. Colour, exposure and lighting are not evidence on the CI software rasteriser (headless-shots-software-renderer.md), and this model changes nothing else — so a green CI run says nothing about whether it looks right. And the WGSL is compiled at runtime, so nothing in cargo check or cargo test touches it. It can be validated offline, which is worth doing before handing a shader change to a workstation:

cargo install naga-cli   # once
{ printf 'struct VertexOutput { @builtin(position) position: vec4<f32>,\n@location(0) world_position: vec4<f32> };\nstruct View { world_position: vec3<f32> };\n@group(0) @binding(0) var<uniform> view: View;\n'
  grep -v '^#import' assets/shaders/clouds.wesl; } > /tmp/c.wgsl
naga /tmp/c.wgsl

That stubs the two bevy_pbr imports, so it validates our code — types, control flow, swizzles — but not the import surface. It catches the errors that would otherwise show up as a black sky on someone else's machine.

Note that rustfmt is not installable via rustup in the container — /usr/local/rustup is root-owned and the component download fails. Fetch the matching component tarball from static.rust-lang.org and run the binary with LD_LIBRARY_PATH pointed at the toolchain's lib/.

Sources