[Unity-13] Unity 3D 遮擋剔除(僅專業版) Occlusion Culling (Pro only)

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.
    定義遮擋區域大小
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章