TA Toolbox Home / TA Glossary

Shadow Mapping: Principles, Algorithms, and Optimization

2026-08-24

What is Shadow Mapping

Shadow mapping is a real-time shadow generation technique widely used in games and real-time rendering. The basic idea is to render the scene's depth information from the light's perspective into a texture (shadow map), then in the main rendering pass, transform each pixel to light space and compare its depth with the depth in the shadow map. If the pixel's depth is greater than the shadow map depth, the pixel is in shadow.

Principles of Shadow Mapping

Shadow mapping typically involves two passes:

  1. **Shadow Pass**: Render the scene from the light's position, writing only depth, to obtain the shadow map. For directional lights, use orthographic projection; for point lights, use perspective projection or cube maps (six faces).
  2. **Main Rendering Pass**: Render the scene normally. In the fragment shader, transform world coordinates to light space, sample the shadow map, and compare depths. If the current fragment's depth is greater than the corresponding depth in the shadow map (plus bias), the fragment is in shadow, and lighting is attenuated.
  3. Common Problems

    Shadow Acne

    Due to limited depth buffer precision, surfaces can be incorrectly self-shadowed, causing stripes or spots. Solution: Use depth bias and normal offset to add a small offset when comparing depths.

    Peter Panning

    Excessive bias causes shadows to detach from objects, making shadows float. Adjust the bias amount.

    Shadow Aliasing

    Limited shadow map resolution leads to jagged edges. Solution: Use Percentage Closer Filtering (PCF) for soft shadows, or increase shadow map resolution.

    Shadow Map Precision

    Shadow maps use depth textures, which have limited precision; distant areas may suffer from Z-fighting. Use Cascaded Shadow Maps (CSM) to improve precision for directional lights in large scenes.

    Optimization Methods

    • **Cascaded Shadow Maps (CSM)**: Divide the view frustum into multiple regions, each using a different resolution shadow map to improve near shadow quality.
    • **Tight Light Frustum**: Adjust the light projection range to tightly enclose the scene, improving shadow map utilization.
    • **Use Lower Resolution Shadow Maps**: For distant or small objects, reduce shadow map resolution.
    • **Shadow Distance Limit**: Do not render shadows beyond a certain distance.
    • **Cache Static Shadows**: Bake shadows for static objects into lightmaps.

    Application in Engines

    • **Unity**: Uses Shadow Map technology, supports CSM, soft shadows, shadow distance, etc.
    • **Unreal**: Uses Shadow Map and Ray Traced Shadows, supports various optimizations.

    Frequently Asked Questions

    What is the difference between shadow mapping and ray traced shadows?

    Shadow mapping is an approximate method based on depth comparison, with precision issues; ray traced shadows are accurate but have high performance cost.

    Why does shadow mapping need depth bias?

    Depth bias avoids self-shadowing (shadow acne) because depth buffer precision is limited.

    How do point lights generate shadow maps?

    Point lights typically use cube shadow maps, rendering depth from six directions, or use single-pass dual-paraboloid mapping.

    How to optimize shadows on mobile?

    On mobile, use low-resolution shadow maps, limit shadow distance, use simple hard shadows, or disable shadows.

    What is CSM?

    Cascaded Shadow Maps divide the view frustum into multiple regions, each with different resolution shadow maps, balancing shadow quality near and far.

FAQ

What is the difference between shadow mapping and ray traced shadows?

Shadow mapping is an approximate method based on depth comparison, with precision issues; ray traced shadows are accurate but have high performance cost.

Why does shadow mapping need depth bias?

Depth bias avoids self-shadowing (shadow acne) because depth buffer precision is limited.

How do point lights generate shadow maps?

Point lights typically use cube shadow maps, rendering depth from six directions, or use single-pass dual-paraboloid mapping.

How to optimize shadows on mobile?

On mobile, use low-resolution shadow maps, limit shadow distance, use simple hard shadows, or disable shadows.

What is CSM?

Cascaded Shadow Maps divide the view frustum into multiple regions, each with different resolution shadow maps, balancing shadow quality near and far.