简单总结一些关于PBR和IBL的知识点,顺便贴一下自己的实现仓库
软光栅实现IBL
辐射度量学
一些定义
Energy
一个光子携带的能量
Q = h c λ Q = \frac{hc}{\lambda}
Q = λ h c
h h h 是普朗克常量,c c c 是真空中光速,λ \lambda λ 是波长
Flux
单位时间内光穿过某个区域的能量
Φ = lim Δ t → 0 Δ Q Δ t = d Q d t \Phi = \lim^{}_{\Delta t \to 0}{\frac{\Delta Q}{\Delta t}} = \frac{dQ}{dt}
Φ = Δ t → 0 lim Δ t Δ Q = d t d Q
辐照度 Irradiance
单位时间通过单位面积的能量
E ( p ) = lim Δ A → 0 Δ Φ ( p ) Δ A = d Φ ( p ) d A ⊥ = d Φ ( p ) d A cos θ E(p) =\lim^{}_{\Delta A \to 0}{\frac{\Delta \Phi(p)}{\Delta A}}
= \frac{d\Phi(p)}{dA^\bot}= \frac{d\Phi(p)}{dA \cos\theta} E ( p ) = Δ A → 0 lim Δ A Δ Φ ( p ) = d A ⊥ d Φ ( p ) = d A cos θ d Φ ( p )
辐射强度 Intensity
单位时间内通过单位立体角的能量
I = lim Δ ω → 0 Δ Φ Δ ω = d Φ d ω I = \lim^{}_{\Delta \omega \to 0}\frac{\Delta \Phi}{\Delta \omega}
=\frac{d\Phi}{d\omega} I = Δ ω → 0 lim Δ ω Δ Φ = d ω d Φ
辐射度 Radiance
单位时间内通过单位立体角单位面积的能量
也可以理解为从一点向某一个方向单位时间发出的能量
L = lim Δ ω → 0 Δ E ω ( p ) Δ ω = d E ω ( p ) d ω = d Φ d ω d A ⊥ = d Φ d ω d A cos θ L = \lim^{}_{\Delta \omega \to 0}\frac{\Delta E_{\omega}(p)}{\Delta \omega}
= \frac{dE_{\omega}(p)}{d\omega}
= \frac{d\Phi}{d\omega dA^\bot}
= \frac{d\Phi}{d\omega dA \cos\theta}
L = Δ ω → 0 lim Δ ω Δ E ω ( p ) = d ω d E ω ( p ) = d ω d A ⊥ d Φ = d ω d A cos θ d Φ
渲染方程
L o ( p , ω o ) = ∫ Ω f r ( p , ω i , ω o ) L i ( p , ω i ) n ⋅ ω i d ω i L_{o}(p,\omega_{o}) = \int^{}_{\Omega}f_{r}(p,\omega_{i},\omega_{o})L_{i}(p,\omega_{i})
n \cdot \omega_{i}d\omega_{i}
L o ( p , ω o ) = ∫ Ω f r ( p , ω i , ω o ) L i ( p , ω i ) n ⋅ ω i d ω i
微表面模型
我们之前常说的漫反射和镜面反射现象其实是光线通过微表面模型反射后的一种统计学现象。
微表面模型认为,宏观上的一个平面,在微观上是由很多高低起伏的粗糙形状构成。
微表面上发生的反射都是完美的镜面发射,但是由于微平面的混乱,这些镜面反射会向着不同的方向发散开来,
产生分布范围更广泛的镜面反射,在宏观上表现为漫反射和glossy反射lobe,
而对于微平面也是十分光滑的平面来说,就会产生宏观上的镜面反射。
由于微平面小到无法逐像素区分,因此用一个粗糙度(Roughness)表示某个向量与微平面的平均法向量取向一致的概率,微平面的平均法向量就是常说的平面法向量,某个向量一般是入射光和发射光方向的中间向量。
BRDF
BRDF定义为 单位面积上的 出射辐射度 与 入射辐照度 的比值:
f r ( p , ω i , ω o ) = d L o ( p , ω o ) d E i ( p , ω i ) f_{r}(p,\omega_{i},\omega_{o}) = \frac{dL_{o}(p,\omega_{o})}{dE_{i}(p,\omega_{i})}
f r ( p , ω i , ω o ) = d E i ( p , ω i ) d L o ( p , ω o )
目前一般实时使用的BRDF模型为Cook-Torrance BRDF模型,其分为漫反射和镜面反射两个部分
f r = k d f l a m b e r t + k s f c o o k − t o r r a n c e f_{r} = k_{d}f_{lambert} + k_{s}f_{cook-torrance}
f r = k d f l a m b e r t + k s f c o o k − t o r r a n c e
光线在微表面发生碰撞时会产生折射和反射现象,其中折射是光线进入物体内部,两者的能量比满足菲涅尔方程,光线在物体内部折射时能量可能被吸收,同时路径也可能会改,也可能因为被介质散射(考虑体介质参与),最后又从物体表面射出,如果重新射出的表面点离原来的入射点很近,那么可以简单将其考虑为一种漫反射现象,一般是朝各个方向均匀射出,如果距离比较远,比如说超过一个像素范围,那么如果要达到好的渲染效果,需要进一步考虑BSSRDF反射模型。
上式中k d kd k d 式入射光被折射的能量占比,而k s ks k s 是反射部分的能量占比,根据能量守恒定律,k s + k d = 1 ks + kd = 1 k s + k d = 1
在Cook-Torrance BRDF模型中,f d f_{d} f d 为常数,采用Lambertian漫反射模型:
f l a m b e r t = c π f_{lambert} = \frac{c}{\pi}
f l a m b e r t = π c
c c c 是表面的漫反射系数albedo,在0~1之间,这里考虑了物体对光线能量的吸收,分母的π \pi π 是为了
考虑积分为1:
∫ 0 2 π ∫ 0 π 2 cos θ sin θ d θ d ϕ = π \int^{2\pi}_{0}\int^{\frac{\pi}{2}}_{0}\cos\theta\sin\theta d\theta d\phi = \pi
∫ 0 2 π ∫ 0 2 π cos θ sin θ d θ d ϕ = π
而镜面项的形式为:
f c o o k − t o r r a n c e = D F G 4 ( ω o ⋅ n ) ( ω i ⋅ n ) f_{cook-torrance} = \frac{DFG}{4(\omega_{o}\cdot n)(\omega_{i}\cdot n)}
f c o o k − t o r r a n c e = 4 ( ω o ⋅ n ) ( ω i ⋅ n ) D F G
这个公式也可以通过推导得到:
假设 θ h \theta_{h} θ h 是 ω i \omega_{i} ω i 和 ω o \omega_{o} ω o 与半程向量 ω h \omega_{h} ω h 的夹角
假设 θ \theta θ 是 ω h \omega_{h} ω h 与 n n n 的夹角
假设 n n n 是平面的法向量, a a a 是平面的粗糙度
假设 θ i \theta_{i} θ i 是入射光与平面法向量的夹角,θ o \theta_{o} θ o 是出射光与平面法向量的夹角
类似于d ω = sin θ d θ d ϕ d\omega = \sin \theta d\theta d\phi d ω = sin θ d θ d ϕ 的关系,对于平面上的法向分布概率函数有以下关系:
d A ( ω h ) = D ( ω h ) d ω h d A dA(\omega_{h}) = D(\omega_{h})d\omega_{h}dA
d A ( ω h ) = D ( ω h ) d ω h d A
D就是法线分布函数
这个点的能量通量为:
d Φ h = d E h d A ( ω h ) d\Phi_{h} = dE_{h}dA(\omega_{h})
d Φ h = d E h d A ( ω h )
出射的辐射度定义为:
L ( ω o ) = d Φ o d ω o d A ( ω o ) L(\omega_{o}) = \frac{d\Phi_{o}}{d\omega_{o}dA(\omega_{o})}
L ( ω o ) = d ω o d A ( ω o ) d Φ o
根据菲涅尔定理有:
d Φ o = F r ( ω o ) d Φ h d\Phi_{o} = F_{r}(\omega_{o})d\Phi_{h}
d Φ o = F r ( ω o ) d Φ h
代入替换一下 d Φ o d\Phi_{o} d Φ o 到 L ( ω o ) L(\omega_{o}) L ( ω o ) 中:
L ( ω o ) = F r ( ω o ) d Φ h d ω o d A ( ω o ) L(\omega_{o}) = \frac{F_{r}(\omega_{o})d\Phi_{h}}{d\omega_{o}dA(\omega_{o})}
L ( ω o ) = d ω o d A ( ω o ) F r ( ω o ) d Φ h
再替换当中的Φ h \Phi_{h} Φ h 得到:
L ( ω o ) = F r ( ω o ) d E h d A ( ω h ) d ω o d A ( ω o ) L(\omega_{o}) = \frac{F_{r}(\omega_{o})dE_{h}dA(\omega_{h})}{d\omega_{o}dA(\omega_{o})}
L ( ω o ) = d ω o d A ( ω o ) F r ( ω o ) d E h d A ( ω h )
在这里需要注意的是d A ( ω h ) dA(\omega_{h}) d A ( ω h ) 与d A ( ω o ) dA(\omega_{o}) d A ( ω o ) 的计算公式是不一样的,
前者是需要通过微表面的法线分布计算得到,后者通过确定的出射方向得到,即:
d A ( ω o ) = cos θ o d A dA(\omega_{o}) = \cos \theta_{o} dA
d A ( ω o ) = cos θ o d A
另外d E h dE_{h} d E h 的定义为:
d E h = L i d ω i cos θ h dE_{h} = L_{i}d\omega_{i} \cos \theta_{h}
d E h = L i d ω i cos θ h
然后再替换一下得到:
L ( ω o ) = F r L i d ω i cos θ h D ( ω h ) d ω h d A d ω o cos θ o d A = F r L i D ( ω h ) cos θ h d ω h d ω i cos θ o d ω o L(\omega_{o}) = \frac{F_{r}L_{i}d\omega_{i} \cos \theta_{h}D(\omega_{h})d\omega_{h}dA}
{d\omega_{o}\cos \theta_{o} dA}
=\frac{F_{r}L_{i} D(\omega_{h})\cos \theta_{h}d\omega_{h}d\omega_{i}}
{\cos \theta_{o}d\omega_{o}}
L ( ω o ) = d ω o cos θ o d A F r L i d ω i cos θ h D ( ω h ) d ω h d A = cos θ o d ω o F r L i D ( ω h ) cos θ h d ω h d ω i
然后w o w_{o} w o 和 w h w_{h} w h 是有几何关系的,假设θ o ′ \theta^{'}_{o} θ o ′ 是出射光与入射光的夹角,
那么以入射光为z轴下有如下关系 θ o = 2 ⋅ θ h \theta_{o} = 2 \cdot \theta_{h} θ o = 2 ⋅ θ h
(特殊的推导出一般情况,因为
d ω h d\omega_{h} d ω h 与 $ d\omega_{o}$ 之间的关系与坐标系的朝向无关)
d ω h d ω o = sin θ h d θ h d ϕ h sin ω o d θ o d ϕ o = sin θ h d θ h d ϕ h sin ( 2 θ h ) d ( 2 θ h ) d ϕ o = 1 4 cos θ h \frac{d\omega_{h}}{d\omega_{o}} = \frac{\sin \theta_{h} d\theta_{h} d\phi_{h}}
{\sin \omega_{o}d\theta_{o} d\phi_{o}} = \frac
{\sin \theta_{h} d\theta_{h} d\phi_{h}}{\sin (2\theta_{h}) d (2\theta_{h}) d\phi_{o}}
=\frac{1}{4 \cos \theta_{h}}
d ω o d ω h = sin ω o d θ o d ϕ o sin θ h d θ h d ϕ h = sin ( 2 θ h ) d ( 2 θ h ) d ϕ o sin θ h d θ h d ϕ h = 4 cos θ h 1
所以代入后,最后化简为:
L ( ω o ) = F r ( ω o ) L i ( ω i ) D ( ω h ) d ω i 4 cos θ o = L i ( ω i ) ⋅ F r ( ω o ) D ( ω h ) 4 cos θ o ⋅ d ω i L(\omega_{o}) = \frac{F_r(\omega_{o})L_{i}(\omega_{i})D(\omega_{h})d\omega_{i}}
{4 \cos \theta_{o}}
=L_{i}(\omega_{i}) \cdot \frac{F_r(\omega_{o})D(\omega_{h})}
{4 \cos \theta_{o}} \cdot d\omega_{i}
L ( ω o ) = 4 cos θ o F r ( ω o ) L i ( ω i ) D ( ω h ) d ω i = L i ( ω i ) ⋅ 4 cos θ o F r ( ω o ) D ( ω h ) ⋅ d ω i
那么BRDF的定义为:
L ( ω o ) = F r ( ω o ) G ( ω o , ω i ) D ( ω h ) 4 cos θ o cos θ i L(\omega_{o}) = \frac{F_r(\omega_{o})G(\omega_{o},\omega_{i})D(\omega_{h})}
{4 \cos \theta_{o} \cos \theta_{i}}
L ( ω o ) = 4 cos θ o cos θ i F r ( ω o ) G ( ω o , ω i ) D ( ω h )
一般来说,F r F_{r} F r 与 ω o , ω h , F 0 \omega_{o},\omega_{h},F_{0} ω o , ω h , F 0 有关,
D D D 与 ω h , n , α \omega_{h},n,\alpha ω h , n , α 有关,
G G G 与 n , ω i , ω o , k n,\omega_{i},\omega_{o},k n , ω i , ω o , k 有关 k k k 是 α \alpha α 针对直接光和IBL的重映射
而 G G G 需要同时考虑 Geometry Shadow 和 Mask,前者是反射光被物体自遮挡,后者是物体挡住了人眼的实现,因此 G G G 的定义为:
G ( n , ω o , ω i , α ) = G S c h l i c k G G X ( n , ω i , k ( α ) ) ⋅ G S c h l i c k G G X ( n , ω o , k ( α ) ) G(n,\omega_{o},\omega_{i},\alpha) = G_{SchlickGGX}(n,\omega_{i},k(\alpha)) \cdot
G_{SchlickGGX}(n,\omega_{o},k(\alpha))
G ( n , ω o , ω i , α ) = G S c h l i c k G G X ( n , ω i , k ( α ) ) ⋅ G S c h l i c k G G X ( n , ω o , k ( α ) )
其中:
G S c h l i c k G G X ( n , v , k ) = n ⋅ v ( n ⋅ v ) ( 1 − k ) + k G_{SchlickGGX}(n,v,k) = \frac{n \cdot v}{(n \cdot v)(1 - k) + k}
G S c h l i c k G G X ( n , v , k ) = ( n ⋅ v ) ( 1 − k ) + k n ⋅ v
k d i r e c t = ( α + 1 ) 2 8 k_{direct} = \frac{(\alpha + 1)^2}{8}
k d i r e c t = 8 ( α + 1 ) 2
k I B L = α 2 2 k_{IBL} = \frac{\alpha^2}{2}
k I B L = 2 α 2
常用的法线分布函数D有Trowbrideg-ReitzGGX,定义为:
N D F G G X T R ( n , h , α ) = α 2 π ( ( n ⋅ h ) 2 ( α 2 − 1 ) + 1 ) 2 NDF_{GGXTR}(n,h,\alpha) = \frac{\alpha^2}
{\pi((n \cdot h)^2(\alpha^2-1)+1)^2}
N D F G G X T R ( n , h , α ) = π ( ( n ⋅ h ) 2 ( α 2 − 1 ) + 1 ) 2 α 2
关于电介质的折射系数n的测量,其实是根据可见光在电介质和真空中的相对速度求得的
r ∥ = η t cos θ i − η i cos θ t η t cos θ i + η i cos θ t r_{\parallel} = \frac
{\eta_{t} \cos \theta_{i} - \eta_{i} \cos \theta_{t}}
{\eta_{t} \cos \theta_{i} + \eta_{i} \cos \theta_{t}}
r ∥ = η t cos θ i + η i cos θ t η t cos θ i − η i cos θ t
r ⊥ = η i cos θ i − η t cos θ t η i cos θ i + η t cos θ t r_{\bot} = \frac
{\eta_{i} \cos \theta_{i} - \eta_{t} \cos \theta_{t}}
{\eta_{i} \cos \theta_{i} + \eta_{t} \cos \theta_{t}}
r ⊥ = η i cos θ i + η t cos θ t η i cos θ i − η t cos θ t
F r = 1 2 ( r ∥ 2 + r ⊥ 2 ) F_{r} = \frac{1}{2}(r^2_{\parallel} + r^2_{\bot})
F r = 2 1 ( r ∥ 2 + r ⊥ 2 )
导体的 折射指数 定义为复数,形式为 η ‾ = η + i k \overline\eta = \eta + ik η = η + i k 公式就不放了
菲涅尔方程比较复杂,求解代价比较大,一般使用Fresnel-Schlick近似法求解:
F S c h l i c k ( h , v , F 0 ) = F 0 + ( 1 − F 0 ) ( 1 − ( h ⋅ v ) ) 5 F_{Schlick}(h,v,F_{0}) = F_{0} + (1 - F_{0})(1-(h \cdot v))^5
F S c h l i c k ( h , v , F 0 ) = F 0 + ( 1 − F 0 ) ( 1 − ( h ⋅ v ) ) 5
F 0 F_{0} F 0 使用折射指数计算的,但是导体的折射指数是复数,无法这么计算,所以把沿着法向入射的反射率作为 F 0 F0 F 0
Cook-Torrance 渲染方程
L o ( p , ω o ) = ∫ Ω ( k d c π + k s D F G 4 ( ω o ⋅ n ) ( ω i ⋅ n ) ) L i ( p , ω i ) n ⋅ ω i d ω i L_{o}(p,\omega_{o})=\int_{\Omega}
(k_{d}\frac{c}{\pi}+k_{s}\frac{DFG}{4(\omega_{o}\cdot n)(\omega_{i}\cdot n)})
L_{i}(p,\omega_{i})n \cdot \omega_{i}d\omega_{i}
L o ( p , ω o ) = ∫ Ω ( k d π c + k s 4 ( ω o ⋅ n ) ( ω i ⋅ n ) D F G ) L i ( p , ω i ) n ⋅ ω i d ω i
IBL
漫反射项
L d ( ω o ) = c π ∫ Ω k d L i ( ω i ) n ⋅ ω i d ω i L_{d}(\omega_{o}) = \frac{c}{\pi}\int_{\Omega}k_{d}L_{i}(\omega_{i})n\cdot \omega_{i}d\omega_{i}
L d ( ω o ) = π c ∫ Ω k d L i ( ω i ) n ⋅ ω i d ω i
因为 k d k_{d} k d 并不是一个常数,需要对其进行近似简化:
k d = ( 1 − F ( n ⋅ ω i ) ) ( 1 − m e t a l n e s s ) ≈ ( 1 − F r o u g h n e s s ( n ⋅ ω o ) ) ( 1 − m e t a l n e s s ) = k d ∗ k_{d} = (1 - F(n\cdot \omega_{i}))(1 - metalness) \\
\approx (1-F_{roughness}(n\cdot \omega_{o}))(1-metalness)
=k^{*}_{d}
k d = ( 1 − F ( n ⋅ ω i ) ) ( 1 − m e t a l n e s s ) ≈ ( 1 − F r o u g h n e s s ( n ⋅ ω o ) ) ( 1 − m e t a l n e s s ) = k d ∗
其中的 F r o u g h n e s s ( n ⋅ ω o ) F_{roughness}(n\cdot \omega_{o}) F r o u g h n e s s ( n ⋅ ω o ) 的定义为:
F r o u g h n e s s ( n ⋅ ω o ) = F 0 + ( m a x ( 1 − r o u g h n e s s , F 0 ) − F 0 ) ( 1 − n ⋅ ω o ) 5 F_{roughness}(n\cdot \omega_{o}) = F_{0} + (max(1-roughness,F_{0}) - F_{0})(1-n\cdot \omega_{o})^5
F r o u g h n e s s ( n ⋅ ω o ) = F 0 + ( m a x ( 1 − r o u g h n e s s , F 0 ) − F 0 ) ( 1 − n ⋅ ω o ) 5
采用蒙特卡洛方法进行预计算,均匀采样每一个方向的L i L_{i} L i
∫ a b f ( x ) d x = b − a N ∑ i = 1 N f ( X i ) \int^{b}_{a}f(x)dx = \frac{b-a}{N} \sum^{N}_{i=1}f(X_{i})
∫ a b f ( x ) d x = N b − a i = 1 ∑ N f ( X i )
转换为:
∫ Ω 1 π L i ( p , ω i ) d ω i ≈ π n 1 ⋅ n 2 ∑ m = 0 n 1 ∑ n = 0 n 2 L i ( p , ϕ m , θ n ) cos ( θ n ) s i n ( θ n ) \int_{\Omega}\frac{1}{\pi}L_{i}(p,\omega_{i})d\omega_{i} \approx
\frac{\pi}{n1 \cdot n2}\sum^{n1}_{m=0} \sum^{n2}_{n=0}
L_{i}(p,\phi_{m},\theta_{n})\cos(\theta_{n})sin(\theta_{n})
∫ Ω π 1 L i ( p , ω i ) d ω i ≈ n 1 ⋅ n 2 π m = 0 ∑ n 1 n = 0 ∑ n 2 L i ( p , ϕ m , θ n ) cos ( θ n ) s i n ( θ n )
也可以用余弦为权重进行半球采样 p ( ω i ) = cos θ π p(\omega_i) = \frac{\cos \theta}{\pi} p ( ω i ) = π cos θ :
1 π ∫ Ω L i ( ω i ) n ⋅ ω i d ω i ≈ 1 π 1 N ∑ i N L ( ω i ) ( n ⋅ ω i ) n ⋅ ω i π = 1 N ∑ i N L ( ω i ) \frac{1}{\pi}\int_{\Omega}L_{i}(\omega_{i})n\cdot \omega_{i}d\omega_{i} \approx
\frac{1}{\pi}\frac{1}{N}\sum^{N}_{i}\frac{L(\omega_{i})(n\cdot \omega_{i})}
{\frac{n\cdot \omega_{i}}{\pi}}
=\frac{1}{N}\sum^{N}_{i}L(\omega_{i})
π 1 ∫ Ω L i ( ω i ) n ⋅ ω i d ω i ≈ π 1 N 1 i ∑ N π n ⋅ ω i L ( ω i ) ( n ⋅ ω i ) = N 1 i ∑ N L ( ω i )
镜面项
使用了split sum近似,将L i ( ω i ) L_{i}(\omega_{i}) L i ( ω i ) 与f s ( ω i , ω o ) n ⋅ ω i f_{s}(\omega_{i},\omega_{o})n\cdot\omega_{i} f s ( ω i , ω o ) n ⋅ ω i 从积分项中分离,具体来说首先进行等价变换:
L s ( ω o ) = ∫ Ω f s ( ω i , ω o ) L i ( ω i ) n ⋅ ω i d ω i = L c ( ω o ) ∫ Ω f s ( ω i , ω o ) n ⋅ ω i d ω i L c ( ω o ) = ∫ Ω f s ( ω i , ω o ) L i ( ω i ) n ⋅ ω i d ω i ∫ Ω f s ( ω i , ω o ) n ⋅ ω i d ω i L_{s}(\omega_{o})=\int_{\Omega}f_{s}(\omega_{i},\omega_{o})L_{i}(\omega_{i})n\cdot \omega_{i}d\omega_{i}
=L_{c}(\omega_{o})\int_{\Omega}f_{s}(\omega_{i},\omega_{o})n\cdot \omega_{i}d\omega_{i}\\
L_{c}({\omega_{o}})=\frac
{\int_{\Omega}f_{s}(\omega_{i},\omega_{o})L_{i}(\omega_{i})n\cdot \omega_{i}d\omega_{i}}
{\int_{\Omega}f_{s}(\omega_{i},\omega_{o})n\cdot \omega_{i}d\omega_{i}}
L s ( ω o ) = ∫ Ω f s ( ω i , ω o ) L i ( ω i ) n ⋅ ω i d ω i = L c ( ω o ) ∫ Ω f s ( ω i , ω o ) n ⋅ ω i d ω i L c ( ω o ) = ∫ Ω f s ( ω i , ω o ) n ⋅ ω i d ω i ∫ Ω f s ( ω i , ω o ) L i ( ω i ) n ⋅ ω i d ω i
Prefilter
之后的L c ( ω o ) L_{c}(\omega_{o}) L c ( ω o ) 的化简结合了使用法线分布函数进行重要性采样,忽略F r F_{r} F r 项,替换掉G G G 项,
以及假设出射和反射方向都是宏观平面法向量方向(即假设所有反射的lobe都是差不多的,这样子会造成在掠射角
grazing angles看表面时无法观察到拖长的反射),最后可以化简为:
L c ( ω o ) ≈ ∑ i N L i ( ω i ) n ⋅ ω i ∑ i N n ⋅ ω i L_{c}(\omega_{o}) \approx \frac{\sum^{N}_{i}L_{i}(\omega_{i})n\cdot \omega_{i}}
{\sum^{N}_{i}n\cdot \omega_{i}}
L c ( ω o ) ≈ ∑ i N n ⋅ ω i ∑ i N L i ( ω i ) n ⋅ ω i
在实际计算过程中,如果只是从原始的环境贴图采样,计算出来的预过滤贴图在明亮区域周围会出现点状图案
prefiltered_color += texture (environmentMap,L).rgb * NdotL; total_weight += NdotL;
可以将一个采样点实际代表的立体角大小和pdf结合考虑,估算应该从哪个lod采样,pdf越大,说明这个方向采样的概率越大,应该采样更高分辨率的贴图,pdf越小,应该采样更低分辨率的贴图,出现点状图案是因为采样没有很好的收敛,采样次数不够多,方差还较大,如果pdf小的时候采样高分辨率的贴图就会有很大概率引入较大的方差。
float D = DistributionGGX(N,H,roughness);float NdotH = max (dot (N, H), 0. f);float HdotV = max (dot (H, V), 0. f);float pdf = D * NdotH / (4. f * HdotV) + 0.0001 f;float resolution = PrefilterMapSize;float sa_texel = 4. f * PI / (6. f * resolution *resolution);float sa_sample = 1. f / (float (SampleCount) * pdf + 0.0001 f);float mip_level = roughness == 0. f ? 0. f : 0.5 f * log2 (sa_sample / sa_texel);prefiltered_color += textureLod (environmentMap, L, mip_level).rgb * NdotL; total_weight += NdotL;
BRDF LUT
原来的积分式子移掉L c ( ω o ) L_{c}(\omega_{o}) L c ( ω o ) 之后进行拆项以及带入具体的brdf定义:
∫ Ω f s ( ω i , ω o ) n ⋅ ω i d ω i = ∫ Ω f s ( ω i , ω o ) F ( ω o , h ) F ( ω o , h ) n ⋅ ω i d ω i = ∫ Ω f s ( ω i , ω o ) F ( ω o , h ) ( F 0 + ( 1 − F 0 ) ( 1 − ω o ⋅ h ) 5 ) n ⋅ ω i d ω i F 0 ∫ Ω f s ( ω i , ω o ) F ( ω o , h ) ( 1 − ( 1 − ω o ⋅ h ) 5 ) n ⋅ ω i d ω i + ∫ Ω f s ( ω i , ω o ) F ( ω o , h ) ( 1 − ω o ⋅ h ) 5 n ⋅ ω i d ω i = F 0 ∗ s c a l e + b i a s \int_{\Omega}f_{s}(\omega_{i},\omega_{o})n\cdot\omega_{i}d\omega_{i}\\
=\int_{\Omega}f_{s}(\omega_{i},\omega_{o})\frac{F(\omega_{o},h)}{F(\omega_{o},h)}n\cdot\omega_{i}d\omega_{i}\\
=\int_{\Omega}\frac{f_{s}(\omega_{i},\omega_{o})}{F(\omega_{o},h)}(F_{0}+(1-F_{0})(1-\omega_{o}\cdot h)^5)n\cdot \omega_{i}d\omega_{i}\\
F_{0}\int_{\Omega}\frac{f_{s}(\omega_{i},\omega_{o})}{F(\omega_{o},h)}(1-(1-\omega_{o}\cdot h)^5)n\cdot\omega_{i}d\omega_{i}
+\int_{\Omega}\frac{f_{s}(\omega_{i},\omega_{o})}{F(\omega_{o},h)}(1-\omega_{o}\cdot h)^5n\cdot\omega_{i}d\omega_{i}\\
=F_{0}*scale+bias
∫ Ω f s ( ω i , ω o ) n ⋅ ω i d ω i = ∫ Ω f s ( ω i , ω o ) F ( ω o , h ) F ( ω o , h ) n ⋅ ω i d ω i = ∫ Ω F ( ω o , h ) f s ( ω i , ω o ) ( F 0 + ( 1 − F 0 ) ( 1 − ω o ⋅ h ) 5 ) n ⋅ ω i d ω i F 0 ∫ Ω F ( ω o , h ) f s ( ω i , ω o ) ( 1 − ( 1 − ω o ⋅ h ) 5 ) n ⋅ ω i d ω i + ∫ Ω F ( ω o , h ) f s ( ω i , ω o ) ( 1 − ω o ⋅ h ) 5 n ⋅ ω i d ω i = F 0 ∗ s c a l e + b i a s
这个积分的未知量维度为9维,包括ω o , n , F 0 ( 由 m e t a l n e s s 和 a l b e d o 决 定 ) , 粗 糙 度 α \omega_{o},n,F_{0}(由metalness和albedo决定),粗糙度\alpha ω o , n , F 0 ( 由 m e t a l n e s s 和 a l b e d o 决 定 ) , 粗 糙 度 α ,一般假设
BRDF都是各向同性的,也就是说不在意ϕ \phi ϕ ,只需要cos θ \cos\theta cos θ 即ω o 与 n 的 夹 角 c o s 值 \omega_{o}与n的夹角cos值 ω o 与 n 的 夹 角 c o s 值 ,因此对于
预计算scale和bias来说只有两个维度:( ω o ⋅ n ) 和 α (\omega_{o}\cdot{n})和\alpha ( ω o ⋅ n ) 和 α 。因此可以将其预计算到一张两个通道的二维纹理当中,之后在最终渲染只需要查表就可以获取scale和bias的值。
实际离散计算积分的时候,需要对s c a l e scale s c a l e 根据发现分布函数进行重要性采样,但是法线分布函数是关于ω h \omega_{h} ω h ,需要转换到ω i \omega_{i} ω i ,根据法线分布函数的定义,单位面积单位立体角微观表面朝向的百分比或概率,因此其满足如下积分等式,几何意义上可以解释为单位面积dA上所有方向的微观表面投影到该平面的面积和等于单位面积dA:
∫ Ω D ( ω h ) n ⋅ ω h d ω h d A = d A ⇒ ∫ Ω D ( ω h ) n ⋅ ω h d ω h = 1 (1) \int_{\Omega}D(\omega_{h})n\cdot\omega_{h}d\omega_{h}dA = dA \Rightarrow
\int_{\Omega}D(\omega_{h})n\cdot\omega_{h}d\omega_{h} = 1 \tag 1
∫ Ω D ( ω h ) n ⋅ ω h d ω h d A = d A ⇒ ∫ Ω D ( ω h ) n ⋅ ω h d ω h = 1 ( 1 )
d ω h d ω i = 1 4 ( ω h ⋅ ω i ) (2) \frac{d\omega_{h}}{d\omega_{i}} = \frac{1}{4(\omega_{h}\cdot\omega_{i})} \tag 2
d ω i d ω h = 4 ( ω h ⋅ ω i ) 1 ( 2 )
把(2)带入(1)得到:
p d f ( ω i ) = ω h ⋅ ω n 4 ( ω h ⋅ ω i ) D ( ω h ) pdf(\omega_{i}) = \frac{\omega_{h}\cdot\omega_{n}}{4(\omega_{h}\cdot\omega_{i})}D(\omega_{h})
p d f ( ω i ) = 4 ( ω h ⋅ ω i ) ω h ⋅ ω n D ( ω h )
因此对s c a l e scale s c a l e 进行重要性采样的公式为:
s c a l e ≈ 1 N ∑ i N D ( ω h ( k ) ) G ( ω o , ω i k ) 4 ( ω o ⋅ n ) ( ω i ( k ) ⋅ n ) ( 1 − ( 1 − ω o ⋅ ω h ( k ) ) 5 ) ( ω i ( k ) ⋅ n ) p ( ω i ( k ) ) = 1 N ∑ i N D ( ω h ( k ) ) G ( ω o , ω i k ) 4 ( ω o ⋅ n ) ( ω i ( k ) ⋅ n ) ( 1 − ( 1 − ω o ⋅ ω h ( k ) ) 5 ) ( ω i ( k ) ⋅ n ) D ( ω h ( k ) ) ( ω h ( k ) ⋅ n ) 4 ( ω o ⋅ ω h ( k ) ) = 1 N ∑ i N G ( ω o , ω i k ) ( ω o ⋅ ω h ( k ) ) ( 1 − ( 1 − ω o ⋅ ω h ( k ) ) 5 ) ( ω h ( k ) ⋅ n ) ( ω o ⋅ n ) scale \approx \frac{1}{N}\sum^{N}_{i}
\frac
{
\frac{
D(\omega^{(k)}_{h})G(\omega_{o},\omega^{k}_{i})}{
4(\omega_{o}\cdot n)(\omega^{(k)}_{i}\cdot n)
}
(1-(1-\omega_{o}\cdot\omega^{(k)}_{h})^5)(\omega^{(k)}_{i}\cdot n)
}
{p(\omega^{(k)}_{i})}\\
=\frac{1}{N}\sum^{N}_{i}
\frac
{
\frac{
D(\omega^{(k)}_{h})G(\omega_{o},\omega^{k}_{i})}{
4(\omega_{o}\cdot n)(\omega^{(k)}_{i}\cdot n)
}
(1-(1-\omega_{o}\cdot\omega^{(k)}_{h})^5)(\omega^{(k)}_{i}\cdot n)
}
{\frac
{D(\omega^{(k)}_{h})(\omega^{(k)}_{h}\cdot n)}
{4(\omega_{o}\cdot\omega^{(k)}_{h})}}\\
=\frac{1}{N}\sum^{N}_{i}
\frac
{
G(\omega_{o},\omega^{k}_{i})(\omega_{o}\cdot\omega^{(k)}_{h})
(1-(1-\omega_{o}\cdot\omega^{(k)}_{h})^5)
}
{(\omega^{(k)}_{h}\cdot n)(\omega_{o}\cdot n)}
s c a l e ≈ N 1 i ∑ N p ( ω i ( k ) ) 4 ( ω o ⋅ n ) ( ω i ( k ) ⋅ n ) D ( ω h ( k ) ) G ( ω o , ω i k ) ( 1 − ( 1 − ω o ⋅ ω h ( k ) ) 5 ) ( ω i ( k ) ⋅ n ) = N 1 i ∑ N 4 ( ω o ⋅ ω h ( k ) ) D ( ω h ( k ) ) ( ω h ( k ) ⋅ n ) 4 ( ω o ⋅ n ) ( ω i ( k ) ⋅ n ) D ( ω h ( k ) ) G ( ω o , ω i k ) ( 1 − ( 1 − ω o ⋅ ω h ( k ) ) 5 ) ( ω i ( k ) ⋅ n ) = N 1 i ∑ N ( ω h ( k ) ⋅ n ) ( ω o ⋅ n ) G ( ω o , ω i k ) ( ω o ⋅ ω h ( k ) ) ( 1 − ( 1 − ω o ⋅ ω h ( k ) ) 5 )
类似的b i a s bias b i a s 为:
b i a s ≈ 1 N ∑ i N G ( ω o , ω i k ) ( ω o ⋅ ω h ( k ) ) ( 1 − ω o ⋅ ω h ( k ) ) 5 ( ω h ( k ) ⋅ n ) ( ω o ⋅ n ) bias \approx
\frac{1}{N}\sum^{N}_{i}
\frac
{
G(\omega_{o},\omega^{k}_{i})(\omega_{o}\cdot\omega^{(k)}_{h})
(1-\omega_{o}\cdot\omega^{(k)}_{h})^5
}
{(\omega^{(k)}_{h}\cdot n)(\omega_{o}\cdot n)}
b i a s ≈ N 1 i ∑ N ( ω h ( k ) ⋅ n ) ( ω o ⋅ n ) G ( ω o , ω i k ) ( ω o ⋅ ω h ( k ) ) ( 1 − ω o ⋅ ω h ( k ) ) 5
BRDF的LUT计算代码如下:
vec2 IntegrateBRDF(float NdotV, float roughness){ vec3 V; V.x = sqrt (1. f - NdotV * NdotV); V.y = 0. f; V.z = NdotV; float A = 0. f; float B = 0. f; vec3 N = vec3 (0. f,0. f,1. f); for (uint i = 0 u; i < SampleCount; i++){ vec2 Xi = Hammersley(i, SampleCount); vec3 H = ImportanceSampleGGX(Xi, N, roughness); vec3 L = normalize (2. f * dot (V, H) * H - V); float NdotL = max (L.z, 0. f); float NdotH = max (H.z, 0. f); float VdotH = max (dot (V, H), 0. f); if (NdotL > 0. f){ float G = GeometrySmith(N, V, L, roughness); float G_Vis = (G * VdotH) / (NdotH * NdotV); float Fc = pow (1. f - VdotH, 5. f); A += (1. f - Fc) * G_Vis; B += Fc * G_Vis; } } A /= float (SampleCount); B /= float (SampleCount); return vec2 (A, B); }
最终计算
vec3 F0 = mix (vec3 (0.04 ),albedo,metalness);vec3 F = fresnelSchlickRoughness(wo,normal,F0,roughness);vec3 kS = F;vec3 kD = (vec3 (1 ) - kS) * (1 - metalness);vec3 irradiance = texture (irradianceMap,n).rgb;vec3 diffuse_color = kD * albedo * irradiance;vec3 prefilteredColor = textureLod (prefilterMap,wi,roughness*MAX_LOD).rgb;vec2 brdf = texture (brdfLUT,vec2 (max (dot (n,wo),0.0 ),roughness)).rg;vec3 specular_color = (F0 * brdf.x + brdf.y) * prefilteredColor;vec3 color = diffuse_color + specular_color;