24

How can one make a render pass with black and white data to display depth of field based on the Settings > Camera dof settings?
I want to blur or defocus in the compositor, similar to the way it is done with the Z pass but with a more accurate dof information that is based on a target, as opposed to distance away from the camera.

I tried to fiddle with the settings in a new Render layer, overwriting the material, but I have been unable to achieve a render pass effect that outputs something useful.

David
  • 49,291
  • 38
  • 159
  • 317
dimitarsp
  • 1,376
  • 3
  • 11
  • 25

3 Answers3

24

Defocus node

There's build-in support for focusing on objects using this node.

  • In the Properties editor under Object data (with the camera selected):
  • In the Depth of Field panel, add your Focus object.

Camera Object Data, Depth of Field settings

  • When setting up your Defocus node, enable Use Z-Buffer.

Defocus node setup

Cycles DoF

Cycles also has built-in support for DoF. Again in Depth of Field panel, set your Focus object and now increase the Size.

Aperture Size

Also called lens radius. If this is zero, all objects will appear in focus, while larger values will make objects farther than the focal distance appear out of focus.

Creating a black and white depth image

  1. In the Node Editor:
  1. In the Properties shelf (N) of the Driver section of the Graph Editor:
  • Change the variable type to Distance and set the two object to the camera and your target object.
  • Set the script expression to -var. Driver setup
  1. Back to the Node Editor:
  • Adjust the Size setting in the Map Value node.
  • Add | Converter | Math(Shift+A,N,M), set to Multiply and the values to -1 and the Map Value node.
  • Add | Color | Mix (Shift+A,C,M), set to Screen and connect the Map Value and Math nodes.

Node setup

Timaroberts
  • 12,395
  • 6
  • 39
  • 73
Aldrik
  • 9,720
  • 22
  • 56
  • Wow, sometimes, some things are so simple that I wouldn't believe it :) Thanks. I wasn't aware that z depth works in relationship to a target setup in the depth of field section, hence the reason I was looking for a way to achieve another zdepth map. But now, that I just did a simple test, one with defocus node, and one with dof settings adjusted in the camera, i see that the results are comparable. I was previously trying to use the normalize node in between the z depth output and the defocus node z input. – dimitarsp Jul 01 '13 at 12:12
  • @Aldrik any idea why I still get a white image after using the "Creating a black and white depth image" technique? – Marten Zander Nov 17 '15 at 22:39
13

This is because the Z-Depth pass outputs values greater than 1 for distance. You will need to use a Normalize node to convert those values to a 0-1 scale:

Normalize

J Sargent
  • 19,269
  • 9
  • 79
  • 131
  • I think it could be helpful to link this answer with this answer, right? – p2or Dec 14 '15 at 13:34
  • Absolutely, I didn't see that one. Good find. – J Sargent Dec 14 '15 at 14:01
  • This is the only thing thats missing in Aldriks answer. I'm not sure, should we ask the mod-team for merging this answer? – p2or Dec 14 '15 at 14:17
  • Maybe, we can ask their opinion – J Sargent Dec 14 '15 at 14:18
  • @NᴏᴠɪᴄᴇIɴDɪsɢᴜɪsᴇ please elaborate on the "what is it useful for?" part of your own question. Also note the Z pass contains acutal distance information in blender uints (or whatever units the scene is set to) that will be lost when you normalize. –  Dec 14 '15 at 16:13
  • What is a completely write tender pass useful for. I didn't know about the Blender Units relation, that is very interesting. Maybe you should write an answer on how to harness that in compositing. – J Sargent Dec 14 '15 at 16:15
  • As of Blender 2.75 I have tested the normalise method stated above. After adding the normalised node to the z buffer, blender's defocus node was unable to interpret the z buffer node correctly. Or I be doing something wrong, maybe the original poster want to explain how it is useful at all. I get wrong results with it. – hawkenfox Dec 25 '15 at 03:59
  • Normalizing the Z pass will make the scale conform from 0 to 1 instead of being a real measure of distance form the camera, so you are indeed loosing depth information in some cases, or at least working in different scale (that is not necessarily what the Z socket of the defocus node is expecting). Normalizing might be useful in some instances to create fake depth of field see: http://blender.stackexchange.com/questions/8126/blender-cycles-manual-dof/8133#8133 –  Dec 25 '15 at 06:42
  • 1
    Use "Map Range" vector node if you are interested in preserving more depth information over an animation and also convert it to 0~1. For example if you do From 0~100 To 0~1 then you will lose everything further from camera than 100, but anything lower will be black to white in your zpass render. If you use Normalize in animation the relationship between 2 zpass renders will not be comparable. (One image's white might be really far, but other might be close) – 5argon Sep 05 '17 at 15:01
10

You can also hijack the Mist Pass. This works for both BI and Cycles.

Enable it in Properties > Render Layers > Passes:

Cycles

And tweak the start/depth settings in Properties > World. If you are using BI, adjust the settings without enabling it, so that the combined pass is not affected:

enter image description here

Enable Mist in Camera Settings > Display to draw guides in the 3D view which make setting the values in Properties > World easier:

enter image description here

Result:

enter image description here

Timaroberts
  • 12,395
  • 6
  • 39
  • 73
gandalf3
  • 157,169
  • 58
  • 601
  • 1,133