Shadow mapping

Also known as: shadow map

On this page 7

Shadow mapping is a rendering technique that determines which surfaces are shadowed by first rendering the scene’s depth from the light’s point of view into a texture, the shadow map, and then comparing each visible pixel’s distance to the light against that stored depth. It is the dominant method for dynamic shadows in real-time 3D graphics, used in game engines, WebGL frameworks and virtual production tools across 3D graphics & animation.

The technique is popular because it runs entirely on the rasterization pipeline of the GPU, works with any geometry the rasterizer can draw and scales reasonably with scene complexity. Its weaknesses are equally well known: aliasing at shadow edges, self-shadowing artifacts and a constant struggle to spend limited texture resolution where the viewer actually looks.

How shadow mapping works in two passes

Shadow mapping works in two rendering passes: a depth pass from the light and a shading pass from the camera. In the first pass, the engine places a virtual camera at the light, draws all shadow casters and writes only their nearest depth values into a depth texture. No color is computed, so this pass is relatively cheap, although it still costs draw calls and vertex processing.

In the second pass, the fragment shader transforms each surface point into the light’s clip space with the light view-projection matrix. It then reads the stored depth at that location and performs a depth comparison: if the point is farther from the light than the stored value, something else blocks the light and the point is in shadow. Modern GPUs perform this comparison in hardware through comparison samplers, which also enable basic filtering.

The table below summarizes the stages and what each one produces.

StageInputOutput
Light setupLight position and directionLight view and projection matrices
Depth passShadow-casting geometryDepth texture (shadow map)
ProjectionSurface position in world spaceShadow map coordinates and depth
ComparisonStored depth and surface depthLit or shadowed result
FilteringNeighboring comparison resultsSoft or anti-aliased shadow edge
Stages of a shadow mapping frame

Shadow map variants for different light types

Each light type needs a different projection for its shadow map. A spot light uses a single perspective projection, a point light needs six faces of a cube map (or a dual-paraboloid map) to cover every direction, and a directional light such as the sun uses an orthographic projection fitted around the visible scene.

Large outdoor scenes rarely work with a single directional map, because one texture cannot cover kilometers of terrain with enough detail near the camera. Cascaded shadow maps (CSM) split the view frustum into several depth ranges and give each range its own map, so nearby objects receive dense texels and distant ones receive coarse texels. Virtual shadow maps, used in some recent engines, extend this idea with very large sparse textures that are allocated in pages on demand.

Light typeProjectionTypical layoutMain cost driver
Spot lightPerspectiveOne 2D mapCone angle and range
Point lightPerspective, 90-degree facesCube map with six facesSix depth passes
Directional lightOrthographic2 to 4 cascadesNumber of cascades
Large open worldOrthographic, pagedVirtual shadow mapPage allocation and updates
Common shadow map layouts by light type

Filtering, bias and common artifacts

Raw shadow mapping produces hard, jagged edges because each shadow map texel covers many screen pixels. Percentage-closer filtering (PCF) fixes this by performing several depth comparisons around the sample point and averaging the binary results, which yields a smooth edge. Percentage-closer soft shadows (PCSS) go further and vary the filter size with the distance between occluder and receiver, imitating the penumbra of an area light.

Limited depth precision and texel size also cause shadow acne, a pattern of false self-shadowing stripes on lit surfaces. The standard remedy is a depth bias, often combined with a slope-scaled bias and a normal offset. Too much bias causes the opposite defect, peter-panning, where shadows detach from the objects that cast them. Tuning this balance is one of the most common tasks in shadow work.

ArtifactCauseUsual fix
Shadow acneDepth precision and texel sizeConstant plus slope-scaled bias
Peter-panningBias set too highLower bias, use normal offset
Jagged edgesLow texel density on screenPCF, higher resolution, cascades
ShimmeringMap moves with sub-texel camera motionSnap cascades to texel grid
Cascade seamsResolution jump between cascadesBlend across cascade borders
Typical shadow mapping artifacts and their fixes

Shadow mapping compared with other shadow methods

Shadow mapping is one of several ways to compute shadows, and each method trades accuracy against cost. Shadow volumes produce pixel-exact hard shadows but depend on silhouette extraction and heavy stencil fill. Light baking stores precomputed shadows in lightmaps, which is cheap at runtime but works only for static lights and geometry. Ray-traced shadows give physically correct soft shadows on hardware with ray tracing support, at a much higher cost per pixel.

MethodDynamic lightsEdge qualityRelative cost
Shadow mappingYesDepends on resolution and filteringModerate
Shadow volumesYesExact hard edgesHigh fill rate
Light bakingNoHigh, precomputedVery low at runtime
Ray-traced shadowsYesAccurate soft edgesHigh, needs RT hardware
Shadow methods in real-time rendering

In practice, engines combine methods: baked shadows for static lighting, shadow maps for moving lights and characters, and screen space ambient occlusion to add contact darkening that shadow maps miss.

Configuring shadow maps in production

Good shadow configuration starts with a shadow budget: how many lights may cast shadows and how much memory and GPU time they may use. Typical resolutions range from 1024 to 4096 texels per side per map; a 2048 by 2048 map with 32-bit depth uses about 16 MB. Restricting shadow casting to the lights that matter visually usually saves more time than any filtering trick.

  • Fit the light frustum tightly around visible casters to raise effective texel density.
  • Use frustum culling in the depth pass so that only relevant casters are drawn.
  • Update distant cascades or static lights less often than every frame when motion allows it.
  • Exclude small props from casting where their shadows are not visible at gameplay distance.
  • Check the result at grazing light angles, where acne and aliasing are most visible.

The idea in brief

Imagine standing where a lamp is and taking a photo. Everything you can see from there is lit. Anything hidden behind another object is in shadow. Shadow mapping does exactly this for a computer image.

The computer first records how far the light can see in every direction. Then, for each point on the screen, it asks one question. Is this point farther away than what the light saw? If yes, something blocks the light, and the point is drawn darker as a shadow. The trick is fast enough to run many times per second in games.

A practical scenario

A small team builds a browser-based architectural walkthrough in WebGL. The sun is a directional light, and visitors can drag a slider to change the time of day, so baked shadows are not an option. The team first uses a single 2048 by 2048 shadow map over the whole site, and nearby window frames show blocky, flickering edges.

They switch to three cascades covering 0 to 15 meters, 15 to 60 meters and 60 to 250 meters, snap each cascade to its texel grid and add a 3 by 3 PCF kernel. Shadow acne on the facades disappears after they tune a slope-scaled bias. The result is stable, soft-edged shadows at a steady 60 frames per second on mid-range laptops, with the shadow passes taking about 2 milliseconds per frame.