SunLightDesc
The SunLightDesc class represents a directional light descriptor that simulates sunlight. It supports high-quality shadow rendering using Cascaded Shadow Maps (CSM) and works in conjunction with atmospheric scattering simulation to reproduce natural sunlight.
The sun direction is automatically calculated based on view.atmosphere.date, and shadow directions change accordingly.
Common Properties
Section titled “Common Properties”visible
Section titled “visible”Type: boolean | undefined
Description: Controls the visibility of the object.
Default: true
Example:
{ visible: false, sun: { ... }}Properties
Section titled “Properties”Type: object | undefined
Description: Configuration options for the sunlight.
distance
Section titled “distance”Type: number | undefined
Description: Specifies the distance of the sunlight from the target position. The unit is meters.
Default: 300
Example:
{ sun: { distance: 300, }}Type: Color | undefined
Description: Specifies the sunlight color using a Color object. Used when applyColor is true.
Default: new Color().setHex(0xffffff)
Example:
import { Color } from "@navaramap/three";
{ sun: { color: new Color().setHex(0xffffee), }}applyColor
Section titled “applyColor”Type: boolean | undefined
Description: Specifies whether to apply the color directly or use atmospheric scattering calculations. When false, the color is dynamically computed from the atmosphere texture.
Default: false
Example:
{ sun: { applyColor: false, }}intensity
Section titled “intensity”Type: number | undefined
Description: Specifies the intensity of the sunlight. Higher values result in brighter light.
Default: 1
Example:
{ sun: { intensity: 1.0, }}Shadow Properties
Section titled “Shadow Properties”castShadow
Section titled “castShadow”Type: boolean | undefined
Description: Specifies whether to cast shadows using Cascaded Shadow Maps (CSM).
Default: true
Example:
{ sun: { castShadow: true, }}shadowCascadeCount
Section titled “shadowCascadeCount”Type: number | undefined
Description: Specifies the number of shadow cascades. More cascades improve shadow quality distribution but consume more GPU resources.
Default: 4
Example:
{ sun: { shadowCascadeCount: 4, }}shadowMapSize
Section titled “shadowMapSize”Type: number | undefined
Description: Specifies the shadow map resolution (per cascade). Higher values improve shadow quality but consume more GPU memory.
Default: 2048
Example:
{ sun: { shadowMapSize: 2048, }}shadowFar
Section titled “shadowFar”Type: number | undefined
Description: The maximum distance from the camera beyond which shadows are not rendered. The unit is meters.
Default: 50000
Example:
{ sun: { shadowFar: 50000, }}shadowMode
Section titled “shadowMode”Type: "uniform" | "logarithmic" | "practical" | undefined
Description: Defines the splitting scheme for the camera frustum.
"uniform": Linear split distribution"logarithmic": Logarithmic split distribution (suitable for large-scale scenes)"practical": A hybrid approach that balances quality and performance (recommended)
Default: "practical"
Example:
{ sun: { shadowMode: "practical", }}shadowLambda
Section titled “shadowLambda”Type: number | undefined
Description: The lambda parameter for the “practical” split mode. Controls the blend between the uniform (0.0) and logarithmic (1.0) splitting schemes.
Default: 0.8
Example:
{ sun: { shadowLambda: 0.8, }}shadowMargin
Section titled “shadowMargin”Type: number | undefined
Description: Defines the distance at which the shadow camera is placed behind the cascade frustum. Larger values prevent shadow clipping but may reduce precision. The unit is meters.
Default: 5000
Example:
{ sun: { shadowMargin: 5000, }}shadowFade
Section titled “shadowFade”Type: boolean | undefined
Description: Enables smooth transitions between shadow cascades to reduce visible seams.
Default: true
Example:
{ sun: { shadowFade: true, }}shadowIntensity
Section titled “shadowIntensity”Type: number | undefined
Description: Specifies the shadow intensity (0 = no shadow, 1 = full shadow).
Default: 1
Example:
{ sun: { shadowIntensity: 1.0, }}shadowBias
Section titled “shadowBias”Type: number | undefined
Description: Shadow map bias to reduce shadow acne. Similar to THREE.LightShadow.bias.
Default: 0.0001
Example:
{ sun: { shadowBias: 0.0001, }}shadowNormalBias
Section titled “shadowNormalBias”Type: number | undefined
Description: Normal-based shadow bias to reduce shadow acne on surfaces at oblique angles.
Default: 0
Example:
{ sun: { shadowNormalBias: 0, }}debugCSMHelper
Section titled “debugCSMHelper”Type: boolean | undefined
Description: Specifies whether to display debug visualization of shadow cascades.
Default: false
Example:
{ sun: { debugCSMHelper: false, }}Usage Examples
Section titled “Usage Examples”Basic Usage
Section titled “Basic Usage”import ThreeView from "@navaramap/three";import { SunLightDesc } from "@navaramap/three_default_descs";import { DefaultPlugin } from "@navaramap/three_default_plugin";
const view = new ThreeView({ shadow: true // Enable shadows});const plugin = new DefaultPlugin();view.addPlugin(plugin);await view.init();
// Add default photorealistic objects (includes SunLightDesc)const defaultLayers = plugin.addDefaultPhotorealScene();
// Update sunlight settingsdefaultLayers.sun.update({ sun: { castShadow: true, shadowMapSize: 2048, shadowCascadeCount: 4 }});Shadow Quality Adjustment
Section titled “Shadow Quality Adjustment”const sun = view.addLight<SunLightDesc>({ sun: { intensity: 1.0, castShadow: true, shadowCascadeCount: 4, shadowMapSize: 4096, // High quality shadowFar: 50000, shadowMode: "practical", shadowLambda: 0.8, shadowFade: true, shadowIntensity: 1.0, shadowBias: 0.0001, shadowNormalBias: 0 }});Debug Visualization
Section titled “Debug Visualization”// Visualize CSM cascades for debuggingsun.update({ sun: { debugCSMHelper: true }});Dynamic Shadow Control
Section titled “Dynamic Shadow Control”// Integrate with UI control panelconst params = { castShadow: true, shadowIntensity: 1.0, shadowMapSize: 2048};
// Toggle shadows on/offsun.update({ sun: { castShadow: params.castShadow }});
// Adjust shadow intensitysun.update({ sun: { shadowIntensity: params.shadowIntensity }});Applying Custom Color
Section titled “Applying Custom Color”import { Color } from "@navaramap/three";
// Disable atmospheric calculation and use custom colorconst sun = view.addLight<SunLightDesc>({ sun: { applyColor: true, // Use custom color color: new Color().setHex(0xffffee), intensity: 1.0 }});About Cascaded Shadow Maps (CSM)
Section titled “About Cascaded Shadow Maps (CSM)”Cascaded Shadow Maps is a technique for achieving high-quality shadows across wide-ranging scenes:
- Multiple shadow maps: Uses multiple shadow maps based on distance from the camera
- Adaptive resolution: High resolution for near distances, low resolution for far distances
- Cascade fading: Seamless transitions hide seams between cascades
Performance Optimization Tips
Section titled “Performance Optimization Tips”- Adjust cascade count: 3-4 is usually sufficient; more improves quality but increases cost
- Shadow map size: 2048 is standard; 4096 is high quality but heavy
- Limit shadowFar: Only render shadows within the required range
- Choose shadowMode: “practical” is usually optimal
Integration with the Atmosphere System
Section titled “Integration with the Atmosphere System”SunLightDesc works in conjunction with atmospheric scattering simulation:
- Sun direction synchronization: Obtained from
view.atmosphere.sunDirection - Transmittance texture usage: Considers light attenuation through the atmosphere
- Dynamic color computation: When
applyColorisfalse, color is computed from the atmosphere
This enables natural sunlight that responds to time of day and atmospheric conditions.
- To use SunLightDesc, you need to specify
shadow: truewhen initializingThreeView. - CSM uses multiple shadow maps, which impacts GPU memory and performance.
- Increasing
shadowMapSizeincreases memory usage (e.g., 4096 = 16MB/cascade). - To receive shadows on terrain or models,
receiveShadow: truemust be set on the material. - To cast shadows,
castShadow: truemust be set on the object. debugCSMHelpershould only be used during development and disabled in production.