Z-Depth from 3D After Effects layers

How to generate a z-depth pass from 3D layers in After Effects, without 3rd party plugins

I recently posted a video tutorial that demonstrates how to generate a z-depth map from Particular, and then use it to composite multiple layers of Particular together.  This technique opens up a whole range of creative possibilities, and allows extremely complex scenes to be created with only a few layers.

Generating a z-depth pass from Particular opens up all sorts of creative possibilities.

 

The next logical question is: Can we generate a z-depth map from a normal After Effects composition?

Simple answer – yes.  And this is how you do it.

While After Effects can’t automatically generate its own z-depth maps (at least not in 2014), we can use the same basic approach that I demonstrated in the Particular tutorial – once we have a composition that we’re happy with, we duplicate it and create a separate version just to output the z-depth.

I’m going to assume that you’ve watched the Particular tutorial and are familiar with z-depth maps and what they are – but if you haven’t watched the videos yet then it’s worth emphasizing that our workround requires 2 separate After Effects compositions, one for the “final” output and one for the z-depth.  From a workflow perspective, this means that the two compositions need to be identical, and any changes to one comp will need to be done in the other.  In the earlier Particular tutorial, I link the cameras between the two compositions so any changes and tweaks to the “master” camera move are automatically made in the z-depth composition too.

Because a z-depth map represents the distance to the camera, they’re only relevant to 3D layers.  Generally, if you’re working with z-depth it’s easiest to work with all 3D layers, as mixing 2D and 3D layers in the same composition can cause problems.

So once we have an After Effects composition with several 3D layers in it, how do we actually make our z-depth map?  I’m going to show 3 different approaches, but they all involve applying a plugin to each 3D layer. 

The basic workflow is to create your After Effects composition, duplicate it to create a z-depth version, and then apply a plugin to each layer in the duplicate to generate the z-depth.

1)  Buena Depth Cue, by Rowbyte.

Although the aim of this article is to demonstrate how to make a z-depth map in After Effects without 3rd party plugins, it’s worth noting that the only way to get an absolutely pixel-perfect z-depth map is to buy one.  Buena Depth Cue has been available for many years now, and one of its many features is the ability to produce a perfect z-depth map in After Effects.  It’s now being developed by the same company that created the awesome Plexus plugin.  Although I’ve only used Buena Depth Cue once, and that was many years ago, I found it to be very slow – that’s the price for perfection.  In fact it was because the Depth Cue plugin was sooooo slow that I started looking for alternatives.

Luckily, if we don’t mind sacrificing a little accuracy, then we can just use standard After Effects plugins and an expression.

2) Simple fill effect

 A z-depth map represents the distance to the camera using brightness, so we can create a simple z-depth map by measuring the distance of each layer to the camera, converting that distance to a shade of grey, and then using the fill effect.  This is simple to do, and very quick to render.

You can download a sample project here, but here’s the basic expression we use.  Apply the fill effect to your layer, and paste this expression to the colour parameter:

 

c=thisComp.layer(“Camera 1”); // choose the active camera //

cp=c.toWorld([0,0,0]); // camera’s position in world space //

p=thisLayer.toWorld(thisLayer.anchorPoint); // this layers position in world space //

d=length(cp,p); // calculate distance from camera to layer //

far=10000; // far distance for black value //

f=linear(d,0,far,1,0); // calculate fill colour from distance //

[f,f,f,1]

 

Once we have a z-depth map for our After Effects comp, we can use it with Particular to create particles and light streaks that are correctly obscured.  As I demonstrate in the original videos, you need to use the same value for the “distance at black” parameter in Particular as you set in the fill expression.

Although this is a simple and effective technique, the problem with using the fill effect is that the entire layer is the same colour, which is fast to render but not perfectly accurate.  If we measure properly, each individual pixel of the layer is actually a slightly different distance to the camera – if we look at a simple rectangle directly in front of the camera, we can see that each corner is further away from the camera than the centre.  A perfect z-depth map would represent this, and calculating each pixel individually is what the 3rd party Buena Depth Cue plugin does, which is why it’s so much slower.

The fill effect is fast and simple, but not pixel perfect.

If you have a large layer that is strongly angled towards the camera, then the difference between a simple fill and a true z-depth is more obvious.

So for simple scenes, using the fill effect is very quick.  But we can do better…

3) 4-colour gradient

To work-around this problem using standard After Effects plugins, we can use the 4-colour gradient instead of the basic fill.  Instead of measuring the distance from the layer’s anchor point to the camera, we can make 4 separate calculations – one for each corner of the layer.  The 4-colour gradient effect can then render a different shade of grey for each corner and blend between them.

While this gives a much better result than the simple fill effect, it is slightly slower.  Even so, it’s still massively faster than the 3rd party alternative and hey- it’s also free.

You can save the 4-colour gradient effect – with the expressions applied – as an animation preset, so you apply it with just one click.  The project file that you can download includes a preset to do just that.

Save as a preset and apply with only one mouse-click.

As I noted in the other tutorial, z-depth maps do have limitations- they don’t deal with partial transparency or motion blur.  These limitations apply to z-depths in general – it’s not just a problem with this technique in After Effects.

To avoid problems with motion blur, simply render without motion blur enabled and then use the Reel Smart Motion Blur plugin over the final render.

Avoiding problems with partial transparency is more difficult, and there’s no magic solution.  However if you do have layers with partial transparency, such as stock footage of fire or dust, then using the levels effect to crunch the alpha so it’s mostly solid can help a lot.

It’s also worth noting that blending modes will cause problems with z-depth maps too, so to play it safe you’ll want to change all your layers to “lighter colour”.  Because the layers are already in 3D space, there shouldn’t be any difference between the “normal” and two lighten blending modes, but by using “lighten” or “lighter colour” you can avoid potential problems with intersecting layers.

One final thing to note is that using this general approach requires the two compositions to be identical.  However if you have used the “wiggle” expression on any layers then you will get different results when you duplicate the composition to make your z-depth version.  To fix this, and the get identical but still random wiggles, you need to add a seed number before the wiggle expression.  If you manually specify a random seed then the wiggles will be the same when you duplicate the composition.

You can download an example project here that demonstrates the fill and 4-colour gradient approaches, and includes an animation preset for the 4-colour gradient.

 

This example demonstrates snow made using Particular being correctly obscured by houses made using 3D After Effects layers.

I’ve also included an example where the “far” distance control is linked to a slider, so all layers can be updated and adjusted together.  This is useful if you want to use this technique to generate fog, and not just a z-depth map.

And if you haven’t watched the original video tutorials on using Particular, then check them out here.  

And if you’re a new visitor to the ProVideo Coalition then check out my other After Effects articles.

 


Share:

Chris Zwar

Chris was born a geek, and was lucky enough to own a Commodore 64 at a time when the number of students at his primary school who owned a computer could be counted on one hand. Upgrading to an Amiga with the onset of the desktop video revolution, Chris began experimenting with digital graphics and animation in his bedroom, and incorporating them into his high-school video productions.

  • Stanislav Yakymenko

    Very useful, many thanks!

  • Victor Nils

    The example projects files are offline.
    I’ve tried using the script but it didn’t work.
    🙁

PVC Logo
PVC App Demo

Now Available in the App Store

Get access to our latest and featured articles from your favorite authors, wherever you are, directly on your phone!

  • Clean and Easy to Read
  • Blazing Fast Notifications for New Articles
  • Free Tuesday Webinar Access
Apple App Store
Google App Store
Get your FREE Search Bins Project File!
Want Us to Remind You?
Enter your email and subscribe!
Get Access!
Please enter your email address to view our free live webinar!
*you will be added to the ProVideoCoalition.com newsletter and if you’re already signed up, it won’t send you duplicate e-mails