Unity基础(5) Shadow Map 概述

这篇是本身看shadow map是的一些笔记,内容稍稍凌乱,若有错误请帮忙纠正html

1.常见阴影处理方式

2. Shadow Map

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

2.1 建立Shadow Map

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

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

2. 2 渲染场景

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

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

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

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

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

  • 若是z大于D(x,y) , 物体是在遮挡物体以后,标记为失败,须要做为阴影来处理
  • 若是(x,y)是在深度纹理范围以外,由程序决定是否将其列入阴影范围(通常默认为在)

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

(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

相关文章
相关标签/搜索