- Table of Contents
- What is Occlusion Culling?
- Understanding Occlusion Culling Workflow
- Configuring Occlusion Culling
- Adjusting Camera, Rendering, and Texture Scales
- Alternative Methods for Occlusion Culling
- Occlusion Area
- Common Questions and Tips
- Occlusion Culling Tutorial (Beginner)
- Adjusting Draw Calls in Unity3D
Occlusion culling is a powerful optimization technique in Unity that helps improve the performance of your games by reducing the number of objects rendered on the screen. In this beginner-friendly guide, we’ll walk you through the process of implementing occlusion culling step-by-step, discussing the configurable options, providing examples, and explain how to adjust camera, rendering, and texture scales.
Table of Contents #
- What is Occlusion Culling?
- Understanding Occlusion Culling Workflow
- Configuring Occlusion Culling
- Adjusting Camera, Rendering, and Texture Scales
- Alternative Methods for Occlusion Culling
- Common Questions and Tips
What is Occlusion Culling? #
Occlusion culling is a process that prevents rendering of objects that are hidden from the camera’s view, usually by other objects. This helps in reducing the computational load on the GPU and CPU, leading to better performance and smoother gameplay.
Understanding Occlusion Culling Workflow #
The occlusion culling workflow in Unity involves the following steps:
- Marking objects as static
- Setting up occlusion areas
- Baking occlusion data
- Configuring the camera for occlusion culling
Marking Objects as Static #
Objects that should be considered for occlusion culling need to be marked as static. This can be done by selecting the GameObject in the scene, then checking the “Static” checkbox in the top right corner of the Inspector window. Remember to mark both the occluder (the object blocking the view) and occludee (the object being hidden) as static.
Setting Up Occlusion Areas #
Occlusion areas help define the space in which occlusion culling should occur. To create an occlusion area, follow these steps:
- Go to GameObject > 3D Object > Occlusion Area.
- Position and scale the Occlusion Area to encompass the area where occlusion culling should happen.
Baking Occlusion Data #
After setting up the occlusion area, you need to bake the occlusion data:
- Open the Occlusion Culling window by going to Window > AI > Occlusion Culling.
- Click the “Bake” tab.
- Adjust the configurable options as needed (we’ll cover these later in the guide).
- Click the “Bake” button.
Configuring the Camera for Occlusion Culling #
Lastly, you need to configure the camera to use occlusion culling:
- Select the main camera in your scene.
- In the Inspector, make sure the “Occlusion Culling” checkbox is checked under the Culling Mask dropdown.
Configuring Occlusion Culling #
Here, we’ll discuss some of the most important configurable options when baking occlusion data:
Smallest Occluder and Smallest Hole #
These two parameters define the smallest occluder and the smallest hole size that the occlusion culling system will consider. Adjust these values based on the scale and complexity of your scene. A smaller value results in a more detailed culling, but it may increase bake time and runtime performance cost.
Backface Threshold #
The backface threshold determines how much an object’s backfaces contribute to occlusion. A lower value means that backfaces are more likely to contribute to occlusion, which may be useful for thin, flat objects like walls. Adjust this value based on your scene’s requirements.
Adjusting Camera, Rendering, and Texture Scales #
Adjusting camera, rendering, and texture scales is crucial for optimizing your game’s performance with occlusion culling. Let’s dive into each of these areas.
Camera Settings #
Proper camera setup is essential for effective occlusion culling. Here are a few settings to keep in mind:
- Far Clipping Plane: Adjust the far clipping plane to a value that includes only objects you want to be visible. This reduces rendering overhead and works well with occlusion culling.
- Culling Mask: Ensure that the camera’s culling mask includes all the layers with objects you want to be considered for occlusion culling.
Rendering Settings #
The quality of rendering can also impact the effectiveness of occlusion culling. You can adjust the rendering settings in Unity by going to Edit > Project Settings > Quality. Here are some settings to consider:
- Pixel Light Count: Reduce the number of pixel lights to lower the rendering overhead.
- Shadow Resolution: Adjust shadow resolution to balance between visual quality and performance.
- Texture Quality: Lowering the texture quality reduces memory usage and rendering overhead.
Texture Scales #
Optimizing textures helps in reducing the overall memory usage and improves occlusion culling efficiency. Here are some tips:
- Use compressed texture formats to reduce memory usage.
- Use texture atlases to combine multiple textures into one, reducing draw calls.
- Use mipmaps for textures, as they allow smoother transitions between different texture resolutions based on distance.
Alternative Methods for Occlusion Culling #
In some cases, you might want to explore alternative methods to Unity’s built-in occlusion culling system:
- Frustum Culling: Unity automatically performs frustum culling, which removes objects outside the camera’s field of view. While not as effective as occlusion culling, it provides a basic level of optimization.
- LOD Groups: Level of Detail (LOD) groups allow you to display simplified versions of objects at a distance, reducing rendering overhead. Combine LOD groups with occlusion culling for optimal performance.
- Custom Culling Scripts: In certain situations, you might want to implement custom culling scripts that fit your game’s specific requirements. This can be done using Unity’s scripting API and custom shaders.
Occlusion Area #
An Occlusion Area in Unity is a volume that defines a region within which occlusion culling will be calculated. It helps optimize the rendering performance of your scene by determining which objects are visible to the camera and which are occluded by other objects. When an object is occluded, it means it is blocked from view by another object, so there is no need to render it.
Occlusion Culling operates based on a pre-computed set of data called the occlusion culling data, which is generated during the baking process. This data determines which objects are potentially visible from different points within the Occlusion Area. Unity uses this data to optimize the scene rendering, ensuring that only visible objects are rendered, thus reducing the workload of the GPU.
To set up an Occlusion Area and bake the occlusion data, follow these steps:
- In the Unity Editor, go to ‘Window’ > ‘Rendering’ > ‘Occlusion Culling’ to open the Occlusion Culling window.
- In the Hierarchy, right-click and choose ‘3D Object’ > ‘Occlusion Area’ to create a new Occlusion Area.
- Position and scale the Occlusion Area so that it encompasses the area where occlusion culling should be calculated. Ensure that the Occlusion Area fully covers the region where the camera can move.
- In the Inspector, you can find the ‘Occlusion Area’ component with several parameters, such as ‘Size’ and ‘Center’. You can adjust these to fine-tune the Occlusion Area.
- Make sure the objects in your scene have the ‘Static’ flag set if they don’t move. Occlusion Culling only works with static objects. To mark an object as static, select the object in the Hierarchy, and in the Inspector, check the ‘Static’ checkbox at the top-right corner.
- In the Occlusion Culling window, click the ‘Bake’ tab. Configure the baking settings as needed, and then click the ‘Bake’ button. Unity will generate the occlusion culling data for the defined Occlusion Area.
- Once the baking process is complete, Unity will automatically use the occlusion culling data to optimize scene rendering during gameplay.
With this comprehensive guide, you’re now equipped with the knowledge and skills needed to effectively implement occlusion culling in your Unity projects.
By setting up an Occlusion Area and baking the occlusion data, you can significantly improve the rendering performance of your scene, particularly in complex environments with many objects.
Common Questions and Tips #
Here are some additional tips and answers to common questions related to occlusion culling:
- For moving or dynamic objects, you can use the “Occlusion Area” component to update the occlusion culling data at runtime.
- Consider using the Profiler window (Window > Analysis > Profiler) to monitor the impact of occlusion culling on your game’s performance.
- Occlusion culling works best in environments with clear occluders, such as rooms separated by walls, rather than open landscapes.
Occlusion Culling Tutorial (Beginner) #
Adjusting Draw Calls in Unity3D #
Draw calls in Unity3D are the commands sent by the CPU to the GPU instructing it to render a group of vertices as a single object or set of objects sharing the same material and rendering properties. Reducing the number of draw calls can improve the performance of your game or application, especially on lower-end devices. Here are some techniques to optimize and reduce draw calls in Unity:
- Batching: Batching is the process of combining multiple objects that share the same material into a single draw call. Unity supports two types of batching: Static Batching and Dynamic Batching.
- Static Batching: Used for objects that do not move or change during gameplay. To enable static batching, select the objects in the Hierarchy, and in the Inspector, check the ‘Static’ checkbox at the top-right corner. In the ‘Player Settings’, make sure that ‘Static Batching’ is enabled.
- Dynamic Batching: Used for objects that move or change during gameplay. Unity automatically attempts to perform dynamic batching for objects with the same material that share similar properties. In the ‘Player Settings’, ensure that ‘Dynamic Batching’ is enabled.
- Use fewer materials: Using fewer materials can reduce the number of draw calls as Unity can batch objects sharing the same material more efficiently. You can achieve this by using Texture Atlases or by sharing materials between different objects.
- LOD (Level of Detail) system: Using the LOD system allows you to create different versions of your models with varying levels of detail based on their distance from the camera. This can help you reduce the number of vertices and draw calls for distant objects.
- GPU Instancing: If you have multiple instances of the same object with the same material, you can use GPU Instancing to reduce the draw calls. In the material’s settings, under ‘Advanced’, enable the ‘Enable GPU Instancing’ checkbox.
- Occlusion Culling: Occlusion Culling prevents rendering of objects hidden by other objects. This can save rendering resources and reduce the number of draw calls. Set up an Occlusion Area and bake the occlusion data as explained in a previous response.
- Frustum Culling: Unity automatically performs Frustum Culling, which means that it does not render objects outside the camera’s field of view. This helps to reduce the number of draw calls without any additional setup.
By using these techniques, you can configure and optimize the draw calls in Unity3D to improve the performance of your game or application. Make sure to profile and test your project to identify bottlenecks and further optimize the draw calls.