Three.js Frustum Culling Visualizer

See frustum culling at work in Three.js, with a top-down inset showing the camera frustum and objects coloured visible or culled, live draw-call and triangle counts, and toggles for culling and the far plane.

Key topics: three.js frustum culling, visibility, camera frustum, draw calls, optimization, camerahelper

What this code does

- Frustum: the six planes of the camera's view volume; anything whose bounding sphere is fully outside them is skipped.
- Automatic Culling: three.js tests every object with `frustumCulled = true` before drawing, which costs little and saves a lot.
- Visualization: an orthographic overview renders in a scissor viewport with a `CameraHelper`, colouring objects green or red.
- Far Plane: pulling the far plane in culls distant objects too.
- When to Disable: instanced, skinned or vertex-animated meshes can leave their bounds, so set `frustumCulled = false` for them.
- Controls: culling on/off, overview inset, camera pan, far plane and object count.

Continue Learning

JavaScript Implementation

ES6+
// The renderer skips objects whose bounding sphere is outside the camera frustum.
// The same test, done manually:
const frustum = new THREE.Frustum()
frustum.setFromProjectionMatrix(new THREE.Matrix4().multiplyMatrices(camera.projectionMatrix, camera.matrixWorldInverse))
for (const mesh of meshes) {
  mesh.material = frustum.intersectsObject(mesh) ? visibleMaterial : culledMaterial
}
// mesh.frustumCulled = false forces an object to be drawn even when off-screen
// (needed for InstancedMesh/skinned meshes whose bounds don't cover their animation)