Fixing circle of confusion

Project Page Next project post Post History

For some time now, I've had an incorrect calculation of the circle of confusion. The circle of confusion (or CoC for short - yes, that's the acronym...) is an optical spot caused by a cone of light rays from a lens not coming to a perfect focus when imaging a point source.

In ray tracing, we use the term CoC and the calculation of it, to determine the focus of the image in the areas before and after the focus plane. Don't know if I've said that right, but we'll go with it for now.

We can visualise what the CoC might look like, by colouring the pixels in the image based on their distance away from the sensor pixel, and whether they're in the foreground or background. For parts of the image in focus, I use black. Red colours I use for the foreground and white I use as the background. I then blend between the three sets depending on whether the area is in focus, out of focus, in the foreground or background. Visually, an example might look like this:

But, in the image above, the colours are not right. If you look closely, the red areas show a kind of circular curve to it. This happens because, in this case, the depth test is like a circle emanating out from the centre of the camera. And the colours are based off the values of the ray hit distance from the camera centre. But this is in fact, not quite correct. The CoC is not calculated like this. We have to calculate it based on where the focus plane is, which is like taking a parallel plane and sitting it at the focus distance. Visually, it might look something like this:

The focus plane shown with a green rectangle

To achieve this, we have to do a bit of trigonometry with our hit depth distance, to change our depth value into a CoC value. After adding in the necessary trig calculations, we end up with an image like this:

In this image, the black regions are in focus. Yes, the dragon's head is slightly red in the nose area, this means that if I was to render the image with depth of field, the dragon's nose would be slightly out of focus. This is because the focus plane is a null object sitting inside the dragon's head. The background goes to white, and the foreground goes to red.

Only a small fix this one, but it could be useful further down the track, so it's worth it. We could for example, use this data for adding post depth of field effects in the compositing stages, like blurring areas of the image that are out of focus in both the foreground and the background, based on the CoC value.

I'll leave this one with a side-by-side comparison of the issue and the fix: