14 minutes
Night and Headlights
The clock has not moved since lesson 1.4. It still reads 16:12. Start it at the scene's speed and the sun sets 43 s after you load. Six seconds later it is night, and not one car on the road has a light.
The dark brings two jobs. The one light in the sky has to change from sun to moon without a jolt. And every car needs headlights, on a renderer that can only afford a few real lights.
Sun and moon
The clock drives two lights, the Sun and the Ambient fill. There is no moon light. When the sun goes down, the Sun entity's light becomes the moon, and dayLightAt decides each frame which of the two it is:
sh is the sun's height from lesson 1.4, and it is a height, not the light's angle. sunToward points at the sun, but its height part never drops under 0.12 and its z gets a fixed lean of 0.35. So at sunset, when sh reaches 0, the light still comes down at 8°. The moon gets one fixed direction, high and to the side.
Each light's strength comes from the smoothstep from lesson 1.5, a ramp that starts and ends gently. With our scene's numbers:
Continue reading
Unlock the Full Course
Every lesson, the runnable examples, and the finished build — yours to keep.