Unity基础(5) Shadow Map 概述

这篇是自己看shadow map是的一些笔记,内容稍稍凌乱,如有错误请帮忙纠正

1.常见阴影处理方式

2. Shadow Map

参考Shadow Map WikiOpenGl Tutorial 16 : Shadow mappingOpenGL Shadow Mapping
mark

2.1 创建Shadow Map

从光源的视角渲染整个场景,获得Shadow Map。对点光源而言,透视投影的视角应该表现效果的角度相同,聚光灯类似。对于方向光,应该使用平行投影。通过这次渲染,深度缓存数据被保存下来,鉴于只需要深度信息,可以禁止Color Buffer更新以及所有光源、贴图计算来节省时间。

深度数据一般保存在显存中。 深度数据应该随着场景中光源、物体的变动而修改,但有些情况是不需要的,如摄像机变动。如果存在多光源,可以对每个光源保存各自单独的一份。在很多实际应用中,可能只需要对场景中一部分物体进行深度采集。同时,为了解决深度接近被绘制物体表面的情况,可以尝试将深度偏移应用于这次渲染。 有些情况下,可以只渲染物体的背面。

2. 2 渲染场景

从摄像机自身的视角渲染场景,主要包含三个主要部分:

(1) 计算光源视角下物体坐标

为了测试Shadow Map和点的关系,需要将场景中做坐标转换到光源坐标系下,这个过程沟通过矩阵转换( matrix multiplication)获得。物体在屏幕上的坐标是通过做坐标转换获得,但是上面第二步需要光源坐标系下的位置信息。将物体世界坐标转换到光源空间的矩阵类似于渲染管线中的MV矩阵以及投影矩阵。

将世界坐标转换到光源空间坐标的矩阵与第一步中计算shadow map的矩阵相同(计算方式),通过齐次变换、透视除法将物体坐标转换到NDC坐标系,通过映射将[-1,1]空间转换到[0,1]空间,然后保存深度纹理中。上述转换在vertex shader中进行,通过插值传递到pixel shader中。

(2) 对比光源视角的深度数据
获取光源空间下物体的坐标之后,根据x/y即可获得深度纹理中的数据,和Z对比来判断是否为阴影。

  • 如果z大于D(x,y) , 物体是在遮挡物体之后,标记为失败,需要作为阴影来处理
  • 如果(x,y)是在深度纹理范围之外,由程序决定是否将其列入阴影范围(一般默认为在)

在shader实现中,这个判断是在pixel shader中实现。需要注意的是如果深度纹理在硬件中不能进行插值,阴影的变换会出现锯齿。 可以通过修改深度测试方式来实现产生soft edge,比如使用一组数值而不是简单通过一个数值来判断是否失败。

(3) 制物体或者阴影
绘制带有shadow的场景有几种方式。如果在shader中实现, depth map test 可以在pixel shader中进行,并根据其结果绘制物体或者阴影。如果不能再shadow中进行:

  1. Render the entire scene in shadow. For the most common lighting models (see Phong reflection model) this should technically be done using only the ambient component of the light, but this is usually adjusted to also include a dim diffuse light to prevent curved surfaces from appearing flat in shadow.

  2. Enable the depth map test, and render the scene lit. Areas where the depth map test fails will not be overwritten, and remain shadowed.

  3. An additional pass may be used for each additional light, using additive blending to combine their effect with the lights already drawn. (Each of these passes requires an additional previous pass to generate the associated shadow map.)

2.3 Shadow map real-time implementations

Shadow mapping的效果受深度纹理尺寸影响,比较常见的比如锯齿或者阴影边缘不连续等,一般情况下可以简以通过增加 shadow map的尺寸来减少锯齿,但是受限于内存和硬件情况,一般是不可能的。解决这个绕开这个问题的改进技术:Cascaded Shadow MapsTrapezoidal Shadow MapsLight Space Perspective Shadow mapsParallel-Split Shadow maps等。

1. Shadow Acne

Shadow Acne知乎
mark
原因:浮点计算精度以及采样问题(多个点从Depth Texture同一个点获得数据,光线角度越大,越明显)
mark
处理: shadow bias,对物体深度进行稍稍的偏移
mark

2. Peter Panning

mark
原因: shadow bias is too much.
处理:背面剔除(不是太理解)

3. Depth Map Aliasing

mark
原因:
Because the depth map has a fixed resolution the depth frequently spans more than one fragment per texel. As a result multiple fragments sample the same depth value from the depth map and come to the same shadow conclusions, which produces these jagged blocky edges.
处理:
Percentage Closer Filtering
Smoothie
Variance Shadow maps.

3. Techniques to Improve Shadow Map

参考:Common Techniques to Improve Shadow Depth Maps

3.1 Process

mark

3.2 Shadow Map Artifacts

1. Perspective Aliasing

It occurs when the mapping of pixels in view space to texels in the shadow map is not a one-to-one ratio. This is because pixels close to the near plane are closer together and require a higher shadow map resolution.
mark
mark
Perspective shadow maps (PSMs) and light space perspective shadow maps (LSPSMs) attempt to address perspective aliasing by skewing the light's projection matrix in order to place more texels near the eye where they are needed. Cascaded shadow maps (CSMs) are the most popular technique for dealing with perspective aliasing.

2. Projective Aliasing
Projective aliasing occurs when the surface normal is orthogonal to the light; these surfaces should be receiving less light based on diffuse lighting equations.
mark
3. Shadow Acne and Erroneous Self-Shadowing
mark
4. Peter Panning
Peter Panning is aggravated when there is insufficient precision in the depth buffer. Calculating tight near planes and far planes also helps avoid Peter Panning.
mark

3.3 Improve Techniques

1. Slope-Scale Depth Bias

polygons with steep slopes (relative to the light) suffer more from projective aliasing than polygons with shallow slopes (relative to the light). Because of this, each depth map value may need a different offset depending on the polygon's slope relative to the light.
mark
https://www.gamedev.net/topic/662625-slope-scale-depth-bias-shadow-map-in-hlsl/#entry5191604

2. Calculating a Tight Projection

Tightly fitting the light's projection to the view frustum increases the shadow map coverage,results in higher perspective aliasing.
mark

3. Calculating the Near Plane and Far Plane

The more closely together the planes are, the more precise the values in the depth buffer.

  • AABB-Based Near Plane and Far Plane is test.
  • Frustum-Based Near Plane and Far Plane
  • Light Frustum Intersected with Scene to Calculate Near and Far Planes
    mark

4. Moving the Light in Texel-Sized Increments

As the camera moves, the pixels along the shadows' edges brighten and darken. This cannot be seen in still images, but it is very noticeable and distracting in real time。
mark

For directional lights, the solution to this problem is to round the minimum/maximum value in X and Y (that make up the orthographic projection bounds) to pixel size increments. This can be done with a divide operation, a floor operation, and a multiply.

参考

OpenGL Shadow Mapping
OpenGl Tutorial 16 : Shadow mapping
Shadow Map Wiki
Shadow Acne知乎
Common Techniques to Improve Shadow Depth Maps
Cascaded Shadow Maps
Percentage Closer Filtering

posted @ 2017-04-11 22:08  RubbyZhang  阅读(8270)  评论(0编辑  收藏  举报