Trowbridge–Reitz / GGX Distribution

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

D(ω)=ag2πcos4θ(ag2+tan2θ)2D(θ,ϕ)=ag2πcos4θ(ag2+tan2θ)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 θ=tan1(agu11u1)\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θπcos4θ(ag2+tan2θ)2=ag2πcos3θ(ag2+tan2θ)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θπcos4θ(ag2+tan2θ)2=ag2tanθπcos2θ(ag2+tan2θ)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θπcos4θ(ag2+tan2θ)2=ag2tanθπcos2θ(ag2+tan2θ)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θπcos2θ(ag2+tan2θ)2dϕ=2πag2tanθπcos2θ(ag2+tan2θ)2=2ag2tanθcos2θ(ag2+tan2θ)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θcos2θ(ag2+tan2θ)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θ=1cos2θ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θ=cos2θdud\theta' = \cos^2\theta' du.

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

Pθ(θ)=2ag2tanθcos2θ(ag2+tan2θ)2dθ=2ag2ucos2θ(ag2+u2)2cos2θdu=2ag2u(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}

2ag2u(ag2+u2)2du=2ag2uv2dv2u=ag21v2dv\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:

ag21v2dv=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+tan2θ]0θ=ag2(1ag2+tan2θ1ag2+tan20)=ag2(1ag2+tan2θ1ag2)=1ag2ag2+tan2θ\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θ(θ)=1ag2ag2+tan2θu11=ag2ag2+tan2θ1u1=ag2ag2+tan2θag2+tan2θ=ag21u1tan2θ=ag21u1ag2tanθ=ag21u1ag2θ=tan1(ag21u1ag2)=tan1(ag2ag2(1u1)1u1)=tan1(ag2ag2+ag2u11u1)=tan1(ag2u11u1)=tan1(agu11u1)\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}