# Real time Shadows 1

## Shadow Maping

阴影纹理

2-Pass Algorithm 两趟的算法

1. light pass：从光源方向渲染，记录物体深度，生成 shadow map
2. camera pass ：从摄像机方向渲染物体时，使用 shadow map

Image-space algorithm

Well known rendering technique

### Issues

Self occlusion ，阴影纹理精度造成的自遮挡

![image-20230202063812326](https://image-1253155090.cos.ap-nanjing.myqcloud.com/202302020638435.png)

#### Bias

使用可以变动的 Bias 项，让阴影判定条件变宽

可能会造成阴影悬空问题

#### Second-depth shadow mapping

不常用，需要有两层的深度，深度测试时比较两层深度的中间深度

这样最上面的面深度下沉就不会被自己遮挡住

#### RTR does not trust in COMPLEXITY

实时渲染不相信理论复杂度，只相信实际的效果

#### Aliasing

走样问题，image base 的方法的通用问题。

解决方案：

* 级联阴影
* 抗锯齿

## The math behind shadow mapping

实时渲染中，把不等式在一定条件下看成近似相等

重要公式：把两个函数，乘积的积分，拆成，积分的乘积

$$
∫\_Ω f(x)g(x)dω \approx \frac{∫\_Ωf(x)dx}{∫\_Ωdx} \cdot ∫\_Ωg(x)dx
$$

分母，归一化的常数，让 f(x) 的积分能量守恒

#### 条件

g(x) low support, low smooth : 实际的积分域小，变动小，两个满足一个即可

### 在阴影渲染中

![image-20230201204811533](https://image-1253155090.cos.ap-nanjing.myqcloud.com/202302012048598.png)

#### 硬阴影的理论基础：

f(x) 用 V 表示，实际上是 Shadow Test，g(x) 是全局光线积分，其在单光线直接光，Diffuse 材质的时候积分域最小并且 smooth。

如果是 glossy 材质并且对全局环境光积分，此时该方程是最不准的

## Percentage closer soft shadows (PCSS) 软阴影

由大面积光源造成的，会产生本影和半影

* 本影：面积光全部被遮挡
* 半影：面积光部分被遮挡

### Percentage Closer Filtering （PCF）

一开始用来做抗锯齿

Filter：加权平均，可以是 3x3,5x5,7x7,9x9

不是对 Shadowmap 做 Filter ，而是在和场景物体像素深度测试计算出阴影值后做 Filter

#### PCSS

接收物和遮挡物之间的距离越大，阴影越软，Filter 越大

![image-20230202070841032](https://image-1253155090.cos.ap-nanjing.myqcloud.com/202302020708119.png)

下图也可以看出，Blocker 越高，面光源在 Shadow map 上的贡献域越广，半影就越大

![image-20230202071428193](https://image-1253155090.cos.ap-nanjing.myqcloud.com/202302020714291.png)

### Algorithm

1. Blocker search
2. Penumbra estimation 半影估计
3. Percentage Closer Filtter


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://okzkx.gitbook.io/blogs/game-tech/engine/render/tutorials/games-202/3.real-time-shadows-1.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
