Optimizing Three.js Shadow Map Properties

This article provides a practical guide on optimizing Three.js shadows by configuring crucial shadow map properties like mapSize, camera frustum boundaries, and bias. Learning how to adjust these settings allows you to strike the perfect balance between high-fidelity visual rendering and smooth frame rates in your WebGL applications.

Understanding shadow.mapSize

The mapSize property (specifically shadow.mapSize.width and shadow.mapSize.height) defines the resolution of the depth map texture used to calculate shadows. By default, Three.js initialized lights use a 512x512 pixel texture.

Tightening the Shadow Camera Frustum

Shadows in Three.js are rendered from the perspective of a hidden camera attached to the light source (light.shadow.camera).

Eliminating Artifacts with bias and normalBias

When depth calculations are slightly imprecise, virtual scenes suffer from “shadow acne”—an unwanted pattern of dark lines or pixel grids appearing across surfaces.

Softening Shadows with shadow.radius

If you are utilizing shadow map types that support custom filtering, such as PCFShadowMap or PCFSoftShadowMap on the WebGLRenderer, you can utilize the shadow.radius property.