8. Optimizing for File Size and Performance 197
8.7 Occlusion Culling (Pro Feature)
8.7 Occlusion Culling (Pro Feature)
We could all use a performance boost! Unity Pro offers something known as occlusion culling. This is a line-of-sight–based system that means the renderer will only draw when it needs to, freeing up precious CPU cycles and helping to keep up the frame rate.
8.7.1 How Does Occlusion Culling Work?
Rendering starts with farther objects being drawn first, then closer objects drawn atop them, then closer objects still atop them, and so on until the view is complete. As the objects are layered one in front of the other, we may end up with a whole host of ob- jects that are actually hidden behind other objects, which do not necessarily need to be drawn.
At runtime, occlusion culling works by disabling the renderers for unseen objects.
It uses precalculated data, which is put together in the Unity editor using a virtual cam- era, to build up information about the visibility of objects in a scene. This data com- prises cells that go together to form a binary tree. Two trees are used at runtime, one for static objects (view cells) and one for moving objects (target cells).
As the data required for occlusion culling is precalculated, everything must be set up in the editor beforehand. Your environment and the objects within it should be bro- ken into appropriately sized pieces, and all of the objects to be affected by the culling should be set to Static in the Inspector window.
Once the scene is set up with static objects, you need to go ahead and build oc- clusion data. Unity will split up the scene into cells (small areas), which will be used to calculate the visibility of the other cells and any objects within them. As you can imag- ine, these cells need to store a significant amount of data, and when you multiply that amount of data by the number of cells in a scene, it may come as no surprise that it takes quite a while for the engine to calculate it. Thankfully, you only need to do this in the editor, and once you have a setup that you are happy with, there is no need to regenerate it unless you make significant changes to the scene.
As occlusion data is substantial in size, it is important to try and get a balanced cell size. Having too many cells in the scene will eat up your RAM; too few will mean that there is no point in culling in the first place. Finding a balance can be time consuming, but it is usually an effort that will pay off in the form of improved performance at the end of it.
Unity does not break up meshes or cull separate pieces of mesh; it simply decides whether or not the renderers attached to the objects should be enabled. To be able to cull a scene correctly, the engine requires level geometry to be split into separate objects. As with the cell sizing, the actual amount of splitting may require a little ex- perimentation. A modeler should try to take occlusion into account when designing levels, as well as aim to break scene objects into reasonably-sized pieces. Having too many separated pieces in a scene will have an adverse effect on performance; a sensible method of grouping should be employed. For example, with realistically-scaled rooms of a house, there would be little benefit in going any smaller than grouping objects on a per-room basis.
The code and logistics behind the occlusion culling system is complex and the system may at first seem a little intimidating, but Unity actually makes it really easy to do. Here is a summary of what it takes for a Unity developer to make occlusion culling happen:
y You are going to check the Static box on your stationary objects and then draw a big rectangle around them.
y Once you’ve done that, you will tell Unity to calculate the data.
y After a long wait, you’ll try the scene to see whether or not the culling worked correctly. If not, tweak a value and repeat until it works properly.
That’s it. No complex skills required—just checking boxes and drawing a rectangle. If you are anything like me, that may come as a welcome relief.
8.7.2 Step 1: Static Meshes
To set up occlusion culling, start by setting all of your stationary objects in the scene to Static. The easiest way to do this is to select all of the stationary objects and click on the Static drop-down menu in the Inspector window. From the list, check “Occlu- sion Static” and select “Yes, change children” when the Confirmation window appears.
Again, select the Static drop-down menu, but the second time, check “Occludee Static”
and confirm that you want this to apply to all children.
8.7.3 Step 2: Check Occlusion Areas and Add More if Required
Open the Occlusion Culling window, which is available through the menu Windows –>Occlusion Culling. By default, the Occlusion window will appear tabbed in on top of the Inspector window. The Scene View will change to show occlusion cells automatically generated by the engine based on all of the objects that you marked as static in the scene.
It is very important that anywhere the camera might go is within the occlusion area (as shown in Figure 8.6). If your camera remains within the occlusion area, during rendering, Unity will use the occlusion data to decide what to draw. If the camera is ever allowed to move outside of the occlusion area, Unity will render everything and no culling will occur.
If the generated occlusion area does not cover everything that you need to include, or if it does not include the entire play area, you can add your own custom occlusion areas. To do this, in the Occlusion window, click the Object button to show object pa- rameters and highlight occlusion areas in the scene filter area. Next to the Create New option is a button labeled Occlusion Area. Click this to add a new GameObject to the scene; it will be named “Occlusion Area.” This new area will be automatically selected and the Occlusion window will extend to show a checkbox labeled “Is View Volume.”
When this checkbox is checked, the occlusion area will define where the camera can be and will occlude static objects.
Occlusion areas have three parameters:
y Is View Volume. When this checkbox is checked, the occlusion area will define where the camera can be and will occlude static objects.
8.7. Occlusion Culling (Pro Feature) 211
y Is Target Volume. Moving objects will not be occluded unless this checkbox is checked. By default, this is set to true.
y Target Resolution. If you are culling moving objects (you have “Is Target Vol- ume” checked), the resolution you describe here will directly affect the size of the cells used for culling moving objects in the occlusion area.
8.7.4 Step 3: Setup Occlusion Properties to Bake
In the Occlusion window, click the Bake button to show the technique, cell size, clip plane, and memory settings (Figure 8.7).
Technique. The Occlusion system currently offers three types of occlusion culling baking:
y PVS only. This method is quickest to calculate, but moving objects will not be culled. It is recommended for games with few moving objects and characters.
Portals cannot be opened or closed at runtime, as culling is based on precal- culated data.
y PVS and dynamic objects. Dynamic objects will be culled using portal culling, and static objects will be culled with precomputed visibility. Portals cannot be opened or closed at runtime, as culling is based on precalculated data.
y Automatic portal generation. This is the most CPU-intensive method. A portal culling method is used for both dynamic and static objects, and portals may be opened and closed at runtime.
For more information on occlusion portals, please consult the Unity documentation.
View cell size. This shows the size of the cells that make up the occlusion areas. Smaller values are more accurate, but use more memory.
Figure 8.6. An occlusion area in the Scene window of the Unity editor.
Near clip plane. This should be set to the nearest clip plane used by any of the cameras used in the scene during the game. It will be used to calculate visibility at close range.
Far clip plane. This value should be set to the farthest clip plane used by any of the cam- eras in the scene. It will be used to calculate visibility at a distance.
Memory limit. This sets a limit for the amount of memory available for portal culling.
This setting is only available when Technique is set to either PVS only or PVS and dy- namic objects.
8.7.5 Step 4: Bake!
Ensure that the editor is not running the game in play mode, double check the occlu- sion settings, then, in the Occlusion window, click on the Bake button at the bottom right to get baking. As Unity generates the data, you will be unable to play the game, change scenes, change occlusion settings, or close the editor. You can still minimize Unity and do other things on the system, although the CPU will be working quite hard to process the data quickly, so you may experience some slowdown across the board.
A progress bar and status message will report on the process in the bottom right of the Editor window.
8.7.6 Step 5: Did It Work?
When the Occlusion window is open (Window–>Occlusion Culling), the Scene View will have a small Occlusion Culling window of its own in the bottom right (as shown in Figure 8.8). Items can be toggled on or off in the display, and there are two modes to
Figure 8.7. Bake settings of the Occlusion window in the Unity editor.
8.7. Occlusion Culling (Pro Feature) 213
affect the window.
y Visualize. This mode allows you to see the effects of occlusion culling on the scene. For example, if you move the main camera around within the scene, you should be able to see the occlusion system hiding and showing objects. You can also watch the effects of culling within the scene as the game is playing.
y Edit. When edit mode is set, a visualization of the occlusion areas and their cells will be shown with anchors to make sizing the areas easier.
There are two ways to test your occlusion culling data. The easiest is to play the game and make sure that nothing odd is happening with the hiding and showing of objects in the scene.
A more advanced method of testing your occlusion data is to use the visualize set- ting of the Scene View’s Occlusion Culling window and move the main camera around the scene and into all of the areas that the game camera will go during game play. The Scene View will help to demonstrate which objects are being culled and why they are being culled, with a greater visibility of scene objects than an in-game camera might provide.
If you see any issues with culling, such as the entire scene disappearing or large objects disappearing at the wrong times, you will need to go back and edit either the occlusion areas or the settings in the Occlusion window. Once changes have been made, you will need to bake the occlusion data again.
If you find for any reason that you no longer need occlusion data, use the Clear but- ton in the Occlusion window.
Figure 8.8. The Scene View with occlusion culling cell visualization.