Three.js Conditional Rendering (Occlusion-Query Proxies)
GPU-driven visibility in Three.js that emulates conditional rendering, querying cheap bounding-box proxies after the occluders and submitting expensive 100k-triangle meshes only when their proxy was visible last frame.
Key topics: three.js conditional rendering, occlusion culling, hierarchical occlusion, proxy geometry, webgl2 queries, performance
Game Over
What this code does
- Idea: native conditional rendering lets the GPU skip a draw when a query found it hidden; WebGL2 lacks it, so results feed back next frame.
- Proxies: each heavy knot gets a cube that bounds every rotation, drawn with colour and depth writes off.
- Order: draw the main scene (occluders fill depth), then the proxy scene with queries against that depth buffer.
- Decision: next frame, heavy meshes whose proxy returned "no samples" are hidden, and unknown results draw to be safe.
- Latency: one frame of lag can pop objects in as occluders move, so real engines expand proxies or add hysteresis.
- Controls: skip occluded draws, show proxy boxes, occluder motion and heavy mesh detail.
Related Demos
Continue Learning
JavaScript Implementation
// "Conditional rendering" in WebGL2: query a cheap proxy, draw the expensive mesh only if it was visible
// frame N: draw occluders → draw proxy box (colorWrite: false) inside a query
// frame N+1: read the result (if available) → heavyMesh.visible = result !== false
const proxy = new THREE.Mesh(new THREE.BoxGeometry(2, 2, 2), new THREE.MeshBasicMaterial({ colorWrite: false, depthWrite: false }))
proxy.onBeforeRender = () => { if (!query) { query = gl.createQuery(); gl.beginQuery(gl.ANY_SAMPLES_PASSED_CONSERVATIVE, query); active = true } }
proxy.onAfterRender = () => { if (active) gl.endQuery(gl.ANY_SAMPLES_PASSED_CONSERVATIVE); active = false }
renderer.render(scene, camera)
renderer.autoClear = false
renderer.render(proxyScene, camera) // tested against the depth already in the buffer
renderer.autoClear = true