Course contentsShow
← Unity Game Development
Lesson 641 of 8289. Rendering and VisualsFree lesson

Optimizing Environment Visuals with Occlusion Culling

Implement occlusion culling to prevent the engine from rendering objects hidden behind walls, significantly reducing the draw call count.

In this lesson, you will learn how to implement occlusion culling to prevent the GPU from wasting resources on objects hidden behind walls, effectively slashing your draw call count.

The Core Concept: Visibility Logic

Even if an object is within your camera's frustum, the GPU will attempt to render it if it isn't explicitly culled. In complex scenes—like a multi-room dungeon or a dense city—rendering objects hidden behind walls is a massive performance drain. Occlusion culling solves this by pre-calculating visibility data; it tells the engine, "If the camera is in Room A, do not send the draw calls for objects in Room B to the GPU."

Implementation Workflow

Occlusion culling isn't a shader setting; it is a baked process. Follow these steps to set it up:

  1. Marking Objects: Select your static environment geometry (walls, floors, large props). In the Inspector, check the Static toggle, and specifically ensure Occluder Static is checked. For objects that should be hidden by others, mark them as Occludee Static.
  2. The Occlusion Window: Go to Window > Rendering > Occlusion Culling.
  3. Baking Data: In the Bake tab, adjust the Smallest Occluder (the size of the smallest object that blocks vision) and Smallest Hole (the smallest gap you want the system to recognize). Click Bake.

Once baked, Unity generates an "Occlusion Portal" map. During runtime, the engine uses this data to perform a Frustum-Occlusion check:

// The engine handles this automatically once baked,
// but you can monitor performance in the stats overlay.
Debug.Log("Draw calls reduced by: " + UnityStats.drawCalls);

Why It Matters

When your scene is packed with high-fidelity meshes, the bottleneck is often the CPU-to-GPU overhead (draw calls). By implementing occlusion culling, you stop the CPU from wasting time on command buffers for items the player cannot possibly see. This frees up your frame budget for more complex effects, like the depth-based transparency and volumetric lighting we covered in previous lessons.

Key Takeaway: Occlusion culling moves the heavy lifting of visibility checks from runtime to a pre-baked data set, ensuring your GPU only works on the geometry currently contributing to the player's view.