天天看点

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部分。

继续阅读