Why Occlusion Culling Improves Performance
Have you ever noticed how certain video game objects load into view as you pan the camera around? You might be watching occlusion culling happen right before your eyes!
Objectives:
· What is Occlusion Culling?
· How does it work?
· How it Improves Performance
· When should I use it?
· How to add Occlusion Culling
· The Result:
What is Occlusion Culling?
Occlusion Culling is a method used by Unity to prevent the execution of rendering calculations for GameObjects that are obscured from view (also known as “occluded”) by other GameObjects.
In simpler terms, anything in your scene that isn’t a priority in views such as objects hidden behind other objects,or objects that are not within your camera’s field of vision, will not be calculated when rendering. Therefore, they will not appear in the scene unnecessarily using up resources.
How does it work?
Using the Unity Editor, Occlusion Culling receives data on your scene. This data is used at runtime to calculate what is within the camera view. This process of gathering or generating data is known as Baking.
Through Baking, Unity divides the scene into cells and generates data to describe the geometry per cell, and the visibility between them. In other to reduce the size of the data, Unity merges the cell wherever it’s possible.
How it Improves Performance
The short answer is simply because it helps us avoid overwhelming the CPU and GPU by spending unnecessary resources rendering everything at once.
When should I use it?
- Occlusion Culling is best used in closed areas like interiors.
- GPU-bound projects: Although it is beneficial for both CPU and GPU, projects that are more expensive on GPU performance could benefit more from Occlusion Culling. This is due to how the culling executes calculations on runtime which takes place in the CPU, rather than GPU.
- When you have enough memory for it — Unity runs culling data into memory at runtime.
How to add Occlusion Culling
- Got to Window →Rendeinrg →Occlusion Culling. This will make the Occlusion Culling window available.
2. Occlusion Culling can be used on objects that are static(none moving objects), or dynamic(moving) objects that have had their tags switched to be viewed as static. This is because Dynamic game objects cannot occlude other game objects, but you can occlude them via occlusion culling.
Note: The built-in occlusion culling of Unity is not appropriate for your Project if it builds Scene geometry at runtime.
In this example, I have a batch of static objects, but I need them to be a specific type of static — we need Ocludder Static and Occludee Static.
Occluder Static: Objects that can hide other objects depending on the camera position.
Occludee Static: Objects that can be hidden by other objects, but cannot hide other objects themselves.
3. With the occlusions specified, go to the Occlusion window and click on Bake.
The Result:
Now, when you move the camera, you can see how objects in your Scene appear and disappear accordingly while everything that needs to be visible in the Game View stays visible.
In the next article, I will talk about Reflection Probes in Unity!
Thank you for your time! If you find this content valuable, give me a follow! I aim to document my learning journey in clear, concise documentation that can help others learn, too.
I will also have some articles on Narrative Design later on!