Beckmann Distribution

The normals of the microfacets are distributed according to the Beckmann distribution:

D⁡(ω)=1πab2cos⁡4θexp⁡−tan⁡2θab2D⁡(θ,ϕ)=sin⁡θπab2cos⁡4θexp⁡−tan⁡2θab2\begin{aligned} \operatorname{D}(\omega) &= \frac{1}{\pi a_b^2\cos^4\theta}\exp{\frac{-\tan^{2}\theta}{a_b^2}}\\ \operatorname{D}(\theta,\phi) &= \frac{\sin\theta}{\pi a_b^2\cos^4\theta}\exp{\frac{-\tan^{2}\theta}{a_b^2}} \end{aligned}

Here is the result:

function sample_beckmann(a){

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

    return [theta,phi];
}

Algorithm

Sample microfacet normal according to the Beckmann distribution:

  1. Choose uniform numbers u1,u2u_1, u_2 in [0,1][0,1]
  2. Calculate θ=tan⁡−1(−ab2log⁡(1−u1))\theta = \tan^{-1}( \sqrt{-a_b^2\log (1-u_1)}) or θ=tan⁡−1(−ab2log⁡(u1))\theta = \tan^{-1}( \sqrt{-a_b^2\log (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(ω)=cos⁡θπab2cos⁡4θexp⁡−tan⁡2θab2=1πab2cos⁡3θexp⁡−tan⁡2θab2p_m(\omega) = \frac{\cos\theta}{\pi a_b^2\cos^4\theta}\exp{\frac{-\tan^{2}\theta}{a_b^2}} = \frac{1}{\pi a_b^2\cos^3\theta}\exp{\frac{-\tan^{2}\theta}{a_b^2}}

pm(θ,ϕ)=cos⁡θsin⁡θπab2cos⁡4θexp⁡−tan⁡2θab2=tan⁡θπab2cos⁡2θexp⁡−tan⁡2θab2p_m(\theta,\phi) = \frac{\cos\theta\sin\theta}{\pi a_b^2\cos^4\theta}\exp{\frac{-\tan^{2}\theta}{a_b^2}} = \frac{\tan\theta}{\pi a_b^2\cos^2\theta}\exp{\frac{-\tan^{2}\theta}{a_b^2}}

Derivation

Due to normalization, the densities are given as follows:

p(ω)=D(ω)cos⁡θp(θ,ϕ)=p(ω)sin⁡θ=sin⁡θcos⁡θπab2cos⁡4θexp⁡−tan⁡2θab2=tan⁡θπab2cos⁡2θexp⁡−tan⁡2θab2\begin{aligned} p(\omega) &= D(\omega)\cos\theta\\ p(\theta,\phi) &= p(\omega)\sin\theta\\ &= \frac{\sin\theta\cos\theta}{\pi a_b^2\cos^4\theta}\exp{\frac{-\tan^{2}\theta}{a_b^2}} \\ &= \frac{\tan\theta}{\pi a_b^2\cos^2\theta}\exp{\frac{-\tan^{2}\theta}{a_b^2}} \end{aligned}

Marginal And Conditional Densities:

pθ(θ)=∫02πp(θ,ϕ)dϕ=∫02πtan⁡θπab2cos⁡2θexp⁡−tan⁡2θab2dϕ=2πtan⁡θπab2cos⁡2θexp⁡−tan⁡2θab2=2tan⁡θab2cos⁡2θexp⁡−tan⁡2θab2=2πp(θ,ϕ)\begin{aligned} p_\theta(\theta) &= \int_0^{2\pi}p(\theta,\phi)d\phi \\ &=\int_0^{2\pi}\frac{\tan\theta}{\pi a_b^2\cos^2\theta}\exp{\frac{-\tan^{2}\theta}{a_b^2}} d\phi \\ &= 2\pi\frac{\tan\theta}{\pi a_b^2\cos^2\theta}\exp{\frac{-\tan^{2}\theta}{a_b^2}} \\ &= 2\frac{\tan\theta}{ a_b^2\cos^2\theta}\exp{\frac{-\tan^{2}\theta}{a_b^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θ2tan⁡θ′ab2cos⁡2θ′exp⁡−tan⁡2θ′ab2dθ′P_\theta(\theta) = \int_{0}^{\theta}2\frac{\tan\theta'}{ a_b^2\cos^2\theta'}\exp{\frac{-\tan^{2}\theta'}{a_b^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 dudu (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θ(θ)=∫2tan⁡θ′ab2cos⁡2θ′exp⁡(−tan⁡2θ′ab2)dθ′=2ab2∫ucos⁡2θ′exp⁡(−u2ab2)cos⁡2θ′du=2ab2∫uexp⁡(−u2ab2)du\begin{aligned} P_\theta(\theta) &= \int 2\frac{\tan\theta'}{ a_b^2\cos^2\theta'}\exp{(\frac{-\tan^{2}\theta'}{a_b^2})} d\theta' \\ &= \frac{2}{a_b^2} \int \frac{u}{ \cos^2\theta'}\exp{(\frac{-u^{2}}{a_b^2})}\cos^2\theta' du\\ &= \frac{2}{a_b^2} \int u\exp{(\frac{-u^{2}}{a_b^2})} du \end{aligned}

The integral can now be solved by basic means by considering the derivative of the exponential.

Pθ(θ)=2ab2∫uexp⁡(−u2ab2)du=2ab2[−12ab2exp⁡(−u2ab2)]=−[exp⁡(−u2ab2)]\begin{aligned} P_\theta(\theta) &= \frac{2}{a_b^2} \int u\exp{(\frac{-u^{2}}{a_b^2})} du\\ &= \frac{2}{a_b^2} [-\frac{1}{2}a_b^2\exp(\frac{-u^2}{a_b^2})] \\ &= -[\exp(\frac{-u^2}{a_b^2})] \end{aligned}

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

Pθ(θ)=2ab2∫uexp⁡(−u2ab2)du=−[exp⁡(−tan⁡2θ′ab2)]0θ=−(exp⁡(−tan⁡2θab2)−exp⁡(−tan⁡20ab2))=−(exp⁡(−tan⁡2θab2)−1)=1−exp⁡(−tan⁡2θab2)\begin{aligned} P_\theta(\theta) &= \frac{2}{a_b^2} \int u\exp{(\frac{-u^{2}}{a_b^2})} du\\ &= -[\exp(\frac{-\tan^2\theta'}{a_b^2})]_0^{\theta}\\ &= -(\exp(\frac{-\tan^2\theta}{a_b^2}) - \exp(\frac{-\tan^20}{a_b^2}) ) \\ &= -(\exp(\frac{-\tan^2\theta}{a_b^2}) - 1 ) \\ &= 1- \exp(\frac{-\tan^2\theta}{a_b^2}) \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−exp⁡(−tan⁡2θab2)u1−1=−exp⁡(−tan⁡2θab2)1−u1=exp⁡(−tan⁡2θab2)log⁡(1−u1)=−tan⁡2θab2−ab2log⁡(1−u1)=tan⁡2θ−ab2log⁡(1−u1)=tan⁡θtan⁡−1(−ab2log⁡(1−u1))=θθ=tan⁡−1(−ab2log⁡(1−u1))\begin{aligned} u_1 &= P_\theta(\theta)\\ &= 1- \exp(\frac{-\tan^2\theta}{a_b^2}) \\ u_1 - 1 &=- \exp(\frac{-\tan^2\theta}{a_b^2}) \\ 1- u_1 &= \exp(\frac{-\tan^2\theta}{a_b^2}) \\ \log (1-u_1) &= \frac{-\tan^2\theta}{a_b^2} \\ -a_b^2\log (1-u_1)&= \tan^2\theta \\ \sqrt{-a_b^2\log (1-u_1)}&= \tan\theta \\ \tan^{-1}( \sqrt{-a_b^2\log (1-u_1)}) &= \theta\\ \theta &=\tan^{-1}( \sqrt{-a_b^2\log (1-u_1)}) \end{aligned}

Since u1u_1 is uniformly distributed in [0,1][0,1], so is 1−u11-u_1, so we can use either of them for the generation.

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