微表面模型

微表面模型

微表面模型

  • Note
  • 考虑如下的特征:MaskingShadowingInterreflection

法线分布函数

  • microfacet distribution function \(D(\omega_m)\):法向为 \(\omega_m\) 的微表面的面积占比
    • 【说是占比,但是积分不为 1,乘 \(\cos\) 积分才为 1】
    • 这里的面积占比指的是蓝色面积宏观表面的比例

  • \(\omega_m\):法线
  • 选择宏观表面 \(\mathrm{d}A\),那么微观表面 \(\mathrm{d}A_{\mu}\) 的投影也应该是 \(\mathrm{d}A\),于是有
    • \(\cos\theta_m=\langle{w_{m}(p),\mathbf{n}}\rangle\)
    • 一般在标准形式中,\(\mathbf{n}=(0,0,1)\)

\[ \mathrm{d}A=\int_{\mathrm{d}A}\mathrm{d}\mathrm{p}=\int_{\mathrm{d}A_{\mu}}w_m(\mathrm{p})\cos\theta_m\;\mathrm{d}\mathrm{p} \]

  • 转化为立体角积分
    • 考虑整个宏观表面 \(S\),法向为 \(\omega_m\) 微表面面积为 \(S\cdot D(\omega_{m})\cdot\mathrm{d}\omega_m\),投影为 \(S\cdot D(\omega_{m})\cos(\theta_m)\;\mathrm{d}\omega_m\)
      • 微元法,认为 \(\omega_m\) 附近的微元 \(\mathrm{d}w_m\) 法向都是 \(\omega_m\)(然后计算投影面积)

\[ S=\int_{\mathcal{H}^2}S\cdot D(\omega_{m})\cos(\theta_m)\;\mathrm{d}\omega_m \]

\[ \int_{\mathcal{H}^2}D(\omega_{m})\cos(\theta_m)\;\mathrm{d}\omega_m=1 \]

  • 纯镜面:\(D(\omega_{m})=\delta(\omega-\mathbf{n})\)

GGX

  • Trowbridge–Reitz model
    • \(\omega_m=(x_m,y_m,z_m)\)

\[ \begin{aligned} D\left(\omega_{\mathrm{m}}\right) =&\frac{1}{\pi \alpha_x \alpha_y \cos ^4 \theta_{m}\left(1+\left(\tan ^2 \theta_{m}\right)\left(\cos ^2 \phi_{m} / \alpha_x^2+\sin ^2 \phi_{m} / \alpha_y^2\right)\right)^2}\\ =&\frac{1}{\pi \alpha_x \alpha_y \left(x_m^2 / \alpha_x^2+y_m^2 / \alpha_y^2+z_m^2\right)^2}\\ \end{aligned} \]

  • 各向同性版本:\(\alpha_x=\alpha_y=\alpha\)

\[ D\left(\omega_{\mathrm{m}}\right)=\frac{\alpha^2}{\pi \left(\alpha^2\cos ^2 \theta_{m}+\sin ^2 \theta_{m}\right)^2} \]

采样各向同性 GGX

  • 采样 \(D(\omega_{m})\cos(\theta_m)\)
  • 因为积分为 1 ,归一化系数就是 1
    • 这个积分还是好积的(换元)

\[ 1 =\int_{\mathcal{H}^2}D(\omega_{m})\cos\theta_m\;\mathrm{d}\omega_m =\int_{\mathcal{H}^2}\frac{\alpha^2}{\pi \left(\alpha^2\cos ^2 \theta+\sin ^2 \theta\right)^2}\cos\theta\sin\theta\;\mathrm{d}\theta\mathrm{d}\phi \]

  • 采样:算 CDF 取逆
  • \(\theta,\phi\) 独立
    • 各向同性:\(p(\phi)=\dfrac{1}{2\pi}\)
  • 于是

\[ p(\theta)=\frac{2\alpha^2\cos\theta\sin\theta}{\left(\alpha^2\cos ^2 \theta+\sin ^2 \theta\right)^2} \]

  • CDF

\[ F(\theta_0) =\int_{0}^{\theta_0}p(\theta)\;\mathrm{d}\theta =\frac{\sin^2\theta_0}{\alpha^2\cos^2\theta_0+\sin^2\theta_0} \]

  • 反函数:0-1均匀分布变量 \(u=F(\theta_0)\)

\[ \theta_0=\arctan \left(\alpha\sqrt{\dfrac{u}{1-u}}\right) \]

Masking

  • 剔除: 背面、自遮挡

  • 计算出可见的部分
  • \(G_1(\omega, \omega_m)\):法向为 \(\omega_m\) 的微面元可见的比例,\(\omega\) 指向 view
  • 如上图,与视线垂直的面积投影为:\(\mathrm{d}A\cos\theta\)
    • 考虑整体面积 \(S\)
    • \(\max\) 剔除背面

\[ S\cdot \cos\theta=\int_{\mathcal{H}^2} S\cdot D(\omega_m)\cdot G_1(\omega, \omega_m)\cdot\max(0,\omega_m \cdot\omega)\;\mathrm{d}\omega_m \]

  • 于是有

\[ \cos\theta=\int_{\mathcal{H}^2} D(\omega_m)\cdot G_1(\omega, \omega_m)\cdot\max(0,\omega_m\cdot\omega)\;\mathrm{d}\omega_m \]

  • 假定:微表面不连续,每个微面元高度与法向是独立的,于是遮挡概率仅由观察方向 \(\omega\) 决定
    • 遮挡不再需要逐微表面计算,而是通过宏观统计值描述

\[ G_1(\omega, \omega_m)=G_1(\omega) \]

  • 于是有

\[ G_1(\omega)=\cos\theta \Bigg/\int_{\mathcal{H}^2} D(\omega_m)\cdot\max(0,\omega_m\cdot\omega)\;\mathrm{d}\omega_m \]

  • 这个被称为 Smith’s approximation,对很多 \(D(\omega_m)\) 都存在解析解
  • 引入辅助函数 \(\Lambda(\cdot)\)
    • 说是表示上有很多好处

\[ G_1(\omega_m)=\dfrac{1}{1+\Lambda(\omega_m)} \]

  • 如果 \(D(\omega_m)\) 是各向同性的 Trowbridge–Reitz distribution(GGX)

\[ \Lambda(\omega)=\frac{-1+\sqrt{1+\alpha^2\tan^2\theta}}{2} \]

  • 各向异性:\(\alpha=\sqrt{\alpha_x^2\cos^2\phi+\alpha_y^2\sin^2\phi}\)

Shadowing

  • 考虑 BSDF 的两个方向都存在不可见
    • \(\omega\):masking
    • \(\omega_i\):shadowing
  • 考虑二者独立

\[ G(\omega_o,\omega_i)=G_1(\omega_o)\cdot G_1(\omega_o) \]

  • 独立性假设过重估计了,导致偏黑
  • 更好的近似

\[ G(\omega_o,\omega_i)=\frac{1}{1+\Lambda(\omega_o)+\Lambda(\omega_i)} \]

Sampling the Distribution of Visible Normals

\[ D_{\omega}(\omega_m) = \frac{D(\omega_m)\cdot G_1(\omega)\cdot\max(0,\omega_m\cdot\omega)}{ \cos\theta} \]

  • 如此,积分和为 1,将其称为 distribution of visible normals
  • 想让采样 \(p(\omega_m)\propto D_{\omega}(\omega_m)\)
    • 等价于 \(p(\omega_m)\propto D(\omega_m)\cdot\max(0,\omega_h\cdot\omega)\)

采样思路

  • 【不太懂为什么这样子采样就能实现上面的 pdf】
  • Sampling the GGX distribution of visible normals (VNDF) is equivalent to sampling the projected area of an ellipsoid, which can be mapped to sampling the projected area of a hemisphere.
  • 将其投影到 \(\omega\) 方向上,变成圆,然后再采样圆,变换回原始的
  • 各向异性处理:一个拉伸的椭球,scale 之后转化为各向同性

img

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Vector3f Sample_wm(Vector3f w, Point2f u) const {
// <<Transform w to hemispherical configuration>>
// 转换成各向同性,方向朝外,归一化

// <<Find orthonormal basis for visible normal sampling>>
// 投影到正交基 (T1, T2, wm),要求 T1 和法向 (0,0,1) 垂直

// <<Generate uniformly distributed points on the unit disk>>
// 单位圆均匀采样,polar

// <<Warp hemispherical projection for visible normal sampling>>
// 变换到投影部分

// <<Reproject to hemisphere and transform normal to ellipsoid configuration>>
// 转换到投影前
}
cpp 代码如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Vector3f Sample_wm(Vector3f w, Point2f u) const {
// Transform _w_ to hemispherical configuration
Vector3f wh = Normalize(Vector3f(alpha_x * w.x, alpha_y * w.y, w.z));
if (wh.z < 0)
wh = -wh;
// Find orthonormal basis for visible normal sampling
Vector3f T1 = (wh.z < 0.99999f) ? Normalize(Cross(Vector3f(0, 0, 1), wh))
: Vector3f(1, 0, 0);
Vector3f T2 = Cross(wh, T1);
// Generate uniformly distributed points on the unit disk
Point2f p = SampleUniformDiskPolar(u);
// Warp hemispherical projection for visible normal sampling
Float h = std::sqrt(1 - Sqr(p.x));
p.y = Lerp((1 + wh.z) / 2, h, p.y);
// Reproject to hemisphere and transform normal to ellipsoid configuration
Float pz = std::sqrt(std::max<Float>(0, 1 - LengthSquared(Vector2f(p))));
Vector3f nh = p.x * T1 + p.y * T2 + pz * wh;
CHECK_RARE(1e-5f, nh.z == 0);
return Normalize(
Vector3f(alpha_x * nh.x, alpha_y * nh.y, std::max<Float>(1e-6f, nh.z)));
}
  • 圆内部 polar 采样

\[ \begin{array}{c} (r,\theta)=(\sqrt{\xi_1},2\pi\xi_2)\\ (x,y)=(r\cos\theta,r\sin\theta) \end{array} \]

  • 不同的 \(w\) 对应的投影情况,非垂直的蓝色半个圆被 \(\cos\theta\) scale

  • 看 T2 轴(\(y\)),均匀采样 \(x\in[-1,1]\)
    • \(y\) 的范围从 \([-1,1]\) 变换为 \([-h\cos\theta,h]\)
  • 转换到投影前
    • \(y=Mx\),法线 \(ny=(M^{-1})^{T}nx\)
    • 于是:\(nx=M^Tny\)

\[ M=\left[ \begin{array}{cc} -T1-\\ -T2-\\ -wh-\\ \end{array} \right]\times M_{\text{scale}} \]

Fresnel 项

  • 光线在场景表面发生反射+折射时,反射光能量所占的比例
    • 其取决于入射光方向、表面法向和两介质折射率
  • 极化方向:\(\perp\)\(\parallel\)

\[ F_r(\omega_i,\omega_t)=\frac{1}{2}(r_{\parallel}^2+r_{\perp}^2)\\r_{\parallel}=\frac{\eta_t\cos\theta_i-\eta_i\cos\theta_t}{\eta_t\cos\theta_i+\eta_i\cos\theta_t}\\r_{\perp}=\frac{\eta_i\cos\theta_i-\eta_t\cos\theta_t}{\eta_i\cos\theta_i+\eta_t\cos\theta_t} \]

  • \(\eta=\eta_t/\eta_i\)

Torrance–Sparrow Model

  • 需要计算 \(\omega_i\)(light direction)的 pdf
    • 因为我们是对 \(\omega_i\) 积分

反射

\[ \omega_i=-\omega_o+2(\omega_o\cdot\omega_m)\omega_m \]

  • 另外一种含义,给定 \(\omega_o\),法线能被 \(\omega_i\) 唯一确定
    • 半角矢量,half-angle/half-direction

\[ \omega_m=\dfrac{\omega_i+\omega_o}{\Vert{\omega_i+\omega_o}\Vert^2} \]

\[ \theta_m=\dfrac{\theta_i+\theta_o}{2} \]

  • \(\theta_i=2\theta_m\)
  • Jacobian

\[ \frac{\mathrm{d}\omega_m}{\mathrm{d}\omega_i} = \frac{\sin\theta_m \, \mathrm{d}\theta_m \, \mathrm{d}\phi_m}{\sin 2\theta_m \, 2 \, \mathrm{d}\theta_m \, \mathrm{d}\phi_m} = \frac{1}{4\cos\theta_m} \]

  • \(\theta_m=\omega_i\cdot\omega_m=\omega_o\cdot\omega_m\)

  • Torrance–Sparrow PDF

\[ p(\omega_i)=D_{\omega_o}(\omega_m)\frac{\mathrm{d}\omega_m}{\mathrm{d}\omega_i} =\frac{D_{\omega_o}(\omega_m)}{4(\omega_o\cdot\omega_m)} \]

  • MC 估计

\[ \frac{f_r(\mathrm{p}, \omega_o, \omega_i) L_i(\mathrm{p}, \omega_i) \vert\cos\theta_i\vert}{p(\omega_i)} \]

  • 物理直观:入射经过 \(G_1\) 遮挡,反射之后经过菲涅尔项 \(F\) 衰减

\[ F(\omega_o \cdot \omega_m) G_1(\omega_i) L_i(\mathrm{p}, \omega_i) \]

  • 二者相等

\[ f_r(\mathrm{p}, \omega_o, \omega_i) =\dfrac{D_{\omega_o}(\omega_m)F(\omega_o \cdot \omega_m) G_1(\omega_i)}{4(\omega_o\cdot\omega_m)\vert\omega_i\cdot\omega_m\vert} =\dfrac{D(\omega_m)F(\omega_o \cdot \omega_m) G(\omega_i)G(\omega_o)}{4(\omega_o\cdot\omega_m)\vert\omega_i\cdot\omega_m\vert} \]

  • 更好的 \(G\) 的近似

\[ f_r(\mathrm{p}, \omega_o, \omega_i) =\dfrac{D(\omega_m)F(\omega_o \cdot \omega_m) G(\omega_i,\omega_o)}{4(\omega_o\cdot\omega_m)\vert\omega_i\cdot\omega_m\vert} \]