Algorithm Search⌘K

Modeladvanced

Smith masking-shadowing function

Also called Smith G term, Smith shadowing function, height-correlated Smith G.

The G factor of a microfacet BRDF. It assumes facet heights and normals are uncorrelated and gives G1 = 1/(1+Lambda), with a closed form for GGX and a separable and a height-correlated G2.

The Smith masking-shadowing function is the GG factor of a microfacet BRDF. A facet that faces both the light and the viewer can still be hidden from the viewer by other facets (masking) or be in the shadow of other facets (shadowing). The Smith model assumes that the height of a facet does not depend on its normal. Under that assumption the visible fraction from a direction ω\omega has the form

G1(ω,m)=χ+(ω⋅m)1+Λ(ω),G_1(\omega, m) = \frac{\chi^+(\omega \cdot m)}{1 + \Lambda(\omega)},

where χ+\chi^+ is 1 for a positive argument and 0 otherwise, and Λ\Lambda depends only on the distribution of slopes. For GGX, Λ\Lambda has a closed form. Smith derived the shadowing of a random rough surface in 1967 for radar. Walter and others brought it to rendering. Heitz (2014) showed that the Smith function and the V-cavity function are both exact for the microsurfaces that they assume, and that the Smith microsurface gives better results at grazing angles. This page gives the factor GG; the GGX page gives DD and Schlick's approximation gives FF.

The machine

The microsurface. A height field over the macro surface, with the macro normal nn as the zz axis and a distribution DD of facet normals normalized by projected area. A direction ω\omega makes the angle θ\theta with nn, and μ=cos⁡θ>0\mu = \cos\theta > 0.

The masking function G1(ω,m)G_1(\omega, m). The fraction of the facets with normal mm that a ray from the direction ω\omega sees, with no other facet in between. Facets that face away, ω⋅m≤0\omega \cdot m \le 0, are never visible.

The constraint. Seen from ω\omega, the visible facets project onto exactly the projected area of the macro surface. With projected areas normalized per unit macro area,

∫H2G1(ω,m) max⁡(0,ω⋅m) D(m) dωm=cos⁡θ.\int_{H^2} G_1(\omega, m)\, \max(0, \omega \cdot m)\, D(m)\, d\omega_m = \cos\theta.

Heitz (2014, Sections 2.4 and 3.5) derives this constraint and uses it as the weak white furnace test: a masking function that fails it does not describe any microsurface.

The Smith assumption. If the heights and the normals of the facets are not correlated, the masking does not depend on mm except through χ+\chi^+, so G1=χ+(ω⋅m) g(ω)G_1 = \chi^+(\omega \cdot m)\, g(\omega). Putting this into the constraint gives

g(ω)=cos⁡θ∫max⁡(0,ω⋅m) D(m) dωm=11+Λ(ω),Λ(ω)=1cos⁡θ∫max⁡(0,ω⋅m) D(m) dωm−1.g(\omega) = \frac{\cos\theta}{\int \max(0, \omega \cdot m)\, D(m)\, d\omega_m} = \frac{1}{1 + \Lambda(\omega)}, \qquad \Lambda(\omega) = \frac{1}{\cos\theta} \int \max(0, \omega \cdot m)\, D(m)\, d\omega_m - 1.

Λ\Lambda is the projected area of the facets that face ω\omega but are hidden, per unit of visible area. For GGX with the parameter α\alpha (Heitz 2014, Equation 72),

Λ(ω)=−1+1+α2tan⁡2θ2,G1(ω)=21+1+α2tan⁡2θ,\Lambda(\omega) = \frac{-1 + \sqrt{1 + \alpha^2 \tan^2\theta}}{2}, \qquad G_1(\omega) = \frac{2}{1 + \sqrt{1 + \alpha^2 \tan^2\theta}},

the second form being the one of Walter et al. (2007). For an anisotropic GGX, α\alpha is replaced by αx2cos⁡2ϕ+αy2sin⁡2ϕ\sqrt{\alpha_x^2 \cos^2\phi + \alpha_y^2 \sin^2\phi} for the azimuth ϕ\phi of ω\omega, by the stretch invariance of Heitz's Section 5.

The joint function G2(l,v,m)G_2(l, v, m). A BRDF needs the fraction of facets that are visible from vv and lit from ll. Two forms are common (Heitz 2014, Section 6):

G2sep=G1(l,m) G1(v,m),G2hc=χ+(l⋅m) χ+(v⋅m)1+Λ(l)+Λ(v).G_2^{\text{sep}} = G_1(l, m)\, G_1(v, m), \qquad G_2^{\text{hc}} = \frac{\chi^+(l \cdot m)\, \chi^+(v \cdot m)}{1 + \Lambda(l) + \Lambda(v)}.

The separable form treats masking and shadowing as independent. In fact both are more likely on low facets, so they are positively correlated, and the separable form counts too much shadowing. The height-correlated form takes the correlation through the height into account. This page uses the height-correlated form, which Heitz suggests for practice because it is more accurate at the same cost. Both forms are symmetric in ll and vv, so the BRDF stays reciprocal.

What it counts

G1G_1 counts the visible fraction of facet area, and G2G_2 the fraction that is both visible and lit. Both are in [0,1][0, 1]. G1=1G_1 = 1 at normal view, because tan⁡θ=0\tan\theta = 0, and G1→0G_1 \to 0 at grazing view, because tan⁡θ→∞\tan\theta \to \infty. A smoother surface (α→0\alpha \to 0) hides less.

Example. G1G_1 for GGX:

α\alpha 0° 30° 45° 60° 75° 80° 85° 89°
0.1 1 0.9992 0.9975 0.9926 0.9674 0.9304 0.7941 0.2934
0.25 1 0.9948 0.9848 0.9571 0.8447 0.7313 0.4966 0.1302
0.5 1 0.9800 0.9443 0.8610 0.6416 0.4992 0.2940 0.0674
1.0 1 0.9282 0.8284 0.6667 0.4112 0.2959 0.1603 0.0343

For ll and vv both at 60° with α=0.5\alpha = 0.5, the separable G2G_2 is 0.86102=0.74130.8610^2 = 0.7413 and the height-correlated one is 1/(1+2⋅0.1614)=0.75591 / (1 + 2 \cdot 0.1614) = 0.7559. For α=1\alpha = 1 they are 0.44440.4444 and 0.50.5. The gap grows with roughness and at grazing angles.

The Schlick-GGX fit. Real-time code often replaces G1G_1 by Schlick's form μ/(μ(1−k)+k)\mu / (\mu (1 - k) + k). Karis (2013) sets k=α/2k = \alpha / 2 and notes that this matches Smith exactly at α=1\alpha = 1: there Λ=(1/μ−1)/2\Lambda = (1 / \mu - 1) / 2, so both give G1=2μ/(1+μ)G_1 = 2\mu / (1 + \mu). For other α\alpha it is a fit. Over all μ∈(0,1]\mu \in (0, 1], the largest error of the fit is 0.0660.066 at α=0.5\alpha = 0.5, 0.1100.110 at α=0.25\alpha = 0.25 and 0.1400.140 at α=0.1\alpha = 0.1, always toward too much masking near grazing angles. For analytic lights only, Karis uses k=(r+1)2/8k = (r + 1)^2 / 8 with the perceptual roughness rr, a tuning that reduces the brightness of smooth surfaces; for image-based light it makes grazing angles too dark. The values above come from the code of this page, which also checks the weak white furnace test by quadrature to 2⋅10−32 \cdot 10^{-3} and by Monte Carlo with a fixed seed.

Relation to other models

  • Microfacet BRDF. G2G_2 is the GG in f=DFG/(4(n⋅l)(n⋅v))f = D F G / (4 (n \cdot l)(n \cdot v)). Many shaders fold G2G_2 and the denominator into one visibility term V=G2/(4(n⋅l)(n⋅v))V = G_2 / (4 (n \cdot l)(n \cdot v)), which for the height-correlated GGX form has a cheap expression.
  • The V-cavity model. The GG of Torrance and Sparrow and of Cook and Torrance, min⁡(1,2(n⋅h)(n⋅v)/(v⋅h),2(n⋅h)(n⋅l)/(v⋅h))\min(1, 2 (n \cdot h)(n \cdot v) / (v \cdot h), 2 (n \cdot h)(n \cdot l) / (v \cdot h)), models the surface as V-shaped grooves. Heitz (2014, Section 4) shows that it is also exact for its own microsurface, but that this microsurface misses the effect of occlusion, which gives wrong glossy highlights at grazing view angles.
  • Beckmann. The Smith Λ\Lambda of Beckmann needs the error function, Λ=(erf⁡(a)−1)/2+e−a2/(2aπ)\Lambda = (\operatorname{erf}(a) - 1) / 2 + e^{-a^2} / (2 a \sqrt\pi) with a=1/(αtan⁡θ)a = 1 / (\alpha \tan\theta); real-time code uses a rational fit.
  • Disney. The Disney principled BRDF of 2012 used the GGX Smith GG with the remapped αg=(0.5+r/2)2\alpha_g = (0.5 + r / 2)^2; the 2014 revision of the notes removed the remap for the main lobe after Heitz's analysis.
  • Multiple scattering. The Smith model counts only single scattering. Light blocked by a facet is lost, so the albedo of a rough surface is too low. The microfacet BRDF page gives the loss, and multiple scattering models add it back.

Known results in the model

  • Range (proved). 0≤G1≤10 \le G_1 \le 1 and 0≤G2sep≤G2hc≤10 \le G_2^{\text{sep}} \le G_2^{\text{hc}} \le 1, because (1+Λ(l))(1+Λ(v))=1+Λ(l)+Λ(v)+Λ(l)Λ(v)≥1+Λ(l)+Λ(v)(1 + \Lambda(l))(1 + \Lambda(v)) = 1 + \Lambda(l) + \Lambda(v) + \Lambda(l)\Lambda(v) \ge 1 + \Lambda(l) + \Lambda(v) for Λ≥0\Lambda \ge 0.
  • Exact for its microsurface (proved by Heitz 2014). The Smith G1G_1 is the exact masking function of a microsurface with no correlation between heights and normals, and it passes the weak white furnace test for every DD.
  • Reciprocity (proved). Both G2G_2 forms are symmetric in ll and vv.
  • Closed form for GGX (proved). The Λ\Lambda above, from the slope distribution of GGX.
  • Schlick-GGX (fit). Exact at α=1\alpha = 1 only, with the errors above.
  • The real surface (measurement). Whether a material has uncorrelated heights and normals is a measurement. Real surfaces can differ, and the model then has an error that the test above cannot show.
Proof: Properties of the Smith functions for GGXfull

Setting. For α>0\alpha > 0 and a direction ω\omega with μ=n⋅ω∈(0,1]\mu = n \cdot \omega \in (0, 1], the code evaluates Λ(μ)=(−1+1+α2tan⁡2θ)/2\Lambda(\mu) = (-1 + \sqrt{1 + \alpha^2 \tan^2\theta}) / 2 with tan⁡2θ=(1−μ2)/μ2\tan^2\theta = (1 - \mu^2) / \mu^2, G1=χ+(ω⋅m)/(1+Λ)G_1 = \chi^+(\omega \cdot m) / (1 + \Lambda), G2sep=G1(l)G1(v)G_2^{\text{sep}} = G_1(l) G_1(v) and G2hc=1/(1+Λ(l)+Λ(v))G_2^{\text{hc}} = 1 / (1 + \Lambda(l) + \Lambda(v)). The claims are about these formulas in exact arithmetic. That a real surface has uncorrelated heights and normals is an assumption of the model, not a claim.

Claim 1 (range). Λ≥0\Lambda \ge 0, so 0<G1≤10 < G_1 \le 1 for μ>0\mu > 0, with G1=1G_1 = 1 at μ=1\mu = 1 and G1→0G_1 \to 0 as μ→0\mu \to 0.

Proof. 1+x≥1\sqrt{1 + x} \ge 1 for x≥0x \ge 0, so Λ≥0\Lambda \ge 0 and 1/(1+Λ)∈(0,1]1 / (1 + \Lambda) \in (0, 1]. At μ=1\mu = 1, tan⁡θ=0\tan\theta = 0 and Λ=0\Lambda = 0. As μ→0\mu \to 0, tan⁡θ→∞\tan\theta \to \infty, so Λ→∞\Lambda \to \infty and G1→0G_1 \to 0. □\square

Claim 2 (order). 0≤G2sep≤G2hc≤10 \le G_2^{\text{sep}} \le G_2^{\text{hc}} \le 1, and both are symmetric in ll and vv.

Proof. (1+Λl)(1+Λv)=1+Λl+Λv+ΛlΛv≥1+Λl+Λv≥1(1 + \Lambda_l)(1 + \Lambda_v) = 1 + \Lambda_l + \Lambda_v + \Lambda_l \Lambda_v \ge 1 + \Lambda_l + \Lambda_v \ge 1, because both Λ\Lambda are not negative by Claim 1. Taking reciprocals reverses the order. Both expressions do not change when ll and vv are swapped. □\square

Claim 3 (the weak white furnace test). For every ω\omega with μ>0\mu > 0, ∫H2G1(ω,m)max⁡(0,ω⋅m) D(m) dωm=μ\int_{H^2} G_1(\omega, m) \max(0, \omega \cdot m)\, D(m)\, d\omega_m = \mu, where DD is GGX with the same α\alpha.

Proof. Let A(ω)=∫max⁡(0,ω⋅m) D(m) dωmA(\omega) = \int \max(0, \omega \cdot m)\, D(m)\, d\omega_m, the projected area of the facets that face ω\omega. Since G1=χ+/(1+Λ)G_1 = \chi^+ / (1 + \Lambda) does not depend on mm otherwise, the integral is A(ω)/(1+Λ(ω))A(\omega) / (1 + \Lambda(\omega)). So the claim is the identity A(ω)=μ(1+Λ(ω))A(\omega) = \mu (1 + \Lambda(\omega)). Write AA in slope space. Turn the frame so that ω=(sin⁡θ,0,cos⁡θ)\omega = (\sin\theta, 0, \cos\theta). A facet with the slope s=(sx,sy)s = (s_x, s_y) has the normal m=(−sx,−sy,1)/1+∣s∣2m = (-s_x, -s_y, 1) / \sqrt{1 + |s|^2}, so n⋅m=1/1+∣s∣2n \cdot m = 1 / \sqrt{1 + |s|^2} and ω⋅m=(cos⁡θ−sxsin⁡θ)/1+∣s∣2\omega \cdot m = (\cos\theta - s_x \sin\theta) / \sqrt{1 + |s|^2}. The slope density PP is defined by D(m) (n⋅m) dωm=P(s) dsD(m)\, (n \cdot m)\, d\omega_m = P(s)\, ds (the GGX page, Claim 3 of its proof), so D(m) dωm=1+∣s∣2 P(s) dsD(m)\, d\omega_m = \sqrt{1 + |s|^2}\, P(s)\, ds. The square roots cancel, and A(ω)=∫R2max⁡(0,cos⁡θ−sxsin⁡θ) P(s) ds=μ∫−∞cot⁡θ(1−qtan⁡θ) P1(q) dqA(\omega) = \int_{\mathbb{R}^2} \max(0, \cos\theta - s_x \sin\theta)\, P(s)\, ds = \mu \int_{-\infty}^{\cot\theta} (1 - q \tan\theta)\, P_1(q)\, dq, where P1P_1 is the marginal density of sxs_x. P1P_1 integrates to 1 and has mean 0, so ∫−∞a(1−qtan⁡θ)P1 dq=1+∫a∞(qtan⁡θ−1)P1 dq\int_{-\infty}^{a} (1 - q \tan\theta) P_1\, dq = 1 + \int_a^\infty (q \tan\theta - 1) P_1\, dq for a=cot⁡θa = \cot\theta. So A=μ(1+Λ)A = \mu (1 + \Lambda) with Λ=∫cot⁡θ∞(qtan⁡θ−1)P1(q) dq\Lambda = \int_{\cot\theta}^\infty (q \tan\theta - 1) P_1(q)\, dq, which is the definition of Smith's Λ\Lambda (Heitz 2014, Section 5). For GGX, P(s)=α2/(π(α2+∣s∣2)2)P(s) = \alpha^2 / (\pi (\alpha^2 + |s|^2)^2), and the integral over sys_y, with ∫Rdy/(c2+y2)2=π/(2c3)\int_{\mathbb{R}} dy / (c^2 + y^2)^2 = \pi / (2 c^3), gives P1(q)=α2/(2(α2+q2)3/2)P_1(q) = \alpha^2 / (2 (\alpha^2 + q^2)^{3/2}). Then with a=cot⁡θa = \cot\theta, ∫a∞qP1=α2/(2α2+a2)\int_a^\infty q P_1 = \alpha^2 / (2 \sqrt{\alpha^2 + a^2}) and ∫a∞P1=(1−a/α2+a2)/2\int_a^\infty P_1 = (1 - a / \sqrt{\alpha^2 + a^2}) / 2. Then Λ=tan⁡θ α2/(2α2+a2)−(1−a/α2+a2)/2=(α2+a2tan⁡θ−1)/2\Lambda = \tan\theta\, \alpha^2 / (2 \sqrt{\alpha^2 + a^2}) - (1 - a / \sqrt{\alpha^2 + a^2}) / 2 = (\sqrt{\alpha^2 + a^2} \tan\theta - 1) / 2, using α2tan⁡θ+a=tan⁡θ(α2+a2)\alpha^2 \tan\theta + a = \tan\theta (\alpha^2 + a^2). Finally α2+a2tan⁡θ=1+α2tan⁡2θ\sqrt{\alpha^2 + a^2} \tan\theta = \sqrt{1 + \alpha^2 \tan^2\theta}, which is the closed form. □\square

Claim 4 (Schlick-GGX at α=1\alpha = 1). With k=1/2k = 1/2, μ/(μ(1−k)+k)=G1\mu / (\mu (1 - k) + k) = G_1 for α=1\alpha = 1.

Proof. For α=1\alpha = 1, 1+tan⁡2θ=1/μ21 + \tan^2\theta = 1 / \mu^2, so Λ=(1/μ−1)/2\Lambda = (1 / \mu - 1) / 2 and G1=2μ/(1+μ)G_1 = 2\mu / (1 + \mu). The Schlick form gives μ/(μ/2+1/2)=2μ/(1+μ)\mu / (\mu / 2 + 1 / 2) = 2\mu / (1 + \mu). □\square

For other α\alpha the Schlick form is a fit, and its largest errors on the page are measured on a grid of 10410^4 points.

What the tests check. Claims 1 and 2 at 2000 random inputs, Claim 3 by midpoint quadrature over the hemisphere to 2⋅10−32 \cdot 10^{-3} and by Monte Carlo with importance sampling of D(n⋅m)D (n \cdot m) within four standard errors, Claim 4 on a grid, and the page values to four digits.

Code

Each implementation follows the pseudocode above. A comment @line links a line of code to a line of the pseudocode. Point at a line of the pseudocode to see the matching code. The build runs the native tests of each language.

smith_masking_shadowing_function.pyI0RawGitHub
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/smith-masking-shadowing-function/. The authors are listed in prov/0245JB.jsonl.

"""The Smith masking and shadowing functions for the GGX distribution.

Directions are in the local frame of the surface, with the normal as the
z axis. A cosine argument is n.w for a direction w. alpha is the parameter
of the GGX distribution itself.
"""

import math
import random

Vec = tuple[float, float, float]


def ggx_d(cos_m: float, alpha: float) -> float:
    """The GGX distribution, needed for the white furnace test."""
    if cos_m <= 0.0:
        return 0.0
    a2 = alpha * alpha
    t = cos_m * cos_m * (a2 - 1.0) + 1.0
    return a2 / (math.pi * t * t)


def lambda_ggx(cos_w: float, alpha: float) -> float:
    """Lambda(w) = (-1 + sqrt(1 + alpha^2 tan^2(theta))) / 2 for GGX."""
    if cos_w >= 1.0:
        return 0.0
    tan2 = (1.0 - cos_w * cos_w) / (cos_w * cos_w)
    return (-1.0 + math.sqrt(1.0 + alpha * alpha * tan2)) / 2.0


def g1(cos_w: float, alpha: float, cos_wm: float = 1.0) -> float:
    """G1(w, m) = chi(w.m) / (1 + Lambda(w)); cos_wm is w.m."""
    if cos_w <= 0.0 or cos_wm <= 0.0:
        return 0.0
    return 1.0 / (1.0 + lambda_ggx(cos_w, alpha))


def g2_separable(cos_l: float, cos_v: float, alpha: float) -> float:
    """G2 = G1(l) G1(v): masking and shadowing taken as independent."""
    return g1(cos_l, alpha) * g1(cos_v, alpha)


def g2_height_correlated(cos_l: float, cos_v: float, alpha: float) -> float:
    """G2 = 1 / (1 + Lambda(l) + Lambda(v)), Heitz 2014, Equation 99."""
    if cos_l <= 0.0 or cos_v <= 0.0:
        return 0.0
    return 1.0 / (1.0 + lambda_ggx(cos_l, alpha) + lambda_ggx(cos_v, alpha))


def g1_schlick_ggx(cos_w: float, k: float) -> float:
    """The fit n.w / ((n.w)(1 - k) + k); Karis 2013 uses k = alpha / 2."""
    if cos_w <= 0.0:
        return 0.0
    return cos_w / (cos_w * (1.0 - k) + k)


def sample_ggx_normal(rng: random.Random, alpha: float) -> Vec:
    """Sample m with the density D(m) (n.m)."""
    u = rng.random()
    phi = 2.0 * math.pi * rng.random()
    tan2 = alpha * alpha * u / (1.0 - u)
    c = 1.0 / math.sqrt(1.0 + tan2)
    s = math.sqrt(max(0.0, 1.0 - c * c))
    return (s * math.cos(phi), s * math.sin(phi), c)


def weak_furnace(theta_o: float, alpha: float, rng: random.Random, n: int) -> tuple[float, float]:
    """Monte Carlo estimate of the weak white furnace integral of Heitz 2014.

    The integral of G1(o, m) D(m) max(0, o.m) / (n.o) over all m is 1 for the
    Smith G1. With m sampled from D(m) (n.m), the weight is
    G1 max(0, o.m) / ((n.o)(n.m)). Returns the mean and its standard error.
    """
    o = (math.sin(theta_o), 0.0, math.cos(theta_o))
    vals = []
    for _ in range(n):
        m = sample_ggx_normal(rng, alpha)
        om = o[0] * m[0] + o[1] * m[1] + o[2] * m[2]
        w = g1(o[2], alpha, om) * max(0.0, om) / (o[2] * m[2])
        vals.append(w)
    mean = sum(vals) / n
    var = sum((x - mean) ** 2 for x in vals) / (n - 1)
    return mean, math.sqrt(var / n)
test_smith_masking_shadowing_function.py 96 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/smith-masking-shadowing-function/. The authors are listed in prov/0245JB.jsonl.

"""Tests of the Smith functions: range, closed forms and the weak white furnace."""

import math
import random

from smith_masking_shadowing_function import (
    g1,
    g1_schlick_ggx,
    g2_height_correlated,
    g2_separable,
    ggx_d,
    lambda_ggx,
    weak_furnace,
)


def cosd(deg: float) -> float:
    return math.cos(math.radians(deg))


def test_range_and_order() -> None:
    rng = random.Random(1)
    for _ in range(2000):
        a = rng.uniform(0.01, 2.0)
        cl, cv = rng.uniform(1e-6, 1.0), rng.uniform(1e-6, 1.0)
        sep = g2_separable(cl, cv, a)
        cor = g2_height_correlated(cl, cv, a)
        assert 0.0 <= sep <= cor <= 1.0
        assert 0.0 <= g1(cv, a) <= 1.0
        assert abs(g2_height_correlated(cl, cv, a) - g2_height_correlated(cv, cl, a)) < 1e-15


def test_end_values() -> None:
    for a in (0.1, 0.5, 1.0):
        assert g1(1.0, a) == 1.0
        assert g1(1e-9, a) < 1e-6
        assert g1(-0.2, a) == 0.0
        assert g1(0.5, a, cos_wm=-0.1) == 0.0
    # A smooth surface hides nothing.
    assert abs(g1(cosd(80.0), 1e-6) - 1.0) < 1e-9


def test_page_values() -> None:
    assert abs(g1(cosd(60.0), 0.5) - 0.8610) < 5e-5
    assert abs(g1(cosd(85.0), 0.5) - 0.2940) < 5e-5
    assert abs(g2_separable(cosd(60.0), cosd(60.0), 0.5) - 0.7413) < 5e-5
    assert abs(g2_height_correlated(cosd(60.0), cosd(60.0), 0.5) - 0.7559) < 5e-5
    # At alpha = 1, Lambda = (1 / mu - 1) / 2 and G1 = 2 mu / (1 + mu).
    for mu in (0.1, 0.5, 0.9):
        assert abs(lambda_ggx(mu, 1.0) - (1.0 / mu - 1.0) / 2.0) < 1e-12
        assert abs(g1(mu, 1.0) - 2.0 * mu / (1.0 + mu)) < 1e-12


def test_schlick_ggx_fit() -> None:
    # Exact at alpha = 1 with k = alpha / 2, and only a fit elsewhere.
    for i in range(1, 1001):
        mu = i / 1000.0
        assert abs(g1_schlick_ggx(mu, 0.5) - g1(mu, 1.0)) < 1e-12
    worst = {}
    for a in (0.1, 0.25, 0.5):
        worst[a] = max(abs(g1_schlick_ggx(i / 10000.0, a / 2.0) - g1(i / 10000.0, a)) for i in range(1, 10001))
    assert abs(worst[0.1] - 0.1401) < 5e-4
    assert abs(worst[0.25] - 0.1095) < 5e-4
    assert abs(worst[0.5] - 0.0664) < 5e-4


def test_weak_white_furnace_quadrature() -> None:
    # Integrate G1(o, m) D(m) max(0, o.m) / (n.o) over the hemisphere of m.
    for a in (0.2, 0.6):
        for deg in (0.0, 45.0, 75.0):
            o = (math.sin(math.radians(deg)), 0.0, cosd(deg))
            nt, npf = 600, 128
            total = 0.0
            for i in range(nt):
                t = (i + 0.5) * (math.pi / 2.0) / nt
                st, ct = math.sin(t), math.cos(t)
                d = ggx_d(ct, a)
                ring = 0.0
                for j in range(npf):
                    p = (j + 0.5) * 2.0 * math.pi / npf
                    om = o[0] * st * math.cos(p) + o[2] * ct
                    ring += max(0.0, om)
                total += d * st * ring
            total *= (math.pi / 2.0 / nt) * (2.0 * math.pi / npf) * g1(o[2], a) / o[2]
            assert abs(total - 1.0) < 2e-3


def test_weak_white_furnace_monte_carlo() -> None:
    rng = random.Random(7)
    for a in (0.3, 1.0):
        for deg in (30.0, 70.0):
            mean, se = weak_furnace(math.radians(deg), a, rng, 20000)
            assert abs(mean - 1.0) < 4.0 * se

References

  1. B. Smith, Geometrical shadowing of a random rough surface, IEEE Transactions on Antennas and Propagation 15(5), 1967, 668 to 671, the shadowing probability of a random rough surface with uncorrelated heights and slopes (general locator)
  2. Eric Heitz, Understanding the masking-shadowing function in microfacet-based BRDFs, Journal of Computer Graphics Techniques 3(2), 2014, 48 to 107, Sections 2.3 and 2.4 (the conservation of projected area as a constraint on G1), Section 3.5 (the weak white furnace test), Section 4 (the Smith and the V-cavity microsurface profiles), Section 5 (stretch invariance, with GGX in Equations 71 and 72), Section 6 (the separable form, Equation 98, and the height-correlated form, Equation 99)
  3. Bruce Walter, Stephen R. Marschner, Hongsong Li and Kenneth E. Torrance, Microfacet models for refraction through rough surfaces, Eurographics Symposium on Rendering, 2007, 195 to 206, the Smith G1 for the GGX and Beckmann distributions and its use in the separable G
  4. Brian Karis, Real shading in Unreal Engine 4, course notes, SIGGRAPH 2013 course Physically Based Shading in Theory and Practice, the specular G (Schlick's form with k = alpha / 2, and k = (roughness + 1)^2 / 8 for analytic lights only), Equation 4
  5. Tomas Akenine-Möller, Eric Haines, Naty Hoffman, Angelo Pesce, Michał Iwanicki and Sébastien Hillaire, Real-Time Rendering, fourth edition, CRC Press, 2018, Chapter 9, Physically Based Shading (the section on microfacet theory, masking and shadowing)

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH2 full proof
  • correctness: no review record reviews/0245JB/proof-correctness.yaml
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
Code (python)I0 not checked yet
  • the tests did not run in this build
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW4 complete
  • a review of the text: no review record reviews/0245JB/text.yaml