Build your own 3D renderer - Distributed ray tracing and anti-aliasing

The images produced by our ray tracer so far have jagged edges around the geometry in the scene. This is because we have only cast one ray per pixel. That ray either sees some geometry, or it doesn't, leading to sharp transitions. This is known as aliasing, because, due to the imprecision of sampling once per pixel, two different scenes may produce the same output image.

A shape is covered only partially by certain pixels, but depending on where the sample is taken, the pixel either sees the shape or doesn't. There is no in-between.

Sampling once per pixel results in aliasing, characterized by jagged edges.

The solution is to cast multiple rays per pixel—each at a different point inside the pixel—then average the results together for that pixel. This approach is known as distributed ray tracing, where multiple rays are cast and the results averaged. While the locations sampled can be regularly spaced inside the pixel, we can also cast rays to randomized locations within the pixel, an approach known as stochastic ray tracing.

In the first image, all four samples intersect with geometry, so the pixel is heavily shaded. In the next two images, two of the four samples intersect with geometry, so the pixel is lightly shaded. In the last image, no sample intersects with geometry, so the pixel is not shaded at all.

Sampling multiple times per pixel allows smoother transitions between visible and not visible.

When used to avoid aliasing, the technique is called supersampling antialiasing, or SSAA. Often, the number of samples is used in the name, such as 4× SSAA.

The increased quality comes at a cost. The number of rays cast in total is multiplied by the number of samples per pixel, resulting in doing that many times more work for a single image.

A related technique is multisampling antialiasing or MSAA, where multiple samples are only taken at the edges of the geometry. This alleviates the performance hit of supersampling antialiasing.

The general principle of distributed ray tracing can be applied to simulate many photorealistic effects:

On the left, a point is not in shadow because it can see the entire light source. In the middle, a point is partially in shadow because it can see part of the light source. Or the right, a point is completely in shadow because it can't see any part of the light source.

If a light source is modelled as an interval, then a point can be partially in shadow (penumbra) or completely in shadow (umbra). This is one application of distributed ray tracing, used here to achieve soft shadows.