From BreakEnderNerd Alert

Nerd Alert: Ambient Occlusion, the Soft Shadows in Every Corner (SSAO and GTAO)

By the BreakEnder newsroom ·

Turn ambient occlusion off in a graphics menu and the world suddenly floats. Here is what SSAO and GTAO actually compute, and why they are cheaper than real shadows.

Every PC graphics menu has a setting with a cryptic name: SSAO, HBAO+, GTAO or just "Ambient Occlusion". Turn it off and the game gets a few frames faster, and somehow everything looks fake. Rocks float on the ground. Characters look pasted onto the scene. Corners of rooms look flat. Turn it back on and the world feels solid again. Let's dig into why.

What "ambient" light is

Outdoors, light does not only come from the sun. It also comes from the whole sky and bounces off the ground, walls and everything else. That soft, everywhere light is called ambient light. In reality, places that can "see" less of the sky receive less of it: the inside of a crack, the gap under a car, the crease where a wall meets the floor, the spot where your character's boot touches the ground.

Ambient occlusion (AO) is a number from 0 to 1 for each point that says how much of that surrounding light is blocked. Multiply the ambient lighting by it and creases darken naturally. Occlusion just means "being blocked".

Baked AO

The oldest approach is to compute AO offline and save it. Artists bake AO into textures for each model, so a character's armpits and the folds of a jacket are always a bit darker. Level designers bake AO into the static world. This is high quality and free at runtime, but it only knows about the object itself or the static level. It cannot darken the ground under a moving character or a crate you just pushed.

Screen-space AO

In 2007, Crytek's Crysis popularised a real-time trick called screen-space ambient occlusion, or SSAO. "Screen space" means it works only from what is already on screen, specifically the depth buffer: an image where each pixel stores how far away the surface is.

The idea is simple. For each pixel, look at a handful of nearby pixels in the depth buffer. If many of them are closer to the camera than you, something is probably sticking up around you and blocking the sky, so darken. If they are all at your depth or further, you are on an open surface, so leave it alone.

occlusion = 0;
for each sample in kernel:
if depthAt(pixel + sample) is in front of expectedDepth: occlusion += 1;
ao = 1 - occlusion / sampleCount;

Using only a few samples per pixel makes the result noisy, so the AO image is then blurred while keeping edges sharp. It is often computed at half resolution to save time.

SSAO's big strength is that it handles everything on screen, moving or not, at a fixed cost per pixel regardless of how complex the scene is. Its weakness is that it only sees what the camera sees. An object hidden behind something cannot occlude, and AO near the screen edges can flicker or vanish when you turn the camera.

Horizon-based AO

Plain SSAO is a bit of a guess. Later methods made it more principled by thinking about horizons. Stand at a point on a surface and look in some direction across the depth buffer. As you step outward, track the highest point you see. That highest angle is the horizon in that direction. Anything below the horizon is blocked from the sky. Do this in several directions and you get a good estimate of how much sky the point can see.

NVIDIA's HBAO (horizon-based ambient occlusion) used this idea, and it became a popular option in PC games.

GTAO

GTAO, ground-truth ambient occlusion, came from Jorge Jimenez and colleagues at Activision, published in 2016 (Jimenez et al., Practical Realtime Strategies for Accurate Indirect Occlusion). The "ground truth" part means its goal was to match the result of a slow, accurate offline reference as closely as possible.

GTAO also searches for horizons, but it does the maths of "how much light gets through between these two horizons" properly, using the surface normal so that light arriving at a grazing angle counts less, as it does in reality. It also spreads its work over several frames, changing the sample directions each frame and averaging the results over time, which gives clean results from very few samples. The paper reports about half a millisecond on a PS4 at 1080p with a half-resolution AO buffer. That is very affordable for a frame budget of 16 milliseconds at 60 frames per second.

The same paper adds two extras: multi-bounce, which brightens AO on light-colored surfaces because light bounces around more there, and specular occlusion, which stops shiny reflections appearing inside creases where they should be blocked.

GTAO and its descendants are now common in engines. Even the free web engine three.js ships a GTAO example (three.js GTAO example).

AO and foliage

Grass is a hard case. Thin blades confuse depth-based AO because each blade is tiny and the depth buffer jumps wildly between blade and ground. The result can be a dark, shimmering mess. Common fixes are to weaken AO on foliage, use a baked "darker near the root" gradient on each blade instead, or let foliage receive AO from the ground but not cast it on itself. When you see a lush field that is dark at the base and bright at the tips, that is usually a painted gradient, not SSAO.

Ray-traced AO

With hardware ray tracing, games can shoot real rays from each point and check whether they hit anything. This sees objects off-screen and behind things, fixing SSAO's blind spots, but costs more. Many games offer it as an "ultra" option on top of screen-space methods.

Spot it yourself

  • Toggle AO in a graphics menu and look where an object meets the ground. That contact darkness is AO.
  • Turn the camera so a wall slides off the screen edge. If the dark corner next to it fades out, you are watching screen-space AO lose its information.
  • Look for a faint dark halo around characters standing in front of distant walls. That is a classic SSAO artefact.

Glossary

  • Ambient light: soft light from the sky and bounces, arriving from all around.
  • Ambient occlusion: how much of that light is blocked at a point.
  • Depth buffer: an image storing the distance to each pixel's surface.
  • Screen space: using only information already rendered on screen.
  • Horizon: the highest blocking angle seen in a direction from a point.

AO makes things sit in the world. Next, we look at the other half of making a frame look right: how games squash bright skies and dark caves into the narrow range a screen can actually show.

Want the full math behind this? Real-Time Rendering, Fourth Edition by Akenine-Möller, Haines and Hoffman is the reference most graphics programmers keep on their desk.

As an Amazon Associate, BreakEnder earns from qualifying purchases.