Add ambient occlusion in Three.js
Updated on . Tested with three.js r187, R3F 9 and drei 10.
Ambient occlusion adds soft shadows where objects meet, which gives your scene depth and makes objects look grounded. If your three.js scene looks flat and is lacking shadows, ambient occlusion can be the easy fix. Here is how to add and tune it.

To add ambient occlusion to a three.js scene, follow these steps:
- Draw the scene with an
EffectComposer, not withrenderer.render. - Add a
GTAOPassafter theRenderPass. - Set
radiusto the size of the gaps you want darker, in scene units. - End with an
OutputPass, so tone mapping and the color space still apply.
Add ambient occlusion with GTAOPass
GTAOPass is the three.js addon that draws ambient occlusion over the finished frame, and it goes between the RenderPass and the OutputPass of an EffectComposer.
import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js';
import { RenderPass } from 'three/addons/postprocessing/RenderPass.js';
import { GTAOPass } from 'three/addons/postprocessing/GTAOPass.js';
import { OutputPass } from 'three/addons/postprocessing/OutputPass.js';
const target = new THREE.WebGLRenderTarget(width, height, { type: THREE.HalfFloatType, samples: 4 });
const composer = new EffectComposer(renderer, target);
composer.addPass(new RenderPass(scene, camera));
const ao = new GTAOPass(scene, camera, width, height);
ao.updateGtaoMaterial({ radius: 0.25 }); // how far the pass looks for nearby surfaces, in scene units
ao.blendIntensity = 1; // 1 is full strength, 0.5 is half
composer.addPass(ao);
composer.addPass(new OutputPass());
renderer.setAnimationLoop(() => composer.render());Each line has a job.
samples: 4on the render target keeps the edges smooth. Theantialiassetting of the renderer does not apply to the render target of a composer.OutputPassapplies your tone mapping and the sRGB color space. A composer leaves both out without it.composer.render()replacesrenderer.render(scene, camera)in your loop.
The pass reads the depth of the frame. It finds the spots that sit near other surfaces. Then it multiplies the picture by a gray value at those spots. It works on everything the camera sees. You do not need to change your models or materials.
See only the ambient occlusion
To see what the pass adds, set its output to GTAOPass.OUTPUT.AO. The canvas then shows the ambient occlusion alone, in gray on white.
function showOnlyAmbientOcclusion(only: boolean) {
ao.output = only ? GTAOPass.OUTPUT.AO : GTAOPass.OUTPUT.Default;
}
Use this view while you tune the settings. The gray belongs in corners and where objects touch. An open wall or floor should stay white.
Remove the gray halo around objects
A gray halo around an object means radius is too large for your scene. Lower it until the shading stays close to the places where surfaces meet.
ao.updateGtaoMaterial({ radius: 0.25 }); // how far the pass looks for nearby surfaces, in scene unitsradius uses the units of your scene. The sideboard in this scene is 0.5 meters tall, and the default of 0.25 works well. That is half the height of the bust. A model built in centimeters or in millimeters needs a larger number.
Make the ambient occlusion weaker or stronger
Set ao.blendIntensity to a number between 0 and 1 to make the shading weaker. With 0.5 the shading is half as dark.
ao.blendIntensity = 1; // 1 is full strength, 0.5 is halfTo make the shading stronger, pass scale to updateGtaoMaterial. The default is 1, and a larger number makes the gray darker.
Use an AO map for one model
An AO map is a grayscale texture that stores ambient occlusion for a single model. You create it in a 3D tool like Blender. Then you assign it to the aoMap property of the material.
async function addAoMap(material: THREE.MeshStandardMaterial, url: string) {
const aoMap = await new THREE.TextureLoader().loadAsync(url);
aoMap.flipY = false; // only for a map made for a glTF model
aoMap.channel = 0; // the UV set the map was made for, 0 is the first
material.aoMap = aoMap;
material.aoMapIntensity = 1;
material.needsUpdate = true;
}You do not need any of this code for a glTF model that already has an AO map. GLTFLoader will set aoMap automatically.
An AO map works differently from the ambient occlusion pass in three ways.
- It only darkens light that arrives from every direction. This includes an environment map, an
AmbientLight, or aHemisphereLight. It does not affect light from aDirectionalLight, aPointLight, or aSpotLight. - It only knows about the model it was built for. A vase with an AO map will not darken the table beneath it.
- It uses only one texture read, so it is very cheap to render.
three.js reads the red channel of the texture. Because of this, you can pack multiple maps into one texture. Put the AO map in red, roughness in green, and metalness in blue.
Ambient occlusion in React Three Fiber
In React Three Fiber, use @react-three/postprocessing. It has its own <EffectComposer> that you put inside your <Canvas>, and each effect is a component inside it. You do not write a render loop or resize anything yourself.
npm install @react-three/postprocessing postprocessingIts ambient occlusion effect is <N8AO>.
import { EffectComposer, N8AO, ToneMapping } from '@react-three/postprocessing';
import { ToneMappingMode } from 'postprocessing';Add the composer inside your <Canvas>, after the objects.
<EffectComposer>
<N8AO aoRadius={0.25} intensity={2} />
<ToneMapping mode={ToneMappingMode.NEUTRAL} />
</EffectComposer>aoRadiusis how far the effect looks for nearby surfaces, in scene units. It works likeradiusonGTAOPass, and a number that is too large gives the same gray halo.intensitymakes the shading darker. Lower it for weaker shading.- This composer turns off the tone mapping on the renderer, so add
<ToneMapping>as the last effect.
<N8AO> is not GTAOPass. It calculates the shading in a different way, so the same radius gives a slightly different picture. If you need the exact look of GTAOPass, you can build the three.js EffectComposer in a component and call composer.render() from useFrame. This is more code, and you have to handle the size and the cleanup yourself.
How to make it look even better
Ambient occlusion in three.js is an estimate based on what the camera sees. To keep things fast, three.js has to simplify how light works.
To get even more realistic shadows Bakery3 renders the same scene with a path tracer, the same way Blender's Cycles does, and returns you a downloadable image. The shading where surfaces meet comes from the light itself, so there is no radius to tune and no halo. The color of the floor also reflects softly onto the walls. Here's an example of how to integrate it into your scene:
import { createBakery3, rememberEnvironmentSource } from '@oeave/bakery3';
// An API key while you test. In production, use a token from your server instead:
// https://bakery3.com/docs#let-your-users-render-without-exposing-your-api-key
const bakery3 = createBakery3({ apiKey: 'bk_sk_…' });
bakery3.attach({ scene, camera, renderer });
// Tell the render which file the environment came from.
rememberEnvironmentSource(environment, '/hdri/studio.hdr');
async function renderWithRealOcclusion(): Promise<string> {
const job = await bakery3.render({ width: 1200, background: { type: 'color', color: '#e9e7e2' } });
const { url } = await job.result();
return url;
}The render reads the scene you already have. Check out rendering an image for a list of every setting.
If the scene must stay interactive, you can bake the light into lightmaps. The shading then becomes part of the scene and costs nothing to draw. The article about lightmaps in three.js shows how a lightmap works.
Common questions
What is the difference between SSAOPass, SAOPass and GTAOPass in three.js?
All three are three.js addons that draw ambient occlusion from the depth of the frame. SSAOPass is the basic one. The three.js source says SAOPass and GTAOPass look better and cost more to draw than SSAOPass. This article uses GTAOPass.
Why does my aoMap do nothing in three.js?
An aoMap in three.js only darkens indirect light. This means light from an environment map, an AmbientLight, a HemisphereLight, a light probe, or a lightmap. If your scene uses only a DirectionalLight, a PointLight, or a SpotLight, the map does nothing. Add an environment map or an ambient light. Then the map will work.
Does an aoMap need a second UV set in three.js?
No. An aoMap in three.js reads the UV set that its channel names, and the default is 0, so it uses the first set, uv. Set aoMap.channel = 1 only when the map was made for the second set, uv1. Before r151 an aoMap always read a second set named uv2, so older answers copy the UVs into uv2. That code is no longer needed.
Does GTAOPass work with WebGPURenderer?
No. GTAOPass runs in an EffectComposer, and that only works with WebGLRenderer. For WebGPURenderer, three.js gives you the ao() node in three/addons/tsl/display/GTAONode.js. Multiply that with the color from your scene pass in a RenderPipeline, which was called PostProcessing before r183.
Is ambient occlusion the same as a shadow in three.js?
No. A shadow in three.js comes from one light and goes one way. Ambient occlusion does not have a direction. It darkens spots that light from any side struggles to reach, like a room corner or the floor under a sofa. Most scenes look better with both.
