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 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 has the form
where is 1 for a positive argument and 0 otherwise, and depends only on the distribution of slopes. For GGX, 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 ; the GGX page gives and Schlick's approximation gives .
The machine
The microsurface. A height field over the macro surface, with the macro normal as the axis and a distribution of facet normals normalized by projected area. A direction makes the angle with , and .
The masking function . The fraction of the facets with normal that a ray from the direction sees, with no other facet in between. Facets that face away, , are never visible.
The constraint. Seen from , the visible facets project onto exactly the projected area of the macro surface. With projected areas normalized per unit macro area,
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 except through , so . Putting this into the constraint gives
is the projected area of the facets that face but are hidden, per unit of visible area. For GGX with the parameter (Heitz 2014, Equation 72),
the second form being the one of Walter et al. (2007). For an anisotropic GGX, is replaced by for the azimuth of , by the stretch invariance of Heitz's Section 5.
The joint function . A BRDF needs the fraction of facets that are visible from and lit from . Two forms are common (Heitz 2014, Section 6):
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 and , so the BRDF stays reciprocal.
What it counts
counts the visible fraction of facet area, and the fraction that is both visible and lit. Both are in . at normal view, because , and at grazing view, because . A smoother surface () hides less.
Example. for GGX:
| 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 and both at 60° with , the separable is and the height-correlated one is . For they are and . The gap grows with roughness and at grazing angles.
The Schlick-GGX fit. Real-time code often replaces by Schlick's form . Karis (2013) sets and notes that this matches Smith exactly at : there , so both give . For other it is a fit. Over all , the largest error of the fit is at , at and at , always toward too much masking near grazing angles. For analytic lights only, Karis uses with the perceptual roughness , 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 and by Monte Carlo with a fixed seed.
Relation to other models
- Microfacet BRDF. is the in . Many shaders fold and the denominator into one visibility term , which for the height-correlated GGX form has a cheap expression.
- The V-cavity model. The of Torrance and Sparrow and of Cook and Torrance, , 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 of Beckmann needs the error function, with ; real-time code uses a rational fit.
- Disney. The Disney principled BRDF of 2012 used the GGX Smith with the remapped ; 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). and , because for .
- Exact for its microsurface (proved by Heitz 2014). The Smith 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 .
- Reciprocity (proved). Both forms are symmetric in and .
- Closed form for GGX (proved). The above, from the slope distribution of GGX.
- Schlick-GGX (fit). Exact at 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 and a direction with , the code evaluates with , , and . 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). , so for , with at and as .
Proof. for , so and . At , and . As , , so and .
Claim 2 (order). , and both are symmetric in and .
Proof. , because both are not negative by Claim 1. Taking reciprocals reverses the order. Both expressions do not change when and are swapped.
Claim 3 (the weak white furnace test). For every with , , where is GGX with the same .
Proof. Let , the projected area of the facets that face . Since does not depend on otherwise, the integral is . So the claim is the identity . Write in slope space. Turn the frame so that . A facet with the slope has the normal , so and . The slope density is defined by (the GGX page, Claim 3 of its proof), so . The square roots cancel, and , where is the marginal density of . integrates to 1 and has mean 0, so for . So with , which is the definition of Smith's (Heitz 2014, Section 5). For GGX, , and the integral over , with , gives . Then with , and . Then , using . Finally , which is the closed form.
Claim 4 (Schlick-GGX at ). With , for .
Proof. For , , so and . The Schlick form gives .
For other the Schlick form is a fit, and its largest errors on the page are measured on a grid of points.
What the tests check. Claims 1 and 2 at 2000 random inputs, Claim 3 by midpoint quadrature over the hemisphere to and by Monte Carlo with importance sampling of 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.
# 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
- 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)
- 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)
- 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
- 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
- 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.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H2 full proof |
|
| Lean | L0 no Lean part |
|
| Code (python) | I0 not checked yet |
|
| Visual | V0 none |
|
| Writing | W4 complete |
|
Tag 0245JB Edit this page History Report an error Markdown