Unity 3D 遮挡剔除(仅专业版) Occlusion Culling (Pro only)

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

               

Occlusion Culling is a feature that disables rendering of objects when they are not currently seen by the camera because they are obscured by other objects. This does not happen automatically in 3D computer graphics since most of the time objects farthest away from the camera are drawn first and closer objects are drawn over the top of them (this is called "overdraw"). Occlusion Culling is different from Frustum Culling. Frustum Culling only disables the renderers for objects that are outside the camera's viewing area but does not disable anything hidden from view by overdraw. Note that when you use Occlusion Culling you will still benefit from Frustum Culling.

遮挡剔除, 当一个物体被其他物体遮挡住而不在摄像机的可视范围内时不对其进行渲染。.遮挡剔除在3D图形计算中并不是自动进行的。因为在绝大多数情况下离 camera 最远的物体首先被渲染,靠近摄像机的物体后渲染并覆盖先前渲染的物体(这被称为重复渲染"overdraw"). 遮挡剔除不同于视锥体剔除. 视锥体剔除只是不渲染摄像机视角范围外的物体而对于被其他物体遮挡但依然在视角范围内的物体,则不会被剔除. 注意当你使用遮挡剔除时你依然受益于视锥体剔除(Frustum Culling).


The scene rendered without Occlusion Culling 场景渲染无遮挡剔除


The same scene rendered with Occlusion Culling 场景渲染带遮挡剔除

The occlusion culling process will go through the scene using a virtual camera to build a hierarchy of potentially visible sets of objects. This data is used at runtime by each camera to identify what 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 performance of the game.

遮挡剔除的运行将通过在场景中使用一个虚拟的摄像机来创建一个物体潜在可视性状态(set)的层级。 这些数据可以实时让每个摄像机来确定什么能看见什么看不见。通过这些数据,Unity 将确定只把可以看见的物体送去渲染。这将降低 绘制调用(draw calls) 的数量并增加 游戏的运行效率。

The data for occlusion culling is composed of cells. Each cell is a subdivision of the entire bounding volume of the scene. More specifically the cells form a binary tree. Occlusion Culling uses two trees, one for View Cells (Static Objects) and the other for Target Cells (Moving Objects). View Cells map to a list of indices that define the visible static objects which gives more accurate culling results for static objects.

遮挡剔除的数据由单元格(cells)组成, 每个单元格是在整个场景的包围体积的一部分,单元格来自一个二叉树( binary tree),遮挡剔除使用两棵树, 一个给 View Cells (静态物体) ,另一个给 Target Cells (移动物体)。 View Cells 映射到一个定义静态可视物体的索引列表 (精确剔除后的静态物体)。

It is important to keep this in mind when creating your objects because you need a good balance between the size of your objects and the size of the cells. Ideally, you shouldn't have cells that are too small in comparison with your objects but equally you shouldn't have objects that cover many cells. You can sometimes improve the culling by breaking large objects into smaller pieces. However, you can still merge small objects together to reduce draw calls and, as long as they all belong to the same cell, occlusion culling will not be affected. The collection of cells and the visibility information that determines which cells are visible from any other cell is known as a PVS (Potentially Visible Set).

非常重要的一点是在创建你的物体时要随时注意,因为你需要在物体的大小和单元格的大小间取得一个好的平衡. 理想情况下,不应该有相比于物体,太小的单元格,但同样,物体不应该覆盖许多单元格.有时你可以通过将大的物体拆成几个部分来改进遮挡剔除效果. 然而,你仍然能够将小的物体合并在一起,来降低绘制调用次数(draw calls), 在它们都属于同一个小单元格的时候, 遮挡剔除将不起作用。单元格的集合和可视信息确定哪些单元格是可见的,被认为是 PVS (潜在可视集合Potentially Visible Set)。

Setting up Occlusion Culling 设置遮挡剔除

In order to use Occlusion Culling, there is some manual setup involved. First, your level geometry must be broken into sensibly sized pieces. It is also helpful to lay out your levels into small, well defined areas that are occluded from each other by large objects such as walls, buildings, etc. The idea here is that each individual mesh will be turned on or off based on the occlusion data. So if you have one object that contains all the furniture in your room then either all or none of the entire set of furniture will be culled. This doesn't make nearly as much sense as making each piece of furniture its own mesh, so each can individually be culled based on the camera's view point.

为了使用遮挡剔除 需要进行相关的手动设置。首先,你关卡中的几何体必须被分割成合理大小的块。这也有助于布置关卡中小块的,明确界定的区域 被其他大物体遮挡(例如墙,建筑物)。这意味着每个单独的网格根据遮挡数据确定是否渲染。所以如果你有一个物体包含了房间里的所有家具,那么所有的家具要么全渲染,要么全不渲染。 使每个家具都有自己的网格,这会有不一样的感觉,那么可以根据摄像机的视点,各个物体可以单独地被剔除。

You need to tag all scene objects that you want to be part of the occlusion to Occlusion Static in the Inspector. The fastest way to do this is to multi-select the objects you want to be included in occlusion calculations, and mark them as Occlusion Static and Occludee Static.

在检视面板(Inspector) 你需要标识(tag) 所有需要应用遮挡剔除的场景物体。最快的方法是选择多个想要遮挡计算的物体,然后标记它们为Occlusion Static 和 Occludee Static。

Marking an object for Occlusion 为遮挡标记物体

When should I use Occludee Static? Transparent objects that do not occlude, as well as small objects that are unlikely to occlude other things, should be marked as Occludees, but not Occluders. This means they will be considered in occlusion by other objects, but will not be considered as occluders themselves, which will help reduce computation.

我应该在什么时候使用Occludee Static?透明物体不能遮挡,以及小物件,都不可能阻挡其他的东西,应标记为Occludees,但不遮挡。这意味着它们将被视为能被其他物体遮挡,但不会被视为作为遮挡物自身,这将有助于减少计算量。

Occlusion Culling Window 遮挡剔除窗口

For most operations dealing with Occlusion Culling, we recommend you use the Occlusion Culling Window (Window->Occlusion Culling)

对于大多数涉及遮挡剔除的操作,我们建议您使用遮挡剔除窗口(Window->Occlusion Culling)

In the Occlusion Culling Window, you can work with occluder meshes, and Occlusion Areas.

在遮挡剔除窗口,您可以使用遮挡物网格和遮挡区域。

If you are in the Object tab of the Occlusion Culling Window and have some Mesh Renderer selected in the scene, you can modify the relevant Static flags:

如果你是在遮挡剔除窗口的Object标签并在场景有一些Mesh Renderer被选择,你可以修改相关静态标识:


Occlusion Culling Window for a Mesh Renderer

If you are in the Object tab of the Occlusion Culling Window and have an Occlusion Area selected, you can work with relevant OcclusionArea properties (for more details go to the Occlusion Area section)

如果你是在遮挡剔除窗口的Object标签并有一个遮挡区域被选择,就可以使用相关OcclusionArea属性(更多细节跳转到遮挡区域章节查看)。


Occlusion Culling Window for the Occlusion Area
遮挡区域的遮挡剔除窗口

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

注意:默认如果你没有创建遮挡区域,遮挡剔除将应用到整个场景。

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

注意:每当相机在遮挡区域之外,遮挡剔除将不应用。重要的是这种遮挡区域覆盖的地方要有相机,但做遮挡区域太大,会导致烘焙更多开销。

Occlusion Culling - Bake 遮挡剔除 - 烘焙


Occlusion culling inspector bake tab.遮挡剔除面板的bake标签

Properties 属性
  • Technique技术
    Select between the types of occlusion culling baking
    选择遮挡剔除烘焙的类型
  •     PVS only
        仅PVS

    Only static objects will be occlusion culled. Dynamic objects will be culled based on the view frustrum only. this technique has the smallest overhead on the CPU, but since dynamic objects are not culled, it is only recommended for games with few moving objects and characters. Since all visibility is precomputed, you cannot open or close portals at runtime.
    只有静态的物体会遮挡剔除。动态物体只根据视图视锥体被剔除。该技术具有最小的CPU的开销,推荐在很少移动物体和人物的游戏上使用。因为所有的可见性是预先计算的,您不能实时打开或关闭入口。
  •     PVS and dynamic objects
        PVS和动态物体

    Static objects are culled using precomputed visibility. Dynamic objects are culled using portal culling. this technique is a good balance between runtime overhead and culling efficiency. Since all visibility is precomputed, you cannot open or close a portal at runtime
    静态对象使用预先计算可见性进行剔除。动态对象使用portal剔除。该技术是实时开销和剔除效率之间很好的平衡。因为所有的可见性是预先计算的,您不能实时打开或关闭入口。
  •     Automatic Portal Generation
        自动生成入口

    Portals are generated automatically. Static and dynamic objects are culled through portals. This allows you to open and close portals at runtime. This technique will cull objects most accurately, but also has the most performance overhead on the CPU.
    入口自动生成。静态和动态物体通过入口剔除。这允许你实时打开和关闭入口。这种技术剔除物体最准确,但CPU的性能开销最大。
  • View Cell Size
    视图单元格大小

    Size of each view area cell. A smaller value produces more accurate occlusion culling. The value is a tradeoff between occlusion accuracy and storage size
    每个视图区域单元格的大小, 大小越小遮挡剔除越精确. 该值是遮挡的准确性和存储大小之间的权衡。 

  • Near Clip Plane
    近裁剪平面

    Near clip plane should be set to the smallest near clip plane that will be used in the game of all the cameras.
    近裁剪面应该设置最小数值 那么游戏中所有摄像机都可用。
  • Far Clip Plane
    远裁剪平面

    Far Clip Plane used to cull the objects. Any object whose distance is greater than this value will be occluded automatically.(Should be set to the largest far clip planed that will be used in the game of all the cameras)
    远裁剪面用于剔除物体. 任何物体的距离大于这个设定值都会被自动遮挡.(远裁剪面应该设置为最大的值,可以被游戏里所有的摄像机使用)
  • Memory limit
    内存限制

    This is a hint for the PVS-based baking, not available in Automatic Portal Generation mode 
    这是一个基于PVS烘焙的提示,Automatic Portal Generation 模式下不可用。

When you have finished tweaking these values you can click on the Bake Button to start processing the Occlusion Culling data. If you are not satisfied with the results, you can click on the Clear button to remove previously calculated data.

当您调整这些值后,您可以点击烘焙Bake 按钮开始处理遮挡剔除数据。如果你不是很满意得到的结果,您可以点击"清除Clear "按钮来删除先前计算的数据。

Occlusion Culling - Visualization 遮挡剔除 - 可视化


Occlusion culling inspector visualization tab.遮挡剔除检视面板visualization(可视化) 选项卡

The near and far planes define a virtual camera that is used to calculate the occlusion data. If you have several cameras with different near or far planes, you should use the smallest near plane and the largest far plane distance of all cameras for correct inclusion of objects.

近平面和远平面定义了一个虚拟摄像机来计算遮挡剔除数据。如果你有近平面或者远平面不同的几个摄像机, 你应该把近平面设置为所有摄像机中最小的值和远平面设置为所有摄像机中最大的值,来正确调整物体的包括范围。

All the objects in the scene affect the size of the bounding volume so try to keep them all within the visible bounds of the scene.

场景里的所有物体影响包围体积的大小,请让你的所有物体保持在场景可视范围内。

When you're ready to generate the occlusion data, click the Bake button. Remember to choose the Memory Limit in the Bake tab. Lower values make the generation quicker and less precise, higher values are to be used for production quality closer to release.

当你准备好生成遮挡数据, 点击 Bake (烘焙)按钮, 记得选择烘焙(Bake )选项卡的内存限制(Memory Limit )。越小的值,生成越快,精确性越小。较高的值,产品质量更接近发布。

Bear in mind that the time taken to build the occlusion data will depend on the cell levels, the data size and the quality you have chosen. Unity will show the status of the PVS generation at the bottom of the main window.

请记住遮挡剔除的数据计算生成速度取决于你设置的cell levels(单元级别), 数据大小大小和质量品质. Unity会在主窗口底部显示 PVS 生成状态.

After the processing is done, you should see some colorful cubes in the View Area. The colored areas are regions that share the same occlusion data.

运算处理结束后, 你会在视图区域看到一些彩色的立方体. 彩色区域是共享遮挡剔除数据的区域.

Click on Clear if you want to remove all the pre-calculated data for Occlusion Culling.

如果你想删除所有遮挡剔除的事先计算好的数据(预计算数据),点击Clear 按钮.

Occlusion Area 遮挡区域

To apply occlusion culling to moving objects you have to create an Occlusion Area and then modify its size to fit the space where the moving objects will be located (of course the moving objects cannot be marked as static). You can create Occlusion Areas is by adding the Occlusion Areacomponent to an empty game object (Component->Rendering->Occlusion Area in the menus)

如果想在运动物体应用遮挡剔除,你必须创建一个Occlusion Area(遮挡区域)然后修改其大小来适应运动物体的活动空间(注意:运动物体不能被标示为静态). 您可以创建遮挡区域,把遮挡区域组件添加到一个空的游戏物体(菜单Component->Rendering->Occlusion Area )

After creating the Occlusion Area, just check the Is Target Volume checkbox to occlude moving objects.

创建遮挡区域( Occlusion Area)后, Is Target Volume  复选框打勾, 来遮挡剔除运动物体。


Occlusion Area properties for moving objects.移动物体的遮挡区域属性

  • Size 大小
    Defines the size of the Occlusion Area.定义遮挡区域大小
  • Center中心
    Sets the center of the Occlusion Area. By default this is 0,0,0 and is located in the center of the box.
    设定遮挡区域的中心。默认0,0,0 并位于盒子的中心
  • Is View Volume是否视图体
    Defines where the camera can be. Check this in order to occlude static objects that are inside this Occlusion Area.
    定义摄像机能到哪里。选中这个选项,来遮挡遮挡区域内的静态物体
  • Is Target Volume是否目标体
    Select this when you want to occlude moving objects. 
    当你想遮挡剔除运动物体时选上。
  • Target Resolution目标分辨率
    Determines how accurate the occlusion culling inside the area will be. This affects the size of the cells in an Occlusion Area. NOTE: This only affects Target Areas.
    确定区域内的遮挡剔除的精度。这影响遮挡区域的单元格大小。注意: 这个选项只对 Target Areas(移动物体)起作用
  •     Low
    This takes less time to calculate but is less accurate.
    这花费较少的时间来计算,但是不够准确。
  •     Medium
    This gives a balance between accuracy and time taken to process the occlusion 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 high resolutions, be aware it takes more time.
    当你想要比高分辨率(high resolutions)更高的精度,使用这个值。注意这会花费更多时间。
  •     Extremely High极高
    Use this value when you want to have almost exact occlusion culling on your moveable objects. Note: This setting takes a lot of time to calculate.
    使用这个值,当你想在可移动的物体上得到几乎确切的遮挡剔除。 注意:这个设定需要大量的时间来计算。

After you have added the Occlusion Area, you need to see how it divides the box into cells. To see how the occlusion area will be calculated, SelectEdit and toggle the View button in the Occlusion Culling Preview Panel.

添加完遮挡区域后, 你需要了解盒子是如何划分成单元格。要查看遮挡区域如何被计算,在遮挡剔除预览面板,选择"编辑Edit "和勾上"视图View "按钮。


Testing the generated occlusion测试生成的遮挡

After your occlusion is set up, you can test it by enabling the Occlusion Culling (in the Occlusion Culling Preview Panel in Visualize mode) and moving the Main Camera around in the scene view.

遮挡设置完毕后, 启用遮挡剔除(在遮挡剔除预览面板Occlusion Culling Preview Panel,可视化模式) 并在场景视窗移动主摄像机Main Camera进行测试。


The Occlusion View mode in Scene View场景视图 中的遮挡 View 模式

As you move the Main Camera around (whether or not you are in Play mode), you'll see various objects disable themselves. The thing you are looking for here is any error in the occlusion data. You'll recognize an error if you see objects suddenly popping into view as you move around. If this happens, your options for fixing the error are either to change the resolution (if you are playing with target volumes) or to move objects around to cover up the error. To debug problems with occlusion, you can move the Main Camera to the problematic position for spot-checking.

当你在左右移动主相机 (无论是否在播放模式下), 你将会看到各种物体禁用自己,你在这里需要找出的是遮挡数据中的任何错误。当你移动摄像机时你可能会发现会有物体突然出现在视野当中,如果这种情况发生, 通过修改分辨率来修复错误(如果是在target volumes模式播放)或左右移动物体左右掩饰错误。要调试遮挡的问题,你可以移动主相机来检查问题的位置。

When the processing is done, you should see some colorful cubes in the View Area. The blue cubes represent the cell divisions for Target Volumes. The white cubes represent cell divisions for View Volumes. If the parameters were set correctly you should see some objects not being rendered. This will be because they are either outside of the view frustum of the camera or else occluded from view by other objects.

运算处理结束后,你会在View Area看到一些不同颜色的cube, 蓝色 cubes表示的是Target Volumes 的单元划分, 白色 cubes表示的是 View Volumes的单元划分,如果参数设置正确你会看到一些物体不被渲染,这表示要么这些物体不在摄像机视角范围内要么被其他物体遮挡住了。

After occlusion is completed, if you don't see anything being occluded in your scene then try breaking your objects into smaller pieces so they can be completely contained inside the cells.

如果遮挡完成后,场景内任何物体也没有被遮挡,拆分物体至更小的块,以便它们能被完整地包含在一个单元格中。

Occlusion Portals 遮挡入口

In order to create occlusion primitive which are openable and closable at runtime, Unity uses Occlusion Portals.

为了创造实时可开启和可关闭的遮挡,Unity使用遮挡入口(Occlusion Portals)。

  • Open打开
    Indicates if the portal is open (scriptable)
    表示入口是否打开(可编写脚本)
  • Center中心
    Sets the center of the Occlusion Area. By default this is 0,0,0 and is located in the center of the box.
    设定遮挡区域的中心. 默认0,0,0 并位于盒子的中心。
  • Size大小
    Defines the size of the Occlusion Area.
    定义遮挡区域大小
           

给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow

这里写图片描述

你可能感兴趣的:(Unity 3D 遮挡剔除(仅专业版) Occlusion Culling (Pro only))