Occlusion Culling-Unity

Unity的Occlusion Culling是一种专业特性,用于优化3D场景渲染性能,通过虚拟相机构建潜在可见集来减少不可见物体的绘制。设置包括标记静态和动态对象,调整Occlusion Culling窗口参数,并烘焙数据。在烘焙后,可以使用Occlusion Areas测试和调整移动物体的遮挡效果。正确的设置能有效减少draw calls,提高游戏运行效率。
摘要由CSDN通过智能技术生成

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.

Occlusion <wbr>Culling <wbr>-Unity
The scene rendered withoutOcclusion Culling
Occlusion <wbr>Culling <wbr>-Unity
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 Visible Set).

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 Occluder Static inthe Inspector. The fastest way to do thisis to multi-select the objects you want to be included in occlusioncalculations, and mark them as OccluderStatic and Occludee Static.

Occlusion <wbr>Culling <wbr>-Unity

Marking an object forOcclusion

When should I use Occludee Static? Transparent objectsthat do not occlude, as well as small objects that are unlikely toocclude other things, should be marked asOccludees, butnot Occluders. This means they will beconsidered in occlusion by other objects, but will not beconsidered as occluders themselves, which will help reducecomputation.

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 OcclusionAreas.

If you are in the Object tab ofthe Occlusion CullingWindow and havesome MeshRenderer selected in the scene, you can modifythe relevant Static flags:

Occlusion <wbr>Culling <wbr>-Unity
Occlusion Culling Window for aMesh Renderer

If you are in the Object tab ofthe Occlusion CullingWindow and havean OcclusionArea selected, you can work with relevantOcclusionArea properties (for more details go tothe OcclusionArea section)

Occlusion <wbr>Culling <wbr>-Unity
Occlusion Culling Window forthe Occlusion Area

NOTE: By default if you don't create anyocclusion areas, occlusion culling will be applied to the wholescene.

NOTE: Whenever your camera is outsideocclusion areas, occlusion culling will not be applied. It isimportant to set up your Occlusion Areas to cover the places wherethe camera can potentially be, but making the areas too large,incurs a cost during baking.

Occlusion Culling - Bake

Occlusion <wbr>Culling <wbr>-Unity
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 AutomaticPortal Generation mode

When you have finished tweaking these values you can click onthe Bake Button to startprocessing the Occlusion Culling data. If you are not satisfiedwith the results, you can click onthe Clear button to removepreviously calculated data.

Occlusion Culling - Visualization

Occlusion <wbr>Culling <wbr>-Unity
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 Bake button. Rememberto choose the Memory Limit inthe Bake tab. Lower valuesmake the generation quicker and less precise, higher values are tobe used for production quality closer to release.

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 Clear if you want toremove all the pre-calculated data for Occlusion Culling.

Occlusion Area (Pro Only)

To apply occlusion culling to moving objects you have to createan Occlusion Area andthen modify its size to fit the space where the moving objects willbe located (of course the moving objects cannot be marked asstatic). You can create Occlusion Areas is by addingthe OcclusionArea component to an empty game object(Component->Rendering->OcclusionArea in the menus)

After creating the Occlusion Area, just checkthe Is TargetVolume checkbox to occlude movingobjects.

Occlusion <wbr>Culling <wbr>-Unity
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 Occlusion Area.
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. NOTE: This only affects TargetAreas.
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 Edit and togglethe Viewbutton inthe Occlusion Culling Preview Panel.

Occlusion <wbr>Culling <wbr>-Unity

Testing the generated occlusion

After your occlusion is set up, you can test it by enablingthe OcclusionCulling (in the Occlusion Culling PreviewPanel in Visualize mode) and movingthe Main Cameraaround in the sceneview.

Occlusion <wbr>Culling <wbr>-Unity
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 Target Volumes. The white cubesrepresent cell divisions for View Volumes. Ifthe parameters were set correctly you should see some objects notbeing rendered. This will be because they are either outside of theview frustum of the camera or else occluded from view by otherobjects.

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 Occlusion Portals.

Occlusion <wbr>Culling <wbr>-Unity
   
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.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值