Overview
Threebox provides realistic sunlight simulation using the SunCalc library to calculate accurate sun positions based on date, time, and coordinates. This enables:- Dynamic directional lighting that follows the sun
- Atmospheric sky layer with accurate sun position
- Terrain/ground layer with sun-based opacity
- Automatic light intensity based on sun altitude
setSunlight
Updates the sun light position and intensity based on date/time and coordinates.The date and time to calculate sun position for. Defaults to current time.
Geographic coordinates for sun calculation. Defaults to
map.getCenter().
Can be an object with lng/lat properties or a [longitude, latitude] array.realSunlight: true in Threebox constructor options.
Example:
Behavior
WhensetSunlight() is called:
- Sun Position Calculation: Calculates azimuth and altitude using SunCalc
- Directional Light: Positions and orients the main directional light
- Light Intensity: Sets intensity based on sun altitude (0 when below horizon)
- Hemisphere Light: Updates ambient hemisphere light intensity
- Mapbox Light: Updates Mapbox’s built-in lighting to match
- Sky Layer: Updates atmospheric sky if enabled
- Ground/Terrain: Updates terrain opacity if enabled
Light Properties Updated
Position calculated from sun azimuth and altitude
Intensity =
Math.max(sin(altitude), 0) (0-1 range)Intensity =
Math.max(sin(altitude), 0.1) (minimum 0.1)Mapbox Light Synchronization
The method also updates Mapbox’s native lighting:getSunSky
Calculates sun position for the sky atmospheric layer.Date/time for calculation. Defaults to current time if omitted.
Pre-calculated sun position. If omitted, calculates from date and map center.
[sunAzimuth, sunAltitude] - Array with azimuth and altitude in degrees.
Sun azimuth in degrees (0-360)
Sun altitude in degrees (0-90)
updateSunSky
Updates the Mapbox sky atmospheric layer with new sun position.Sun position array from
getSunSky() with azimuth and altitude in degreessky: true in Threebox constructor options.
Example:
Sky Layer
The sky atmospheric layer is created with:updateSunGround
Updates terrain/ground layer opacity based on sun altitude.Sun position object from
getSunPosition() with azimuth and altitude in radiansterrain: true option).
Example:
Opacity Calculation
The ground layer opacity is calculated as:- Minimum opacity: 0.25 (night)
- Maximum opacity: 1.0 (day)
- Gradual transition based on sun altitude
Supporting Methods
getSunPosition
Calculates sun position using SunCalc library.Date and time for calculation
Geographic coordinates
{azimuth, altitude} - Sun position in radians
Sun azimuth in radians (-π to π)
Sun altitude in radians (-π/2 to π/2)
getSunTimes
Calculates sun times (sunrise, sunset, etc.) for a given date and location.Date for calculation
Geographic coordinates
Sunrise (top edge of sun appears on horizon)
Sunrise ends (bottom edge touches horizon)
Morning golden hour ends
Solar noon (sun at highest position)
Evening golden hour starts
Sunset starts
Sunset (sun disappears below horizon)
Dusk (evening nautical twilight starts)
Nautical dusk
Night starts
Darkest moment (sun lowest position)
Night ends
Nautical dawn
Dawn (morning civil twilight starts)
Complete Example
Notes
- Sun calculations use the SunCalc library (https://github.com/mourner/suncalc)
- Azimuth is measured from north (0°) clockwise
- Altitude is measured from horizon (0°) to zenith (90°)
- Light intensity automatically becomes 0 when sun is below horizon
- The
realSunlightHelperoption shows a visual indicator of light direction - Sky and terrain layers update automatically when
setSunlight()is called - Performance:
setSunlight()includes caching to avoid redundant calculations