🎯 Sampling the hemisphere

17-minute read

Introduction

Generating samples within a hemisphere around a point is needed when doing Monte-Carlo path tracing. These samples can be chosen in a smart way, to be proportional to the BRDF. The following sections explain how to generate these samples, and how to properly weight them using the pdf, for several BRDF models.

The result of importance sampling can be a significant reduction in noise. In the following image, the sphere on the left uses cosine-weighted sampling while the sphere on the right uses BRDF importance-sampling.

Sable.
Cosine-weighted sampling.
The Last of Us.
BRDF importance-sampling

Spherical and cartesian coordinates

When generating samples in the hemisphere, this is usually done using spherical coordinates by calculating a θ\theta and ϕ\phi angle. These can then be converted to cartesian coordinates using the following formula.

{x=cos(ϕ)sin(θ)y=sin(ϕ)sin(θ)z=cos(θ)\left\{\begin{matrix} \begin{aligned} x&=\cos(\phi) \cdot \sin(\theta)\\ y&=\sin(\phi) \cdot \sin(\theta)\\ z&=\cos(\theta) \end{aligned} \end{matrix}\right.

In these snippets you'll see that the sine/cosine is often calculated directly instead of calculating the angle θ\theta and then taking the sine/cosine.

Technique

The following steps are performed.

  1. Calculate the normalized PDF
  2. Split up the PDF (one for θ\theta, one for ϕ\phi)
  3. Calculate the CDF by integrating the PDF
  4. Set the CDF equal to some random number between 0 and 1 to obtain a θ\theta and ϕ\phi angle which define the sample

A common action that is performed throughout the following equations is going from an integral over the hemisphere using solid angles, to an integral over the hemisphere using spherical coordinates. This transformation is done as follows.

Ωf(θ)dw=02π0π2f(θ,ϕ)sinθdθdϕ\int _{\Omega }f(\theta)dw = \int_{0}^{2\pi }\int_{0}^{\frac{\pi}{2}}f(\theta, \phi) \sin\theta d \theta d \phi

Uniform hemisphere

{θ=cos1(ε0)ϕ=2πε1\left\{\begin{matrix} \begin{aligned} \theta &= \cos^{-1}(\varepsilon_{0}) \\ \phi &= 2\pi \varepsilon_{1} \end{aligned}\end{matrix}\right.

pdf(wi)=12πpdf(w_{i}) = \frac{1}{2\pi}

Derivation

For uniform sampling, the PDF should be proportional to 1. We PDF is normalized by dividing by the PDF, integrated over the whole hemisphere.

pdf(ω)=1Ω1dω=102π0π21sinθdθdϕ=12πpdf(θ,ϕ)=pdf(ω)sinθ=sinθ2π\begin{aligned} pdf(\omega) &= \frac{1}{\int _{\Omega }1d\omega} = \frac{1}{\int_{0}^{2\pi }\int_{0}^{\frac{\pi}{2}}1\sin\theta d \theta d \phi} = \frac{1}{2\pi} \\ pdf(\theta,\phi) &= pdf(\omega ) \sin\theta = \frac{\sin\theta}{2\pi} \end{aligned}

Next, the PDF is split up. We obtain the pdf for θ\theta by integrating over the whole domain of the ϕ\phi angle which is the same as multiplying by 2π2\pi since the pdf is isotropic. The pdf for ϕ\phi may be obtained by using conditional probabilities and the rule of Bayes.

pdf(θ)=02πpdf(θ,ϕ)dϕ=sinθpdf(ϕθ)=pdf(θ,ϕ)pdf(θ)=sinθ2πsinθ=12π\begin{aligned} pdf(\theta) &= \int_{0}^{2\pi}pdf(\theta, \phi) d\phi = \sin\theta \\ pdf(\phi|\theta) &= \frac{pdf(\theta, \phi)}{pdf(\theta)} = \frac{\frac{\sin\theta}{2\pi}}{\sin\theta} = \frac{1}{2\pi} \end{aligned}

We can then integrate the PDF to get the CDF.

cdf(θ)=0θpdf(θ)dθ=0θsinθdθ=1cosθcdf(ϕθ)=0ϕpdf(ϕθ)dθ=0ϕ12πdϕ=1ϕ2π\begin{aligned} cdf(\theta) &= \int_{0}^{\theta}pdf(\theta)d\theta = \int_{0}^{\theta}\sin\theta d\theta = 1-\cos\theta \\ cdf(\phi|\theta) &= \int_{0}^{\phi}pdf(\phi|\theta) d\theta = \int_{0}^{\phi}\frac{1}{2\pi} d\phi = \frac{1\phi}{2\pi} \end{aligned}

By setting these CDFs equal to a random number ε,ϵ[0,1]\varepsilon, \epsilon \left [ 0, 1 \right ], we can find an equation for the angle θ\theta and ϕ\phi that we can use for our sample.

cdf(θ)=1cosθ=ε0θ=cos1(1ε0)cdf(ϕθ)=1ϕ2π=ε1ϕ=2πε1\begin{alignat*}{2} cdf(\theta) &= 1-\cos\theta {}& =\varepsilon_{0} \rightarrow \theta &= \cos^{-1}(1-\varepsilon_{0}) \\ cdf(\phi|\theta) &= \frac{1\phi}{2\pi} {}& =\varepsilon_{1} \rightarrow \phi &= 2\pi \varepsilon_{1} \end{alignat*}

Since ε\varepsilon is a number between 0 and 1, these equations can be written a bit cleaner as follows.

{θ=cos1(ε0)ϕ=2πε1\left\{\begin{matrix} \begin{aligned} \theta &= \cos^{-1}(\varepsilon_{0}) \\ \phi &= 2\pi \varepsilon_{1} \end{aligned} \end{matrix}\right.

Cosine-weighted hemisphere

{θ=cos1(ε0)ϕ=2πε1\left\{\begin{matrix} \begin{aligned} \theta &= \cos^{-1}(\sqrt{\varepsilon_{0}}) \\ \phi &= 2\pi \varepsilon_{1} \end{aligned} \end{matrix}\right.

pdf(wi)=cosθπpdf(w_{i}) = \frac{\cos\theta}{\pi}

Derivation

For cosine-weighted sampling, the PDF should be proportional to a cosine. The PDF is normalized by dividing by the result of integrating the PDF over the whole hemisphere.

pdf(ω)=cosθΩcosθdω=cosθ02π0π2sinθcosθdθdϕ=cosθπpdf(θ,ϕ)=pdf(ω)sinθ=sinθcosθπ\begin{aligned} pdf(\omega) &= \frac{\cos\theta}{\int _{\Omega }\cos\theta d\omega} = \frac{\cos\theta}{\int_{0}^{2\pi }\int_{0}^{\frac{\pi}{2}}\sin\theta\cos\theta d \theta d \phi} = \frac{\cos\theta}{\pi} \\ pdf(\theta,\phi) &= pdf(\omega ) \sin\theta = \frac{\sin\theta\cos\theta}{\pi} \end{aligned}

Next, the PDF is split up. We obtain the pdf for θ\theta by integrating over the whole domain of the ϕ\phi angle which is the same as multiplying by 2π2\pi since the pdf is isotropic. The pdf for ϕ\phi may be obtained by using conditional probabilities and the rule of Bayes.

pdf(θ)=02πpdf(θ,ϕ)dϕ=2sinθcosθpdf(ϕθ)=pdf(θ,ϕ)pdf(θ)=sinθcosθπ2sinθcosθ=12π\begin{aligned} pdf(\theta) &= \int_{0}^{2\pi}pdf(\theta, \phi) d\phi = 2\sin\theta\cos\theta \\ pdf(\phi|\theta) &= \frac{pdf(\theta, \phi)}{pdf(\theta)} = \frac{\frac{\sin\theta\cos\theta}{\pi}}{2\sin\theta\cos\theta} = \frac{1}{2\pi} \end{aligned}

We can then integrate the PDF to get the CDF.

cdf(θ)=0θpdf(θ)dθ=0θ2sinθcosθdθ=sin2θcdf(ϕθ)=0ϕpdf(ϕθ)dθ=0ϕ12πdϕ=1ϕ2π\begin{aligned} cdf(\theta) &= \int_{0}^{\theta}pdf(\theta)d\theta = \int_{0}^{\theta}2\sin\theta\cos\theta d\theta = \sin^2\theta \\ cdf(\phi|\theta) &= \int_{0}^{\phi}pdf(\phi|\theta) d\theta = \int_{0}^{\phi}\frac{1}{2\pi} d\phi = \frac{1\phi}{2\pi} \end{aligned}

By setting these CDFs equal to a random number ε,ϵ[0,1]\varepsilon, \epsilon \left [ 0, 1 \right ], we can find an equation for the angle θ\theta and ϕ\phi that we can use for our sample.

cdf(θ)=sin2θ=ε0θ=sin1(ε0)=cos1(1ε0)cdf(ϕθ)=1ϕ2π=ε1ϕ=2πε1\begin{alignat*}{2} cdf(\theta) &= \sin^2\theta {}& =\varepsilon_{0} \rightarrow \theta &= \sin^{-1}(\sqrt{\varepsilon_{0}}) = \cos^{-1}(\sqrt{1-\varepsilon_{0}}) \\ cdf(\phi|\theta) &= \frac{1\phi}{2\pi} {}& =\varepsilon_{1} \rightarrow \phi &= 2\pi \varepsilon_{1} \end{alignat*}

Since ε\varepsilon is a number between 0 and 1, these equations can be written a bit cleaner as follows.

{θ=cos1(ε0)ϕ=2πε1\left\{\begin{matrix} \begin{aligned} \theta &= \cos^{-1}(\sqrt{\varepsilon_{0}}) \\ \phi &= 2\pi \varepsilon_{1} \end{aligned} \end{matrix}\right.

Power cosine-weighted hemisphere

This is a generalization of the cosine-weighted sampling scheme in the previous section. Instead of sampling proportional to cosθ\cos\theta, here we sample proportional to cosαθ\cos^{\alpha}\theta.

{θ=cos1(ε01α+1)ϕ=2πε1\left\{\begin{matrix} \begin{aligned} \theta &= \cos^{-1}(\varepsilon_{0}^{\frac{1}{\alpha+1}}) \\ \phi &= 2\pi \varepsilon_{1} \end{aligned} \end{matrix}\right.

pdf(wi)=(α+1)cosαθ2πpdf(w_{i}) = \frac{(\alpha+1)\cos^{\alpha}\theta}{2\pi}

Derivation

For power cosine-weighted sampling, the PDF should be proportional to a cosine with a power exponent. The PDF is normalized by dividing by the result of integrating the PDF over the whole hemisphere

pdf(ω)=cosαθΩcosαθdω=cosαθ02π0π2sinθcosαθdθdϕ=(α+1)cosαθ2πpdf(θ,ϕ)=pdf(ω)sinθ=(α+1)cosαθsinθ2π\begin{aligned} pdf(\omega) &= \frac{\cos^{\alpha}\theta}{\int _{\Omega }\cos^{\alpha}\theta d\omega} = \frac{\cos^{\alpha}\theta}{\int_{0}^{2\pi }\int_{0}^{\frac{\pi}{2}}\sin\theta\cos^{\alpha}\theta d \theta d \phi} = \frac{(\alpha+1)\cos^{\alpha}\theta}{2\pi} \\ pdf(\theta,\phi) &= pdf(\omega ) \sin\theta = \frac{(\alpha+1)\cos^{\alpha}\theta\sin\theta}{2\pi} \end{aligned}

Next, the PDF is split up. We obtain the pdf for θ\theta by integrating over the whole domain of the ϕ\phi angle which is the same as multiplying by 2π2\pi since the pdf is isotropic. The pdf for ϕ\phi may be obtained by using conditional probabilities and the rule of Bayes.

pdf(θ)=02πpdf(θ,ϕ)dϕ=(α+1)cosαθsinθpdf(ϕθ)=pdf(θ,ϕ)pdf(θ)=(α+1)cosαθsinθ2π(α+1)cosαθsinθ=12π\begin{aligned} pdf(\theta) &= \int_{0}^{2\pi}pdf(\theta, \phi) d\phi = (\alpha+1)\cos^{\alpha}\theta\sin\theta \\ pdf(\phi|\theta) &= \frac{pdf(\theta, \phi)}{pdf(\theta)} = \frac{\frac{(\alpha+1)\cos^{\alpha}\theta\sin\theta}{2\pi}}{(\alpha+1)\cos^{\alpha}\theta\sin\theta} = \frac{1}{2\pi} \end{aligned}

We can then integrate the PDF to get the CDF.

cdf(θ)=0θpdf(θ)dθ=0θ(α+1)cosαθsinθdθ=1cos(α+1)θcdf(ϕθ)=0ϕpdf(ϕθ)dθ=0ϕ12πdϕ=1ϕ2π\begin{aligned} cdf(\theta) &= \int_{0}^{\theta}pdf(\theta)d\theta = \int_{0}^{\theta}(\alpha+1)\cos^{\alpha}\theta\sin\theta d\theta = 1-\cos^{(\alpha+1)}\theta \\ cdf(\phi|\theta) &= \int_{0}^{\phi}pdf(\phi|\theta) d\theta = \int_{0}^{\phi}\frac{1}{2\pi} d\phi = \frac{1\phi}{2\pi} \end{aligned}

By setting these CDFs equal to a random number ε,ϵ[0,1]\varepsilon, \epsilon \left [ 0, 1 \right ], we can find an equation for the angle θ\theta and ϕ\phi that we can use for our sample.

cdf(θ)=1cos(α+1)θ=ε0θ=cos1((1ε0)1α+1)cdf(ϕθ)=1ϕ2π=ε1ϕ=2πε1\begin{alignat*}{2} cdf(\theta) &= 1-\cos^{(\alpha+1)}\theta {}& =\varepsilon_{0} \rightarrow \theta &= \cos^{-1}((1-\varepsilon_{0})^{\frac{1}{\alpha+1}}) \\ cdf(\phi|\theta) &= \frac{1\phi}{2\pi} {}& =\varepsilon_{1} \rightarrow \phi &= 2\pi \varepsilon_{1} \end{alignat*}

Since ε\varepsilon is a number between 0 and 1, these equations can be written a bit cleaner as follows.

{θ=cos1(ε01α+1)ϕ=2πε1\left\{\begin{matrix} \begin{aligned} \theta &= \cos^{-1}(\varepsilon_{0}^{\frac{1}{\alpha+1}}) \\ \phi &= 2\pi \varepsilon_{1} \end{aligned} \end{matrix}\right.

Microfacet-BRDF hemisphere

For microfacet BRDFs, the sampling can be made proportional to the used normal distribution function D(h)D(h). The steps are similar for each normal distribution function. The PDFs may be calculated by noting that NDFs need to satisfy the following equation where θ\theta is the angle between the normal vector NN and the halfway vector HH.

ΩD(h)cosθdh=1\int _{\Omega }D(h)\cos\theta dh = 1

Blinn-Phong

D(h)=(α+2)cosθα2πD(h) = \frac{(\alpha+2)\cos\theta^{\alpha}}{2\pi}

{θ=cos1(ε01α+2)ϕ=2πε1\left\{\begin{matrix} \begin{aligned} \theta &= \cos^{-1}(\varepsilon_{0}^{\frac{1}{\alpha+2}}) \\ \phi &= 2\pi \varepsilon_{1} \end{aligned} \end{matrix}\right.

pdf(wi)=(α+2)cosθ(α+1)2πpdf(w_{i}) = \frac{(\alpha+2)\cos\theta^{(\alpha+1)}}{2\pi}

Derivation

pdf(ω)=D(h)cosθ=(α+2)cosθαcosθ2π=(α+2)cosθ(α+1)2πpdf(θ,ϕ)=pdf(ω)sinθ=(α+2)cosθ(α+1)sinθ2π\begin{aligned} pdf(\omega) &= D(h)\cos\theta = \frac{(\alpha+2)\cos\theta^{\alpha}\cos\theta}{2\pi} = \frac{(\alpha+2)\cos\theta^{(\alpha+1)}}{2\pi} \\ pdf(\theta,\phi) &= pdf(\omega ) \sin\theta = \frac{(\alpha+2)\cos\theta^{(\alpha+1)}\sin\theta}{2\pi} \end{aligned}

Next, the PDF is split up. We obtain the pdf for θ\theta by integrating over the whole domain of the ϕ\phi angle which is the same as multiplying by 2π2\pi since the pdf is isotropic. The pdf for ϕ\phi may be obtained by using conditional probabilities and the rule of Bayes.

pdf(θ)=02πpdf(θ,ϕ)dϕ=(α+2)cosθ(α+1)sinθpdf(ϕθ)=pdf(θ,ϕ)pdf(θ)=(α+2)cosθ(α+1)sinθ2π(α+2)cosθ(α+1)sinθ=12π\begin{aligned} pdf(\theta) &= \int_{0}^{2\pi}pdf(\theta, \phi) d\phi = (\alpha+2)\cos\theta^{(\alpha+1)}\sin\theta \\ pdf(\phi|\theta) &= \frac{pdf(\theta, \phi)}{pdf(\theta)} = \frac{\frac{(\alpha+2)\cos\theta^{(\alpha+1)}\sin\theta}{2\pi}}{(\alpha+2)\cos\theta^{(\alpha+1)}\sin\theta} = \frac{1}{2\pi} \end{aligned}

We can then integrate the PDF to get the CDF.

cdf(θ)=0θpdf(θ)dθ=0θ(α+2)cosθ(α+1)sinθdθ=1cosθ(α+2)cdf(ϕθ)=0ϕpdf(ϕθ)dθ=0ϕ12πdϕ=1ϕ2π\begin{aligned} cdf(\theta) &= \int_{0}^{\theta}pdf(\theta)d\theta = \int_{0}^{\theta}(\alpha+2)\cos\theta^{(\alpha+1)}\sin\theta d\theta = 1-\cos\theta^{(\alpha+2)} \\ cdf(\phi|\theta) &= \int_{0}^{\phi}pdf(\phi|\theta) d\theta = \int_{0}^{\phi}\frac{1}{2\pi} d\phi = \frac{1\phi}{2\pi} \end{aligned}

By setting these CDFs equal to a random number ε,ϵ[0,1]\varepsilon, \epsilon \left [ 0, 1 \right ], we can find an equation for the angle θ\theta and ϕ\phi that we can use for our sample.

cdf(θ)=1cosθ(α+2)θ=ε0θ=cos1((1ε0)1α+2)cdf(ϕθ)=1ϕ2π=ε1ϕ=2πε1\begin{alignat*}{2} cdf(\theta) &= 1-\cos\theta^{(\alpha+2)}\theta {}& =\varepsilon_{0} \rightarrow \theta &= \cos^{-1}((1-\varepsilon_{0})^{\frac{1}{\alpha+2}}) \\ cdf(\phi|\theta) &= \frac{1\phi}{2\pi} {}& =\varepsilon_{1} \rightarrow \phi &= 2\pi \varepsilon_{1} \end{alignat*}

Since ε\varepsilon is a number between 0 and 1, these equations can be written a bit cleaner as follows.

{θ=cos1(ε01α+2)ϕ=2πε1\left\{\begin{matrix} \begin{aligned} \theta &= \cos^{-1}(\varepsilon_{0}^{\frac{1}{\alpha+2}}) \\ \phi &= 2\pi \varepsilon_{1} \end{aligned} \end{matrix}\right.

Beckmann

D(h)=etanθ2α2α2cos4θD(h) = \frac{e^{\frac{-\tan\theta^{2}}{\alpha^{2}}}}{\alpha^{2}\cos^4\theta}

{θ=tan1α2ln(1ε0π)ϕ=2πε1\left\{\begin{matrix} \begin{aligned} \theta &= \tan^{-1}\sqrt{-\alpha^2\ln(1-\frac{\varepsilon_{0}}{\pi})} \\ \phi &= 2\pi \varepsilon_{1} \end{aligned} \end{matrix}\right.

pdf(wi)=etanθ2α2α2cos3θpdf(w_{i}) = \frac{e^{\frac{-\tan\theta^{2}}{\alpha^{2}}}}{\alpha^{2}\cos^3\theta}

Derivation

pdf(ω)=D(h)cosθ=etanθ2α2cosθα2cos4θ=etanθ2α2α2cos3θpdf(θ,ϕ)=pdf(ω)sinθ=etanθ2α2sinθα2cos3θ\begin{aligned} pdf(\omega) &= D(h)\cos\theta = \frac{e^{\frac{-\tan\theta^{2}}{\alpha^{2}}}\cos\theta}{\alpha^{2}\cos^4\theta} = \frac{e^{\frac{-\tan\theta^{2}}{\alpha^{2}}}}{\alpha^{2}\cos^3\theta} \\ pdf(\theta,\phi) &= pdf(\omega ) \sin\theta = \frac{e^{\frac{-\tan\theta^{2}}{\alpha^{2}}}\sin\theta}{\alpha^{2}\cos^3\theta} \end{aligned}

Next, the PDF is split up. We obtain the pdf for θ\theta by integrating over the whole domain of the ϕ\phi angle which is the same as multiplying by 2π2\pi since the pdf is isotropic. The pdf for ϕ\phi may be obtained by using conditional probabilities and the rule of Bayes.

pdf(θ)=02πpdf(θ,ϕ)dϕ=2πetanθ2α2sinθα2cos3θpdf(ϕθ)=pdf(θ,ϕ)pdf(θ)=etanθ2α2sinθα2cos3θ2πetanθ2α2sinθα2cos3θ=12π\begin{aligned} pdf(\theta) &= \int_{0}^{2\pi}pdf(\theta, \phi) d\phi = \frac{2\pi e^{\frac{-\tan\theta^{2}}{\alpha^{2}}}\sin\theta}{\alpha^{2}\cos^3\theta} \\ pdf(\phi|\theta) &= \frac{pdf(\theta, \phi)}{pdf(\theta)} = \frac{\frac{e^{\frac{-\tan\theta^{2}}{\alpha^{2}}}\sin\theta}{\alpha^{2}\cos^3\theta}}{\frac{2\pi e^{\frac{-\tan\theta^{2}}{\alpha^{2}}}\sin\theta}{\alpha^{2}\cos^3\theta}} = \frac{1}{2\pi} \end{aligned}

We can then integrate the PDF to get the CDF.

cdf(θ)=0θpdf(θ)dθ=0θ2πetanθ2α2sinθα2cos3θdθ=π(1etanθ2α2)cdf(ϕθ)=0ϕpdf(ϕθ)dθ=0ϕ12πdϕ=1ϕ2π\begin{aligned} cdf(\theta) &= \int_{0}^{\theta}pdf(\theta)d\theta = \int_{0}^{\theta}\frac{2\pi e^{\frac{-\tan\theta^{2}}{\alpha^{2}}}\sin\theta}{\alpha^{2}\cos^3\theta} d\theta = \pi(1-e^{\frac{-\tan\theta^{2}}{\alpha^{2}}})\\ cdf(\phi|\theta) &= \int_{0}^{\phi}pdf(\phi|\theta) d\theta = \int_{0}^{\phi}\frac{1}{2\pi} d\phi = \frac{1\phi}{2\pi} \end{aligned}

By setting these CDFs equal to a random number ε,ϵ[0,1]\varepsilon, \epsilon \left [ 0, 1 \right ], we can find an equation for the angle θ\theta and ϕ\phi that we can use for our sample.

cdf(θ)=π(1etanθ2α2)=ε0θ=tan1α2ln(1ε0π)cdf(ϕθ)=1ϕ2π=ε1ϕ=2πε1\begin{alignat*}{2} cdf(\theta) &= \pi(1-e^{\frac{-\tan\theta^{2}}{\alpha^{2}}}) {}& =\varepsilon_{0} \rightarrow \theta &= \tan^{-1}\sqrt{-\alpha^2\ln(1-\frac{\varepsilon_{0}}{\pi})} \\ cdf(\phi|\theta) &= \frac{1\phi}{2\pi} {}& =\varepsilon_{1} \rightarrow \phi &= 2\pi \varepsilon_{1} \end{alignat*}

This gives us the following samples.

{θ=tan1α2ln(1ε0π)ϕ=2πε1\left\{\begin{matrix} \begin{aligned} \theta &= \tan^{-1}\sqrt{-\alpha^2\ln(1-\frac{\varepsilon_{0}}{\pi})} \\ \phi &= 2\pi \varepsilon_{1} \end{aligned} \end{matrix}\right.

Additional resources

http://www.igorsklyar.com/system/documents/papers/4/fiscourse.comp.pdf

https://agraphicsguynotes.com/posts/sample_microfacet_brdf/

https://www.reedbeta.com/blog/hows-the-ndf-really-defined/

https://www.graphics.cornell.edu/~bjw/wardnotes.pdf

https://cseweb.ucsd.edu/~ravir/6998/papers/p265-ward.pdf

https://schuttejoe.github.io/post/ggximportancesamplingpart1/