07 Ray Tracing
Intro#
why ray tracing? rasterization couldn't handle global effects well
- soft shadow
- glossy reflection
- indirect illumination
- Ray tracing s accurate, but very slow
- rasterization: real-time, ray tracing: offline
Light Rays#
- 沿直线传播
- 不会相互碰撞
- 从光源到相机,可逆性 (reciprocity)
Ray Casting#
- 对每个像素投射一束光线
- 光线遇到场景中的一个点,判断这个点是否对光源可见
- 依据光照情况,渲染像素颜色
problem
仍然忽略了折射
Recursive (Whitted-Style) Ray Tracing#
Ray Equation#
Ray Intersection#
with sphere#
然后计算二次方程:
with implicit surface#
with single triangle#
Note
- 与三角形所在平面相交
- 判断交点是否在三角形内
Plane Equation#
plane equation
\(\mathbf{p}:(\mathbf{p}-\mathbf{p}^{\prime})\cdot\mathbf{N}=0 \implies ax+by+cz+d=0\)
Moller Trumbore Algorithm#
直接求解重心坐标,判断是否有 \(1-b_{1}-b_{2},b_{1},b_{2}\geq0\)
with triangle mesh#
naive solution
每个三角形面都判断光线是否过三角形
Bounding Volumes#
如果光线和包围盒不相交,一定不会与盒中的物体相交
Axis-Aligned Bounding Box (AABB)
- 任何一个面都是与坐标轴面平行的
- 横平竖直的平面求解交点更加方便
Note
- 分别求出穿过三组对面的 \(t_{\text{min}},t_{\text{max}}\)
- 求交集 \(t_{\text{enter}}=\max\{t_{\text{min}}\}, t_{{\text{exit}}}=\min\{t_{\text{max}}\}\)
- 最晚进入一个对面的时间是进入盒子时间
- 最早离开一个对面的时间是离开盒子时间
- 如果 \(t_{\text{enter}}<t_{\text{exit}}\),那么相交
ray is not a line
- \(t_{\text{exit}}<0\),盒子在光线后面,不会相交
- \(t_{\text{exit}}\geq 0, t_{\text{enter}}<0\),光源在盒子里面
- \(t_{\text{exit}}\geq 0,t_{\text{enter}}<t_{\text{exit}}\),那么相交
Uniform Spatial Partitions (Grids)#
假设
- 与 AABB 的求交非常快
- 与物体的求交相对较慢
Preprocess - Build Acceleration Grid#
Note
- 找到一个大的包围盒
- 内部划分网格
- 记录有物体的网格
Process#
Note
- 光线与一个盒子相交时,下一个相交的盒子只可能是右边或者上面的格子
- 直到与有物体的盒子相交时,进行与物体相交求解
performance
- 格子太稀疏,或者格子太密集,加速效果都不好
- \(\#\text{cells}=C\cdot\#\text{objs}, C\approx 27\text{ in 3D}\)
performance
- 物体分布均匀时,效率高
- 空的部分太多时,效率低
Spatial Partioning#
Note
- Oct-Tree: 一直划分,直到格子里的物体数量小于阈值
- KD-Tree: 分别垂直于 x y z 轴进行递归划分
- BSP-Tree: 维度高的时候计算复杂
KD-Tree#
Note
Huffman Tree,在中间节点存储子节点指针
Note
- 与一个节点的 AABB 有交点,则需要判断与其子节点是否有交点,直到叶子节点
- 判断与最小叶子节点内物体是否有交点
Bug
- 判断一个三角形与 AABB 相交比较复杂,即使三个顶点都不在盒子内,也可能相交
- 一个物体可能和不同的盒子都有交集,会出现在多个叶子节点里
Object Partition & Bounding Volume Hierarchy (BVH)#
Note
- 将 bounding box 内的物体划分为两个部分,重新计算 bounding box,直到叶子节点有足够少的三角形
- bounding box 可能相交,重叠部分越小越好
how to subdivide?
- choose a dimension to split
- heuristic #1: 总是分割最长的轴
- heuristic #2: split node at location of median object, 让两边的三角形个数相近
Note
场景变化后,要重新计算 BVH
Note
实际应用中 BVH 的应用更加广泛
Basic Radiometry (辐射度量学)#
motivation
要让场景更加真实,需要精确地定义光的属性,不能随便定义各种参数
Radiant Energy and Flux (Power)#
Basic concepts#
Radiant Energy: 电磁辐射的能量
Radiant Flux (power): 单位时间辐射能量 #photons flowing through a sensor in unit time
Radiant Intensity: the power per unit solid angle (立体角)
angles and solid andgles
- \(\theta=\frac{l}{r}\) 弧长除以半径
- \(\Omega =\frac{A}{r^2}\) 面积除以半径平方
- 微分立体角,整个球的立体角为 \(4\pi\)
- 后面会用 \(\omega\) 来表示一个单位长度的方向向量
对于一个向所有方向均匀辐射的点光源,\(I=\frac{\Phi}{4\pi}\)
Irradiance: power per unit area
Note
需要考虑 cosine law
Note
回忆平方反比,其实 intensity(单位立体角上的功率) 没有衰减,是 irradiance(单位面积接收能量) 在衰减
Radiance(Luminance): power per unit solid angle, per projected unit area
Note
下面的 cos 表示 \(\omega\) 方向上的投影面积
Tip
- 一次微分
- Irradiance: power per projected unit area
- Intensity: power per solid angle
- 两次微分
- Radiance: Irradiance per solid angle
- Radiance: Intensity per projected unit area
Exiting Radiance: 和 Radiance 一样,但是发出的能量
Irradiance vs. Radiance#
- Irradiance: 面积微元 \(\mathrm{d}A\) 上接收的总能量
- Radiance: 面积微元 \(\mathrm{d}A\) 从 \(\mathrm{d}\omega\) 方向上接收到的能量
Bidirectional Reflectance Distribution Function (BRDF)#
双向反射分布函数 从 \(\omega_{i}\) 来的 Radiance 打到 \(\mathrm{d}A\) 上,能量 \(E\) 转化到反射光线中,给出了反射光线的分布
- differential irradiance incoming (入射 irradiance): \(\mathrm{d}E(\omega_{i})=L(\omega_{i})\cos \theta_{i}\mathrm{d}\omega_{i}\)
- differential radiance exiting (due to \(\mathrm{d}E(\omega_{i})\)): \(\mathrm{d}L_{r}(\omega_{r})\)
Note
将漫反射和镜面反射一起考虑
The Reflection Equation#
Note
将所有可能的入射方向积分,得到观测方向的能量
challenge: recursive equation
光线可能弹射多次,一个面积微元的入射光可能来自另一个面积微元
The Rendering Equation#
考虑物体本身的发光,重写 reflection eqation
Note
- 假设所有的向量都是向外的
- \(\cos \theta=n\cdot w_{i}\)
进行简化:
这里的 \(L\) 是全局光照(Global Illimination)
Note
- 光线弹射0、1、2、... 次得到的光照
- rasterization 最多考虑了一次弹射(direct illumination),也就是 \(E+KE\)
- 考虑无限次光线弹射,最终亮度会收敛
Path Tracing#
Whitted-Style recap: 不准确的估计
- 遇到光滑面,考虑镜面反射光线
- 遇到漫反射面,不考虑反射光线
Monte Carlo Integration#
why
we want to solve an integral, but it can be too difficult to solve analytically.
对于定积分 \(\int_{a}^b f(x)\mathrm{d}x\),取随机变量 \(X_i\sim p(x)\),则 Monte Carlo Integration 为:
一种特殊情况是平均分布 \(X_{i}\sim p(x)=\frac{1}{b-a}\):
Note
- 采样越多,方差越小
- 在 x 上积分,就在 x 上采样
Solve Rendering Equation#
challenge
- 在半球面上积分
- recursive execution
Direct Illumination#
考虑要渲染一个像素(pixel, point)的 direct illumination
使用 Monte Carlo 进行采样
得到
direct illumination | |
---|---|
Global Illumination#
递归特性
Q 反射到 P 的光线,相当于从 P 点看 Q 点的 direct illumination
global illumination | |
---|---|
Path Tracing#
done? NO
- 光线数量爆炸 \(\#\text{rays}=N^{\#\text{bounces}}\)
- 只有 \(N=1\) 的时候,不会爆炸
global illumination, path tracing | |
---|---|
Note
Path Tracing 就是 \(N=1\) 的情况,如果 \(N\neq 1\),称为 Distributed Ray Tracing
Problem 1: Noise Control#
too noisy?
multiple paths,求平均
global illumination, path tracing, multiple paths | |
---|---|
Problem 2: Recursion#
Russian Roulette (RR)
- with probability \(0<p<1\), continue
- with probability \(1-p\), stop
- 期望进行 \(\frac{1}{1-p}\) 次
- 一个点接收到一个光线后:
- 以 \(p\) 的概率继续发射光线追踪,得到 \(L_{o}/p\) (人为除以 \(p\))
- 以 \(1-p\) 的概率不追踪,得到 \(0\)
- 那么期望是 \(E=p \cdot (L_{o}/p)+(1-p)\cdot 0=L_{o}\),期望与真实值一样!
Advanced Problem: Path Tracing is not efficient#
Note
SPP(samples per pixel) 足够大,效果才能好,但是效率会变低
Note
- 光源大的时候,并不需要太多光线
- 光源小的时候,需要很多光线
- 均匀向所有方向采样,会造成浪费,需要用非均匀的 PDF
Sampling the light#
如何实现对光源采样?
- 渲染方程在半球上积分,光源采样在光源 \(A\) 上积分 \(p=1/A\)
- 需要找到 \(A\) 和 \(\omega\) 的关系找到,将 Monte Carlo 写成在 \(A\) 上采样
根据立体角关系得到:
改变积分域:
light source v.s. other#
分成两部分考虑
- 光源部分直接在光源上采样,不用使用 RR
- 其他部分仍然使用 RR
Important
注意光源采样时,要保证光源不被遮挡
Outro#
- Path Tracing is PHOTO-REALISTIC
- Ray tracing: Previous v.s. Modern Concepts
- previous
- ray tracing == Whitted-style ray tracing
- modern
- The general solution fo light transport
- path tracing
- photon mapping
- ...
- previous
-
how to uniformly sample the hemisphere?
-
what's the best pdf?
- random number matters(low discrepancy sequences)
- 结合 hemishpere 和 light 两种采样方法,可以得到更好的效果
- radiance 不是 color,需要经过 gamma correction 才能得到 rgb color