Is there a way to automatically decrease layer opacity as a layer moves away from a camera?
Sure, a simple expression will do:
Adjust values as needed...
Okay, I tried using this as an expression in the opacity of the layer, but I couldn't get it to work.
I believe I understand the logic of your expression:
You define the minimum and max distances/opacities, then find the distance between the camera and the layer and do a linear interpolation to find the resulting opacity.
There are no errors in implementation of the expression, but I also can't seem to get it to work. Will the opacity pick up the fade value? Do I have to add a line to tell it to somehow?
Let's break down the thought process for the expression and see if we can get it to work for you.
First you need to measure the distance between the camera and the layer. The After Effects Expression Language Menu has a method we can use under vector math that measures the distance between two points. Use the camera for one point and the layer's position for the other and we have the first number.
Now we need to decide what we want the opacity to do in relation to the distance. When the layer is the same distance from the camera as the zoom value it will be at the equivalent of 100% scale. This may be a good place to have the layer be fully visible. That distance is easily set by assigning the fully visible distance to the Active Camera's zoom value. Next we need to decide how far away the layer should be when it's fully invisible (opacity = 0). Personally, I think the easiest way to do this is to use a multiplier for the zoom value. 4X with most lens settings is a pleasing distance.
To set up the relationship between distance and maximum opacity you can use one of the Interpolation methods from the Expression Language Menu. We'll use the method with maximum and minimum values.
Let's put that together in an expression. The two methods to use are
linear(t, tMin, tMax, value1, value2)
Define some variables for each of the values and we're done:
point1 = thisComp.activeCamera.position; point2 = position; tMin = thisComp.activeCamera.zoom; tMax = tMin * 4; value1 = 100; value2 = 0; t = length(point1, point2); easeIn(t, tMin, tMax, value1, value2)
If you really want to get fancy then you can add a layer space transformation to the camera and to the layer so that the expression would work the same with or without parent layers for the camera and/or the layer. The toWorld modification looks like this:
point1 = thisComp.activeCamera.toWorld([0,0,0]); point2 = thisLayer.toWorld(anchorPoint);
By the way, I usually use more descriptive and shorter names for the variables but thought this would make it easier to understand what was going on in the expression. I also usually use Active Camera for my expressions rather than thisComp.layer("Camera 1") because it works no matter what you've named your active camera.