11 October 2005

Depth of Field as a Post Process: A Maya Tutorial
by Kris Kapp

I stumbled upon this method as a way to get around Maya 1.0’s lack of depth of field. I saw a tutorial on Tom Kluyskens’ Maya Queen page on how to fake environmental fog. His tutorial shows how to make a shader-based fog that renders fairly fast. It was based on the sampler node’s ability to give you info about a whole range of things. Tom Kluyskens used it to determine the distance of an object from the camera. When I started messing around with his tutorial I immediately realized that I could use the sampler node info much like a Z buffer but with much more controllable results.

I ended up using the output from my custom fast fog shader as a separate channel for a blur event in Composer. I found that I could change the amount of the DOF by changing the amount of the blur. I got immediate feedback and I didn’t have to re-render the scene if I later changed my mind on how much DOF I wanted. Once I saw that I could do this in Composer, I became a big fan of doing things like glows and fog effects as a post process. Now, I don’t know how scientifically accurate this approach is but I am pretty happy with the results I’ve gotten.

What we’ll do is take a finished scene, render it, then create a fog shader and apply that to all the objects in the scene. Then we’ll re-render the scene and import both of the rendered results into AfterEffects. We use the re-rendered image files as a matte for a blur effect to simulate Depth of Field.

Here is my screen capture of my alley scene:

This first part is going to follow pretty close the Fast Environmental Fog tutorial, but I’ll make a couple of changes to customize it for my scene. You can do these steps in the Multilister or the Hypershade, whatever you’re more comfortable with. Create a Sampler Info Utility and a Set Range Utility. We’ll use the Sampler Info to determine an object’s distance from the camera and the Set Range to convert that distance from 0 to 1. With the Connection Editor, connect the Point Camera Z from the Sampler Info Node to the Value X in the Set Range Node.

Using the Modify>Measure>Distance Tool in the top view of our scene, we can compute the distance from the camera to the furthest object in our scene. This will give a ballpark figure to use.

We can then plug that value into the Set Range Attribute on the Set Range utility node.

The distance from the camera to the farthest object is 326 units. I fudged the amounts a little at both ends and ended up using the values –300 to –10. These numbers worked fine for my purposes. The Set Range node remaps (-300 to –10) to (0 to 1). Next, create a Blend Colors Utility node. We’ll connect the output of the Set Range into the Blend Colors node. In the Connection Editor connect the Out Value X to the Blender attribute.

The Blend Colors node mixes two colors based on the Blender attribute. Open the blendColors node and make Color 1 black and Color 2 white. Create a Surface Shader Group and connect the blendColors node’s Out Color RGB to the Out Color attribute of the Surface Shader. What you’ve done so far is to make a shader that blends from one color to another based on how far that object is from the camera.

Go ahead and render out your scene.

The plan is to take the shader we just created and assign it to every object in the scene. Also you will have to change your lighting. I ended up using a directional light with shadows turned off. You don’t want shadows because in your final rendered output is going to be basically a grayscale image. Once you have rendered out your original scene, assign the new shader to all the objects in your scene. Change your lighting and re-render your scene.

Now we have two different rendered version of the same scene. We are going to take both of these renders and import them into AfterEffects. We are going to use the fog images as a luma matte combined with the original image being blurred to get the depth of field effect. In a new composition window drag your original rendered images twice so you have two layers of the same image. Next, drag down your fog rendered images. Click on the Switches/Mode button to get to the Transfer Mode panel. You are going to turn your fog renders into a transparency mask by defining the Track Matte as a Luma Matte in the layer just underneath the fog render layer. Now add a Gaussian Blur effect to the layer you have the Track Matt on. By changing the amount of the blur you can change how much of a depth of field effect you get. You can add a Levels effect to the fog layer to control how deep in the image the depth of field effect take place.

Hopefully the above image will make things a little clearer. You can do all kinds of things with this setup. By animating the different effects you can change the focus over time. I took a fog render, inverted it, then had it fade into the original fog layer to get an effect that shifted the focus from the foreground to the background. You can also add or multiply another fog channel over your renders to get a soft kind of haze effect that is controllable independently from your original renders. I used this in the Rabbit of the Apocalypse short that was nominated for the Big Kahuna Award. You can check it out at my site that is still under construction and in desperate need of updating at http://spudk.home.texas.net under the animations section. As always there are probably easier ways to accomplish the same effect but this worked pretty good for me, and it was fast. It also gave me a great deal of control over the final result. Hopefully you’ll find it useful and any comments are appreciated.

<<< Back to Artist's Corner Home

 

GIGnews is a publication of GIGnews.com, Inc.
"Get In the Game" is a registered trademark used with permission.

© 1
999- 2005 GIGnews.com, Inc.
Legal