天天看點

Directional light shadows 平行光陰影 光照系列9

Directional light shadows 平行光陰影

本文檔主要是對Unity官方手冊的個人了解與總結(其實以翻譯記錄為主:>)

僅作為個人學習使用,不得作為商業用途,歡迎轉載,并請注明出處。

文章中涉及到的操作都是基于Unity2018.3版本

參考連結:https://docs.unity3d.com/Manual/DirLightShadows.html

A directional light typically simulates sunlight and a single light can illuminate the whole of a scene. This means that the shadow map will often cover a large portion of the scene at once and this makes the shadows susceptible to a problem called perspective aliasing. Perspective aliasing means that shadow map pixels seen close to the camera look enlarged and chunky compared to those farther away.

方向光通常模拟陽光,一盞光燈可以照亮整個場景。這意味着陰影映射通常會同時覆寫場景的大部分,這使得陰影容易受到透視鋸齒問題的影響。透視鋸齒意味着,與遠處的像素相比,靠近相機的陰影貼圖像素看起來更大更粗。

Directional light shadows 平行光陰影 光照系列9

Shadows in the distance (A) have an appropriate resolution, whereas shadows close to camera (B) show perspective aliasing.

距離(A)處的陰影具有适當的分辨率,而距離(B)處的陰影具有透視鋸齒。

Perspective aliasing is less noticeable when you are using soft shadows and a high resolution for the shadow map. However, using these features will increase demands on the graphics hardware and so framerate might suffer.

當你使用軟陰影和高分辨率的陰影映射時,透視鋸齒不太明顯。然而,使用這些特性會增加對圖形硬體的需求,是以幀速率可能會受到影響。

Shadow cascades 陰影級聯

The reason perspective aliasing occurs is that different areas of the shadow map are scaled disproportionately by the camera’s perspective. The shadow map from a light needs to cover only the part of the scene visible to the camera, which is defined by the camera’s view frustum. If you imagine a simple case where the directional light comes directly from above, you can see the relationship between the frustum and the shadow map.

産生透視鋸齒的原因是陰影貼圖的不同區域被相機的透視不成比例地縮放。來自光源的陰影貼圖隻需要覆寫對錄影機可見的部分場景,這是由錄影機的視錐體定義的。如果您設想一個簡單的情況,方向光直接從上面來,您可以看到截錐體和陰影映射之間的關系。

Directional light shadows 平行光陰影 光照系列9

The distant end of the frustum is covered by 20 pixels of shadow map while the near end is covered by only 4 pixels. However, both ends appear the same size onscreen. The result is that the resolution of the map is effectively much less for shadow areas that are close to the camera. (Note that in reality, the resolution is much higher than 20x20 and the map is usually not perfectly square-on to the camera.)

視錐體的遠端被20個像素的陰影貼圖覆寫,近端僅被4個像素覆寫。但是,兩端在螢幕上顯示的大小相同。結果是,對于靠近錄影機的陰影區域,地圖的分辨率實際上要低得多。(請注意,在現實中,分辨率要比20x20高得多,而且地圖通常不是完全平方的——對着相機。)

Using a higher resolution for the whole map can reduce the effect of the chunky areas but this uses up more memory and bandwidth while rendering. You will notice from the diagram, though, that a large part of the shadow map is wasted at the near end of the frustum because it will never be seen; also shadow resolution far away from the camera is likely to be too high. It is possible to split the frustum area into two zones based on distance from the camera. The zone at the near end can use a separate shadow map at a reduced size (but with the same resolution) so that the number of pixels is evened out somewhat.

對整個地圖使用更高的分辨率可以減少大塊區域的影響,但這會在渲染時消耗更多的記憶體和帶寬。但是,您将從圖中注意到,陰影映射的很大一部分在視錐體的近端浪費了,因為它将永遠不會被看到;而且陰影分辨率離相機太遠也可能太高。可以根據距離相機的距離将截頭區域劃分為兩個區域。靠近末端的區域可以使用一個縮小尺寸的單獨的陰影貼圖(但是分辨率相同),這樣像素的數量就會變得平緩一些。

Directional light shadows 平行光陰影 光照系列9

These staged reductions in shadow map size are known as cascaded shadow maps (sometimes called “Parallel Split Shadow Maps”). From the Quality window, you can set zero, two or four cascades for a given quality level.

這些陰影貼圖大小的階段性縮減稱為級聯陰影貼圖(有時稱為“平行分割陰影貼圖”)。在“品質”視窗中,可以為給定的品質級别設定0級、2級或4級級聯。

Directional light shadows 平行光陰影 光照系列9

The more cascades you use, the less your shadows will be affected by perspective aliasing, but increasing the number does come with a rendering overhead. However, this overhead is still less than it would be if you were to use a high resolution map across the whole shadow.

你使用的級聯越多,你的陰影受透視鋸齒的影響就越少,但是增加這個數量會帶來渲染開銷。然而,如果你在整個陰影中使用高分辨率的貼圖,這個開銷仍然比它要小。

Directional light shadows 平行光陰影 光照系列9

Shadow from the earlier example with four cascades

Shadow distance 陰影距離

Shadows from objects tend to become less noticeable the farther the objects are from the camera; they appear smaller onscreen and also, distant objects are usually not the focus of attention. Unity lets you take advantage of this effect by providing a Shadow Distance property in the Quality window. Objects beyond this distance (from the camera) cast no shadows at all, while the shadows from objects approaching this distance gradually fade out.

物體離相機越遠,陰影就越不明顯;它們在螢幕上顯得更小,而且,遠處的物體通常不是人們關注的焦點。Unity通過在品質視窗中提供陰影距離屬性,讓你利用這個效果。超過這個距離(從相機)的物體不會投射任何陰影,而接近這個距離的物體的陰影會逐漸消失。

Set the shadow distance as low as possible to help improve rendering performance. This works because distant objects do not need to be rendered into the shadow map at all. Additionally, the Scene often looks better with distant shadows removed. Getting the shadow distance right is especially important for performance on mobile platforms, because they don’t support shadow cascades. If the current camera far plane is smaller than the shadow distance, Unity uses the camera far plane instead of the shadow distance.

設定陰影距離盡可能低,以幫助提高渲染性能。這是可行的,因為遙遠的物體根本不需要渲染到陰影地圖。此外,如果去掉遠處的陰影,場景看起來會更好。獲得正确的陰影距離對于移動平台上的性能尤其重要,因為它們不支援陰影級聯。如果目前相機遠平面小于陰影距離,Unity使用相機遠平面代替陰影距離。

Visualising shadow parameter adjustments 可視化陰影參數調整

The Scene View has a draw mode called Shadow Cascades, which uses coloration to show the parts of the Scene using different cascade levels. Use this to help you get the shadow distance, cascade count and cascade split ratios just right. Note that this visualization use the Scene view far plane, which is usually bigger than the shadow distance, so you might need to lower the Shadow distance if you want to match the in-game behavior of the Camera with a small far plane.

場景視圖有一個稱為“陰影級聯”的繪制模式,它使用着色來顯示使用不同級聯級别的場景部分。用這個來幫助你得到陰影距離,級聯計數和級聯分割比剛剛好。注意,這種可視化使用場景視圖的遠平面,它通常比陰影距離大,是以如果你想用一個小的遠平面來比對相機的遊戲内行為,你可能需要降低陰影距離。

Directional light shadows 平行光陰影 光照系列9

Shadow Cascades draw mode in the Scene View

Shadow Pancaking 陰影Pancaking

To further prevent shadow acne we are using a technique known as Shadow pancaking. The idea is to reduce the range of the light space used when rendering the shadow map along the light direction. This lead to an increased precision in the shadow map, reducing shadow acne.

為了進一步防止陰影粉刺,我們使用一種稱為陰影Pancaking的技術。這個想法是為了減少在繪制陰影貼圖時使用的光空間範圍。這導緻陰影地圖的精度增加,減少陰影粉刺。

Directional light shadows 平行光陰影 光照系列9

A diagram showing the shadow pancaking principle

In the above digram:

The light blue circles represent the shadow casters 淺藍色的圓圈代表陰影投射者

The dark blue rectangle represents the original light space 深藍色的矩形代表原始的光照空間

The green line represents the optimized near plane (excluding any shadow casters not visible in the view frustum) 綠線表示優化後的近平面(不包括在視圖截錐體中不可見的任何陰影投射者)

Clamp these shadow casters to the near clip plane of the optimized space (in the Vertex Shader). Note that while this works well in general, it can create artifacts for very large triangles crossing the near clip plane:

将這些陰影投射者夾取在優化空間的近剪切平面上(在頂點着色器中)。注意,雖然這在一般情況下工作良好,但它需要為跨越近剪切平面非常大的三角形人為建立:

Directional light shadows 平行光陰影 光照系列9

Large triangle problem

In this case, only one vertex of the blue triangle is behind the near clip plane and gets clamped to it. However, this alters the triangle shape, and can create incorrect shadowing.

在這種情況下,隻有一個藍色三角形的頂點在近剪切平面後面,并被夾取。但是,這會改變三角形形狀,并可能建立不正确的陰影。

You can tweak the Shadow Near Plane Offset property from the Quality window to avoid this problem. This pulls back the near clip plane. However, setting this value very high eventually introduces shadow acne, because it raises the range that the shadow map needs to cover in the light direction. Alternatively, you can also tesselate the problematic shadow casting triangles. See the bias section in Shadow Overview for more information.

您可以從“品質”視窗調整“平面偏移量”屬性附近的陰影,以避免這個問題。這将拉回近剪輯平面。然而,設定這個值非常高最終會引入陰影粉刺,因為它提高了陰影映射在光方向上需要覆寫的範圍。或者,您也可以細分有問題的陰影級聯的三角形。有關更多資訊,請參見Shadow Overview中的bias部分。

繼續閱讀