Sometimes it's handy to have a 3D layer only be visible when it's facing the camera. One application of this might be when you want a different image to appear when the object is facing away from the camera.

There are different ways to approach this, but here we'll focus on creating an opacity expression that can detect when a camera can see the layer's front side. In that case we want the opacity to be 100%. We want the opacity to be 0% when the camera can see the back of the layer. Once we have such an expression, we can apply it to layers representing both sides of an object, rotate the back layer 180 degrees on its y axis, and then parent the back layer to the front layer. Then when we rotate the front layer or orbit the camera, we will always see the correct image. We also want this to work even if there is no camera.

To make this work we're going to take advantage of one of the expression language's extremely handy (but, unfortunately, somewhat difficult to understand) features - layer space transforms.

In this case we will use the toCompVec() transform to convert a vector pointing out of the layer (in the direction of the z axis) into the coordinate system of the camera view. Then we need to examine the z component of that transform. If it's positive, the layer is facing the camera. If it's negative, the layer is facing away from the camera. Note that if there is no camera, toCompVec() converts the vector to the comp's default view, so it will still work.

This all sounds incredibly complicated, but the code itself is deceptively simple.


if (toCompVec([0, 0, 1])[2] > 0 ) value else 0

Opacity expression used to make each layer invisible when facing away from camera.

Nerd Note - a vector is an entity that has a direction and a magnitute, but no specific location in space. It's helpful to picture a vector as an arrow that you can move around in space, but it has a fixed orientation and length.