Algorithm Search⌘K

Modeladvanced

Disney principled BRDF

Also called principled BRDF, Burley BRDF.

A BRDF with a few intuitive parameters such as base color, metallic and roughness that blends diffuse, specular, sheen and clearcoat lobes.

The Disney principled BRDF is the material model that Brent Burley and Walt Disney Animation Studios built for the film Wreck-It Ralph (2012). The team first studied the MERL 100, a set of 100 measured isotropic materials, and then wrote a model that an artist controls with one color and ten scalar parameters. Each parameter goes from 0 to 1 over its plausible range and has a visible meaning, such as roughness or metallic, instead of a physical quantity such as an index of refraction. The model is a sum of lobes: an empirical diffuse term, a GGX specular lobe with Schlick's Fresnel and a Smith G, a sheen lobe for cloth and a second specular lobe for a clear coat. Epic Games used a reduced form of it in Unreal Engine 4 (Karis 2013). This page describes one color channel of the model with the 2014 revision of the specular G.

The machine

Directions. nn is the normal, ll the light direction and vv the view direction, all unit vectors that point away from the surface. hh is the unit half vector of ll and vv. The angles are θl\theta_l and θv\theta_v from nn, θh\theta_h between nn and hh, and θd\theta_d between ll and hh, which is also the angle between vv and hh.

The parameters (Burley 2012, Section 5.2). baseColor; subsurface; metallic, which the notes call "a linear blend between two different models"; specular, used "in lieu of an explicit index-of-refraction"; specularTint; roughness; anisotropic; sheen, "primarily intended for cloth"; sheenTint; clearcoat; and clearcoatGloss, with 0 for satin and 1 for gloss. The principles of Section 5.1 are these: the parameters are intuitive rather than physical, there are as few as possible, each goes from 0 to 1 over its plausible range, a parameter may go past that range where it makes sense, and all combinations must be as robust and plausible as possible.

The BRDF. With bb the base color, mm metallic, rr roughness, ss sheen and cc clearcoat, the page uses

f=(1−m)(fd+s FH)+fs+fc,FH=(1−cos⁡θd)5.f = (1 - m)\big(f_d + s\, F_H\big) + f_s + f_c, \qquad F_H = (1 - \cos\theta_d)^5.

Diffuse (Section 5.3).

fd=bπ(1+(FD90−1)(1−cos⁡θl)5)(1+(FD90−1)(1−cos⁡θv)5),FD90=0.5+2rcos⁡2θd.f_d = \frac{b}{\pi}\big(1 + (F_{D90} - 1)(1 - \cos\theta_l)^5\big)\big(1 + (F_{D90} - 1)(1 - \cos\theta_v)^5\big), \qquad F_{D90} = 0.5 + 2 r \cos^2\theta_d.

At grazing angles a smooth surface keeps half of the Lambertian value in each factor, and a rough one gains up to a factor of 2.5. The notes describe it as an empirical model for diffuse retroreflection and say that the Lambert model is often "too dark on the edges". It ignores the index of refraction and assumes no diffuse loss at entry, so the artist sets the diffuse color directly.

Specular (Sections 5.4 to 5.6). The distribution is the generalized Trowbridge-Reitz (GTR) family

Dγ(θh)=cγ(α2cos⁡2θh+sin⁡2θh)γ,D_\gamma(\theta_h) = \frac{c_\gamma}{(\alpha^2 \cos^2\theta_h + \sin^2\theta_h)^\gamma},

with cγc_\gamma set so that ∫Dcos⁡θh dω=1\int D \cos\theta_h\, d\omega = 1 (Appendix B). γ=2\gamma = 2 is GGX with c2=α2/πc_2 = \alpha^2 / \pi, and γ=1\gamma = 1 has the longer tail with c1=(α2−1)/(πln⁡α2)c_1 = (\alpha^2 - 1) / (\pi \ln \alpha^2). The primary lobe uses γ=2\gamma = 2 and α=r2\alpha = r^2, a remap that the notes found more perceptually linear. Its Fresnel term is Schlick's form in θd\theta_d,

F=F0+(1−F0)FH,F0=(1−m) 0.08 specular+m b.F = F_0 + (1 - F_0)F_H, \qquad F_0 = (1 - m)\, 0.08\, \text{specular} + m\, b.

The specular range [0, 0.08] is the reflectance at normal incidence for an index of refraction from 1.0 to about 1.8, and the default 0.5 gives 0.04, the value for an index of 1.5. G is the separable Smith G of GGX, G=G1(l)G1(v)G = G_1(l) G_1(v) with G1(w)=2/(1+1+α2tan⁡2θw)G_1(w) = 2 / (1 + \sqrt{1 + \alpha^2 \tan^2\theta_w}). Then

fs=D2(θh) F G4cos⁡θlcos⁡θv.f_s = \frac{D_2(\theta_h)\, F\, G}{4 \cos\theta_l \cos\theta_v}.

Clear coat. A second, isotropic and non-metallic lobe with γ=1\gamma = 1, a fixed F0=0.04F_0 = 0.04 (an index of 1.5, as for polyurethane) and the GGX G at a fixed roughness of 0.25:

fc=0.25 c D1(θh;αc) F0.04 G0.254cos⁡θlcos⁡θv.f_c = 0.25\, c\, \frac{D_1(\theta_h; \alpha_c)\, F_{0.04}\, G_{0.25}}{4 \cos\theta_l \cos\theta_v}.

The factor 0.25 maps the clearcoat range [0, 1] to a strength of [0, 0.25]. The notes take no energy from the base layer for the coat, and the coat costs nothing when c=0c = 0. clearcoatGloss sets αc\alpha_c; the notes do not give that map, so the code of this page takes αc\alpha_c as a direct input.

Sheen (2014 Addenda). The lobe s(1−cos⁡θd)5s (1 - \cos\theta_d)^5, with an optional tint toward the base color. This page puts it with the diffuse, so a metal has no sheen.

The 2012 G and the 2014 change. The 2012 notes computed G with αg=(0.5+r/2)2\alpha_g = (0.5 + r / 2)^2 instead of r2r^2, because the specular of smooth surfaces was "too hot" against the measured data and in the opinion of the artists. For r=0.2r = 0.2 this is αg=0.36\alpha_g = 0.36 instead of α=0.04\alpha = 0.04. After Heitz (2014) the Addenda removed this remap for the primary lobe and kept it only for the clear coat. The code has a flag for the old form.

Left out here. One color channel means that specularTint and sheenTint have no effect. The subsurface parameter (a blend toward a shape inspired by the Hanrahan-Krueger model) and the anisotropic parameter (Addenda: aspect =1−0.9 anisotropic= \sqrt{1 - 0.9\, \text{anisotropic}}, αx=r2/aspect\alpha_x = r^2 / \text{aspect}, αy=r2⋅aspect\alpha_y = r^2 \cdot \text{aspect}) are also not in the code.

What it counts

ff is a BRDF in 1/sr, the reflected radiance per unit irradiance from one direction. The directional albedo E(θv)=∫fcos⁡θl dωlE(\theta_v) = \int f \cos\theta_l\, d\omega_l is the fraction of light from one direction that leaves the surface. The notes state that the albedo of a real material must be below 1, and they show that the measured albedo of most MERL materials is almost flat for the first 70 degrees. The model does not hold to that bound in every case.

Example: the diffuse term alone. With b=1b = 1, the albedo of fdf_d for the view angle θv\theta_v, by quadrature in the code of this page:

rr 0° 45° 60° 75° 85°
0 0.9762 0.9751 0.9609 0.8670 0.6668
0.25 0.9911 0.9899 0.9845 0.9435 0.8418
0.5 1.0060 1.0047 1.0083 1.0220 1.0224
1 1.0357 1.0344 1.0569 1.1853 1.4006

A smooth surface is darker at grazing angles than a Lambertian one, and a rough surface is brighter. At r=1r = 1 the diffuse term alone reflects 40 percent more light than it receives at 85°.

Example: a white dielectric. With b=1b = 1, specular 0.5 and r=1r = 1, the full albedo is 1.0480 at 0°, 1.0756 at 60° and 1.3074 at 80°. The diffuse and specular lobes are added and the specular takes no energy from the diffuse.

Example: a white metal. With b=1b = 1 and m=1m = 1, F=1F = 1, so the albedo is the single-scattering albedo of GGX with the separable Smith G. By Monte Carlo with 20000 samples:

rr 0° 60° 80°
0.2 0.998 0.994 0.970
0.5 0.916 0.853 0.828
1 0.305 0.405 0.525

A smooth metal keeps almost all the light. A rough one loses much of it, because the model counts only one bounce on the microsurface.

Relation to other models

  • Microfacet BRDFs. The specular and clear coat lobes are microfacet lobes DFG/(4cos⁡θlcos⁡θv)DFG / (4 \cos\theta_l \cos\theta_v). The diffuse and sheen lobes are not derived from a microsurface; they are fits to measured data.
  • GGX. GTR with γ=2\gamma = 2. The notes say that GGX is equivalent to Trowbridge-Reitz and that GTR with γ=1\gamma = 1 comes from a form used by Berry (1923).
  • Smith G. The 2014 revision uses the Smith G of GGX with no remap, after the weak white furnace test of Heitz showed that the Smith and V-cavity models are the plausible ones among those known.
  • Lambertian reflectance. If FD90=1F_{D90} = 1 the diffuse term is b/πb / \pi. Karis (2013) tested Burley diffuse for Unreal Engine 4, saw only small differences from Lambert and kept Lambert, which is also easier to use with image-based and spherical harmonic lighting.
  • Oren-Nayar. Another rough diffuse model, derived from V-cavities. Burley diffuse has a different shape and was chosen because it matched the MERL data and the artists liked it.
  • Blinn-Phong. An older empirical model. Its exponent has no 0 to 1 range, it has no Fresnel term, and its normalized form is still not energy conserving.
  • Unreal Engine 4. Karis (2013) kept BaseColor, Metallic and Roughness, replaced Specular with a constant F0=0.04F_0 = 0.04 for nonmetals and a Cavity parameter for small-scale shadowing, and treated subsurface, anisotropy, clear coat and sheen as special shading models. With the split-sum approximation it lights the same material with analytic lights and with environment maps.

Known results in the model

  • GTR normalization (proved). For γ=1\gamma = 1 and γ=2\gamma = 2, the constants above give ∫Dcos⁡θh dω=1\int D \cos\theta_h\, d\omega = 1 for all α∈(0,1]\alpha \in (0, 1]; γ=1\gamma = 1 tends to the constant 1/π1 / \pi as α→1\alpha \to 1.
  • Reciprocity (proved). Each lobe is symmetric in ll and vv, so f(l,v)=f(v,l)f(l, v) = f(v, l), as Helmholtz reciprocity asks.
  • Metallic is a blend (proved). Because the specular lobe is affine in F0F_0, mixing F0F_0 by mm gives the same specular as (1−m)(1 - m) times the dielectric lobe plus mm times the metal lobe. So the full model with the coat removed is the linear blend of a dielectric model and a metal model that the notes describe.
  • Lambert limit of the diffuse (proved). FD90=1F_{D90} = 1 gives fd=b/πf_d = b / \pi.
  • No energy bound (measurement). The diffuse term alone has an albedo up to 1.40 for r=1r = 1, and a white rough dielectric reaches 1.31 at 80°. Principle 5 asks for plausible combinations, not for energy conservation, and Burley lists "should we be perfectly energy conserving?" as an open design question.
  • Energy loss of rough metals (measurement). With one bounce on the microsurface, a white metal reflects only about 0.31 of the light at normal view for r=1r = 1.
  • The constants (tuning). 0.5 and 2 in FD90F_{D90}, the range 0.08 of specular, the strength 0.25 and fixed roughness 0.25 of the coat, the 2012 G remap and the map α=r2\alpha = r^2 are chosen to match the MERL data and to please the artists.
  • Layering (measurement of practice). Because the parameters are normalized and close to perceptually linear, two materials blend well by interpolating their parameters, and the studio made all spatial variation with masks between material presets (Section 5.7).
Proof: Properties of the one-channel principled BRDFfull

Setting. The code computes, for unit ll and vv above the surface with n=zn = z, the value f=(1−m)(fd+sFH)+fs+fcf = (1 - m)(f_d + s F_H) + f_s + f_c of the page, with h=(l+v)/∣l+v∣h = (l + v) / |l + v|, cos⁡θd=l⋅h\cos\theta_d = l \cdot h, FH=(1−cos⁡θd)5F_H = (1 - \cos\theta_d)^5, α=max⁡(10−4,r2)\alpha = \max(10^{-4}, r^2), F0=(1−m) 0.08 specular+mbF_0 = (1 - m)\, 0.08\, \text{specular} + m b and the separable Smith G of GGX. The claims are about these formulas. How well they match measured materials is a measurement reported by Burley (2012), not a claim here.

Claim 1 (GTR normalization). For 0<α<10 < \alpha < 1, with D2=α2/(π(1+(α2−1)cos⁡2θ)2)D_2 = \alpha^2 / (\pi (1 + (\alpha^2 - 1)\cos^2\theta)^2) and D1=(α2−1)/(πln⁡(α2)(1+(α2−1)cos⁡2θ))D_1 = (\alpha^2 - 1) / (\pi \ln(\alpha^2)(1 + (\alpha^2 - 1)\cos^2\theta)), ∫H2Dcos⁡θ dω=1\int_{H^2} D \cos\theta\, d\omega = 1. At α=1\alpha = 1 both are 1/π1 / \pi, and D1→1/πD_1 \to 1 / \pi as α→1\alpha \to 1.

Proof. First, α2cos⁡2θ+sin⁡2θ=1+(α2−1)cos⁡2θ\alpha^2 \cos^2\theta + \sin^2\theta = 1 + (\alpha^2 - 1)\cos^2\theta, so these are the GTR forms. With u=cos⁡2θu = \cos^2\theta, du=−2cos⁡θsin⁡θ dθdu = -2 \cos\theta \sin\theta\, d\theta, so ∫H2Dcos⁡θ dω=2π∫0π/2Dcos⁡θsin⁡θ dθ=π∫01D du\int_{H^2} D \cos\theta\, d\omega = 2\pi \int_0^{\pi / 2} D \cos\theta \sin\theta\, d\theta = \pi \int_0^1 D\, du. Put k=α2−1∈(−1,0)k = \alpha^2 - 1 \in (-1, 0). For γ=2\gamma = 2, ∫01(1+ku)−2du=1k(1−11+k)=11+k=1α2\int_0^1 (1 + k u)^{-2} du = \frac{1}{k}\big(1 - \frac{1}{1 + k}\big) = \frac{1}{1 + k} = \frac{1}{\alpha^2}, so the integral is π⋅α2π⋅1α2=1\pi \cdot \frac{\alpha^2}{\pi} \cdot \frac{1}{\alpha^2} = 1. For γ=1\gamma = 1, ∫01(1+ku)−1du=ln⁡(1+k)/k=ln⁡(α2)/(α2−1)\int_0^1 (1 + k u)^{-1} du = \ln(1 + k) / k = \ln(\alpha^2) / (\alpha^2 - 1), so the integral is π⋅α2−1πln⁡α2⋅ln⁡α2α2−1=1\pi \cdot \frac{\alpha^2 - 1}{\pi \ln \alpha^2} \cdot \frac{\ln \alpha^2}{\alpha^2 - 1} = 1. At α=1\alpha = 1, D2=1/πD_2 = 1 / \pi, and (α2−1)/ln⁡(α2)→1(\alpha^2 - 1) / \ln(\alpha^2) \to 1 because ln⁡(1+k)/k→1\ln(1 + k) / k \to 1, so D1→1/πD_1 \to 1 / \pi, the value the code returns. □\square

Claim 2 (reciprocity). f(l,v)=f(v,l)f(l, v) = f(v, l).

Proof. hh is symmetric in ll and vv, so θh\theta_h is too. l⋅h=(1+l⋅v)/∣l+v∣=v⋅hl \cdot h = (1 + l \cdot v) / |l + v| = v \cdot h, so θd\theta_d and FHF_H are symmetric, and so is FD90F_{D90}. fdf_d is a product of a factor in θl\theta_l and the same factor in θv\theta_v, so swapping ll and vv swaps the factors. The Fresnel terms depend on θd\theta_d only. G=G1(l)G1(v)G = G_1(l) G_1(v) and the denominator 4cos⁡θlcos⁡θv4 \cos\theta_l \cos\theta_v are symmetric. So every lobe is symmetric, and so is their sum. □\square

Claim 3 (metallic is a linear blend). Let f(0)f^{(0)} be ff with m=0m = 0 and f(1)f^{(1)} be ff with m=1m = 1, both with c=0c = 0. Then f=(1−m)f(0)+mf(1)f = (1 - m) f^{(0)} + m f^{(1)} for c=0c = 0.

Proof. F=F0+(1−F0)FH=FH+F0(1−FH)F = F_0 + (1 - F_0) F_H = F_H + F_0 (1 - F_H) is affine in F0F_0, and DD, GG and the denominator do not depend on mm, so fsf_s is affine in F0F_0. An affine function gg has g((1−m)x+my)=(1−m)g(x)+mg(y)g((1 - m) x + m y) = (1 - m) g(x) + m g(y). With x=0.08 specularx = 0.08\, \text{specular} and y=by = b, fs=(1−m)fs(0)+mfs(1)f_s = (1 - m) f_s^{(0)} + m f_s^{(1)}. The diffuse and sheen terms are (1−m)(1 - m) times their value at m=0m = 0, and their value at m=1m = 1 is 0. Adding the two parts gives the claim. □\square

Claim 4 (the diffuse and sheen lobes). (a) If FD90=1F_{D90} = 1, then fd=b/πf_d = b / \pi. (b) For m=0m = 0, increasing ss by 1 adds exactly FHF_H to ff. (c) For m=1m = 1, ff does not depend on ss.

Proof. (a) Both factors are 1+0⋅(…)=11 + 0 \cdot (\ldots) = 1. (b) and (c) ss appears only in the term (1−m)sFH(1 - m) s F_H. □\square

Claim 5 (the furnace estimator). For b=1b = 1, m=1m = 1 and c=0c = 0, F=1F = 1 and the directional albedo is E(v)=∫fscos⁡θl dωlE(v) = \int f_s \cos\theta_l\, d\omega_l. If hh is drawn with the density D(h)cos⁡θhD(h) \cos\theta_h and l=2(v⋅h)h−vl = 2 (v \cdot h) h - v, then the expectation of w=G (v⋅h)/(cos⁡θvcos⁡θh)w = G\, (v \cdot h) / (\cos\theta_v \cos\theta_h), with w=0w = 0 when v⋅h≤0v \cdot h \le 0 or cos⁡θl≤0\cos\theta_l \le 0, is E(v)E(v).

Proof. F=F0+(1−F0)FHF = F_0 + (1 - F_0) F_H with F0=b=1F_0 = b = 1 gives F=1F = 1. The map h↦lh \mapsto l is the reflection of vv about hh; its Jacobian is dωh/dωl=1/(4 v⋅h)d\omega_h / d\omega_l = 1 / (4\, v \cdot h), so ll has the density p(l)=Dcos⁡θh/(4 v⋅h)p(l) = D \cos\theta_h / (4\, v \cdot h) where v⋅h>0v \cdot h > 0. The samples with v⋅h≤0v \cdot h \le 0 give no ll in the support, and the samples with cos⁡θl≤0\cos\theta_l \le 0 have f=0f = 0. Elsewhere fscos⁡θl/p=DG4cos⁡θlcos⁡θvcos⁡θl⋅4 v⋅hDcos⁡θh=wf_s \cos\theta_l / p = \frac{D G}{4 \cos\theta_l \cos\theta_v} \cos\theta_l \cdot \frac{4\, v \cdot h}{D \cos\theta_h} = w. Each ll in the upper hemisphere comes from exactly one hh, the half vector of ll and vv, so E[w]=∫fscos⁡θl dωlE[w] = \int f_s \cos\theta_l\, d\omega_l. □\square

Claim 6 (the 2012 G remap masks more). For 0≤r<10 \le r < 1, αg=(0.5+r/2)2>r2\alpha_g = (0.5 + r / 2)^2 > r^2, so G1G_1 with αg\alpha_g is smaller than with r2r^2 at every θw∈(0,π/2)\theta_w \in (0, \pi / 2).

Proof. 0.5+r/2−r=0.5(1−r)>00.5 + r / 2 - r = 0.5 (1 - r) > 0, and both sides are nonnegative, so squaring keeps the order. G1=2/(1+1+α2tan⁡2θw)G_1 = 2 / (1 + \sqrt{1 + \alpha^2 \tan^2\theta_w}) decreases in α\alpha when tan⁡θw>0\tan\theta_w > 0. The specular lobe has G=G1(l)G1(v)G = G_1(l) G_1(v) with all other factors unchanged and positive, so it is smaller. □\square

What the tests check. Claim 1 by quadrature in u=cos⁡2θu = \cos^2\theta for five values of α\alpha, and the longer tail of D1D_1. Claim 2 and f≥0f \ge 0 at 1000 random inputs. Claim 3 at 500 random inputs. Claim 4 at fixed directions. The diffuse albedo table and the white dielectric albedo of the page against 2D quadrature. Claim 5 by comparing the Monte Carlo estimate with deterministic quadrature at roughness 0.5 and 1, with the page values and with the bound E≤1E \le 1 within four standard errors. Claim 6 at three roughness values for a grazing mirror configuration.

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.

disney_principled_brdf.pyI0RawGitHub
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/disney-principled-brdf/. The authors are listed in prov/X3TDVD.jsonl.

"""The Disney principled BRDF of Burley (2012, revised 2014), one color channel.

The lobes are the Burley diffuse term, a GGX specular lobe with Schlick's
Fresnel and the Smith G of GGX, a sheen lobe and a clear coat lobe with the
GTR distribution for gamma = 1. One channel means that the tint parameters
have no effect, so they are left out; the subsurface and anisotropic
parameters are also left out. Vectors point away from the surface and the
normal is the z axis.
"""

import math
import random

Vec = tuple[float, float, float]


def dot(a: Vec, b: Vec) -> float:
    return a[0] * b[0] + a[1] * b[1] + a[2] * b[2]


def normalize(a: Vec) -> Vec:
    r = math.sqrt(dot(a, a))
    return (a[0] / r, a[1] / r, a[2] / r)


def schlick_weight(c: float) -> float:
    """(1 - c)^5 with c clamped to [0, 1]."""
    m = 1.0 - min(1.0, max(0.0, c))
    return (m * m) * (m * m) * m


def mix(a: float, b: float, t: float) -> float:
    return a * (1.0 - t) + b * t


def gtr2(cos_h: float, alpha: float) -> float:
    """GTR with gamma = 2, which is GGX: alpha^2 / (pi (1 + (alpha^2 - 1) cos^2)^2)."""
    if cos_h <= 0.0:
        return 0.0
    a2 = alpha * alpha
    t = 1.0 + (a2 - 1.0) * cos_h * cos_h
    return a2 / (math.pi * t * t)


def gtr1(cos_h: float, alpha: float) -> float:
    """GTR with gamma = 1: (alpha^2 - 1) / (pi log(alpha^2) (1 + (alpha^2 - 1) cos^2)).

    Burley 2012, Appendix B. At alpha = 1 the limit is 1 / pi.
    """
    if cos_h <= 0.0:
        return 0.0
    if alpha >= 1.0:
        return 1.0 / math.pi
    a2 = alpha * alpha
    return (a2 - 1.0) / (math.pi * math.log(a2) * (1.0 + (a2 - 1.0) * cos_h * cos_h))


def smith_g1_ggx(cos_w: float, alpha: float) -> float:
    """Smith masking for GGX, 2 / (1 + sqrt(1 + alpha^2 tan^2))."""
    if cos_w <= 0.0:
        return 0.0
    c2 = cos_w * cos_w
    return 2.0 / (1.0 + math.sqrt(1.0 + alpha * alpha * (1.0 - c2) / c2))


def burley_diffuse(cos_l: float, cos_v: float, cos_d: float, base: float, roughness: float) -> float:
    """base / pi (1 + (FD90 - 1) FL) (1 + (FD90 - 1) FV), FD90 = 0.5 + 2 r cos^2(theta_d)."""
    fd90 = 0.5 + 2.0 * roughness * cos_d * cos_d
    fl, fv = schlick_weight(cos_l), schlick_weight(cos_v)
    return base / math.pi * (1.0 + (fd90 - 1.0) * fl) * (1.0 + (fd90 - 1.0) * fv)


def principled_brdf(
    l: Vec,
    v: Vec,
    base: float = 0.8,
    metallic: float = 0.0,
    specular: float = 0.5,
    roughness: float = 0.5,
    sheen: float = 0.0,
    clearcoat: float = 0.0,
    clearcoat_alpha: float = 0.1,
    remap_g: bool = False,
) -> float:
    """The BRDF value in 1/sr for one color channel; n = (0, 0, 1).

    remap_g = True uses the 2012 remap alpha_g = (0.5 + r / 2)^2 for the
    specular G; the 2014 revision of the notes dropped it.
    """
    cl, cv = l[2], v[2]
    if cl <= 0.0 or cv <= 0.0:
        return 0.0
    h = normalize((l[0] + v[0], l[1] + v[1], l[2] + v[2]))
    ch = h[2]
    cd = dot(l, h)  # cos(theta_d), the angle between l and h (and v and h)
    fh = schlick_weight(cd)

    diffuse = burley_diffuse(cl, cv, cd, base, roughness)
    sheen_term = sheen * fh  # sheen (1 - cos theta_d)^5 with no tint

    alpha = max(1e-4, roughness * roughness)
    f0 = mix(0.08 * specular, base, metallic)
    ds = gtr2(ch, alpha)
    fs = mix(f0, 1.0, fh)
    ag = (0.5 + roughness / 2.0) ** 2 if remap_g else alpha
    gs = smith_g1_ggx(cl, ag) * smith_g1_ggx(cv, ag)
    spec = ds * fs * gs / (4.0 * cl * cv)

    dr = gtr1(ch, clearcoat_alpha)
    fr = mix(0.04, 1.0, fh)
    gr = smith_g1_ggx(cl, 0.25) * smith_g1_ggx(cv, 0.25)
    coat = 0.25 * clearcoat * dr * fr * gr / (4.0 * cl * cv)

    return (diffuse + sheen_term) * (1.0 - metallic) + spec + coat


def directional_albedo(theta_v: float, nt: int = 160, npf: int = 64, **params: float) -> float:
    """The integral of f (n.l) over the hemisphere of l, by the midpoint rule in theta_l and phi_l."""
    v = (math.sin(theta_v), 0.0, math.cos(theta_v))
    total = 0.0
    dt = (math.pi / 2.0) / nt
    dp = 2.0 * math.pi / npf
    for i in range(nt):
        t = (i + 0.5) * dt
        st, ct = math.sin(t), math.cos(t)
        ring = 0.0
        for j in range(npf):
            p = (j + 0.5) * dp
            l = (st * math.cos(p), st * math.sin(p), ct)
            ring += principled_brdf(l, v, **params)
        total += ring * ct * st
    return total * dt * dp


def sample_ggx_normal(rng: random.Random, alpha: float) -> Vec:
    """A normal m with the density D(m) (n.m) for GGX."""
    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 furnace_metal(theta_v: float, roughness: float, rng: random.Random, n: int) -> tuple[float, float]:
    """Monte Carlo albedo of a white metal (base 1, metallic 1), so F = 1.

    l is the mirror of v about a GGX normal h, with the pdf D (n.h) / (4 v.h).
    The weight f (n.l) / pdf is G (v.h) / ((n.v)(n.h)). Returns the mean and
    its standard error.
    """
    v = (math.sin(theta_v), 0.0, math.cos(theta_v))
    alpha = max(1e-4, roughness * roughness)
    vals = []
    for _ in range(n):
        h = sample_ggx_normal(rng, alpha)
        vh = dot(v, h)
        if vh <= 0.0:
            vals.append(0.0)
            continue
        l = (2.0 * vh * h[0] - v[0], 2.0 * vh * h[1] - v[1], 2.0 * vh * h[2] - v[2])
        if l[2] <= 0.0:
            vals.append(0.0)
            continue
        g = smith_g1_ggx(l[2], alpha) * smith_g1_ggx(v[2], alpha)
        vals.append(g * vh / (v[2] * h[2]))
    mean = sum(vals) / n
    var = sum((x - mean) ** 2 for x in vals) / (n - 1)
    return mean, math.sqrt(var / n)
test_disney_principled_brdf.py 152 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/disney-principled-brdf/. The authors are listed in prov/X3TDVD.jsonl.

"""Tests of the principled BRDF: GTR normalization, reciprocity, lobes and a white furnace."""

import math
import random

from disney_principled_brdf import (
    burley_diffuse,
    directional_albedo,
    dot,
    furnace_metal,
    gtr1,
    gtr2,
    normalize,
    principled_brdf,
)


def sph(t: float, p: float) -> tuple[float, float, float]:
    return (math.sin(t) * math.cos(p), math.sin(t) * math.sin(p), math.cos(t))


def integrate_d(d, alpha: float, steps: int = 40000) -> float:
    """The integral of D (n.h) over the hemisphere, in u = cos^2 so that peaks are resolved.

    With u = cos^2(theta), 2 pi D cos sin d theta = pi D du.
    """
    du = 1.0 / steps
    return math.pi * du * sum(d(math.sqrt((i + 0.5) * du), alpha) for i in range(steps))


def diffuse_albedo(theta_v: float, r: float, nt: int = 200, npf: int = 64) -> float:
    v = sph(theta_v, 0.0)
    total = 0.0
    dt, dp = (math.pi / 2.0) / nt, 2.0 * math.pi / npf
    for i in range(nt):
        t = (i + 0.5) * dt
        ring = 0.0
        for j in range(npf):
            l = sph(t, (j + 0.5) * dp)
            h = normalize((l[0] + v[0], l[1] + v[1], l[2] + v[2]))
            ring += burley_diffuse(l[2], v[2], dot(l, h), 1.0, r)
        total += ring * math.cos(t) * math.sin(t)
    return total * dt * dp


def test_gtr_normalization() -> None:
    for alpha in (0.05, 0.2, 0.5, 0.9, 1.0):
        assert abs(integrate_d(gtr2, alpha) - 1.0) < 1e-4
        assert abs(integrate_d(gtr1, alpha) - 1.0) < 1e-4
    # GTR1 has the longer tail: at 60 degrees it is far above GTR2 with the same peak width.
    c = math.cos(math.radians(60.0))
    assert gtr1(c, 0.1) > 10.0 * gtr2(c, 0.1)


def test_reciprocity_and_positivity() -> None:
    rng = random.Random(1)
    for _ in range(1000):
        l = sph(rng.uniform(0.0, 1.55), rng.uniform(0.0, 2.0 * math.pi))
        v = sph(rng.uniform(0.0, 1.55), rng.uniform(0.0, 2.0 * math.pi))
        p = dict(
            base=rng.random(),
            metallic=rng.random(),
            specular=rng.random(),
            roughness=rng.uniform(0.05, 1.0),
            sheen=rng.random(),
            clearcoat=rng.random(),
            clearcoat_alpha=rng.uniform(0.01, 0.3),
        )
        a, b = principled_brdf(l, v, **p), principled_brdf(v, l, **p)
        assert a >= 0.0
        assert abs(a - b) <= 1e-9 * max(1.0, a)


def test_metallic_is_linear_blend() -> None:
    rng = random.Random(3)
    for _ in range(500):
        l = sph(rng.uniform(0.0, 1.55), rng.uniform(0.0, 2.0 * math.pi))
        v = sph(rng.uniform(0.0, 1.55), rng.uniform(0.0, 2.0 * math.pi))
        p = dict(base=rng.random(), specular=rng.random(), roughness=rng.uniform(0.05, 1.0), sheen=rng.random())
        m = rng.random()
        blend = (1.0 - m) * principled_brdf(l, v, metallic=0.0, **p) + m * principled_brdf(l, v, metallic=1.0, **p)
        a = principled_brdf(l, v, metallic=m, **p)
        assert abs(a - blend) <= 1e-9 * max(1.0, a)


def test_lobes() -> None:
    l, v = sph(0.5, 0.0), sph(0.7, 2.5)
    # A metal has no diffuse or sheen.
    m = principled_brdf(l, v, base=0.9, metallic=1.0, roughness=0.4, sheen=1.0)
    assert abs(m - principled_brdf(l, v, base=0.9, metallic=1.0, roughness=0.4, sheen=0.0)) < 1e-15
    # Sheen adds sheen (1 - cos theta_d)^5 on a dielectric.
    h = normalize((l[0] + v[0], l[1] + v[1], l[2] + v[2]))
    w = (1.0 - dot(l, h)) ** 5
    d = principled_brdf(l, v, sheen=1.0) - principled_brdf(l, v, sheen=0.0)
    assert abs(d - w) < 1e-12
    # FD90 = 1 gives the Lambertian value base / pi.
    cd = dot(l, h)
    r = 0.5 / (2.0 * cd * cd)
    assert abs(burley_diffuse(l[2], v[2], cd, 0.7, r) - 0.7 / math.pi) < 1e-12


def test_burley_diffuse_albedo() -> None:
    table = {
        0.0: (0.9762, 0.9751, 0.9609, 0.8670, 0.6668),
        0.5: (1.0060, 1.0047, 1.0083, 1.0220, 1.0224),
        1.0: (1.0357, 1.0344, 1.0569, 1.1853, 1.4006),
    }
    for r, vals in table.items():
        for deg, val in zip((0.0, 45.0, 60.0, 75.0, 85.0), vals):
            assert abs(diffuse_albedo(math.radians(deg), r, nt=100, npf=24) - val) < 2e-3


def test_white_dielectric_albedo_above_one() -> None:
    # Diffuse and specular are added with no energy taken from the diffuse,
    # so a white rough dielectric reflects more than all the light.
    for deg, val in ((0.0, 1.0480), (60.0, 1.0756), (80.0, 1.3074)):
        q = directional_albedo(math.radians(deg), nt=100, npf=32, base=1.0, roughness=1.0)
        assert abs(q - val) < 3e-3
        assert q > 1.0


def test_white_furnace_metal() -> None:
    # A white metal reflects at most all light; the single-scattering loss grows with roughness.
    rng = random.Random(5)
    means = []
    for r in (0.2, 0.5, 1.0):
        mean, se = furnace_metal(0.0, r, rng, 20000)
        assert mean <= 1.0 + 4.0 * se
        means.append(mean)
    assert means[0] > means[1] > means[2]
    assert abs(means[0] - 0.998) < 0.003
    assert abs(means[1] - 0.916) < 0.01
    assert abs(means[2] - 0.307) < 0.01
    # Against deterministic quadrature where the lobe is wide enough for the grid.
    for r in (0.5, 1.0):
        for deg in (0.0, 60.0):
            q = directional_albedo(math.radians(deg), nt=120, npf=64, base=1.0, metallic=1.0, roughness=r)
            mean, se = furnace_metal(math.radians(deg), r, rng, 20000)
            assert abs(q - mean) < 4.0 * se + 5e-3


def test_g_remap_darkens_grazing() -> None:
    # For r < 1 the 2012 remap alpha_g = (0.5 + r / 2)^2 is larger than r^2,
    # so it masks more and the grazing specular is lower.
    l, v = sph(1.2, 0.0), sph(1.2, math.pi)
    for r in (0.2, 0.5, 0.8):
        a = principled_brdf(l, v, base=1.0, metallic=1.0, roughness=r, remap_g=False)
        b = principled_brdf(l, v, base=1.0, metallic=1.0, roughness=r, remap_g=True)
        assert b < a

References

  1. Brent Burley, Physically-based shading at Disney, course notes, SIGGRAPH 2012 course Practical Physically Based Shading in Film and Game Production, revised 2014, Section 4 (observations of the MERL 100 measured materials), Section 5.1 (the principles), Section 5.2 (the parameters), Sections 5.3 to 5.6 (the diffuse, D, F and G terms), Section 5.7 (layering and parameter blending), Appendix B (normalization of GTR) and the 2014 Addenda (sheen details, specular G revisited)
  2. Brian Karis, Real shading in Unreal Engine 4, course notes, SIGGRAPH 2013 course Physically Based Shading in Theory and Practice, the sections Shading Model (Diffuse BRDF) and Material Model (BaseColor, Metallic, Roughness and Cavity, and the parameters left out)
  3. Eric Heitz, Understanding the masking-shadowing function in microfacet-based BRDFs, Journal of Computer Graphics Techniques 3(2), 2014, 48 to 107, Section 3.5 (the weak white furnace test) and Section 6 (the Smith G2 forms), the analysis that led to the 2014 change of the specular G
  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 (general locator)

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/X3TDVD/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/X3TDVD/text.yaml