TExcelTAD
Home › 3D Web Games with Three.js › LOD & Frustum Culling

LOD & Frustum Culling

The fastest triangle is the one you never draw. Two techniques decide what gets drawn each frame:

  • Frustum culling skips objects outside the camera's view. Three.js does it for you, per object.
  • Level of detail (LOD) draws a simpler version of an object when it's far away and covers only a few pixels. THREE.LOD switches versions by distance.

Together with fog and a sensible far plane, they let an open level contain far more than the GPU could draw at once.

Frustum culling

Before drawing, the renderer tests each object's bounding sphere (from its geometry, transformed to world space) against the six planes of the camera frustum. If the sphere is completely outside any plane, the object is skipped: no draw call, no triangles. Each plane is stored as a normal pointing into the frustum plus a constant, and the test is one dot product per plane:

js · try it yourself
// Sphere vs frustum planes (normals point inward). A 2D slice: left/right planes of a 90° view along -Z, plus near/far.
const planes = [
  { name: 'left',  n: [Math.SQRT1_2, 0, -Math.SQRT1_2], d: 0 },
  { name: 'right', n: [-Math.SQRT1_2, 0, -Math.SQRT1_2], d: 0 },
  { name: 'near',  n: [0, 0, -1], d: -0.1 },          // z <= -0.1
  { name: 'far',   n: [0, 0, 1], d: 50 },             // z >= -50
];
function visible([x, y, z], r) {
  for (const p of planes) {
    const dist = p.n[0] * x + p.n[1] * y + p.n[2] * z + p.d;   // signed distance to the plane
    if (dist < -r) return `culled by ${p.name}`;               // entirely on the outside
  }
  return 'drawn';
}
const tests = [['in front', [0, 0, -10], 1], ['far left', [-20, 0, -10], 1], ['straddling the edge', [-10.5, 0, -10], 1],
  ['behind the camera', [0, 0, 5], 1], ['beyond far', [0, 0, -60], 1], ['huge, beyond far', [0, 0, -60], 15]];
for (const [label, c, r] of tests) console.log(`${label.padEnd(20)} r=${String(r).padEnd(2)} -> ${visible(c, r)}`);
in front             r=1  -> drawn
far left             r=1  -> culled by left
straddling the edge  r=1  -> drawn
behind the camera    r=1  -> culled by left
beyond far           r=1  -> culled by far
huge, beyond far     r=15 -> drawn

The point behind the camera is reported by the left plane because that's the first plane it fails; failing any one plane is enough. The test is also conservative: an object partly inside is drawn in full, and an object whose sphere grazes the frustum is drawn even if none of its triangles are visible. Three.js exposes the same machinery as THREE.Frustum (frustum.setFromProjectionMatrix(…), intersectsObject, intersectsSphere, containsPoint), useful for your own checks, such as "only update AI for enemies on screen".

Things that break automatic culling:

  • Stale bounds. The bounding sphere is computed once from the geometry. If you move vertices on the CPU, or a skinned/morphing mesh deforms far outside its rest pose, call geometry.computeBoundingSphere() (or mesh.computeBoundingSphere() for skinned and instanced meshes) or the object may vanish at the screen edge.
  • Vertex shaders that move vertices (wind, waves) can push them outside the bounds. Enlarge geometry.boundingSphere.radius to cover the motion.
  • object.frustumCulled = false disables the test for that object: needed for things like a sky dome or a particle system that spans everything, wasteful elsewhere.
  • Culling is per object. One merged mesh for a whole level is never culled; a level split into chunks (rooms, grid cells) culls well.

THREE.LOD

A LOD object holds several versions of an object and shows exactly one, chosen by distance from the camera:

js
const tree = new THREE.LOD();
tree.addLevel(highDetailMesh, 0);      // from 0 m
tree.addLevel(mediumMesh, 25);         // from 25 m
tree.addLevel(billboardMesh, 60);      // from 60 m: a flat card or impostor
tree.addLevel(new THREE.Object3D(), 120);   // beyond 120 m: nothing at all
scene.add(tree);
// renderer.render() updates every LOD automatically (lod.autoUpdate = true)

addLevel(object, distance, hysteresis) takes an optional hysteresis (a fraction such as 0.1): switching back to the closer level then waits until the camera is 10% closer than the threshold, so an object at exactly the boundary doesn't flicker between versions as the camera bobs. lod.getCurrentLevel() tells you which level is shown.

The playground plants 600 trees, each a LOD with three levels: a detailed tree (about 2,100 triangles), a simple one (a trunk and one cone, 32 triangles) and a single four-sided cone (8 triangles). The camera flies over the forest; turn on color by level to see the rings of detail around it, and compare the triangle count with LOD off.

html · try it yourself
<style>html, body { margin: 0; height: 100%; overflow: hidden } canvas { display: block }
#ui { position: fixed; left: 8px; top: 8px; display: flex; gap: 4px; color: #fff; font: 12px system-ui, sans-serif }
#stats { position: fixed; left: 8px; bottom: 8px; color: #fff; font: 12px ui-monospace, monospace; background: rgba(0,0,0,.55); padding: 4px 6px; border-radius: 4px; white-space: pre }</style>
<div id="ui"><label><input id="lod" type="checkbox" checked> LOD</label><label><input id="tint" type="checkbox"> color by level</label></div>
<div id="stats"></div>
<script type="module">
import * as THREE from 'three';
import { mergeGeometries } from 'three/addons/utils/BufferGeometryUtils.js';

const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
document.body.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color('#a8c8e8');
scene.fog = new THREE.Fog('#a8c8e8', 60, 160);         // hides the far plane and the coarsest level
const camera = new THREE.PerspectiveCamera(60, innerWidth / innerHeight, 0.5, 170);
addEventListener('resize', () => {
  camera.aspect = innerWidth / innerHeight;
  camera.updateProjectionMatrix();
  renderer.setSize(innerWidth, innerHeight);
});
scene.add(new THREE.HemisphereLight('#e8f2ff', '#4b5a3a', 1.4));
const sun = new THREE.DirectionalLight('#fff', 2);
sun.position.set(-4, 10, 3);
scene.add(sun);
const ground = new THREE.Mesh(new THREE.PlaneGeometry(400, 400), new THREE.MeshStandardMaterial({ color: '#6c9655' }));
ground.rotation.x = -Math.PI / 2;
scene.add(ground);

// Three versions of a tree (merged into one geometry each, so each level is one mesh).
function treeGeometry(segments, layers) {
  const parts = [new THREE.CylinderGeometry(0.2, 0.3, 2, Math.max(3, segments / 2)).translate(0, 1, 0)];
  for (let i = 0; i < layers; i++) parts.push(new THREE.ConeGeometry(1.6 - i * 0.35, 2, segments, Math.max(1, segments / 4)).translate(0, 2.2 + i * 1.1, 0));
  return mergeGeometries(parts.map((g) => g.toNonIndexed()));
}
const levels = [
  { geo: treeGeometry(32, 4), dist: 0, tint: '#e53935' },
  { geo: new THREE.ConeGeometry(1.5, 4.5, 8).translate(0, 2.4, 0).toNonIndexed(), dist: 30, tint: '#fdd835' },
  { geo: new THREE.ConeGeometry(1.4, 4.5, 4).translate(0, 2.4, 0).toNonIndexed(), dist: 70, tint: '#1e88e5' },
];
levels[1].geo = mergeGeometries([new THREE.CylinderGeometry(0.2, 0.3, 2, 4).translate(0, 1, 0).toNonIndexed(), levels[1].geo]);
for (const l of levels) l.tris = l.geo.attributes.position.count / 3;
const green = new THREE.MeshStandardMaterial({ color: '#2f6e3a', flatShading: true });
const tints = levels.map((l) => new THREE.MeshStandardMaterial({ color: l.tint, flatShading: true }));

function mulberry32(seed) {
  return () => {
    seed |= 0; seed = (seed + 0x6d2b79f5) | 0;
    let t = Math.imul(seed ^ (seed >>> 15), 1 | seed);
    t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;
    return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
  };
}
const rng = mulberry32(5);
const lods = [], fulls = [];
for (let i = 0; i < 600; i++) {
  const x = (rng() - 0.5) * 300, z = (rng() - 0.5) * 300, s = 0.8 + rng() * 0.5;
  const lod = new THREE.LOD();
  levels.forEach((l, k) => {
    const m = new THREE.Mesh(l.geo, green);
    m.userData.level = k;
    lod.addLevel(m, l.dist * s, 0.1);                  // 10% hysteresis
  });
  lod.position.set(x, 0, z);
  lod.scale.setScalar(s);
  scene.add(lod);
  lods.push(lod);
  const full = new THREE.Mesh(levels[0].geo, green);   // the same tree without LOD, for comparison
  full.position.copy(lod.position);
  full.scale.copy(lod.scale);
  full.visible = false;
  scene.add(full);
  fulls.push(full);
}
console.log(`triangles per tree: ${levels.map((l) => l.tris).join(' / ')}`);

const lodBox = document.getElementById('lod'), tintBox = document.getElementById('tint');
lodBox.onchange = () => { for (let i = 0; i < lods.length; i++) { lods[i].visible = lodBox.checked; fulls[i].visible = !lodBox.checked; } };
tintBox.onchange = () => { for (const lod of lods) for (const { object } of lod.levels) object.material = tintBox.checked ? tints[object.userData.level] : green; };
const stats = document.getElementById('stats');

renderer.setAnimationLoop((t) => {
  const a = t / 14000;
  camera.position.set(Math.sin(a) * 60, 12, Math.cos(a) * 60);
  camera.lookAt(Math.sin(a + 0.6) * 60, 4, Math.cos(a + 0.6) * 60);
  renderer.render(scene, camera);
  const counts = [0, 0, 0];
  if (lodBox.checked) for (const lod of lods) counts[lod.getCurrentLevel()]++;
  stats.textContent = `draw calls ${renderer.info.render.calls}   triangles ${renderer.info.render.triangles}\n` +
    (lodBox.checked ? `trees at level 0/1/2: ${counts.join(' / ')}` : 'LOD off: every tree at full detail');
});
</script>

With LOD off, every visible tree costs about 2,100 triangles; with LOD on, only the few near the camera do. (Frustum culling is active in both cases: the draw-call count is always well below 600 because the trees behind the camera are skipped.) The same idea applies to every system, not just meshes: animate far characters at a lower rate, skip their shadows, turn far particle systems off.

Where the LOD meshes come from

  • Modelling tools: artists make LOD versions in Blender (Decimate modifier) and export them, named Tree_LOD0, Tree_LOD1… in one glTF.
  • Automatic simplification: SimplifyModifier in the Three.js addons, or offline with meshoptimizer's gltfpack / glTF Transform's simplify command (DRACO, KTX2 & Meshopt), which produce much better results than simplifying at runtime.
  • Impostors: the farthest level is often a camera-facing card (a Sprite or a quad) with a pre-rendered picture of the object.

Tip: LOD and instancing combine: give each LOD level its own InstancedMesh, and each frame (or every few frames) sort instances into the level buckets by distance. That's how large forests and grass fields are drawn with a handful of draw calls.

Test yourself

What does Three.js test to decide whether an object is frustum-culled?

  1. Every triangle against the screen
  2. The object's world-space bounding sphere against the six frustum planes
  3. Whether the object is in front of the camera's near plane only
  4. The object's distance from the origin

A tree mesh whose vertices are moved by a wind shader sometimes disappears at the edge of the screen. Why?

  1. LOD switched it off
  2. Its bounding sphere doesn't cover the displaced vertices, so it is culled while still partly visible
  3. Fog hides it
  4. The near plane is too large

What does the hysteresis argument of lod.addLevel(object, distance, hysteresis) prevent?

  1. Shadow acne
  2. Rapid flickering between two levels when the camera hovers around the switching distance
  3. Draw calls
  4. Culling