# Shadow
When the light from the light source cannot reach an object due to the occlusion of other objects, the object is in shadow. Shadows allow the observer to obtain the spatial position and proportional relationship between objects, and the depth and realism of scenes and objects are improved.
# Shadow map
Based on actual application scenarios, currently only real-time shadows of the main directional light are supported. First of all, let's first understand the working idea of shadow mapping: rendering is based on the position of the light source, and the places that cannot be seen are in the shadows, otherwise they will be normally illuminated by the light. In this rendering, the depth buffer is recorded in a map. When the scene is rendered with real lighting, the surface closest to the light can be traced through this depth map.
# Cascading shadow
The basic shadow map has texture accuracy problems in large scenes:
- Insufficient accuracy of shadow map, resulting in aliasing
- The shadow map pixel coverage corresponding to the scene near the camera is relatively low
This causes the shadows near the camera to be more jagged than the far end, which is called perspective error The common method to solve the perspective error is to cascade shadow maps:
- Divide the frustum into multiple parts, and render shadow maps separately for each part
- The part close to the camera covers a smaller area, while the far end covers a wider area
- Objects close to the camera generate higher texture quality than shadows of distant objects
# Shadow configuration
In the MeshRenderer panel, there are options to set whether to accept shadows and cast shadows: You can also set whether the shadow is turned on in the script code:
const meshRenderer = entity.getComponent(engine.MeshRenderer);
meshRenderer.castShadow = true;
meshRenderer.receiveShadow = false;
In the camera panel, we can configure the relevant properties of the shadow:
Properties | Features |
---|---|
shadowMode | Toggle multi-level shadow |
cascadedSplits | When using multi-level shading, the division ratio of each level |
shadowFitMode | Frustum calculation mode when rendering shadows |
shadowDistance | Objects within the maximum distance will cast shadows |