Introduction#
The lighting in Basic Lighting has a big gap: it works out how much each surface faces the light, but never checks whether anything is in the way. Everything is lit, even the ground under a table. Shadows are one of the most important clues our eyes use to understand a 3D scene, and the most common way games draw them is shadow mapping. This post covers:
- The idea: a point is in shadow if the light can’t see it
- The two passes: drawing a shadow map from the light, then comparing against it
- The three classic problems: shadow acne, jagged edges and hard edges, with their fixes
It combines render targets, the depth buffer and cameras.
The Idea: What Can the Light See?#
Stand where the light is and look at the scene. Every surface you can see is lit. Every surface hidden behind something else is in shadow. So to find shadows, we need to know, for any point, whether it’s the nearest thing to the light in that direction.
That’s exactly what a depth buffer records: the distance to the nearest surface at every pixel. So shadow mapping draws the scene twice:
- From the light. Put a camera at the light, pointing the way the light shines, and draw the scene’s depth only into a render target. This depth texture is the shadow map: for every direction, how far the light reaches before hitting something. A directional light like the sun uses an orthographic camera (cameras), since its rays are parallel.
- From the camera. Draw the scene normally. For each pixel, also work out where that point lands in the light’s view and how far it is from the light, then look up the shadow map there. If the shadow map holds a nearer depth, something is between this point and the light: it’s in shadow.
In the fragment shader, the test is a single comparison:
vec3 p = lightSpacePosition.xyz / lightSpacePosition.w * 0.5 + 0.5; // 0..1 in the shadow map
float closest = texture(uShadowMap, p.xy).r; // nearest surface to the light, in this direction
float shadow = p.z - bias > closest ? 1.0 : 0.0; // something nearer? then we're in shadow
Here lightSpacePosition is this pixel’s position as the light’s camera sees it (the world position multiplied by the light’s view and projection matrices). The variable p is the same position turned into shadow-map terms: p.xy says where to look in the shadow map, and p.z is this point’s depth from the light. The value closest is the depth stored in the shadow map there, and bias is a tiny allowance explained below. The first line does the divide by w from part 5 of the fundamentals, then turns â1 to 1 into 0 to 1, the range used to read a texture. The last line uses a ? b : c (“if a, then b, otherwise c”).
Try It#
The inset in the corner is the shadow map itself: the scene’s depth as seen from the light (darker is nearer). Move the light around and watch the shadows and the shadow map change together. Drag the main view to look around.
Shadow mapping, with the lightâs depth map shown in the corner
The Classic Problems#
Shadow acne#
Drag the bias to 0. Lit surfaces break out in stripes and blotches of shadow, called shadow acne. Each shadow map texel covers a small patch of surface but stores just one depth. Across that patch, the real surface is sometimes slightly in front of the stored depth and sometimes slightly behind it, so it shadows itself in a pattern. Lower the light to make it worse: at a shallow angle, the depth changes more across each texel.
The fix is the bias: subtract a little from the depth before comparing, so a surface never shadows itself. Too much bias causes the opposite problem, peter-panning: the shadow detaches from the base of the object, so it seems to float. (Raise the bias right up and look at the bottom of the box.) Real engines scale the bias with the surface’s angle to the light, to need as little as possible.
Jagged edges#
Set the shadow map size to 64 Ă 64. The shadow edges turn into big blocky staircases: each shadow map texel now covers about 15 cm of the floor (if one unit is a metre), and every pixel in that patch gets the same answer. Larger shadow maps help but cost memory and time, and big outdoor scenes need shadows near the camera to be sharp and shadows far away to exist at all. Most games solve that with cascaded shadow maps: several shadow maps, each covering a bigger area further from the camera.
Hard edges#
Real shadows have soft edges, because real lights aren’t single points. Tick Soften edges (PCF). Percentage-closer filtering makes the comparison at nine nearby points of the shadow map and averages the results, giving a soft transition. (Averaging the depths first wouldn’t work: you’d get a meaningless in-between depth, not a softer shadow.)
Graphics cards have hardware support for exactly this comparison. A depth texture can be given a compare mode: the texture unit then does the depth comparison itself and returns 0 or 1 instead of a depth. Better still, with linear filtering turned on, it compares the four nearest texels and blends the four results bilinearly, so you get a little PCF for the price of a single texture read. Drawing the shadow map is also cheap per pixel, because it writes only depth and needs no fragment shader work at all, so the graphics card can run it at full speed. Its cost is in drawing all the scene’s triangles one extra time per light, per cascade.
Summary#
- A point is in shadow if something is nearer to the light in its direction.
- Pass 1 draws the scene’s depth from the light into a shadow map; pass 2 compares each pixel against it.
- Bias prevents shadow acne, but too much causes peter-panning.
- Shadow map resolution sets how sharp shadows can be; cascades cover large scenes.
- PCF averages several comparisons to soften the edges.
Further Reading#
- Lance Williams, Casting curved shadows on curved surfaces (SIGGRAPH 1978): the paper that introduced shadow mapping.
- LearnOpenGL, Shadow Mapping: the technique in code, with acne, bias and PCF.
- Microsoft, Common Techniques to Improve Shadow Depth Maps: a detailed guide to the problems and fixes.
- Microsoft, Cascaded Shadow Maps: how large outdoor scenes get sharp shadows.