Three.js Cascaded Shadow Maps (CSM)

Cascaded shadow maps in Three.js with the CSM addon, keeping shadows sharp near the camera across a 180 m avenue, with adjustable cascades, practical/uniform/logarithmic splits, cascade fading and frustum visualization.

Key topics: three.js csm, cascaded shadow maps, shadows, directional light, shadow resolution, large scenes

What this code does

- The Problem: one shadow map stretched over a large view leaves few texels per metre, so nearby shadows go blocky.
- Cascades: the view frustum is split by distance and each slice gets its own shadow map, small near and large far.
- Split Schemes: uniform, logarithmic, or "practical" (a blend) decide where slices start and end.
- Material Setup: `csm.setupMaterial` injects cascade selection into each material's shader.
- Fading: cascades blend at their borders to hide the resolution jump.
- Comparison: set cascades to 1 to see a single shadow map over the same range.
- Controls: cascades, split scheme, shadow distance, map size, fade, frustum helper and moving sun.

Continue Learning

JavaScript Implementation

ES6+
import { CSM } from 'three/examples/jsm/csm/CSM.js'

renderer.shadowMap.enabled = true
const csm = new CSM({
  maxFar: 180,
  cascades: 4,
  mode: 'practical',          // blend of uniform and logarithmic split distances
  parent: scene,
  shadowMapSize: 2048,
  lightDirection: new THREE.Vector3(0.4, -0.75, -0.5).normalize(),
  camera
})
const material = new THREE.MeshStandardMaterial({ color: 0xd6d3d1 })
csm.setupMaterial(material) // injects cascade selection into the shader

function animate () {
  requestAnimationFrame(animate)
  csm.update()                 // refit cascades to the camera frustum each frame
  renderer.render(scene, camera)
}
animate()