Gamedev Hub

Shadow Mapping

Shadow mapping is the standard technique for rendering shadows in 3D games. It works by treating the light as a camera and determining which surfaces are “visible” to the light and which are blocked.

1. The Two-Pass Process

Pass 1: The Shadow Pass

Render the scene from the Light’s point of view.

Pass 2: The Lighting Pass

Render the scene from the Camera’s perspective. For every pixel on screen:

  1. Convert its world position into Light Space.
  2. Calculate its current distance from the light (pixelDepth).
  3. Sample the Shadow Map to see the closest thing the light hit (mapDepth).
  4. The Comparison:
    • If pixelDepth > mapDepth, something is blocking the light. The pixel is In Shadow.
    • Otherwise, it is Lit.

2. Common Challenges

Shadow Acne

Tiny precision errors cause surfaces to shadow themselves, creating “Z-fighting” stripes.

Peter Panning

If the bias is too high, the shadow “detaches” from the object and appears to float.

Resolution & Cascades

Standard shadow maps look blocky in the distance.

3. Implementation (HLSL Snippet)

float4 lightSpacePos = mul(_WorldToShadowMatrix, float4(i.worldPos, 1.0));
float depthFromMap = tex2D(_ShadowMap, lightSpacePos.xy).r;

// The Comparison
float shadow = lightSpacePos.z > (depthFromMap + _Bias) ? 0.5 : 1.0;

return col * shadow;

4. Summary