Trowbridge–Reitz / GGX Distribution

The normals of the microfacets are distributed according to the Trowbridge–Reitz / GGX distribution:

D⁡(ω)=ag2πcos⁡4θ(ag2+tan⁡2θ)2D⁡(θ,ϕ)=ag2πcos⁡4θ(ag2+tan⁡2θ)2sin⁡θ\begin{aligned} \operatorname{D}(\omega) &= \frac{a_g^2}{\pi\cos^4\theta (a_g^2 + \tan^2\theta)^2}\\ \operatorname{D}(\theta,\phi) &= \frac{a_g^2}{\pi\cos^4\theta (a_g^2 + \tan^2\theta)^2}\sin\theta \end{aligned}

Here is the result:

function sample_ggx(a) {
    
    const u_1 = Math.random();
    const u_2 = Math.random();
    
    // (u_1) works the same way as (1.0 - u_1)
    const theta = Math.atan(Math.sqrt(a * a * u_1 / (1.0 - u_1)));
    const phi = u_2 * 2.0 * Math.PI;

    return [theta,phi];
}

Algorithm

Sample microfacet normal according to the Trowbridge–Reitz / GGX distribution:

  1. Choose uniform numbers u1,u2u_1, u_2 in [0,1][0,1]
  2. Calculate θ=tan⁡−1(agu11−u1)\theta = \tan^{-1}(\frac{a_g\sqrt{u_1}}{\sqrt{1- u_1}})
  3. Calculate ϕ=2πu2\phi = 2\pi u_2

Optionally convert to cartesian coordinates with:

x=sin⁡θcos⁡ϕx = \sin\theta\cos\phi

y=sin⁡θsin⁡ϕy = \sin\theta\sin\phi

z=cos⁡θz = \cos\theta

PDF Used In Monte Carlo Integration

pm(ω)=ag2cos⁡θπcos⁡4θ(ag2+tan⁡2θ)2=ag2πcos⁡3θ(ag2+tan⁡2θ)2p_m(\omega) = \frac{a_g^2\cos\theta}{\pi\cos^4\theta (a_g^2 + \tan^2\theta)^2} = \frac{a_g^2}{\pi\cos^3\theta (a_g^2 + \tan^2\theta)^2}

pm(θ,ϕ)=ag2cos⁡θsin⁡θπcos⁡4θ(ag2+tan⁡2θ)2=ag2tan⁡θπcos⁡2θ(ag2+tan⁡2θ)2p_m(\theta,\phi) = \frac{a_g^2\cos\theta\sin\theta}{\pi\cos^4\theta (a_g^2 + \tan^2\theta)^2}= \frac{a_g^2\tan\theta}{\pi\cos^2\theta (a_g^2 + \tan^2\theta)^2}

Derivation

Due to normalization, the densities are given as follows:

p(ω)=D(ω)cos⁡θp(θ,ϕ)=p(ω)sin⁡θ=ag2cos⁡θsin⁡θπcos⁡4θ(ag2+tan⁡2θ)2=ag2tan⁡θπcos⁡2θ(ag2+tan⁡2θ)2\begin{aligned} p(\omega) &= D(\omega)\cos\theta\\ p(\theta,\phi) &= p(\omega)\sin\theta\\ &= \frac{a_g^2\cos\theta\sin\theta}{\pi\cos^4\theta (a_g^2 + \tan^2\theta)^2}\\ &= \frac{a_g^2\tan\theta}{\pi\cos^2\theta (a_g^2 + \tan^2\theta)^2} \end{aligned}

Marginal And Conditional Densities:

pθ(θ)=∫02πp(θ,ϕ)dϕ=∫02πag2tan⁡θπcos⁡2θ(ag2+tan⁡2θ)2dϕ=2πag2tan⁡θπcos⁡2θ(ag2+tan⁡2θ)2=2ag2tan⁡θcos⁡2θ(ag2+tan⁡2θ)2=2πp(θ,ϕ)\begin{aligned} p_\theta(\theta) &= \int_0^{2\pi}p(\theta,\phi)d\phi \\ &=\int_0^{2\pi}\frac{a_g^2\tan\theta}{\pi\cos^2\theta (a_g^2 + \tan^2\theta)^2} d\phi \\ &= 2\pi\frac{a_g^2\tan\theta}{\pi\cos^2\theta (a_g^2 + \tan^2\theta)^2}\\ &= 2a_g^2\frac{\tan\theta}{\cos^2\theta (a_g^2 + \tan^2\theta)^2}\\ &= 2\pi p(\theta, \phi) \end{aligned}

p(ϕ∣θ)=p(θ,ϕ)pθ(θ)=p(θ,ϕ)2πp(θ,ϕ)=12πp(\phi \vert \theta) = \frac{p(\theta,\phi)}{p_\theta(\theta)} = \frac{p(\theta,\phi)}{2\pi p(\theta,\phi)} = \frac{1}{2\pi}

Compute CDFs

Pθ(θ)=∫0θ2ag2tan⁡θ′cos⁡2θ′(ag2+tan⁡2θ′)2dθ′P_\theta(\theta) = \int_{0}^{\theta}2a_g^2\frac{\tan\theta'}{\cos^2\theta' (a_g^2 + \tan^2\theta')^2}d\theta'

To solve this integral, we change variables and introduce u=tan⁡θ′u = \tan\theta'. That gives us du=(tan⁡θ′)′dθ′=1cos⁡2θ′dθ′du = (\tan\theta')'d\theta' = \frac{1}{\cos^2\theta'}d\theta'. Solving for dθ′d\theta' (with the usual slight abuse of notation): dθ′=cos⁡2θ′dud\theta' = \cos^2\theta' du.

As we will resubstitute later, for now let's disregard the limits:

Pθ(θ)=∫2ag2tan⁡θ′cos⁡2θ′(ag2+tan⁡2θ′)2dθ′=2ag2∫ucos⁡2θ′(ag2+u2)2cos⁡2θ′du=2ag2∫u(ag2+u2)2du\begin{aligned} P_\theta(\theta) &= \int 2a_g^2\frac{\tan\theta'}{\cos^2\theta' (a_g^2 + \tan^2\theta')^2}d\theta' \\ &= 2a_g^2 \int \frac{u}{\cos^2\theta' (a_g^2 + u^2)^2}\cos^2\theta'du\\ &= 2a_g^2 \int \frac{u}{(a_g^2 + u^2)^2} du\\ \end{aligned}

We will do a second substitution: v=ag2+u2v = a_g^2 + u^2. That gives us dv=(ag2+u2)′du=2ududv = (a_g^2 +u^2)'du = 2udu. Solving for dudu: du=dv2udu = \frac{dv}{2u}

2ag2∫u(ag2+u2)2du=2ag2∫uv2dv2u=ag2∫1v2dv\begin{aligned} 2a_g^2 \int \frac{u}{(a_g^2 + u^2)^2} du &= 2a_g^2 \int \frac{u}{v^2} \frac{dv}{2u} \\ &= a_g^2 \int \frac{1}{v^2} dv \end{aligned}

This can be easily solved:

ag2∫1v2dv=ag2[−1v]=−ag2[1v]\begin{aligned} a_g^2 \int \frac{1}{v^2} dv \\ &= a_g^2 [-\frac{1}{v}] \\ &= -a_g^2 [\frac{1}{v}] \end{aligned}

Resubstituting v=ag2+u2v = a_g^2 + u^2:

−ag2[1v]=−ag2[1ag2+u2]\begin{aligned} -a_g^2 [\frac{1}{v}] &= -a_g^2 [\frac{1}{a_g^2 + u^2}] \end{aligned}

Resubstituting u=tan⁡θ′u = \tan\theta':

Pθ(θ)=−ag2[1ag2+u2]=−ag2[1ag2+tan⁡2θ′]0θ=−ag2(1ag2+tan⁡2θ−1ag2+tan⁡20)=−ag2(1ag2+tan⁡2θ−1ag2)=1−ag2ag2+tan⁡2θ\begin{aligned} P_\theta(\theta) &= -a_g^2 [\frac{1}{a_g^2 + u^2}]\\ &= -a_g^2 [\frac{1}{a_g^2 + \tan^2\theta'}]_0^{\theta}\\ &= -a_g^2 (\frac{1}{a_g^2 + \tan^2\theta} - \frac{1}{a_g^2 + \tan^20})\\ &= -a_g^2 (\frac{1}{a_g^2 + \tan^2\theta} - \frac{1}{a_g^2})\\ &= 1 - \frac{a_g^2}{a_g^2 + \tan^2\theta} \end{aligned}

P(ϕ∣θ)=∫0ϕp(ϕ′∣θ)dϕ′=∫0ϕ12πdϕ′=ϕ2πP(\phi \vert \theta) = \int_{0}^{\phi}p(\phi ' \vert \theta)d\phi' = \int_{0}^{\phi} \frac{1}{2\pi}d\phi' =\frac{\phi}{2\pi}

Invert CDFs

u1=Pθ(θ)=1−ag2ag2+tan⁡2θu1−1=−ag2ag2+tan⁡2θ1−u1=ag2ag2+tan⁡2θag2+tan⁡2θ=ag21−u1tan⁡2θ=ag21−u1−ag2tan⁡θ=ag21−u1−ag2θ=tan⁡−1(ag21−u1−ag2)=tan⁡−1(ag2−ag2(1−u1)1−u1)=tan⁡−1(ag2−ag2+ag2u11−u1)=tan⁡−1(ag2u11−u1)=tan⁡−1(agu11−u1)\begin{aligned} u_1 &= P_\theta(\theta)\\ &= 1 - \frac{a_g^2}{a_g^2 + \tan^2\theta} \\ u_1 - 1&=- \frac{a_g^2}{a_g^2 + \tan^2\theta} \\ 1- u_1 &= \frac{a_g^2}{a_g^2 + \tan^2\theta} \\ a_g^2 + \tan^2\theta &= \frac{a_g^2}{1- u_1} \\ \tan^2\theta &= \frac{a_g^2}{1- u_1} -a_g^2 \\ \tan\theta &= \sqrt{\frac{a_g^2}{1- u_1} -a_g^2}\\ \theta &= \tan^{-1}(\sqrt{\frac{a_g^2}{1- u_1} -a_g^2}) \\ &= \tan^{-1}(\sqrt{\frac{a_g^2 - a_g^2(1-u_1)}{1- u_1}}) \\ &= \tan^{-1}(\sqrt{\frac{a_g^2- a_g^2+ a_g^2u_1}{1- u_1}}) \\ &= \tan^{-1}(\sqrt{\frac{a_g^2u_1}{1- u_1}}) \\ &= \tan^{-1}(\frac{a_g\sqrt{u_1}}{\sqrt{1- u_1}}) \end{aligned}

Here the last line is the formulation found in Walter et al.

u2=P(ϕ∣θ)=ϕ2πϕ=u22π\begin{aligned} u_2 &= P(\phi \vert \theta) \\ &= \frac{\phi}{2\pi}\\ \phi &= u_22\pi \end{aligned}