Skip to content

Shadow Mapping

You'll give your real-time scene shadows by rendering depth from the light's point of view, then fight acne, peter-panning and jagged edges until they look right.

About

Shadow mapping is the way nearly every game draws shadows, and it's three steps: render the scene from the light into a depth texture, then when drawing from the camera, check whether each pixel is farther from the light than the depth stored there. The steps are simple and the artifacts are not. You'll meet shadow acne (stripes on every lit surface), peter-panning (shadows floating away from their objects once you fix the acne) and edges like a staircase. Each has a known fix, and knowing them by sight is what separates someone who followed a tutorial from someone who can debug a renderer. Plan on two weeks of evenings.

At a glance

Per requirement
35 XP
Completion bonus
300 XP
Requirements
6
Takes
Two to three weeks
Skill level
Advanced (years of practice)
Cost
Free
Supplies
Nothing special
Where
At home
Other people
On your own
Locked for now
Earn two more Uncommon badges in Graphics Programming to open this one. Log in to see how far along you are.

Requirements

  1. 1
    Render the scene from a directional light into a depth texture and show it. Use an orthographic projection from the light and draw the depth texture into a corner of the screen as greyscale. If it's all white, your near and far planes are wrong. Take a screenshot with the depth map visible in the corner. Photo
  2. 2
    Project the shadow map in the main pass and get a shadow on the floor. Transform each fragment into the light's clip space, compare its depth to the stored one, and darken it if it's farther. Take a screenshot of the first shadow, acne and all. Photo
  3. 3
    Fix the acne with a slope-scaled bias and show the before and after. A fixed bias fixes flat surfaces and not grazing ones, so scale it by the angle between the normal and the light. Too much bias and the shadow detaches from the object, which is the peter-panning you'll fix next. Post two screenshots, one with acne and one clean. Photo
  4. 4
    Cull front faces in the shadow pass, or fix peter-panning another way. Rendering only back faces into the depth map moves the bias problem to the inside of objects, where nobody looks. Take a screenshot of a cube sitting firmly on its shadow with no gap at the base. Photo
  5. 5
    Soften the edges with percentage-closer filtering. Sample the shadow map in a 3 by 3 or 5 by 5 grid and average the results. Take a screenshot of the same edge before and after, zoomed in. Photo
  6. 6
    Walk a camera around a scene with at least five shadowed objects at 60 frames a second. Print the frame time on screen. Film twenty seconds of moving through the scene with the light slowly rotating so the shadows swing. Video