005 — Sky Gradient, Time of Day and Ambient Light from the Sky

2026-11-05 → 2026-11-06

The background was a flat grey. It is now a sky: a gradient from zenith to horizon to ground, that changes with the time of day, and that also lights the scene. Bluish shadows against a warm sun is the cheapest effect for the "painted" look I'm after — the sun comes in the next step, the sky had to be there first.

A full day in 60 seconds, four views of the same scene in one window:

Night → dawn → noon → dusk → night. The view from below shows the zenith color, the view from above mostly the ground color. There is still no sun: what tints the ruins is only the sky.

What was implemented

  • Four keyframes, always there: night, dawn, noon, dusk. ARAL has defaults, the game only replaces them — no add / clear / counter. Beyond four, skies will come as textures later.
  • One keyframe = three linear HDR colors (zenith, horizon, ground), an intensity, the sharpness of the horizon and of the ground transitions, and a time in [0, 1] (0 = midnight, time = hour / 24): the moment this gradient is reached at 100 %.
  • setTimeOfDay(t): the game decides the speed (a 10-minute day, a 3-minute evening…). A static sky is just a frozen t. Between dusk and night the blend wraps through 1 → 0.
  • The CPU blends, the GPU draws one sky: the two keyframes around t are mixed on the CPU (~40 lerps a frame) and the result goes at the end of the frame UBO. No extra binding: same update rate as the UBO, already bound everywhere.
  • The sky pass: a full-screen triangle at depth 1, drawn after the opaque objects with LESS_OR_EQUAL, no depth write: thanks to early-Z, it only costs the pixels where the sky is actually visible. The gradient follows dir.z (InGa is Z-up). It is drawn even when nothing else is.
  • Ambient light in spherical harmonics (L2, 9 RGB coefficients), rather than a simple sky / ground hemisphere: the horizon color shows on vertical faces, and the same format will be reused as-is by the light probes later.
    • computed on the CPU, per keyframe, only when a keyframe changes: numeric integration of the gradient (~64×32 directions weighted by solid angle), then irradiance convolution
    • each frame: the 9 coefficients of the two keyframes are blended (exact, SH are linear)
  • Normals in the vertex shader: world normal = cofactor of the 3×3 (handles non-uniform scale) × sign(det) for mirrored objects; the fragment shader does albedo · evalSH(n). Faces looking up are tinted by the sky, faces looking down by the ground, walls by the horizon.

Usage

Inga::SSkyKeyframe dusk;
renderer->getSkyKeyframe(Inga::ESkyKeyframe::eDUSK, &dusk);
dusk.horizon = {1.0f, 0.30f, 0.10f};   // redder horizon
dusk.time    = 0.78f;                  // 18:43
renderer->setSkyKeyframe(Inga::ESkyKeyframe::eDUSK, dusk);

renderer->setTimeOfDay(t);             // t in [0, 1], driven by the game

The setters are virtual on IRenderer: no-op on SOLID, the game never casts to ARAL. A keyframe whose time is not strictly between its neighbours is refused with an error — no silent sorting. Changing mood per biome = applying the four keyframes again when entering the zone.

And at the console, to tune by eye:

inga:timeOfDay 1 0.25
inga:sky 1 dusk horizon 1.0 0.3 0.1
inga:sky 1 noon intensity 1.2
inga:skyPrint 1

inga:skyPrint writes the four keyframes in the log as C++, ready to paste into main.cpp.

How it was tested

  • the sky shows no banding (tonemap dithering from devlog 003);
  • faces up tinted sky, faces down tinted ground, walls tinted horizon;
  • the SH were checked against a uniform sky first (constant color → constant ambient);
  • a keyframe with an invalid time is refused, and the console says so, not only the log.

Cost: gpu.aral.sky 11 µs on the RX 6800, so ~130 µs estimated on the Steam Deck with the ×12 factor from the previous post. Not measured on the Deck yet: the next step rewrites the fragment shader anyway.

Little problems

  • Same trap as the frustum lines. The view direction comes from invViewProj; at z = 1 (far plane), w ≈ 7·10⁻⁴ and the sign gets unstable in float. The direction uses z = 0.5 instead.
  • Blending exponents ≠ blending two gradients. The CPU mixes the sharpness values, not the two computed gradients: the result is slightly different, but continuous, and only during a transition.

Tested on

AMD RX 6800 (RADV), Linux / Wayland.

Next step

Step 13: PBR and directional lights (4 max).