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

I like long walks on the beach and yelling out during movies. My cat's name is dog, and my dog's name is cat. I am hilarious. I like Blender.

121 Responses to “Creating Depth of Field”

  1. Charlie D. November 10, 2009 at 3:26 pm #

    This was a great tutorial. However, the particle system seems to crash every time I try to simulate it. Is there anything I’m missing? I tried adjusting the start and end frames and baking it first but it still crashes. I wish I knew more about the particle system besides fur.

  2. Charlie D. November 10, 2009 at 9:25 pm #

    I think I figured out the problem. It seems to work find on my laptop which is running Mac OS X 10.4

    The computer I was trying to run it off of has 10.5 and I’ve noticed the build for Blender is still a little buggy for that version of Mac OS X

  3. Andrew Price November 10, 2009 at 10:41 pm #

    Good to hear you got it working Charlie! If you run into anymore problems let me know.

  4. yondertoy November 11, 2009 at 3:08 am #

    Hi,
    Very nice tutorial, many thanks !
    But, did you packed textures images files because it doesn’t appear in the render ? ( ‘PlanksOld0019_L.jpg’ & ‘test.jpg’ )
    THX !

  5. Mike November 11, 2009 at 8:05 pm #

    Haha I want that multi-input node to exist too!

    Thanks for sharing, helped me a lot.

  6. Andrew November 12, 2009 at 5:38 pm #

    Hey yondertoy,
    I wasn’t sure if people would want the textures I used, so I didn’t include them.

    But obviously people do so I’ve re-uploaded it with the packed textures ;)

    Regards,
    Andrew

  7. draeath November 13, 2009 at 3:23 am #

    Now, couldn’t one use an Add node to merge the render layers BEFORE using the defocus node?

  8. Andrew November 13, 2009 at 3:48 am #

    @draeath
    Tried that. It merges them fine, but then it’s using the Z-Depth from one layer only, so the DoF for the other layer is maxed out.

    I experimented for ages and this was the best solution I found. Also I think I remember pulling apart a source file from Big Buck Bunny and they had used the same method, so I stuck with it.

  9. Husam November 13, 2009 at 4:59 am #

    I think you could combine the Z-Depth passes using a Z Combine node. No?!

  10. Regius November 13, 2009 at 5:39 am #

    Woohaa! A node tutorial ! I would have done this DOF thing just passing the task to LuxRender, lazy me! Lets dig a little into node editor!

  11. ROUBAL November 13, 2009 at 5:44 am #

    Nice tutorial Andrew, as always !

    Thank you !

  12. zika November 13, 2009 at 7:50 am #

    awsome tutorial, thanks for sharing! I have additional questionis there any tutorial how you create the shattered glass … it looks fantastic!!!

    Thank you!

  13. Craigsnedeker November 13, 2009 at 8:05 am #

    Great tutorial!

  14. pkblender November 13, 2009 at 9:36 am #

    Very clear, well explained.

    Thanks!

  15. Benjamin Bailey (Banor) November 13, 2009 at 10:23 am #

    Excellent job :)

    • Monick September 22, 2011 at 3:07 pm #

      Loock here : ,

  16. yondertoy November 13, 2009 at 11:53 am #

    Thanks a lot ! ;o)

  17. Lucas November 13, 2009 at 2:24 pm #

    Really nice tutorial :) . Thanks for good work.

  18. Alexander Blank November 13, 2009 at 2:29 pm #

    Very concise tutorial… :-)

  19. John Robbins November 13, 2009 at 2:44 pm #

    This is a great tutorial. I knew how to do the defocus, but was not familiar with the more complex method for multiple render layers. The final product looks great.

    What I am curious about is whether there is a way to create a depth field map (luminance-based) that I can render alongside my video or still to load into a program like After Effects. I’m just really digging into 3D and I’m far more familiar with compositing programs. Any help that anyone may have would be greatly appreciated.

    -J

  20. Marcus November 13, 2009 at 3:04 pm #

    @John Robbins: You mean something like a Z depth channel that says After Effects with different grayscale values how far different things were away from the camera?

    @Andrew: Is the bulb shattered with explode modifier or manually? Is there a quick way to break meshes up in a random, natural way?

  21. John Robbins November 13, 2009 at 4:36 pm #

    @Marcus: Yes, that’s exactly it. Then I could load it into the lens blur effect in after effects and edit any look I want in real time.

    Any idea on how to render a Z depth channel?

  22. Richard November 13, 2009 at 7:52 pm #

    Thank you for this. Quick, easy-to-follow, special thanks for the .blend files so I could play along at home.

  23. Jonathan November 13, 2009 at 11:58 pm #

    THANKS!!!!!!! I was wondering how to do that.

  24. Marcus November 14, 2009 at 12:13 pm #

    Depth Map Rendering:

    http://blenderartists.org/forum/showthread.php?t=80020&highlight=depth

  25. Marcus November 14, 2009 at 12:15 pm #

    Can you make a tutorial about how the particle system is set up in this scene?

  26. John Robbins November 14, 2009 at 12:45 pm #

    @ Marcus: Oh sweet. That is awesome. You win the internet.
    -J

  27. NeutrinoX November 17, 2009 at 7:33 am #

    I have the same crash issue with OSX 10.6

  28. hurm November 23, 2009 at 12:27 am #

    Very nice tutorial., thanks, Very clear and usefull

    now, one question: How i do to animate the focus on the scene?

    thanks

  29. Andrew Price November 24, 2009 at 8:26 am #

    To animate the focus simple add an Empty and set the “DoF Obj” value to ‘Empty’ (or whatever it’s name is). Then simple animate the Empty as you normally would.
    Eg.
    Hit i> select Loc> change frame>move empty to desired position> Hit i again> select Loc (repeat as necessary)

  30. hurm November 24, 2009 at 6:23 pm #

    Thank you very much, Andrew.

    Very clear.

  31. Uakino November 25, 2009 at 5:17 pm #

    Вот что-то наподобие у меня уже полгода из головы не выходит!

  32. Adam December 12, 2009 at 11:23 am #

    Quote:
    >To animate the focus simple add an Empty and set
    >the “DoF Obj” value to ‘Empty’ (or whatever it’s name is).
    >Then simple animate the Empty as you normally would.
    >Eg.
    >Hit i> select Loc> change frame>move empty to desired
    >position> Hit i again> select Loc (repeat as necessary)

    Thank you!!!!! Great tutorial; i’ve been looking all over for this!

  33. Hiram December 12, 2009 at 11:31 am #

    Adam, if you want animate the focus, beware with the fstop value. because a big value can make focus troubles. this make me headache lol

    cheers

  34. devchushka December 23, 2009 at 10:20 am #

    Да уж. Как говорится в устоявшемся выражении:
    Это мы разрешили немецкие фильмы и запретили грузинские вина…

  35. sdort December 31, 2009 at 9:26 pm #

    How did you manage to shatter you light bulb??
    I got the .blend file, and saw there wasn’t a explode modifier..

    Can you tut it?

  36. Deriksims January 4, 2010 at 8:08 am #

    Да уж. По поводу коментариев – навеяла на меня где-то услышанная фраза:
    Ведь именно мы занимаем бОльшую часть Евразии, чем Евразия не очень довольна.

  37. Marc Jacobs March 13, 2010 at 3:08 pm #

    Dear Andy,

    The camera isn’t even creating a D.O.F. or blurring at all. what could i possibly be doing wrong? it’s still rendering, but everything is unfortunately, crystal clear and sharp. :/

    Thanks,
    Marc

  38. hiram March 13, 2010 at 3:16 pm #

    did you check the ‘do composite’ button?

  39. Marc Jacobs March 13, 2010 at 3:29 pm #

    unfortunatley, yes :(

  40. hiram March 13, 2010 at 3:36 pm #

    ok, next steps:

    In the camera editing options (press f9), check the name of the empty for the focus, (Dof Ob:)

    OR

    In composite Node editor, maybe the defocus node needs a bigger value on FStop property

  41. Marc Jacobs March 13, 2010 at 3:42 pm #

    that’s all good according to blender :/

  42. hiram March 13, 2010 at 3:47 pm #

    take it easy, must be something very simple…

  43. Tom April 1, 2010 at 3:47 pm #

    Awesome tutorial! Thanks! I’ve been wondering about DoF in blender since I started a few months ago.Time to experiment!

  44. Araujo, Ricardo April 13, 2010 at 9:30 am #

    GreeeatFull!!!!

  45. Novakon April 22, 2010 at 12:29 am #

    fresh n funny… i wonder if there’s any tutorial of this quality in China.

  46. dyllon April 29, 2010 at 5:31 pm #

    superb now im gonna play with it and add loads more defects!

  47. AndriV May 5, 2010 at 10:29 pm #

    great tutorial…

  48. seamus May 6, 2010 at 7:18 am #

    you are the best at blender man

  49. todd McIntosh May 17, 2010 at 4:32 am #

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

  50. todd McIntosh May 17, 2010 at 4:33 am #

    [pre]
    iiiiiiiiiiiiiiiiiiii|
    wwwwwwwwwwwwwwwwwwww|
    [/pre]

Trackbacks/Pingbacks

  1. heathenx's status on Friday, 13-Nov-09 12:03:59 UTC - Identi.ca - November 13, 2009

    [...] http://www.blenderguru.com/creating-depth-of-field/ a few seconds ago from seesmic [...]

  2. linuxartist | Creating Depth of Field with Blender - November 18, 2009

    [...] guru has posted a great tutorial on creating depth of field using [...]

  3. Why Do Light Bulbs Burn Out Just As They’re Turned On? | Gizmodo Australia - September 4, 2010

    [...] image by Andrew Price. Tagged:electricitylight [...]

  4. RayBlender » Blog Archive » Glass mesh - November 29, 2010

    [...] render result. In Blender this involves a lot of guessing. I used the node setup as explained by BlenderGuru. This method involves using a [...]

  5. Tutoriais para as férias! « Laboratório 3D - December 20, 2010

    [...] http://www.blenderguru.com/creating-depth-of-field/ [...]

Leave a Reply