Caustics 2D
January 15, 2021
This is a 2D shader that uses a Monte Carlo method to render the scene. For every pixel, multiple samples are taken to determine the colour of the pixel. Each sample will have a random direction ray. We will then only check if the ray hits a light source, or misses a light source. If a light source is hit, then that lights colour will contribute to the pixel sample. However, if the ray hits an object, then we will reflect the ray, and march the reflected ray through the scene. The number of times that we reflect the ray when an object is hit, is called the number of bounces.
Having explained the basic principle behind the rendering technique, the algorithm that follows from it is quite straightforward:
- For each pixel position $O$.
- Generate a random normalized direction vector $D$. This can be done by selecting a random variable $\underline{x} \sim \textrm{uniform}(0, 1)$, which is then used to create the vector $D = [\cos(2\pi\cdot\underline{x}), \sin(2\pi\cdot\underline{x})]$. Because this point is on the unit circle, it is already normalized.
- Shoot the ray $O + t\cdot D$ into the scene, and determine if an object is hit.
- If the object that is hit is a light source, add the contribution of the light source to the pixel result, and go to the next sample.
- If the object that is hit is not a light source, calculate the reflection ray, and use this ray in step 3. If the number of bounces exceeds a certain threshold, then the result of the pixel is zero.
This implementation uses two reflection ray bounces to determine if a light source is hit. If we never hit a light source we will return an ambient colour, instead of black.