Occlusion Culling (Pro only)
Occlusion Culling is a feature that disables rendering of objectswhen they are not currently seen by the camera because they areobscured by other objects. This does not happen automatically in 3Dcomputer graphics since most of the time objects farthest away fromthe camera are drawn first and closer objects are drawn over thetop of them (this is called "overdraw"). Occlusion Culling isdifferent from Frustum Culling. Frustum Culling only disables therenderers for objects that are outside the camera's viewing areabut does not disable anything hidden from view by overdraw. Notethat when you use Occlusion Culling you will still benefit fromFrustum Culling.
The scene rendered withoutOcclusion Culling
The same scene rendered withOcclusion Culling
The occlusion culling process will go through the scene using avirtual camera to build a hierarchy of potentially visible sets ofobjects. This data is used at runtime by each camera to identifywhat is visible and what is not. Equipped with this information,Unity will ensure only visible objects get sent to be rendered.This reduces the number of draw calls and increases the performanceof the game.
The data for occlusion culling is composed of cells. Each cell is asubdivision of the entire bounding volume of the scene. Morespecifically the cells form a binary tree. Occlusion Culling usestwo trees, one for View Cells (Static Objects) and the other forTarget Cells (Moving Objects). View Cells map to a list of indicesthat define the visible static objects which gives more accurateculling results for static objects.
It is important to keep this in mind when creating your objectsbecause you need a good balance between the size of your objectsand the size of the cells. Ideally, you shouldn't have cells thatare too small in comparison with your objects but equally youshouldn't have objects that cover many cells. You can sometimesimprove the culling by breaking large objects into smaller pieces.However, you can still merge small objects together to reduce drawcalls and, as long as they all belong to the same cell, occlusionculling will not be affected. The collection of cells and thevisibility information that determines which cells are visible fromany other cell is known as a PVS(Potentially
Setting up Occlusion Culling
In order to use Occlusion Culling, there is some manual setupinvolved. First, your level geometry must be broken into sensiblysized pieces. It is also helpful to lay out your levels into small,well defined areas that are occluded from each other by largeobjects such as walls, buildings, etc. The idea here is that eachindividual mesh will be turned on or off based on the occlusiondata. So if you have one object that contains all the furniture inyour room then either all or none of the entire set of furniturewill be culled. This doesn't make nearly as much sense as makingeach piece of furniture its own mesh, so each can individually beculled based on the camera's view point.
You need to tag all scene objects that you want to be part of theocclusion to
Marking an object forOcclusion
When should I use
Occlusion Culling Window
For most operations dealing with Occlusion Culling, we recommendyou use the Occlusion Culling Window (Window->Occlusion Culling)
In the Occlusion Culling Window, you can work with occluder meshes,and
If you are in the
Occlusion Culling Window for aMesh Renderer
If you are in the
Occlusion Culling Window forthe Occlusion Area
NOTE:
NOTE:
Occlusion Culling - Bake
Occlusion culling inspectorbake tab.
Properties
Technique | Select between the types of occlusion culling baking |
PVS only | Only static objects will be occlusion culled. Dynamic objects willbe culled based on the view frustrum only. this technique has thesmallest overhead on the CPU, but since dynamic objects are notculled, it is only recommended for games with few moving objectsand characters. Since all visibility is precomputed, you cannotopen or close portals at runtime. |
PVS and dynamic objects | Static objects are culled using precomputed visibility. Dynamicobjects are culled using portal culling. this technique is a goodbalance between runtime overhead and culling efficiency. Since allvisibility is precomputed, you cannot open or close a portal atruntime |
Automatic Portal Generation | Portals are generated automatically. Static and dynamic objects areculled through portals. This allows you to open and close portalsat runtime. This technique will cull objects most accurately, butalso has the most performance overhead on the CPU. |
View CellSize | Size of each view area cell. A smaller value produces more accurateocclusion culling. The value is a tradeoff between occlusionaccuracy and storage size |
Near ClipPlane | Near clip plane should be set to the smallest near clip plane thatwill be used in the game of all the cameras. |
Far ClipPlane | Far Clip Plane used to cull the objects. Any object whose distanceis greater than this value will be occluded automatically.(Shouldbe set to the largest far clip planed that will be used in the gameof all the cameras) |
Memorylimit | This is a hint for the PVS-based baking, not availablein |
When you have finished tweaking these values you can click onthe
Occlusion Culling - Visualization
Occlusion culling inspectorvisualization tab.
The near and far planes define a virtual camera that is used tocalculate the occlusion data. If you have several cameras withdifferent near or far planes, you should use the smallest nearplane and the largest far plane distance of all cameras for correctinclusion of objects.
All the objects in the scene affect the size of the bounding volumeso try to keep them all within the visible bounds of the scene.
When you're ready to generate the occlusion data, clickthe
Bear in mind that the time taken to build the occlusion data willdepend on the cell levels, the data size and the quality you havechosen. Unity will show the status of the PVS generation at thebottom of the main window.
After the processing is done, you should see some colorful cubes inthe View Area. The colored areas are regions that share the sameocclusion data.
Click on
Occlusion Area (Pro Only)
To apply occlusion culling to moving objects you have to createan
After creating the
Occlusion Area properties formoving objects.
Size | Defines the size of the Occlusion Area. |
Center | Sets the center of the Occlusion Area. By default this is 0,0,0 andis located in the center of the box. |
Is ViewVolume | Defines where the camera can be. Check this in order to occludestatic objects that are inside this |
Is TargetVolume | Select this when you want to occlude moving objects. |
TargetResolution | Determines how accurate the occlusion culling inside the area willbe. This affects the size of the cells in an OcclusionArea. |
Low | This takes less time to calculate but is less accurate. |
Medium | This gives a balance between accuracy and time taken to process theocclusion culling data. |
High | This takes longer to calculate but has better accuracy. |
Very High | Use this value when you want to have more accuracy than highresolutions, be aware it takes more time. |
Extremely High | Use this value when you want to have almost exact occlusion cullingon your moveable objects. Note: This setting takes a lot of time tocalculate. |
After you have added the Occlusion Area, you need to see how itdivides the box into cells. To see how the occlusion area will becalculated, Select
Testing the generated occlusion
After your occlusion is set up, you can test it by enablingthe
The Occlusion View mode inScene View
As you move the Main Camera around (whether or not you are in Playmode), you'll see various objects disable themselves. The thing youare looking for here is any error in the occlusion data. You'llrecognize an error if you see objects suddenly popping into view asyou move around. If this happens, your options for fixing the errorare either to change the resolution (if you are playing with targetvolumes) or to move objects around to cover up the error. To debugproblems with occlusion, you can move the Main Camera to theproblematic position for spot-checking.
When the processing is done, you should see some colorful cubes inthe View Area. The blue cubes represent the cell divisionsfor
After occlusion is completed, if you don't see anything beingoccluded in your scene then try breaking your objects into smallerpieces so they can be completely contained inside the cells.
Occlusion Portals
In order to create occlusion primitive which are openable andclosable at runtime, Unity uses
Open | Indicates if the portal is open (scriptable) |
Center | Sets the center of the Occlusion Area. By default this is 0,0,0 andis located in the center of the box. |
Size | Defines the size of the Occlusion Area. |