We are going to see what is the "Depth of Field" (DoF) effect and how to simulate it within Blender. Or something like that.
DoF is a pretty common effect in photography, it consists in the loss of focus of objects too near or too far away from the camera's focus point, which makes them "blurred" to the eye. This is particullarly obvious in photographs of tiny objects where a small shift of the focus can ruin the shot.
In computer generated images this doesn't occur and because of that an effective way to make our images "realistic" is by simulating the DoF (that is blowing out the image). Blender doesn't have a built in option to deal with this though we'll see that IT IS afterall possible to get the DoF by means of a number of sly techniques.
Techniques we're going to describe here are:
- Strubi's Z-Blur plugin DoF
- "Spinning" DoF
- Caronte's Blur DoF
Most of these techniques can be applied to static images as well as to animations. Since some of them are better adapted to some situations than other, the preferred technique will vary depending on the particular project we're working on.
Strubi's Z-Blur plugin uses Blender's Z Buffer to give the DoF and was written by Strubi, a well known blenderhead and author of several plugins. Source code is freely available at http://honk.physik.uni-konstanz.de/~strubi/3d/ (where you can find instructions clearer than these ;) ) or at the Official Blender Site under the Resources section. It is available as a precompiled DLL for windows, and linux users will have to compile it by themselves from the source. It is a plugin to use in the "Sequence Editor" that some of you might be familiar with.
To use it in our scenes we must open the Blender Sequence Editor (the little "movie" button) and insert our scene with SHIFT+A --> SCENE --> 1 (or whatever our scene is called) and interactively allocate it at any frame, though the best would be to allocate it at frame 1.
Then we'll have to load the zblur plugin with SHIFT+A --> PLUGIN --> "path to zblur.so or zblur.dll", a new strip will appear above the original scene strip.
Now what ?, well, select the plugin bar and press N to show up the plugin's menu which looks like this:
Before getting deeper into the plugin's use, we need to understand its options. The ZBuffer is a memory area where Blender stores "depth" information of all the points in a line that covers the "eye" distance from the camera to the farthest point visible in the scene. As you may know, this line starts in ClipSta and ends at ClipEnd. ZBlur takes the ZBuffer information to stablish the right blur of a 3D point.
Using the values ZMin and camera's ClipSta the plugin computes a blur gradient from the camera to the focus plane. Normally the maximum blur before the focus plane is located at ClipSta. It's important to set ClipSta as high as possible in order to see near objects blurred but sometimes ClipSta could not be changed (our image could be clipped). In that case Zmin will shift the maximum blur. Zmin is a normalized value of distance, being 0 at ClipSta and 1 at ClipEnd. Another workaround is to play with the camera's Lens parameter in order to "gain" ClipSta.
To set the focus we have the "Focus" parameter (who could have guess it ?) which like Zmin is a normalized value. In any case Zmin could not be greater than Focus. If this happens we'll get a red image when render (original way to say that something's wrong).
Let's see some nice images to clarify these concepts. I have prepared a test scene. Without DoF it looks like this:
Now if we load the scene in the sequence editor and adjust the options as indicated, we'll get things like these:
Zmin = 0.75
Zmin = 0
Zmin = 0.75
Zmin = 0.75
The blur intensity in theimage can be adjusted by tweaking the BlurSize parameter. The higher BlurSize the stronger the blur effect.
We have to activate the "Do Sequence" button in Blender when we are going to render the image otherwise the plugin action could not be appreciated.
Strubi suggests a technique to determine the exact values of ZMin and Focus (since they are normalized and do not represent actual values of distance) and is done by activating the "Autofocus" option. When this is activated the focus is "locked" and we can control it by parenting an empty to the camera with a "Make Track" (CTRL+T). Then when we move the empty the camera will track it and the focus will be set at the empty's location. This value will be sent to the standard output (the console if we fire Blender from it) so we can enter it manually later. Another technique from Strubi's to know the ZBuffer range is using the "IPO Focus" option which let us asign an IPO curve along the Zbuffer. In the Y axis is plotted the whole ZBuffer range (normalized from 0 to 1) and in the X axis is plotted the duration of the animation (normalized from 0 to 100).
Though Strubi is stubborn repeating that the plugin doesn't simulate the real optical effect, and that it fails in some situations (I'm not going to argue with him) in my personal experience the plugin works quite good.
This method is very quick and compact in its use, the only "drawback" could be that as a sequence editor plugin it will force us to learn the sequence editor use and how to manage with it. For instance, to see how different settings in the plugin effects in our image we have to "clear" the ZBuffer between renders, otherwise we'll be seeing "old" information.
I think it was Randall Rickert the first one in talk about this method in the Blender Forums, I'm not sure. Anyway, a comprehensive explanation of this method from Macke's pen is available at http://www.elysiun.com , very nice site BTW.
In this method the "trick" is making the camera spin around itself while keeping it focused in a point of the scene (the "focal point"). The whole spin takes place in one frame and the image is rendered with the "Motion Blur" option activated. This way (details apart) we get an image where all objects are blurred except those near the focal point.
Setting of the scene is as follows:
Once this is done (believe me, the tutorial at Elysiun is WAY more clear) we have a basic setting to apply the technique. Only thing that is left to do is to activate the MBLUR button in Blender, render and see the results.
The image we'll get will depend on several things. Mainly the size of the curve where the camera is spinning, a bigger curve will lead to a more "drastic" effect (it could easily lead to a "shaky" picture). No less important are the Motion Blur's Buffer factor and the IPO curve shape.
Here we can see some results with the correspondig settings (some of them are very ugly):
Same parameters, focus on background
Focus on infinity. Bf=2.5
Same focus. Bf=5.0 (maximum)
The relation indicated as IPO is the location of the second point of the IPO curve as an X, Y pair. In other words IPO=3:1 means X=3, Y=1 for the second point of the IPO curve (the first one is always X=0, Y=0). This changes the speed of the "path" of the camera.
This technique have several advantages and drawbacks. In the first place is the slowest of all techniques. Each rendered frame takes 7 or 8 extra frames to produce the "Motion Blur" for which is not advisable to render animations (unless you got a Pentium 4 at 2 GHz). Moreover is quite difficult to set the parameters in a satisfactory way so get things right may take a little while. It's painfully easy to get a "shaky" picture instead of the DoF effect.
However it gives the most "credible" DoF when properly set. Once mastered it could lead to very impressive results (don't look for them among my images)
Caronte, has developed a technique that doesn't require any plugins nor any giroscopical extravaganza though it requires no less funny stuff.
His technique is very well explained at his website Caronte's World so I will only explain the basis of its functioning (besides it took me a lot to understand it...).
The most efficient way to applied it is downloading Caronte's original .blend filed from here. This is a very useful scene which we'll briefly describe:
It has an empty (cleverly called "Focus"), a cube (called "Material Deposit") and an edge that works like a "slider" to adjust the focus and that is called "Lenght" which acts over the empty. The cube has a particular material with the following textures in sequential texture channels:
This material is called DoF and we'll be back on it in a minute.
So, I want DoF, what is all this for ? Well, to apply Caronte's DoF we have first to load our original scene, render it, and save it as Targa. Now append form the original Caronte's scene (dof.blend) the objects "Material Deposit", "Focus" and "Lenght" (SHIFT + F1).
When importing the "Material Deposit" object we are really importing the DoF material to our scene, the cube is not necesary "per se" and it won't show up in our final render (it could even be on a hidden layer when the render is done). Keep this in mind.
After importing we will assign this DoF material to the WHOLE scene by selecting all the objects in it and the cube (in that order) and pressing CTRL+L->Materials.
Now all the objects have the DoF material. Since the Image textures on the material are mapped as WIN, the different objects will be seen in the screen as if they were one only object because the texture is mapped using the window's X and Y coordenates. Moreover since the material is Shadeless it will be seen with the same light intensity as in the images and no extra lighting will be added to it. To realize how strange this is deactivate for a moment the Shadeless set in the material or even better do not apply the DoF material to all the objects and leave some of them with their original materials.
In this render the floor still have its original material while the coins were assigned with the shadeless DoF material. The image was rendered using a different extra camera to make the WIN mapping noticeable, if rendered with the same camera it wouldn't have been possible to tell where the different objects were (that is precisely the idea behind this) . Look that the coins' contour act as a "window" where the Image textures were mapped using the X,Y coordenates from the original image.
Now we will copy the "Material Deposit" cube's location and rotation to our camera (CTRL+C -> Loc y CTRL+C->Rot selecting first the empty and the the camera).
Finally adjust the Focus position by scaling up and down the "Lenght" object to the desired location. Because of the way Caronte has arranaged the objects of the scene this is very easy and quick to do. The focal zone can be extended by scaling up the "Focus" empty.
render the scene again and enjoy the results of our (?) hard work. We will see the DoF effect applied following the Blend texture (mapped with the empty's coordenates). Isn't this cool ?.
Focus on foreground. Blend type Ease
Focus on background. Same settings as previous.
I have changed the Blend type to Quad
Blurred texture with Filter=15
This technique allows the user to fully control the DoF effect, combinations of Blend gradients and colourbands as well as different Filter values for the textures and the scaling of the Focus let us do almost every little thing with our DoF. It is very fast to apply (but not so easy to understand at first glance) and works great with animations by changing the Targa file with an AVI file in the textures. What else can I say ? IT'S JUST COOL !
We have covered the most important DoF methods in Blender, this article wasn't intended to fully explain every technique but offer with an easy-to-understand approach a panorama of choices. What method is the best ? It depends on every situation. For a static image it might be the "Spinning" method since it offers the most "realistic" results once carefully set. However ZBlur and Caronte will give excellent results with a much better render time which makes them my favorites. This feature makes them almost the only choice for animations. Both techniques could "fail" in some situations if the user has a too exquisite taste for perfection. Strubi's plugin produces some artifacts at sharp edges, and Caronte's technique sometimes blurs edges that should be sharp. In any case the artifacts are practically unnoticeable in all situations for most of us mortals.
One last piece of advice. DoF is an effect. It's NOT the main theme of an image. To get a "realistic" picture is important to apply techniques and effects but without ABUSING of them. Remember that a bad texture could not be fixed with any DoF.
So, you have the options, choose the most suitable for your project and... go blending !