# Mist + dof + alpha in cycles

I'm working on an animation where I want both mist and dof combined with a rather dark world background. The main problem is my mist pass washes out my background, and the only solution I found was rendering the background in a separate pass. What happens then is I get a white border around all objects against the alpha background (I presume the border was always there, but becomes visible in alpha because it doesn't have the washed out background there). It even occurs to objects in the foreground where the mist pass shouldn't be effective. I've looked at tons of tutorials and forums, but can't seem to find a solution for this. It only affects objects out of focus, so it seems like the mist and the dof-defocus somehow have a conflict, where the blurry border around objects out of focus become white in the mistpass.

Does anyone have any experience with this? I'm pretty noob-y at blender and 3d in general, and though I've tried fooling around with nodes and stuff I'm not experienced enough to actually know what I'm doing. Any advice would be much appreciated!!

• Try using ColorRamp instead of MapRange. – Mr Zak Nov 23 '16 at 12:47

This is a tricky one, because you're going to deal with two separate issues that clash with each other.

As @cegaton suggested, volumetrics could make things easier for you, but if you need to solve it with compositing alone anyway, there's a couple of things you need to know:

• The mist pass can help you but ONLY if you intend to composite the pass over the composited plate (no alpha channel). Since it's antialiased, compositing a mist pass over an image with alpha channel will result in color contamination in AA pixels and pixels convoluted by the DoF effect, as you experienced in your tests.
• The Z buffer in Blender plays really bad with cycles DoF. I dare to say that it's completely useless when DoF is used. Just normalize it and examine it with the viewer, the sampling in the defocused areas makes it really inadequate for compositing, unless you're going to use an insanely high number of samples, which beats the purpose of not using volumetrics.
• Alpha channel is also tricky when used with depth effects because alpha compositing is intended for blending a foreground plate over a background plate, not multiple planes in different depths.

With that in mind it becomes obvious that there isn't a silver-bullet solution to composite this kind of imagery in one single pass (unless you work on the composited beauty pass directly, without alpha channel).

If you need to produce layers to be composited using DoF and mist, you'll have to deal with the aliased Z pass and cut slices out of your scene's depth to apply the DoF effect in post without producing bleeds and artifacts. It's a somewhat complex and tedious process and you'll have to evaluate whether it is worth to invest so much time and effort in compositing to spare yourself some extra minutes of rendering.

• Thanks for the elaborate (but depressing) answer, Gez! Very helpful :) – achoc Nov 29 '16 at 9:18
• @achoc Glad you find it helpful. Remember to vote and mark as solved when you find an answer that clears up your doubts. – Gez Nov 29 '16 at 13:29

A possible solution without compositing or using the mist settings, is to enclose your scene in a mesh and use volume absorption and volume scattering. Scattering will give you the effect of suspended particles in the air (which is what fog or haze are) The visibility of the objects in the distance is controlled by the density of the volume scatter.

click on the image to enlarge

The background for the scene will still be visible (depending on the density of volume absorption and/or scatter, and you have total control on the depth of field for the scene.

click on the image to enlarge

• Thanks cegaton! My problem is that this is a 3 minute long animation and render time is already killing me, so I'm trying to stay clear of volumetrics as much as possible, and this solution will probably take forever to render, am I right? – achoc Nov 25 '16 at 12:56

If you want the poles to get grayer as they get further from the camera, use the compositor and the "Z" output. Z outputs the distance from the camera.

You could also create a pass for the objects, so that only the objects get affected by the mist.

• Thanks 10 replies! Do you have a link with a good example of a node setup for this? I've tried on my own, but like I said, I don't really understand how nodes work and usually can't get the result I'm trying for unless I follow a recipe.. – achoc Nov 25 '16 at 12:59
• Check out the update @achoc – 10 Replies Nov 25 '16 at 16:19
• If the goal is to composite mist over a beauty pass without alpha, this solution is enough. It can be optimized by removing the math and ramp nodes and using just a mist pass to feed a mix of mist colour over the clean plate, but in general this solution is just fine. Notice however, that this solution won't work for plates with alpha transparency (and DoF will make things even worse) as the mist pass will contaminate the semi-transparent pixels in antialiasing and DoF bokeh. – Gez Nov 25 '16 at 23:48

When I tried to avoid "white border" around my object because of Mist and the node Alpha Over, I tried to thinness the Alpha layer with Dilate/Erode node, and surprise, it works!

With threshold set to $$-1$$ you get a perfect crop between your background and your object misted.