Cosine Weighted Hemisphere

The probability of each sample is weighted by its cosine with the normal.

Here is the result:

function cos_hemisphere(){

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

    return [theta,phi];
}

Algorithm

Generate points weighted by the cosine hemisphere.

  1. Choose uniform numbers u1,u2u_1, u_2 in [0,1][0,1]
  2. Calculate θ=cos1(1u1)\theta = \cos^{-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

p(ω)=1πcosθp(\omega) = \frac{1}{\pi}\cos\theta

p(θ,ϕ)=1πcosθsinθp(\theta,\phi) = \frac{1}{\pi}\cos\theta\sin\theta

Derivation

This is a special case of the power cosine weighted hemisphere cap with θmax=π2,n=1\theta_{max} = \frac{\pi}{2}, n = 1