课程简介
课程简介
本课程聚焦路径追踪(Path Tracing)、双向路径追踪(Bidirectional Path Tracing)和光子扩散(Photon Diffusion)等高级全局光照算法,通过数学推导、代码实现和性能优化,帮助学员突破传统渲染技术的局限。课程包含3个核心模块、6个实战案例,覆盖算法原理、参数调优、GPU加速等关键技术点,适用于高质量游戏场景、影视动画等领域的渲染需求。
前置依赖课程
后续扩展课程
课程大纲
- 1.1 路径追踪原理60min
- 1.2 蒙特卡洛积分应用75min
- 2.1 双向路径追踪原理60min
- 2.2 双向路径追踪实现90min
- 3.1 光子扩散原理60min
- 3.2 光子扩散优化75min
课程资料
高级全局光照技术:深度解析与应用
第一章 路径追踪基础
1.1 路径追踪原理
路径追踪(Path Tracing)是全局光照的核心算法之一,通过模拟光子在场景中的随机传播路径,计算每个像素的辐射度(Radiance)。其数学基础为渲染方程:
$$ L_o(x, \omega_o) = L_e(x, \omega_o) + \int_{\Omega} f_r(x, \omega_i, \omega_o) L_i(x, \omega_i) \cos\theta_i \, d\omega_i $$其中:
- $L_o$:出射辐射度
- $L_e$:自发光辐射度
- $f_r$:BRDF(双向反射分布函数)
- $L_i$:入射辐射度
算法流程:
- 从相机发射一条光线,与场景交于点 \(x\)
- 根据BRDF随机选择一个出射方向 $\omega_o$
- 递归追踪该方向的光线,直到达到最大深度或击中光源
- 累积所有路径的辐射度贡献
import numpy as np
def path_tracing(scene, max_depth=5):
# 初始化像素颜色
color = np.zeros(3)
# 发射主光线
ray = scene.camera.generate_ray()
# 递归追踪
for depth in range(max_depth):
intersection = scene.intersect(ray)
if not intersection:
break
# 计算直接光照
color += intersection.material.emissive
# 随机选择新方向
new_dir = intersection.material.sample_brdf(-ray.direction)
# 更新光线
ray = Ray(intersection.position, new_dir)
return color
本节小结
- 路径追踪通过随机采样解决渲染方程的积分问题
- 递归深度直接影响渲染质量和性能
- 算法天然支持漫反射、镜面反射等复杂材质
1.2 蒙特卡洛积分应用
蒙特卡洛积分(Monte Carlo Integration)是路径追踪的核心数学工具,通过随机采样近似计算多维积分。其核心公式为:
$$ \int_{\Omega} f(x) \, dx \approx \frac{1}{N} \sum_{i=1}^N \frac{f(x_i)}{p(x_i)} $$其中 $p(x_i)$ 是采样点的概率密度函数(PDF)。
重要性采样(Importance Sampling): 通过选择与被积函数形状相似的PDF,显著降低方差。例如,对漫反射材质,使用余弦加权采样:
def cosine_weighted_sample():
# 生成半球均匀随机点
u1, u2 = np.random.rand(), np.random.rand()
theta = 2 * np.pi * u1
phi = np.arccos(np.sqrt(1 - u2))
# 转换为方向向量
x = np.sin(phi) * np.cos(theta)
y = np.sin(phi) * np.sin(theta)
z = np.cos(phi)
return np.array([x, y, z])
实战案例:直接光照计算
def compute_direct_light(intersection, lights):
total = np.zeros(3)
for light in lights:
# 采样光源上的点
light_pos = light.sample_position()
# 计算方向向量
to_light = light_pos - intersection.position
distance = np.linalg.norm(to_light)
direction = to_light / distance
# 计算遮挡
if not scene.is_occluded(intersection.position, direction, distance):
# 计算辐射度贡献
cos_theta = np.dot(intersection.normal, direction)
if cos_theta > 0:
total += light.intensity * cos_theta / light.pdf
return total
本节小结
- 蒙特卡洛积分通过随机采样近似计算积分
- 重要性采样可显著提升收敛速度
- 直接光照计算需处理光源遮挡问题
第二章 双向路径追踪
2.1 双向路径追踪原理
传统路径追踪在处理**焦散(Caustics)**等复杂光路时效率低下。双向路径追踪(Bidirectional Path Tracing, BDPT)通过同时从光源和相机发射光线,连接两端路径提升采样效率。
核心思想:
- 从相机发射 \(s\) 条路径段(Eye Path)
- 从光源发射 \(t\) 条路径段(Light Path)
- 连接所有可能的 \((s,t)\) 组合,计算辐射度贡献
数学推导: BDPT的辐射度贡献公式为:
$$ L_{s,t} = \frac{f_s f_t}{p_s p_t} \cdot \cos\theta_s \cos\theta_t \cdot G(x_s, x_t) $$其中 $G(x_s, x_t)$ 是几何项,$p_s$ 和 $p_t$ 是两端路径的采样概率。
2.2 双向路径追踪实现
关键步骤:
路径生成:
- 相机路径:使用传统路径追踪方法
- 光源路径:在光源表面随机采样起点和方向
路径连接:
- 对每对 \((s,t)\) 路径,计算连接点的可见性
- 累积有效的辐射度贡献
def bidirectional_path_tracing(scene, s_max=3, t_max=3):
total = np.zeros(3)
# 生成相机路径
eye_paths = [generate_eye_path(scene, s) for s in range(s_max)]
# 生成光源路径
light_paths = [generate_light_path(scene, t) for t in range(t_max)]
# 连接所有路径对
for s, eye_path in enumerate(eye_paths):
for t, light_path in enumerate(light_paths):
# 获取路径末端点
x_s = eye_path[-1].position
x_t = light_path[-1].position
# 检查可见性
if not scene.is_occluded(x_s, x_t - x_s, np.linalg.norm(x_t - x_s)):
# 计算辐射度贡献
contrib = compute_bdpt_contrib(eye_path, light_path)
total += contrib
return total / (s_max * t_max)
实战案例:焦散渲染
# 初始化场景:玻璃球+水面
scene = Scene()
scene.add_object(Sphere(position=(0, 0, 0), radius=0.5, material=Glass()))
scene.add_object(Plane(position=(0, -1, 0), normal=(0, 1, 0), material=Water()))
# 添加点光源
scene.add_light(PointLight(position=(0, 2, 0), intensity=100))
# 使用BDPT渲染
image = bdpt_render(scene, resolution=(512, 512))
本节小结
- BDPT通过双向采样提升复杂光路的渲染效率
- 路径连接时需处理几何项和可见性判断
- 适合渲染焦散、间接光照等传统方法难以处理的效果
第三章 光子扩散技术
3.1 光子扩散原理
光子扩散(Photon Diffusion)是**光子映射(Photon Mapping)**的核心步骤,通过模拟光子在介质中的传播和散射,计算参与介质(如烟雾、皮肤)的辐射度。
算法流程:
- 光子发射:从光源发射大量光子,记录其位置和能量
- 光子传播:根据介质属性(散射系数、吸收系数)随机改变光子方向
- 光子存储:将光子存储在空间数据结构(如KD树)中
- 辐射度估计:查询查询点周围的光子,计算辐射度
class Photon:
def __init__(self, position, direction, power):
self.position = position
self.direction = direction
self.power = power
def photon_mapping(scene, photon_count=1e6):
# 发射光子
photons = []
for _ in range(photon_count):
# 随机选择光源
light = np.random.choice(scene.lights)
# 采样光源位置和方向
pos, dir = light.sample_photon()
photons.append(Photon(pos, dir, light.intensity / photon_count))
# 传播光子
for photon in photons:
for _ in range(10): # 最大传播步数
# 根据介质属性随机散射
if np.random.rand() < scene.medium.scattering_prob:
photon.direction = sample_henyey_greenstein(scene.medium.g)
# 衰减光子能量
photon.power *= scene.medium.albedo
# 存储光子
scene.photon_map.insert(photon)
3.2 光子扩散优化
优化技术:
- 俄罗斯轮盘赌(Russian Roulette):根据光子能量动态终止传播
- 最终聚集(Final Gathering):在查询点周围发射多条光线,提升辐射度估计精度
- 光子密度估计:使用核密度估计(KDE)平滑光子分布
def estimate_radiance(scene, position, normal, photon_count=50):
# 查询附近光子
neighbors = scene.photon_map.query(position, photon_count)
if not neighbors:
return np.zeros(3)
# 计算光子贡献
total = np.zeros(3)
for photon in neighbors:
# 计算方向差异
to_photon = photon.position - position
distance = np.linalg.norm(to_photon)
direction = to_photon / distance
cos_theta = np.dot(normal, direction)
if cos_theta > 0:
# 使用核函数加权
kernel = 1 / (distance**2 + 0.01) # 避免除零
total += photon.power * cos_theta * kernel
return total / photon_count
实战案例:皮肤渲染
# 初始化皮肤材质
skin = Medium(
scattering_coeff=0.5,
absorption_coeff=0.1,
g=0.9, # 各向异性参数
albedo=0.8
)
# 创建场景:头部模型+区域光
scene = Scene()
scene.add_object(Mesh("head.obj", material=skin))
scene.add_light(AreaLight(position=(0, 2, 0), size=(1, 1), intensity=50))
# 光子映射渲染
photons = photon_mapping(scene, photon_count=5e6)
image = final_gather_render(scene, photons, resolution=(512, 512))
本节小结
- 光子扩散通过模拟光子传播计算参与介质辐射度
- 俄罗斯轮盘赌和最终聚集可显著提升性能
- 适合渲染烟雾、皮肤等半透明材质
课程总结:本课程深入讲解了路径追踪、双向路径追踪和光子扩散三大高级全局光照算法,通过理论推导、代码实现和性能优化,帮助学员掌握复杂光路渲染的核心技术。实际应用中,可根据场景需求选择合适算法或组合使用(如路径追踪+光子扩散),平衡渲染质量和效率。