Depth of Field

Depth of Field (DoF) is an interesting effect in real world photography which adds a lot to CG generated images. It is also known as Focal Blur.

The phenomenon is linked to the fact that a real world camera can focus on a subject at a given distance, so objects closer to the camera and objects further away will be out of the focal plane, and will therefore be slightly blurred in the resulting photograph.

The amount of blurring of the nearest and furthest objects varies a lot with the focal length and aperture size of the lens and, if skilfully used, can give very pleasing effects.

Blender's renderer does not provide an automatic mechanism for obtaining DoF, but there are two alternative way to achieve it. One relies solely on Blender's internals, and will be described here. The other requires an external sequence plugin and will be outlined in the Sequence Editor Chapter.

The hack to obtain DoF in Blender relies on skilful use of the Motion Blur effect described before, making the Camera move circularly around what would be the aperture of the 'real world camera' lens, constantly pointing at a point where 'perfect' focus is desired.

Assume that you have a scene of aligned spheres, as shown on the the left of Figure 24. A standard Blender rendering will result in the image on the right of Figure 24, with all spheres perfectly sharp and in focus.

Figure 24. Depth of Field test scene.

The first step is to place an Empty (SPACE>>ADD>>Empty) where the focus will be. In our case at the center of the middle sphere (Figure 25).

Figure 25. Setting the Focus Empty.

Then, assuming that your Camera is already placed in the correct position, place the cursor on the Camera (Select the Camera, SHIFT+S>>Curs->Sel) and add a NURBS circle (SPACE>>ADD>>Curve>>NURBS Circle).

Out of EditMode (TAB) scale the circle. This is very arbitrary, and you might want to re-scale it later on to achieve better results. Basically, the circle size is linked to the physical aperture size, or diaphragm, of your 'real world camera'. The larger the circle the narrower the region with perfect focus will be, and the near and far objects will be more blurred. The DoF blurring will be less evident the smaller the circle.

Now, keeping the circle selected, also select the Empty and press CTRL+T to have the circle track the Empty as in Figure 26. Since the normal to the plane containing the circle is the local z-axis, you will have to set up tracking correctly so that the local z-axis of the circle points to the Empty (Figure 27) and the circle is orthogonal to the line connecting its center to the Empty.

Figure 26. NURBS circle tracking the focus Empty.

Figure 27. Correct tracking settings for the circle.

Select the Camera and then the circle and parent the Camera to the circle (CTRL+P). In the Animation Buttons Window (F7) press the CurvePath button.

With the circle still selected, open an IPO window (SHIFT+F6) select the Curve IPO with the button. The only available IPO is 'Speed'. CTRL+LMB twice at random in the IPO window to add an IPO with two random points. Then set these points numerically by using the new set of buttons which have appeared in the Animation Buttons Window. You should set Xmin and Ymin to 0, Xmax and Ymax to 1, then press the SET button. To complete the IPO editing make it cyclic via the button. The final result should be as shown in Figure 28.

Figure 28. Speed IPO for the NURBS circle path.

With these settings we have effectively made the Camera circle around its former position along the NURBS circle path in exactly 1 frame. This makes the Motion Blur option take slightly different views of the scene and create the Focal Blur effect in the end.

There is still one more setting to perform. First select the Camera and then the focal Empty, and make the Camera track the Empty (CTRL+T). The Camera will most probably go crazy because it might already have a rotation and it is parented to a circle too, so press ALT+R and select Clear rotation from the menu which appears to clear all Camera rotations except the tracking.

The Camera should now track the Empty, as in Figure 29.

Figure 29. Camera tracking the focal Empty.

If you press ALT+A now you won't see any movement because the Camera does exactly one full circle path in each frame, so it appears to be still, nevertheless the Motion Blur engine will detect these moves.

The last touch is then to go to the rendering buttons window (F10) and select the MBLUR button. You most probably don't need the OSA button active, since Motion Blur will implicitly do some antialiasing. It is strongly recommended that you set the Motion Blur factor to 1, since this way you will span the entire frame for blurring, taking the whole circle length. It is also necessary to set the oversamples to the maximum level (16) for best results (Figure 30).

Figure 30. Motion blur settings.

A rendering (F12) will yield the desired result. This can be much slower than a non-DoF rendering since Blender effectively renders 16 images and then merges them. Figure 31 shows the result, to be compared with the one in Figure 24. It must be noted that the circle has been scaled much less to obtain this picture than has been shown in the example screenshots. These latter were made with a large radius (equal to 0.5 Blender units) to demonstrate the technique better. On the other hand, Figure 31 has a circle whose radius is 0.06 Blender units.

Figure 31. Motion blur final rendering.

This technique is interesting and with it it's pretty easy to obtain small degrees of Depth of Field. For big Focal Blurs it is limited by the fact that it is not possible to have more than 16 oversamples.