Reversed Depth + PostFX Compatibility (r183)

Long depth-range scene rendered with `reverseDepthBuffer` and `EffectComposer`, demonstrating post-processing compatibility under reversed depth.

Key topics: three.js reverse depth, effectcomposer, post-processing, depth precision, r183

Overview

Scenes that span huge distances (planets seen from orbit, flight simulators, open worlds) run into z-fighting: distant surfaces flicker through each other because the depth buffer cannot tell them apart. A reversed depth buffer fixes most of this. This demo renders a scene with a near plane of 0.1 and a far plane of 50,000 using reverseDepthBuffer, and runs it through an EffectComposer to confirm that post-processing still works.

How it works

A standard perspective projection maps depth non-linearly: most of the precision is spent close to the near plane and very little is left for distant objects. Floating-point numbers have the opposite distribution, with most of their precision close to zero. A conventional depth buffer therefore combines two sources of imprecision in the far distance.

Reversing depth maps the near plane to 1 and the far plane to 0. Now the far distance, where the projection is least precise, lands where floating-point values are most precise, and the two errors largely cancel. With a 32-bit float depth buffer the result is almost uniform precision across the whole range. In Three.js you enable it with new WebGLRenderer({ reverseDepthBuffer: true }); the renderer flips the projection matrix and depth test for you when the browser supports the required clip-control extension. The demo shows whether reverse depth is actually active on your device.

Exploring the demo

Fly towards and away from the distant geometry and look for flickering where surfaces nearly overlap. The vignette shader pass applied through the composer should look identical whether reverse depth is on or off, which is what this demo verifies: screen-space effects that do not read the depth buffer directly need no changes.

Practical advice

Custom shaders that read depth (SSAO, fog, depth of field) must account for the reversed range, so test effects that sample depth textures. Logarithmic depth buffers are an alternative but disable early depth testing and cost fill rate. For large outdoor scenes, also see cascaded shadow maps, the flight simulator and the mountains scene.

What this code does

- Reversed Depth Renderer: initializes `WebGLRenderer` with `reverseDepthBuffer: true` for improved depth precision over long ranges.
- Massive Depth Span: camera near/far setup (`0.1` to `50000`) stresses depth buffering across distant geometry.
- PostFX Pipeline: `EffectComposer` with `RenderPass` + `ShaderPass` verifies screen-space effects still render correctly.
- Capability Detection: displays whether reverse depth is active based on runtime renderer capabilities.
- Practical Benefit: helps reduce z-fighting in large scenes while retaining modern post-processing workflows.

Continue Learning

JavaScript Implementation

ES6+
import * as THREE from 'three'
import { EffectComposer } from 'three/examples/jsm/postprocessing/EffectComposer.js'
import { RenderPass } from 'three/examples/jsm/postprocessing/RenderPass.js'
import { ShaderPass } from 'three/examples/jsm/postprocessing/ShaderPass.js'
import { VignetteShader } from 'three/examples/jsm/shaders/VignetteShader.js'

const renderer = new THREE.WebGLRenderer({
  antialias: true,
  reverseDepthBuffer: true
})
const camera = new THREE.PerspectiveCamera(58, width / height, 0.1, 50000)

const composer = new EffectComposer(renderer)
composer.addPass(new RenderPass(scene, camera))
const vignette = new ShaderPass(VignetteShader)
composer.addPass(vignette)

function animate () {
  requestAnimationFrame(animate)
  composer.render()
}
animate()

Related demos

Trending this week

Most popular