DevLog #9 – The sun is half a degree wide. Our shadows finally know it

In DevLog #7 our campfires learned to cast shadows. This one is about the light that casts all the others: the sun.

Fire gave us a clean rule: a few lights near the camera are allowed to cast shadows, and everything else glows without them. The sun is harder. It lights the whole map, it moves all day, the weather changes it, and the camera looking at it can be 18 metres above a single rider or 75 metres above half a battlefield. One shadow setting can’t be right for all of that. This devlog covers how we got from “Unity’s default shadows” to shadows that behave like a 0.53° disc in the sky.

sun shadows hero
Mission 1, a little after four in the afternoon, sun about 11° up. Every shadow is sharp where it touches its caster and soft at the far end.

Part one: putting the shadow map where the camera actually looks

A shadow map is a picture of the scene taken from the sun. Its resolution is fixed, so the real question is how much ground you stretch it over. Stretch it too far and every texel is a fat square smeared across the grass. Stretch it too little and shadows end in a hard line halfway up the screen.

The usual answer is a fixed shadow distance, which suits a third-person camera. Ours tilts and zooms. Fully zoomed in, the far edge of the screen might be 40 metres away. Zoomed out with the horizon in view, it’s hundreds. So the shadow distance is no longer a setting. Every frame, ShadowFrustumFitter finds the farthest ground the camera can see and sets the shadow distance to that, plus a 12 % margin and 8 metres:

  • Distance = farthest visible ground × 1.12 + 8 m, capped at 200 m and scaled by the quality preset.
  • Quantised to 4 m steps with hysteresis. Without this, every scroll of the mouse wheel moved the cascades slightly, and the shadow edges crawled.
  • Cascade splits use the “practical” scheme (λ = 0.7), measured from the nearest visible ground, not from the camera. On an RTS camera the first 18 metres in front of the lens are air. Spending a cascade on them is wasted resolution.
  • Bias is left alone. URP already scales it per cascade texel, so the fitter only moves distance and splits.

All of this runs on a runtime clone of the pipeline asset, so nothing the game does at runtime ever writes over the project’s settings on disk.

The same pass brought in the smaller options. Unit shadows can be Off, Blob (a cheap soft disc under each unit) or Projected (the real silhouette). Ambient occlusion has Off, Low and High. And the fire budget from DevLog #7 is here too: 0, 2 or 6 point lights with shadows, depending on the preset.

sun shadows cascades
Cascades tinted for debugging, one colour per cascade. At the closest zoom (left) the fitter pulls the shadow distance in to 60 m. Zoomed out (right) it stretches to 144 m.

Part two: the sun is not a point

This is where the good shadows come from, and it’s simple geometry.

Seen from the ground, the sun is a disc about 0.53° wide. Each edge of a blocker therefore casts not one shadow line but a fan of them, one from every point on that disc. Where the fan hits the ground, you get a gradient instead of a line: the penumbra. Its width depends on only one thing, the distance the light travels from the blocker’s edge to the ground:

sun penumbra geometry
About one centimetre of blur for every metre of gap. Where the shadow touches its caster, the gap is zero and so is the blur.

That one line explains things people notice without being able to name. A fence post’s shadow is crisp. A tree’s shadow is crisp at the trunk and soft at the crown. And everything goes softer toward sunset, because a low sun makes the light’s path from blocker to ground much longer. A 12 m ombú at 30° of sun elevation has a gap of about 24 m at its crown. At 10° that grows to 69 m, and the penumbra goes from 22 cm to 64 cm.

Then the RTS question: can our camera even show this? At 1080p, one pixel in the middle of the screen covers about 2 cm of ground at the closest zoom and about 7 cm at the farthest. Plot the penumbra against those two lines and the answer splits in two:

sun penumbra vs pixels
Trees and buildings land well above one pixel, so their soft edges are visible. A rider’s head lands right at one pixel. These two problems need two different fixes.

  • Big things (trees, the casona, a cabildo, a wall) have penumbrae many pixels wide. A uniform blur gets them wrong in both directions: too soft at the base, too sharp at the tip. They need blur that varies with distance.
  • Small things (people, horses, fence posts, the legs of a cart) have penumbrae under a pixel. Blur doesn’t matter for them. What matters is that the shadow actually touches them. In a shadow map stretched over a battlefield, a texel can be wider than a soldier’s boot, so the shadow starts a little way off the foot and the unit seems to hover above the grass.

PCSS for the big things

We wrote our own renderer feature, ByzenScreenSpaceShadows. It resolves the sun’s shadow once per frame into URP’s screen-space shadow texture. Our materials, including the wet-ground shader from the weather devlogs, already read that texture, so no material had to change.

For each pixel it uses percentage-closer soft shadows (PCSS):

  • search the shadow map around the pixel for blockers and average how far above the pixel they are;
  • turn that gap into a penumbra width with the 0.53° rule above;
  • filter the shadow over exactly that width.

The base of the ombú stays sharp and its crown goes soft, without anyone tuning anything per object. It also blends across cascade boundaries, so the line where one cascade hands over to the next no longer shows as a step in shadow quality.

sun shadows pcss before afterSame frame, sun at 11°. Left: Off, URP’s own soft shadows. Right: PCSS. Look at the tips of the crown’s shadow, and at the grass, which picks up its own contact shading.

Clouds widen the sun

A clear sky gives the 0.53° disc. Under cloud, the light no longer comes from a disc. It comes from a bright, blurry patch of sky, so shadows lose their edges long before they disappear. The weather system already knows the cloud cover, so we pass it in, and the effective size of the sun grows up to ×4 under full overcast. It’s an approximation, not a simulation of scattering. But it fixed the effect that bothered us most: before, a grey storm afternoon in Byzen had shadows as crisp as noon in January.

sun shadows cloudsSame hour, same sun angle. Under heavy cloud (cover 0.85) the penumbra grows 3.6× and the shadows dissolve into soft pools.

Contact shadows for the small things

For units, the fix happens at screen resolution. After the shadow map is resolved, a screen-space test steps a short way from each pixel toward the sun through the depth buffer, looking for anything that sticks up in the way. It’s cheap because the distance is short, and it’s precise because it works in pixels, not shadow-map texels. That’s what puts a dark line where a boot meets the grass, under a horse’s hooves and around the base of a fence post, at exactly the scale where the shadow map gives up.

sun shadows contactNoon, closest zoom. Without contact shadows (left), the logs and the people around the fire sit a little off the grass. With them (right), they rest on it.

Four settings, and what each one costs you

All of this sits under one option in Graphics, Shadow detail, which the presets set like this:

  • Low: Off. URP’s own shadows, nothing on top.
  • Medium: Contact only. Units stand on the ground again, which matters most in an RTS.
  • High: PCSS. Contact shadows plus shadows that soften with distance.
  • Ultra: PCSS ultra. The highest-quality version of the same pass.

The order is deliberate. If a machine can afford only one of the two, it gets contact shadows. A blurry tree reads as “low settings”. A soldier floating above the grass reads as “broken”.

sun shadows presetsThe same frame at each Shadow detail level. Only that option changes.

Two traps we fell into

Shader stripping. URP compiles the screen-space shadow path only if the build believes something uses it. If Unity’s own Screen Space Shadows feature isn’t on the renderer, the build strips those shader variants. Our pass then draws a perfect shadow texture that no material reads, and in the editor everything looks fine. The fix is unglamorous: register Unity’s feature so the variants survive, and turn it off from code at runtime so ours does the work.

Old settings files. The first time the new option ran on a real install, it came up Off. The saved settings predated the key, so the loader filled the gap with the “nothing” value instead of the preset’s value. A missing setting now falls back to Contact, the Medium value, so an old install at least gets its units standing on the ground. It’s an easy bug to ship, because on the machine of whoever wrote the feature, the key always exists.

What’s still missing

  • Firelight shadows are still hard-edged shadow maps. PCSS covers only the sun. A torch is small and close, so its penumbrae are naturally tight, but they aren’t right.
  • We don’t publish GPU cost yet. The passes are verified on GPU, but we haven’t profiled them on enough machines to give numbers we’d stand behind. When we do, they’ll go in a performance devlog.
  • Contact shadows can’t see behind things. They work from the depth buffer, so anything hidden behind something else from the camera’s point of view doesn’t count. From a camera looking down, this rarely shows. When it does, it’s at the edge of a roof.