Unity基础(5) Shadow Map 概述

本文详细介绍了阴影映射技术的基本原理及其应用场景。首先探讨了两种常见的阴影处理方式:ShadowMap 和 ShadowVolume。接着深入讲解了 ShadowMap 的创建过程,包括从光源视角渲染整个场景以获取 ShadowMap 的方法。此外,还讨论了如何从摄像机视角渲染场景,并对比光源视角的深度数据以判断阴影。最后,提出了多种改进 ShadowMap 质量的技术。
摘要由CSDN通过智能技术生成

这篇是自己看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

Unity中,Shadow Map是一种用于实时渲染阴影的技术,它允许你为场景中的物体创建深度信息映射,以便在光照计算时考虑阴影。以下是获取Shadow Map的基本步骤: 1. **设置光源**:首先,你需要一个能够投射阴影的光源,通常选择Point Light、Directional Light或Spot Light,并在它的组件属性中启用“Cast Shadows”选项。 2. **设置Shadow Camera**:Unity提供了一个特殊的相机——Shadow Camera,用于捕获阴影信息。打开Inspector窗口,在`Light`组件下找到`Shadow Quality`或`Shadow Settings`部分,会有一个名为`Shadow Camera`的链接。点击它,你可以创建并配置这个相机。 3. **配置Shadow Camera**:调整 Shadow Camera 的设置,如远近裁剪平面、分辨率等。确保它的焦距足够大,以便捕捉到远处的阴影细节。 4. **获取Shadow Texture**:当你运行游戏时,Shadow Camera 自动开始捕获阴影数据。在C#脚本中,你可以通过访问`light.shadowmapTexture`来获取当前阴影贴图(Texture2D)。注意,这通常是在阴影渲染阶段完成的。 5. **应用阴影**:将Shadow Texture应用到你的游戏中需要阴影效果的物体上。例如,你可以创建一个Material并在其着色器中使用`shadowCoord`UV坐标来采样阴影。 ```csharp Shader "Custom/MyShader" { Properties { _MainTex ("Base (RGB)", 2D) = "white" {} _ShadowMap ("Shadow Map", 2D) = "white" {} } Pass { CGPROGRAM #pragma vertex vert #pragma fragment frag sampler2D _MainTex; sampler2D _ShadowMap; struct appdata { float4 vertex : POSITION; float2 uv : TEXCOORD0; }; struct v2f { float2 uv : TEXCOORD0; float4 vertex : SV_POSITION; }; v2f vert (appdata v) { v2f o; o.vertex = UnityObjectToClipPos(v.vertex); o.uv = v.uv; return o; } fixed4 frag (v2f i) : SV_Target { fixed4 col = tex2D(_MainTex, i.uv); col.a *= tex2D(_ShadowMap, shadowCoord(i)).r; // 注意这里的shadowCoord函数 return col; } ENDCG } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值