02 / 08 · RELIGHTING
Count the light, one direction at a time把每个方向的光加起来
Build the rendering equation from multiplication and addition.从乘法与加法搭出渲染方程。
A tilted card catches less light倾斜的卡片接到的光更少
n is a unit arrow out of the surface; l points from the surface toward the light. Their dot product is cos θ: multiply matching x, y, z components and add. Straight toward the light gives 1; sideways gives 0. Negative means light is behind the surface, so clamp it to 0.n 是朝向表面外侧的单位箭头,l 从表面指向光源。点积等于 cos θ:对应的 x、y、z 分量相乘再相加。正对光源为 1,侧对为 0,负数表示光源在背后,所以截断为 0。
For n=(0,0,1), l=(0.6,0,0.8), the dot product is 0×0.6+0×0+1×0.8=0.8. A fixed beam spreads over a larger tilted area; this projected-area factor gives the patch 80% of the perpendicular illumination.n=(0,0,1),l=(0.6,0,0.8) 时,点积是 0×0.6+0×0+1×0.8=0.8。同一束光落在倾斜表面会铺得更开;投影面积因子使它接到正对光源时 80% 的照明。
Why divide by π?为什么要除以 π?
Reflectance ρ is the fraction of incident power reflected by a diffuse surface. Its Lambert BRDF is ρ/π. Outgoing directions share the reflected energy: integrating the cosine over a hemisphere gives π, so division by π makes the total reflected fraction exactly ρ.漫反射反射率 ρ 是入射功率中被反射的比例,Lambert BRDF 是 ρ/π。反射能量要分配给所有出射方向:半球上余弦的积分为 π,所以除以 π 才能让总反射比例恰好为 ρ。
Here is the integral in small steps. Integrating azimuth φ from 0 to 2π gives 2π. For the angle from the normal θ, substitute u=sin θ in ∫cos θ sin θ dθ. Between 0 and π/2 it becomes [u²/2]₀¹=1/2. Multiply: 2π×1/2=π.积分可拆成小步:方位角 φ 从 0 到 2π 积分得到 2π;对与法线的夹角 θ,在 ∫cos θ sin θ dθ 中令 u=sin θ,从 0 到 π/2 得到 [u²/2]₀¹=1/2。两者相乘就是 2π×1/2=π。
From one light to the hemisphere从一盏灯到整个半球
For our ideal distant directional light, E⊥ is irradiance on a patch facing the light. Pure Lambert radiance is (ρ/π)E⊥q. With ρ=0.6, E⊥=2, q=0.8, it is 0.6×2×0.8/π≈0.3056. This is linear radiance, not a display byte. Our GGX lab additionally reserves Fresnel energy, multiplying diffuse by (1−F).对理想远处平行光,E⊥ 是正对光线的小面片所受辐照度。纯 Lambert 辐亮度为 (ρ/π)E⊥q。ρ=0.6、E⊥=2、q=0.8 时,得到 0.6×2×0.8/π≈0.3056。这是线性辐亮度,不是显示字节。GGX 实验还为 Fresnel 反射保留能量,漫反射另乘 (1−F)。
Split the sky into small solid-angle patches Δω. Each contributes BRDF × incident radiance × visibility × cosine × patch size. Add them all; shrinking patches turns the sum into an integral. Visibility V is 0 for a blocked direction and 1 for a clear one.把天空分成小立体角块 Δω。每块贡献为 BRDF × 入射辐亮度 × 可见性 × 余弦 × 块大小。全部加起来;把块越分越小,加法就变成积分。方向被挡住时可见性 V=0,畅通时 V=1。
Predict it, then run it先预测,再运行
Open this experiment →打开对应实验 →The formula meets actual code公式连接真实代码
This excerpt is extracted from the repository during the site build. SH environment lighting and inverse training are future extensions; their chapters point to the current direct-light/data boundary, not an implemented trainer.此片段在网站构建时从仓库提取。SH 环境光照与逆向训练是未来扩展;对应章节指向当前直射光/数据接口,不代表已实现训练器。
src/relight.wgsl · L24–54
fn masking(cosine: f32, a2: f32) -> f32 {
return 2.0 * cosine / max(cosine + sqrt(a2 + (1.0-a2)*cosine*cosine), 1e-6);
}
fn shade(s: Splat) -> vec3<f32> {
let n = normalize(s.normal.xyz);
let v = normalize(g.eye.xyz - s.center.xyz);
let l = g.light.xyz;
let mode = u32(g.material.z);
if mode == 3u { return n * 0.5 + 0.5; }
if mode == 4u { return s.base.xyz; }
let nl = max(dot(n,l), 0.0);
let nv = max(dot(n,v), 0.0);
if nl <= 0.0 || nv <= 0.0 { return vec3(0.0); }
let h = normalize(l+v);
let nh = max(dot(n,h), 0.0);
let vh = max(dot(v,h), 0.0);
let a = g.material.x * g.material.x;
let a2 = a*a;
let denominator = nh*nh*(a2-1.0)+1.0;
let d = a2 / (3.14159265359 * denominator * denominator);
let f0 = mix(vec3(0.04), s.base.xyz, g.material.y);
let fresnel = f0 + (1.0-f0)*pow(1.0-vh, 5.0);
let geometry = masking(nl,a2)*masking(nv,a2);
let specular = d*fresnel*geometry / max(4.0*nl*nv, 1e-6);
let diffuse = (1.0-fresnel)*(1.0-g.material.y)*s.base.xyz/3.14159265359;
var brdf = diffuse+specular;
if mode == 1u { brdf = diffuse; }
if mode == 2u { brdf = specular; }
// One white distant light, no occlusion, no indirect illumination.
return brdf * nl * g.light.w;
}Check your understanding检查是否真正理解
What happens to pure Lambert radiance when q changes from 0.8 to 0.4?q 从 0.8 变为 0.4,纯 Lambert 辐亮度怎样变化?
Reveal the reasoning展开推理
With material and light fixed, it halves. This applies before nonlinear tone mapping.材质与光照固定时,辐亮度减半。这在非线性色调映射之前成立。