DevLog #6 – We replaced our sun, and the first sunrise came in at five times noon

Byzen has had a day/night cycle for a while, and the part that decides the game has always been solid. The sun is computed for Montevideo’s real latitude (−34.9°) and the day of the year, so in the southern hemisphere it sits in the north at noon, winter days are short and shadows run long. One number, daylight from 0 to 1, drives everything the player feels. At night units see 55% as far and ranged fire does 70% of its damage. Melee is untouched on purpose, so fighting up close gets relatively better in the dark. And light sources carve out islands where vision comes back.

The picture was the weak half. Colours and intensities were interpolated by hand between keyframes, and it showed. Last month we handed the image to a physical atmosphere model: Rayleigh and Mie scattering, ozone absorption and second-order scattering. The sun, the moon, the sky, the ambient light, reflections, fog and exposure now all come out of that one model instead of being painted separately.

Six captures of the same base on 18 May: 09:00, noon, 16:00, sunset, blue hour and night Mission 1, 18 May 1811, the day of Las Piedras. Same camera, six hours. At noon the sun is due north, so shadows fall south.

Who owns what

The rule was that gameplay must not move by a single number. So the new system draws, and the old one keeps deciding:

  • One clock. The game calendar is still the only source of date and time. The atmosphere model never advances time on its own; every frame it’s told what time it is. A jump of more than 15 game minutes (a console command, loading a save) counts as a cut, so exposure snaps instead of slowly adapting.
  • One sun. The model’s orbit is configured with our latitude and our solar declination, which makes it the same formula as the gameplay sun. A test checks both against each other to 0.05°, across five dates and 48 hours. The sun you see is the sun that decides who can see whom.
  • Gameplay math unchanged. Vision, aiming and the day phases use the same static API as before. Fog of war, guard AI, combat and the HUD weren’t touched.
  • Weather enters as physics. Clouds go in as cloud cover, fog as fog density, and the storm darkening as an exposure compensation (more on that below). The weather system no longer dims the lights itself.

We checked the model against published illuminance values. At sunset it gives 997 lux against a reference of about 930. At the end of civil twilight, 6.45 against 6. At the end of nautical twilight, 0.0084 against 0.004. A moonless night, 0.0018 against 0.001. With a full moon at midnight, 0.26 lux, where the real figure is 0.1 to 0.3. Close enough that when something looked wrong, it was usually something else being wrong.

The moon is always full and always opposite the sun. That’s a simplification, and a deliberate one: with a real 29.5-day cycle, a night mission that happened to fall on a new moon would be literally black. The moon is also a real second light, not a tint on the sun, because a switched-off sun casts no shadows, and without them the terrain at night flattens into a painted board.

Bug one: the grass couldn’t see the north

The first noon we rendered with the new lighting had no shadows. Not faint ones: none. The buildings, the trees and the terrain all looked like an overcast day under a clear sky.

The same noon frame before and after fixing the grass normal map: no shadows, then sharp shadows falling south Same frame, same sun. The only change is one import setting on one texture.

We went down the obvious list, turning off the terrain, the sun’s shadows, the extra lights and the moon, and the answer turned out to be one texture. The grass normal map on mission 1’s main terrain layer had been imported as a colour texture. Read as colour, it gets gamma-decoded, and every normal in the field gets pushed off-axis in the same direction. The ground effectively leaned away from the sun whenever the sun came from the north or the east. In the southern hemisphere that’s the whole morning and all of noon.

The old hand-tuned ambient had hidden it, because it lit the ground from everywhere at once. A physically based ambient doesn’t forgive that. It was the only terrain layer with the problem. There may be other mis-imported textures elsewhere in the project, but under this lighting they won’t stay hidden for long.

Bug two: a second sun

The main scene has a directional light in it, from long before this system existed, and it stayed alive during missions. With the old cycle nobody could tell. With the physical one it was a second sun, fixed in the south-east, with no exposure applied to it, lighting the night like midday. The cycle now switches off every directional light that isn’t its own sun or moon while a mission runs, and gives them back when it unloads.

Bug three: storms lit up the night

This one took two attempts, and the first attempt is the useful part.

A clear night, a stormy night before the fix (pale and bright) and the same storm after the fix Mean frame brightness, 0–255, measured on the captures. The storm made the night 3.4 times brighter than a clear one.

Mean brightness across the frame: 24 on a clear night, 82 in a thunderstorm. The diagnosis looked obvious. The model has a virtual eye that adapts to the light, so a darker sky makes it open up, and a storm ends up looking almost the same as clear weather. The fix that goes with that diagnosis is an exposure compensation driven by the weather: from −1.75 EV at midday to −4.75 EV at night, per unit of storm darkening. At this storm that meant going from −0.9 EV to −3.3.

The frame went from 82 to 86. Brighter.

The problem wasn’t exposure at all. What was lighting that frame wasn’t lit. The low clouds, the shadow side of the volumetric clouds and the rain are all drawn with fixed colours, because they’re effects, not surfaces. At night they formed a pale veil over a dark field, and no physical light model can darken something it doesn’t light. The fix is to scale everything unlit by the square root of the perceived brightness, with a floor of 0.35 on the rain so you can still read that it’s raining. The frame dropped to 35: darker than a clear night by the numbers you’d expect, and the storm still reads.

Bug four: the sunrise

The note from playtesting was short: the light changes at dawn are too harsh. We measured it, logging the sun’s direct intensity every two game minutes at the default speed of one real minute per game hour.

Charts of direct sunlight and frame brightness during sunrise, before and after the horizon ramp Left: the sun’s direct light. Right: what the player actually sees. Both plotted from Play-mode logs and the captures.

The sun’s direct light went from 0 to 5 within the first fifth of a degree above the horizon, which is about one real second. At 2.5° it reached 6.7, five and a half times the noon sun. Long shadows snapped in at 3°. It felt like someone had flipped a switch on the landscape.

It’s the virtual eye again, and this time the diagnosis was right. With the sun below the horizon the ground gets very little light, so exposure climbs. When the sun appears, everything facing it gets that full exposure multiplied by fresh direct light. Physically defensible, and it reads like a light switch.

The first fix we tried kept the physics and changed what the eye measures: meter the exposure against light hitting sun-facing surfaces as well, not just the ground. It lowered the peak. It also made the ground (which is most of the frame in an RTS) get darker as the sun came up, from 76 to 57. A sunrise that dims the world is worse than a harsh one. We took it out.

What shipped is a small patch to the package. Direct light from the sun and moon ramps in with a smoothstep between 0° and 20° of altitude, and the ramp is included when the exposure is metered, so the eye follows the light rather than fighting it. Shadows now fade in by 8° instead of 3°. The sun grows from 0 to 2.4 over about an hour of game time, peaking at 12° at twice the noon value, and the frame brightens from 54 to 72 without a single dip. Sunset is symmetric, so the golden hour got softer too. Three tests pin it: the sun enters slowly, its low-angle peak stays under 65% of the unramped one, and the ground never gets darker between dawn and noon.

Fitting URP to a camera that never sees the sky

The package came with a reference configuration: 500 m shadow distance, four cascades, 4096 shadow maps, 64-bit HDR. Copying it would have been easy, and wrong. Our camera is tilted 52°, has a 60° field of view and flies between 18 and 75 m. The top edge of the frame looks 22° below the horizon, so the sky never appears. The farthest point you can see is about 173 m away, and 42 m when you’re zoomed all the way in.

  • Shadows go as far as the player’s graphics setting allows, 150 m at most. 500 m would be shadows nobody sees.
  • The cascade split sits at 45 m, which is exactly what you see at full zoom.
  • Forward became Forward+. Forward capped every object at four lights, so torches flickered as units walked between them.
  • Tonemapping stays Neutral, not the package’s ACES, because all our art was approved under Neutral, and changing the tonemapper changes the colour of every surface in the game at once. That’s why our exposure key is 2.5 instead of 6.
  • HDR stays at 32 bits. Banding shows up in skies, and we don’t have one on screen.
  • The package’s fullscreen fog pass is installed but off. It draws before transparent objects, so water and fire would come out unfogged. We drive Unity’s built-in fog with the physically computed colour of each hour instead.

Nights you can play

Torches, braziers and campfires are scaled by how open the virtual eye is, times a gain of 4. At midday that comes out near zero, because a campfire at noon doesn’t compete with the sun. By day a fire is sold by the heat shimmer above it, at night by its light. At night they come up to full. We picked the 4 from captures: at 1, the islands of light that the gameplay depends on were barely visible in a physically dark night.

Midnight: the base in darkness, units lit by their torches and a campfire glowing in the field Midnight under a full moon: about 0.26 lux. Vision recovers inside each pool of light, fully at its centre, and stays at 55% everywhere else. Overlapping lights take the maximum, not the sum, so a camp with twenty fires doesn’t cancel the night for the whole map.

Two smaller things turned up along the way:

  • Night vision. The blue shift and desaturation of the eye in the dark had to be added to the weather’s colour-grading volume, not a new one. Two global volumes don’t add up: the one with higher priority simply overrides the other, and the weather’s grade would have erased it.
  • Lightning at night. Lightning used to brighten the sun. At night the sun is off, so brightening it lit nothing. It now adds ambient light directly.

We also found and patched a bug in the package itself. After a time jump, its multiple-scattering pass kept the previous hour’s ambient, so the night sky came out inflated: 0.064 lux at −12° instead of 0.008. The eye adapted to that fake brightness, and the actual night went nearly black for several seconds. It now restarts the scattering after a jump and iterates until it converges, at most eight times.

Cost: about 0.3 ms of CPU. The sky is recomputed at most ten times a second, and the sky’s reflection probe is 128 px, updated one face at a time.

Still wrong

  • The blue hour is brown. You can see it in the 17:30 frame at the top. The ambient drifts to a reddish brown with the package’s default haze. It’s a tuning job, not a bug.
  • A midday storm doesn’t darken the frame. The exposed light drops from 2.3 to 0.8, but storm fog is physically bright, so a heavy-rain noon averages 84 against 79 on a clear one. If we want a moodier storm, we’ll lean harder on the exposure compensation.
  • The watchtower doesn’t cast a shadow at night. The design says it should. Shadows from lights other than the sun are still switched off in our URP settings.
  • Night lightning is unverified. The code is there, but strikes are random and we haven’t caught one on camera yet.
  • Cutscenes still write their own ambient and fog and need to learn to talk to the cycle.