Thursday, November 12, 2009

2.49b Depth of Field Nodes

Join my Blender 3D forum, where you can network with other Blender friends and get your Blender 3D questions answered.

Hi. The eye naturally focuses on sharper objects in a scene and tends to overlook blurred objects. Depth of Field (DOF) is a technique in which you highlight objects in focus, giving them the illusion of being closer to the camera, and blur out the parts of the scene that are not as important. You can use this to force your viewers to focus on the objects you consider important. Using Blender's composite nodes, with some mathematical trickery, you can set up Depth of Field.

So let's get started. Fire up Blender, delete the default cube (Right Click, Delete key, and Enter to confirm). Add the monkey mesh (Space - Add - Mesh - Monkey). Let's make the monkey green. Press F5 (Shading). Then click Add New to add a new material. Set R=0, G=1, and B=0. Go to the Edit buttons (F9). Press the Set Smooth button to make Suzanne more presentable.

Let's make more Suzannes. Click the Modifier button, at the extreme right. Add an Array Modifier. Make sure Relative Offset is clicked. Set X= -1, Y=0, and Z=0. Set Count = 8. Press F12 to render. We now have 8 monkeys, each a bit farther away from the camera than the previous one. The last two monkeys are out of camera range. To fix this, press N to bring up the Transform Properties window. Set LocX to 3. Now there are 8 monkeys in camera range.

Suppose we want our viewers to focus on the first monkey and gradually blur the rest of them as they get further from the camera. We can achieve this effect with Composite Nodes. To do this, change the window type of the 3D Viewport to Node Editor. We start out with two nodes. The input Render Layers node is our original scene before the composite node magic is applied. The output Composite node is the final, composited result. Move the Composite node all the way to the right.

Add a Map Value node (Space - Add - Vector - Map Value). Map Value takes information and maps it into a range of values. Map Value can also add a number (the Offset), as well as multiply by a number (the Scale) from the input. We're going to play with the offset and scale in a minute. In this case, we are going to map the Z value, which is the distance, in Blender Units, of each pixel, from the camera. Connect the Z socket of the Render Layers node to the Value socket of the Map Value node.

Next, add a Color Ramp node (Space - Add - Converter - ColorRamp). The color ramp gives us a visual representation of the Z values, with values from 0 (black) to 1 (white), Connect the Map Value's Value socket to the Factor socket of the Color Ramp node. Then, connect the ColorRamp image socket to the Composite Node image socket.

Right now the compositor shows all white. That's because all the monkeys map to a value of 1 or greater. The first monkey is about 5 Blender Units from the camera. With this mapping, the monkeys all map to white.

However, if we adjust the offset to -6 and the Size to .1, we get an interesting falloff from black to white. What happens is that for distances between 6 and 16 Blender units, we map a value between 0 and 1. Here's an example. Take a pixel at 10 Blender Units from the camera. First subtract 6, to get 4, then multiply 4 by .1, yielding a value of .4, which maps to a grayish color 40% between black and white. Any Z distance less than 6 produces black. A value greater than 16 produces white. We can translate this falloff into a gradual blurring.

To do this, add a Blur node (Space - Add - Filter - Blur). Connect the Image socket of the Render Layer node to the Image socket of the Blur node. That will feed the original scene to the blur node. Then, connect the Image socket of the ColorRamp node to the Size socket of the Blur node, feeding the adjusted Z values to the Blur node. Connect the Image socket of the Blur node to the Image socket of the Composite node.

You need to fiddle around with the X and Y values in the blur node to set up the exact amount of blur falloff. I found that X=6 and Y=6 worked well for focusing on the first monkey and gradually fading out the others.

We're ready to render. Go to the scene buttons (F10). Press the Do Composite button to tell the Renderer that we are using composite nodes. Press F12 to render. You should get the depth of field effect we're aiming for.

That's it. I hope this gives you some interesting ideas for highlighting objects in your scene using depth of field. Happy Blendering!