Drawing day and night on a 3D globe: the shader behind my world clock
Every world clock I've used is a table. You read "Tokyo +9, São Paulo −3" and you still don't feel who's asleep. I wanted the opposite: look at the Earth, see the daylight band, and know instantly whether it's a reasonable hour to ping someone. That became Timetate — a world clock and meeting planner where the main interface is a 3D globe with a day/night terminator that tracks the real sun. This…
Every world clock I've ever used is a physical table. You read "Tokyo +9, São Paulo −3" and still don't know who is asleep. I wanted something different: a 3D globe showing daylight and darkness, so you can instantly determine if it's appropriate to contact someone. That idea became Timetate - a world clock and meeting planner with a 3D globe interface. This post explains how the daylight/nighttime terminator is drawn and why the initial approach was incorrect.
The globe consists of three concentric spheres, each with its own purpose: Earth, clouds, and atmosphere. The Earth sphere uses a shader to create the day/night terminator. The cloud sphere is a textured shell with a slightly higher radius and additive blending for an outer rim glow. The atmosphere sphere is rendered with a slightly larger radius and back-facing sides to create a rim effect around the planet. All textures are 1024px WebP maps: day, city lights, clouds, specular, and normal.
To determine the position of the sun, we calculate the subsolar point using the current date and time. This calculation considers declination (north/south position) and subsolar longitude (east/west position). The resulting sun direction vector is then applied to the Earth shader. Clouds rotate slowly to simulate cloud movement.
There is one crucial detail that initially caused issues: normal vectors. The vertex shader provides two normals: view space and world space. Initially, the renderer used the view space normal, which led to incorrect shading. Switching to world space normals fixed the problem, resulting in a proper day/night terminator on the globe.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.