Creating Depth of Field

Even though we’re creating digital renders in a 3d world, we need to keep in mind that we are still imitating a real world camera lens. Cameras and human eyes have something called ‘Depth of Field‘ (or ‘DoF’), which makes objects in the background and foreground appear blurry.

When used correctly in your artwork, it can draw the attention to the subject and make the scene more aesthetically pleasing. When used wrong, it can seem like another overused tiresome effect. Learn the difference and how to use it effectively.

Exploding Lightbulbs

To play along at home, download this light bulb scene.

wire-scene

The final .blend will be provided at the end of the tutorial.

Let’s get cracking!

Step 1

Open the file and select the camera and press the ‘Show Limits’ button in the edit panel. You will notice that a yellow cross hair appears at the base of your camera.

camera-limits1

This is the point that blender will focus on. You can change the position of the cross hair two ways: by changing the DoF Distance value. Or by typing in the name of an object for the camera to focus on. The latter is very useful for animations where you want to change what the camera focuses on in the middle of the animation. I used this technique extensively in my film ‘The Ballad of the M4 Carbine‘, where I made the camera focus on a moving empty that changed moved mid animation.

However because this is only a still we can use the first method.

Step 2

So by clicking the next arrow, make the yellow cross hair sit in line with the exploding light bulb.

camera-dof-dist2

With the camera now setup to render, we can move onto the compositor.

Step 3

Switch to the Node Editor by selecting it from the Window Type popup list.
node-editor

Step 4

Select the compositor icon and the Use Nodes button.

node-editor1

Step 5

Go ahead and disconnect the ‘Render Layer’ from the Composition node.

disconnect-nodes

Step 6

Hit the space bar and add a Defocus Node (Add>Filter>Defocus) and connect to the render layer and composite layer as shown in red here.

defocus1

Step 7

We want the node to use the data from the cameras Z-depth, so connect the Z input to the render layers Z Output.

defocus-zdepth

Step 8

Go ahead and turn off the No zbuffer and Preview buttons. They are rarely used and seem to exist only so we can turn them off.

no-preview-zbuffer

Now onto the important part: the fStop value.

fstop-point

Without getting too technical, the fStop value defines how much on either side of the yellow cross hair to make in focus. The smaller you make the fStop value the less of your scene will be in focus. Many artists get carried away by how cool DoF looks, and make this value unnecessarily small. This in turn makes everything appear miniature.

Much like this cheerio.

Like this guy.

For an example of how DoF can affect your perception of size, watch this video. It was created using a clever photographers effect called ’tilt shift’ which fakes shallow depth of field.

Step 9

With your understanding of fStop, you should know that light bulbs are fairly small and as a result will need a smaller fStop value. So go ahead and set it to 3.5.

fstop

Step 10

Another interesting aspect of DoF that you have control over is bokeh, which defines the shape of the out of focus lights in your scene. Typically old cameras suffered from jagged bokeh but as technology improved the bokeh took on a more circular shape.

reference-bokeh

So smooth.

However I have found that using a polygonal shape like a hexagon can give a more unique effect. So go ahead and change the bokeh type to Hexagonal and Rotate to 90

hexagon-901

The 90 degree rotation simply turns the hexagon on it’s side to make it more obvious.

Step 11

do_composite

Press Do Composite in the render panel (F10), and hit render!

If you’re a beginner you can stop here as you probably won’t need the next step for your scene, but feel free to read on if you want to get into the tricky stuff.

Using DoF with Multiple Render Layers

You may notice that in the final product, the light filaments of the light bulbs are glowing. To achieve this effect I had to move the filaments to the second layer and make it into a separate render layer.

glow-layer

Glowing node setup

However, when you do this a problem arises: the Z-Depth for the DoF was calculated on the first layer, not the second. Sure, it’d be great if we could just jam two outputs from different render layers into the one hole, but sadly this is not the case.

if-only

It's photoshopped. Please don't try this at home.

The ugly truth is: each render layer needs a separate defocus node. Always. However rather than adding it the traditional way and having to adjust the settings on each node to match the other, I have a far more painless way.

Select the original defocus node and hit Ctrl-G.

defocus_group

defocus_group2

This will make the single node into a group. Calling it a group is confusing, but if you duplicate it (Shift-D) you will notice that a number appears next to it’s name.

defocus_group_duplicated

This is the number of users associated with it. Meaning you can make several copies of it and everything in it will mirror the settings of the groups.

Duplicate the group for each render layer in your scene, and compile the layers as you normally would.

complete_node_setup

Complete node setup

Voila!

download_finished_blend

Thanks for reading! I hope you now have a better understanding of this often overlooked feature in Blender. If there’s something I’ve missed or anything you’d like me to go into more detail, please let me know in the comment section below!

Tags: ,

About Andrew Price

User of Blender for 9+ years. I've written tutorials for 3d World Magazine and spoken at three Blender conferences. My goal is to help artists get employed in the industry by making training accessible and easy to understand. I'm an Aussie and I live in South Korea ;)
  • Shazz

    Something’s wrong. I just downloaded and tried the finished file after trying a kajillion settings on my own. Your finished example doesn’t render any depth of field blurring either. I think someting got broken in blender since you made this tute. Thanks anyway, though.

  • http://www.facebook.com/ArvindaRaja Arvinda

    Hi Andrew :D is it possible to create Dof in Blender from the Camera Properties like the vray camera..Is there any Python Script or addon that could make it possible in Blender now?

    • http://www.facebook.com/ArvindaRaja Arvinda

      Just found the answer Blender Cycles :D hehe Thank’s this site is very Helpfull

  • 1456

    the artical is stupid thats all i have to say

    • caca

      cool^^

  • http://chocostyle4@gmail.com Brett Lee

    toronto taxi
    Thank you for sharing this information this post is very useful.

  • http://www.artoftheproblem.net brit

    hey can i use this model in a video and credit you?

    • http://www.blenderguru.com Andrew Price

      Yep, that’s fine :)

  • Felicks

    great tutorial, everything was fine in Blender 2.4
    But now, where can I find the “do composite” button in blender 2.5???

  • John

    HI, when i tried to open your starter image, the page turned out to be a bunch of random symbols, is there another location for your starter image?

  • Neil

    Where is the defocus node in 2.56? i tried searching for it by hitting space bar but there was nothing. Nothing in the node menu either. Any help?

    • http://blenderguru.com Andrew Price

      @Neil
      Hit Shift+A and go to Filter>Defocus.

      Hope this helps :)

  • http://miked.byethost13.com/ Michael Dedek

    Thanks for the tutorial! Nodes in Blender are awesome and powerful. Here’s my work with DoF http://i.imgur.com/Lzgdm.jpg

  • afro

    i am a beginner,,,so lead me everyone,,,,

    • caca

      NO

  • Aidan Vernée

    Can you create this tutorial also in Blender 2.5? Beye the way: Awesome tutorial!

    Regards Aidan Vernée

  • RC

    How long should it take to render at a reasonable resolution? RC

    • caca

      about 4000-6000 years :o

  • terrachild

    Andrew, the video you mentioned as an example of the photographer’s trick called “tilt shift” was not shot with “tilt shift.” It was all faked in post with a filter.

  • Pingback: RayBlender » Blog Archive » Glass mesh

  • http://www.youtube.com/onjofilms FloridaJo

    Great Tut. Yes, Use Z-Buffers must be checked.
    Thanks Andrew

  • Steffo

    Hi Jaskegreen,

    Look at Step 6. In the Defocus Node you will find Use Zbuffer. In 2.49 (the one I think Andrew used in this example) it’s called No Zbuffer and should be turned off. In 2.5 it’s called Use Zbuffer and should be turned on. Small difference, big deal. I hope you get some help with my explanation.

  • jaskegreen

    Steffo,
    Where is the Turn On ZBuffer button located. Fairly new to the 2.5 version and can’t find it.

    Thanks

  • Pingback: Why Do Light Bulbs Burn Out Just As They’re Turned On? | Gizmodo Australia

  • Mark

    As an extra tip, you may want to enable “Save buffers” and “FullSample” in the Render Output tab. Without it, you may get weird artifacts along the edges due to the fact that the z-buffer pass is not anti-aliased.

  • http://www.muxmedialab.no KrisBerg

    Thanks alot for sharing, mate! Works great i Blender 2.53.

  • http://therealnoz.blogspot.com therealnoz

    Yes, I did set the focal point to the object that I wanted to be in focus. I’m sorry if I’m not being much help here.

  • Steffo

    Sorry Andrew,

    But I figured out what’s wrong. In 2.49 you have to click to turn off the No zbuffer, in 2.5 you have to turn on Use Zbuffer to make it work. Fools me. Gahh!

  • http://Didatest Steffo

    The tutorial works fine in 2.49 but not in 2.5 a2. Not for me…

  • http://Allisoutoffocus Steffo

    Hi Andrew,

    Thanks for the great tutorials. I use 2.5 but like therealnoz I have problems with DoF. Everything is out of focus. I have done this tutorial three times with the same result. Hmmm…

  • http://blenderguru.com Andrew Price

    @therealnoz
    Did you set the DOF focal point for the active camera? That could be the problem.

  • http://therealnoz.blogspot.com therealnoz

    I’m sorry if this seems like a very obvious problem. But for some reason, no matter what I do, everything is out of focus. I have gone through the tutorial 5 times.

  • http://www.gemusoft.de Draufwalker

    @Morten: I am not such an expert.. but maybe take the glass-stuff into another render layer….?
    so at first the blur of the background is rendered and then the glass is being rendered.. but I don’t know if that works ,)

    -thanks for that tutorial! I used it to render about 4000 cookies :) :D :D ;) :) (now they look really impressive :)

  • Morten

    Discovered this little problem:If things are behind a glass, everything you see throug the glass is rendered with the same depht blur as the glass. So if your glass is in focus it looks as everything behind it is in focus, and if youre focusing on something through a glass it will be blury if the glass is out of focus.
    Any idea how to fix, anyone?

    • 1456

      no

  • todd McIntosh

    [pre]
    iiiiiiiiiiiiiiiiiiii|
    wwwwwwwwwwwwwwwwwwww|
    [/pre]

  • todd McIntosh

    [sourcecode language="css"]
    iiiiiiiiiiiiiiiiiiii|
    wwwwwwwwwwwwwwwwwwww|
    [/sourcecode]