Skip to main content

Occlusion culling

The camera already skips objects outside its view. But inside a building, or in a street between houses, most of what is in front of the camera is still hidden behind a wall, and each of those objects is still drawn. Occlusion culling works out ahead of time which objects can be seen from each part of the scene, and the game then leaves out the ones that can't. There are fewer draw calls, which matters most on phones. It works like Unity's Occlusion Culling.

It is for scenes with lots of walls: rooms and corridors, towns, caves, a level split into areas. An open field with nothing in the way gains nothing.

Bake it​

  1. Select the big solid things that hide what is behind them (walls, buildings, floors, big rocks) and turn on Occluder Static (under Shape & look).
  2. Select the things that may be hidden (houses, furniture, props, and the walls themselves too) and turn on Occludee Static. Most scenery gets both.
  3. Open the Scene tab → Occlusion Culling → Bake.

The bar shows how far it got; Cancel stops it. When it is done the card shows how many objects it can skip, the grid it made and the size of its cells. Press Play: the stats line at the top left of the game view says how many objects are occluded in each frame, and so does the Profiler under Rendering.

Moved a wall or a house? Press Bake again. Clear removes the baked data, and the camera draws everything again.

See it in the scene view​

Turn on Gizmos → Occlusion culling in the 3D view (Unity's occlusion Visualization). The view then:

  • outlines the baked cells (a large grid shows its bottom layer and outer box),
  • outlines the cell the camera is in,
  • leaves out the objects the game would skip from there.

The camera is the selected object's camera when one is selected, so you can see what the game camera culls while you look at the level from anywhere. With nothing selected, it is the view's own camera: fly around and the hidden objects come and go as the game would draw them.

Which flag to use​

ObjectOccluder StaticOccludee Static
Walls, buildings, floors, big rocksOnOn
Furniture, crates, small props, treesOff: too small or see-through to hide muchOn
Glass, fences, thin railingsOff: you can see through themOn
Anything that moves (player, enemies, doors, physics boxes)OffOff

Only objects that are both shown and switched on when you bake count. An object moved away from where it was baked is always drawn, so a script that moves scenery can't make it disappear. Objects made while the game runs are always drawn.

Cell size​

The bake lays a grid of cells over the static objects (4 m by default) and works out what can be seen from somewhere in each cell. The game looks up the camera's cell every frame.

  • Smaller cells skip more, as each cell sees less, but the bake takes longer and the data is bigger.
  • Bigger cells bake faster, for big outdoor scenes.

The grid also takes in where each camera of the scene starts, when that is up to 50 m from the static objects, so a game that starts on a road outside town is culled from its first frame. A camera that starts further out is left out, and the bake says so with a message; move it closer or leave it, as it is culled as soon as it comes into the grid.

A grid has at most 8,000 cells: past that, the bake makes the cells bigger by itself, and the card shows the size it used. Outside the grid (the camera high above the level, say) everything is drawn.

Good to know​

  • Shadows stay. A skipped object still casts its shadow, so a building behind you still shades the street in front of you.
  • Batching. Objects with the same shape and material are usually drawn together in one call. Once a scene has occlusion data, its Occludee Static objects are drawn one by one instead, so each can be skipped. Their shadows are still drawn together: copies of the same shape share one shadow draw call per shadow picture (each cascade of the sun is one), even when baked lighting gave each its own light map.
  • Errs on the side of drawing. Each object is checked by the box around it, which is bigger than the object. So an object near the edge of a wall may be drawn when it is just out of sight, but never left out when part of it shows.
  • Per camera. The camera component has an Occlusion Culling switch (on by default). Turn it off for a camera that flies above the level, or to compare the draw calls with and without.
  • The scene view in the editor draws everything, unless Gizmos → Occlusion culling is on (see above).
  • Claude can mark objects and bake through the Claude link (bake_occlusion).

How it is made​

From every corner and the middle of every cell, the bake draws the scene in all six directions on the graphics card: the occluders as solid shapes, and each occludee as its box in its own color. The colors that make it to the picture are the objects that can be seen from that point. A cell can see what any of its nine points can see. Cells that see the same objects share one list, which keeps the data small. It is stored in the scene, so it goes into every build.