Three.js Ambient Occlusion (GTAO vs SSAO)

Screen-space ambient occlusion in Three.js comparing GTAOPass (ground-truth horizon-based AO with Poisson denoise) and SSAOPass (hemisphere kernel) on a crevice-heavy scene, with AO-only and no-AO views.

Key topics: three.js ssao, gtao, ambient occlusion, effectcomposer, post processing, screen space

What this code does

- Why AO: under flat sky lighting, ambient occlusion darkens creases and contact points that light can't easily reach.
- GTAO: integrates visible horizon angles around each pixel from depth and normals, which is close to ray-traced AO.
- SSAO: tests a hemisphere of sample points against the depth buffer and counts how many are occluded.
- Denoise / Blur: both run at screen resolution with few samples, so a Poisson or box blur removes the noise.
- Composer: RenderPass → AO pass → OutputPass; the composer size follows the renderer on resize.
- Controls: technique, view (beauty+AO, AO only, none), radius, intensity, GTAO samples and spin.

Continue Learning

JavaScript Implementation

ES6+
import { EffectComposer } from 'three/examples/jsm/postprocessing/EffectComposer.js'
import { RenderPass } from 'three/examples/jsm/postprocessing/RenderPass.js'
import { GTAOPass } from 'three/examples/jsm/postprocessing/GTAOPass.js'
import { OutputPass } from 'three/examples/jsm/postprocessing/OutputPass.js'

const composer = new EffectComposer(renderer)
composer.addPass(new RenderPass(scene, camera))
const ao = new GTAOPass(scene, camera, width, height)
ao.updateGtaoMaterial({ radius: 0.6, samples: 16 })
// ao.output = GTAOPass.OUTPUT.Denoise  // show the AO buffer only
composer.addPass(ao)
composer.addPass(new OutputPass())

function animate () {
  requestAnimationFrame(animate)
  composer.render() // keep composer.setSize in sync with the renderer on resize
}
animate()