2-PASS DEPTH OF FIELD & RACK FOCUSGetting it Right Once and For All

By Lance Evans • Category: Tutorials

articlethumb_1.jpg
A shorter version of this tutorial originally appeared in 3D World Magazine issue #39 Why another article on depth of field (DOF)? The reason is that each solution I have seen comes with its own set of glitches. The two-pass technique I offer here minimizes many of these glitches, while trying to keep any new ones to a minimum. But before jumping into that technique, let’s take a quick look at what DOF is, why it’s so hard to create, and a few of the other solutions available. We will slant this towards the Maya workflow a bit, but most information is applicable to other software.

Why another article on depth of field (DOF)? The reason is that each solution I have seen comes with its own set of glitches. The two-pass technique I offer here minimizes many of these glitches, while trying to keep any new ones to a minimum. But before jumping into that technique, let’s take a quick look at what DOF is, why it’s so hard to create, and a few of the other solutions available. We will slant this towards the Maya workflow a bit, but most information is applicable to other software.

REAL DEPTH OF FIELD
The term depth of field (DOF) refers to the shallow range of optical focus found in most photography and film work. This DOF is created when a camera lens is unable to properly resolve light rays being refracted through its glass-predominantly the outer edges of the glass since rays passing through middle portion are more easily focused. The simple solution to this problem came in the form of an iris, which reduces the lens’ diameter, blocks the rays entering the periphery, and delivers an image with greater DOF.

DOF IN 3D
In contrast, our 3D worlds produce infinite DOF, which in most situations is unnatural looking. Since our applications do not have an iris’ aperture to easily change the DOF, developers and animators have had to come up with ways to synthesize the shallow depth of focus we seek. Some are more successful than others, but all have a place in your bag of tricks.

1: Depth of Object
1: Depth of Object

One of the most successful attempts at synthesizing DOF from within a 3D application is found in Alias’ Maya software. Based on real-world camera settings, the rendering camera’s Depth of Field settings are easy to use. Object depths are easily determined by turning on Display > Heads Up Display > Object Details, and clicking on the object of focus.

2: Maya's DOF
2: Maya’s DOF

Maya first generates a depth map which it uses as a guide to adding blur. It is able to do this more successfully than if a depth map were used in post, and the results are truly gorgeous. Notice how blur increases both in back of and also in front of the first model, which is the object of focus. But this feature falls apart when used with either transparent objects or fog. If either of these two attributes are in a scene, then the file will need multi-pass and compositing in post. Technology aside, the greatest limitation of any internal solution is lack of artistic flexibility. It can be hard to know just what the DOF should look like until everything is put together in post. Having the wrong amount of DOF applied at rendering means a full re-render. Ouch!

3: Images w/ Z-Depth
3: Images w/ Z-Depth

Generating an RGB file with an included z-depth channel is a great solution, in theory. In reality there are some caveats. First, z-depth channels are not anti-aliased since they only read one depth per pixel. Other issues are file incompatibilities. Only RLA and IFF files from Maya include the extra z-depth channel, but After Effects does not see the Z channel in IFF files, and its handling of RLA files is often unstable. Shake works well with RLA files, but will only import them after the extension has been removed (Windows). I am told RLA files work well with Combustion, but am unable to verify this in time for publication. Z-depth files can also be output as separate files, but these cannot be used with the 3D Depth plugins in AE. Note: Users of Electric Image can rejoice, as their .eiz depth map files work flawlessly with AE and its 3D plugins. (Ed. Note: Some of these compatibility issues have been resolved since this article was written. For example, After Effects handling of RLA files is now stable.)

4: Incandescence
4: Incandescence

An older, tried and true method of generating depth maps is to use a simple Fog effect. The idea is simple and brilliant, and we use the resulting image as an Alpha channel in post. This technique works with almost any 3D app, is very reliable, and offers a lot of control. This can be accomplished a number of ways, and there are even some shaders and plugins available for Maya designed for this purpose. I find it easy enough to set this up manually by creating a new Material and simply dragging its Incandescence slider all the way to the right until it is white. Apply this material to every item in the scene with the “Apply to all selected objects” command, and turn off or delete all lights. If you were to render the scene now, it should be totally white.

5: Setting up Fog
5: Setting up Fog

Next, in the Render Globals > Render Options window click on the map button for Environmental Fog. This will create a new fog node. Click on the button once again to open its editor. Under Simple Fog change the Color to black. Saturation Distance will determine the point at which the fog is fully dense. Fog Near Distance and Fog Far Distance, found under Clipping Planes, determine where the fog starts and stops. A zero in the Near would indicate the camera’s position for a start point. Think of these 3 controls like the Input Levels in Photoshop. They will control the points where the depth of the fog is transparent, resulting in a white image; and where it is fully dense resulting in black. Make sure the Saturation value resides between the other two values, and that both the Apply Fog in Post and the Color Based Transparency toggles are left unchecked.

6: Fog Rendering
6: Fog Rendering

It should not be too hard to create a scene that looks something like the one in figure 6, where the closest items are near white, and the farthest are near black. How you balance the tonalities here will affect the DOF in post. Save this scene under a different name so as not to overwrite the original scene file. Output to an appropriate format, but leave the other settings to match your main full color render. Turning off any unneeded features like texture maps and shaders will allow this pass to render very quickly.

7: Fog vs. Z-Depth
7: Fog vs. Z-Depth antialiasing

In this magnified view, notice how the fog generated depth map (left) includes anti-aliasing, as opposed to the automatically generated z-depth map which does not (right). Some animators swear the fog render delivers a smoother image, but in most cases it will be subtle. Since it is a controllable render, you can decide just how much anti-aliasing you actually want. Generally a small amount is all you need. But don’t toss out those Z-depth renders, we have a great use for them later!

8: Shake applying ZBlur
8: Shake applying ZBlur to imported RLA

If you chose to use any of the “all in one” file formats which include a z-depth channel, basic depth treatments can be produced using plugins available in your compositing software. In figure 8 we see Shake applying its ZBlur filter on an imported RLA file. In After Effects use the pro bundle’s DOF filter (EFFECT > 3D Channel > Depth of Field).

9: Bleeding Blur
9: Bleeding Blur

Sliding into the heart of this tutorial, here we see the scene brought into After Effects. There are a few ways to set up a fog-based DOF project. The old way was to stack 2 copies of the scene and set the fog layer as the top image’s alpha. I like to use the Set Matte filter for this (EFFECT > Channel), but others work as well. Then add some (Gaussian) blur to the lower layer and you are there! The updated version of this technique is to only use a single copy of the scene and apply the Compound Blur filter which combines both alpha selection and then blur. So we are done? If you only need a bit of blur for softening, yes. If you try to apply more that a few points of blur you quickly get what I call “bleeding blur”. This haloing effect appears where near and distant items abut one another.

10: Background Pass
10: Background Pass

There are a few things we can do to solve this problem. “Bleeding Blur” makes a great case for using a Z-depth file instead of the fog solution, as it will hold this bleeding back a bit more than the anti-aliased fog render. You can also do some fancy masking, track matting, and even create what I call a layered luma-depth mask. A simpler approach is to break the scene into 2 parts, in this case the statues and the background elements (ground/dome). Figure 10 shows the background pass. The statues are not here but their shadows still render. This is done by opening each statue’s editor and under its Render Stats deselecting the Primary Visibility toggle. A depth pass is also rendered, as seen in the inset, and it is composited with the RGB pass using AE’s Compound Blur filter.

11: Front and Back Alphas
11: Front and Back Alphas

The statues are a trickier matter. We will need to take an extra step to prevent any bleeding coming out from under either the front or back statues. To do this we need to take the old technique a step further. Rather than laying one layer with a depth alpha on top of another, we are going to combine two layers that both have depth alphas. One is alpha keyed from the front to back, while the other is keyed back to front. Thus we avoid any overlay and bleeding blur effect! You can see the two alpha passes above. There are a variety of ways to generate these in both 3D and post, depending on your scene’s needs and software.

12: Final Composit
12: Final Composit

Figure 12 shows the final composited image, which exhibits some very clean and well controlled depth blur treatments. There is one caveat with this technique. Since we are removing density from both the front and rear elements of the statues, the mid-section can find itself without enough density. A good way to check for this is to throw a strong colored Solid layer behind the statue layers. The more obnoxious the color, the easier it is to see-I often use an electronic pink-ugly but very useful here. If density is lacking, you will see it. To fix this problem, take the assigned matte layer and Pre-compose it. In the new comp add a Levels filter to correct its gamma to allow a bit more RGB image density through the mid-tones. You will need to check these adjustments in the main comp to see if they worked well. This step must be done in another comp because filter effects applied to layers within the same comp are not read properly. Once you have it all working correctly, you will find that the resulting scene can have far more Gaussian Blur applied without any bleeding. This sample has 20 points of blur added, far more than the earlier sample that caused the bleeding blur. Customized Blur Now that you know about the modified matte comp trick, let’s take it a step further and make some more use of it for some fine tuning. Depth renders are rarely perfect, whether created manually or automatically. As we progress in a project our needs begin to vary, as the scene’s needs change or as imagery evolves. Depth also varies as we begin to animate a scene, move the camera or objects thus altering the point of focus. We can accommodate all of these needs.

13:Refocusing in Post
13: Refocusing in Post

By moving all of your masking layers into their own comps, they can be tweaked-and in turn the imagery they control-to adjust and animate the focus over time. Figure 13 shows the front statue being moved in and out of focus over time using the Input White levels control. Buying Blur off the Rack We are not done yet. We can push what we have learned further still. We can create a real-time “rack focus” rig. If you are not familiar with the term, rack focus is a term used in the traditional photography (large format) and film industries. It simply refers to a lens that is placed on a sliding rack support system. It can then be controlled manually or using more automated systems to vary the point of focus as needed. So the focus can start off near and move progressively further away, for example if following a subject in the scene. This is also referred to as executing a “follow-focus” on a subject. So far we have explored ways to DOF focus and out of focus in our scene, and even added a bit of controls and refinement. But now we are going to do something far more interesting. We are going to learn to control the placement of the focal point itself in the scene.

14: Rack Focus
14: Rack Focus

By using the AEFFECT > Adjust > Curves filter instead of the Levels, it is actually possible to generate a rack-focus effect on the fly! How does this work? Keep in mind that we are only dealing with grayscale tones of a mask layer. Where the mask image is white we get a sharply focused RGB image, and conversely where it is dark the RGB image is made proportionately more blurred. Unlike a Levels filter, a Curve filter gives us the ability to place each range of the mask’s tonality just where we want it. This means we can use the normal H & D curve (the 45 degree straight line) which would put the focus on the nearest scene items in our sample, or we can invert that line and put the focus point towards the rear of the scene. Where it gets interesting though is that we can create a bell shaped curve and place the focal point midway in the scene, allowing items both in front of and behind to gradually out of focus. Thus, by modifying this curve we can place our focus in the scene at will-and in real time. If we were to combine this technique with the two-pass depth control discussed earlier, our project would certainly start to get complex. However, there are ways to rig a file using Expressions and Expression Controls that would make the process much easier to control in a production environment. Once properly set up a project can be very functional, and such a solution would still be faster and more flexible than rendering the effect in 3D.

LAST TIP:
Remember we said to hang onto that Z-depth pass, we would want it later? Well, the anti-aliased pixels of a fog depth map do not behave well as we would like when using the curve filter effect for this rack-focus. Bringing back the Z-depth pass and using it instead will give subtly cleaner results. Have fun!

Comments are closed.