# How is Depth of Field Implemented?

I know that depth of field involves blurring.

There's some great information about how to do a Gaussian blur in the question How is Gaussian Blur Implemented?

But, other than that, how is depth of field implemented?

What are the rules about how you blur each pixel, and how do you handle the case of when there is a pixel that wants to blur a lot next to a pixel that doesn't want to blur as much?

I've also heard of the "circle of confusion" but have no idea what that is.

Can anyone explain DOF in a plain, easy to understand way?

## 1 Answer

The depth of field is a characteristic of a camera lens setting, although the name "Depth of Field" is commonly used to describe the effect caused by such characteristic.

Camera lenses can only perfectly focus on one single point, but there is a distance for which the image will still look reasonably sharp. Such distance is what actually the Depth Of Field is. This distance is variable depending on various factors, but let's say for the sake of brevity that the easiest way to adjust it is by changing your camera aperture.

As for the circle of confusion it is helpful to look a pic from wikipedia:

In all of the three cases a single point is "projected" onto our image plane, but as you can see that is at different distances from the lens.

Basically because, as I said, not everything is perfectly in focus (that is the focal point is on the image plane), a point can be projected to our image plane not to a single point but to an area. This, as it is clear from the pic, is because the focal point is either before or after the image plane. This "circle" that is created on the image plane is what is called the circle of confusion.

Now from this should be clear that varying the depth of field you vary the CoC size, hence it can be used as a measure of the DoF itself and an intuitive one when thinking in terms of blurring.

As per how it is implemented in rendering there are really lots of methods out there. Usually in games is done as a post process using the depth information to split your scene in different "planes"; for example a focus plane that does not need to be blurred, a near plane and a far/background plane that need to be blurred. Once you blurred your planes you can composite them back together to get your final image. How much you blur the various planes it is dependent on the effect you want to achieve. Advanced system usually implement this and other "lenses" effect using appropriate parameters coming from the photography world like the aperture.

Note that there are many ways to implement the effect, the one I described is just one way that also has variations (for example performing the whole thing at lower res). The limit is dependent on what your target is and how much you budgeted for this effect. You can go from adopting the "3 planes" method at extremely low res to computing the circle of confusion for each pixel and applying the consequent ad-hoc blur to that pixel.

EDIT: The comment from @NathanReed needs to be more in evidence as part of the answer:

Matt Pettineo's blog posts How To Fake Bokeh and Bokeh II: The Sequel are great introductions to how to practically implement post-process DoF and address the typical artifacts you get from it.

• A cheap way to do this is simply use Gaussian blur on your final scene and store two of them. One for foreground and one for background. They can have the same blur level or different and it is up to you. Upon finishing the blur, simply lerp from depth 0 to some determined (and tweak-able) depth (say 0.10). What you are learning is this blurred scene to the perfectly in-focused scene. For the background blur you can simply do the same except you are blurring from perfect in-focus to the blurred image. Leave some room for the middle to create a perfectly in focus region. Jul 5, 2017 at 3:55