## Introduction

I’ve recently been diving into the world of importance sampling and I decided to share my derivation for importance sampling the cosine lobe.

## Shade

When we’re shading a point in path tracing, we typically shoot a ray from our surface in a uniformity random direction contained on a hemisphere centered about our normal. This has the downside of introducing quite a bit of variance into our renders.

Imagine that we have a very bright light that only occupies a very small projected area from our shaded point.

We would be very likely to miss this light with most of our samples and our render could turn out much darker than we would expect.

This is where importance sampling comes in.

If you imagine that your illumination is a polar function

If we were to sample a random variable with a distribution that matches this function, we would be much more likely to hit the important points of our function. (Hence importance sampling)

I won’t dive deeply into this topic, as there are a variety of excellent resources detailing this topic. [1]

The essence of it however, is that you want to find a Probability Density Function (PDF) that matches the shape of your illumination function. Once you’ve define this PDF, you can sample it using the Cumulative Density Function (CDF).

## Derivation

Since our cosine lobe illumination will look like this:

We will use this as the basis to derive our distribution since we’re most likely to get the most light from directions arriving parallel to our normal.

Thankfully, our cosine lobe has an analytical formula that we can use as our PDF.

(1)

Our PDF must integrate to 1, we integrate the PDF across our hemisphere

Plug in (1)

(2)

Plug in (2)

(3)

Since our PDF has to integrate to 1,

Plug in (3),

(4)

Finally, plug in (4) into our PDF,

(5)

Now that we have our PDF, we can calculate our PDF in terms of and .

(6)

Now we integrate with respect to to get

And then to get ,

Now we want to calculate the CDF of each function,

Now we want to invert our CDF to sample it using our random variable ,

(7)

For ,

(8)

Now we have our CDFs and our PDFs, we can finally calculate our direction.

In pseudo code you can simply do:

With these directions, you can now sample your scene:

Plug in (5)

## Conclusion

That’s it! These formulas will sample the hemisphere where it is receiving more light defined by the cosine lobe. The results are pretty awesome.

## 1 Comment