-
Expression to dynamically adjust values based on proximity to camera
Hey yall!
Let me preface this by saying this is my first deep foray into expressions. I’ve gotten thus far based on my spotty PHP/Actionscript knowledge but have hit some walls.
I’m designing a hallucination effect where a 3D camera passes through several 3D layers in Z. I’d like to whip up an expression I can insert into some layer properties so that they’ll adjust based on how close the camera comes.
The biggest property is using mask expansion to (elegantly) poke a hole through the layers so the camera can pass through them. But there are others I’d like to apply it to.
Here’s a sample of what I mean: https://thinkmad.com/clients/northofny/xxyyxx/VFX/DMT/V.3D_2.mov
My basic strategy is setting a ‘threshold’ distance that once the camera hits, we begin adjusting value. I set a layer marker at the point where the camera’s Z crosses the layer’s Z… and calculate the frames between the marker and the threshold. I then try to iterate, per frame, a value between the start and end values I specify.
Apologies if there’s a simpler way to go about this… it’s pretty hacked together. Here’s my code:
////////////////////////////////
// Setup:
//
// 1. Marker must be set at point that camera crosses the layer.
// 2. There must always be a marker at the beginning and end of the layer, crossing these returns an error.
// 3. The final evaluation (increasing or decreasing value), + or – will need to be set manually.affectedProperty = mask(“Mask 1”).maskExpansion; // Property to affect – can only be single dimension
cameraThresh = 500; // Z depth threshold
propertyStartValue = 0; // Property start value
propertyEndValue = 100; // Property end value
// End User Variables, Find difference between values
propertyDifference = Math.abs(propertyEndValue – propertyStartValue);
// Find out how many frames until cameraThresh = 0
closestKeyframe = this_layer.marker.nearestKey(time);
if (closestKeyframe.time <= time) { // Closest marker is before now, or now
closestKeyframe = this_layer.marker.key(closestKeyframe.index+1); // Choose next keyframe, due to above
}
remainingFrames = timeToFrames(closestKeyframe.time) – timeToFrames(time);
// Find out how much to increase the value per frame
propertyShift = propertyDifference / remainingFrames;
// If the camera threshold is met, adjust value incrementally per frame
if (length(this_layer.position[2], thisComp.layer(“motionCam”).position[2]) < cameraThresh) {
[affectedProperty + propertyShift];
} else {
[propertyStartValue];
}
////////////////////////////////
I think the problem lies in this line:
propertyShift = propertyDifference / remainingFrames;
When the camera crosses the threshold, currently, the value drops suddenly to 0, then only increases when it’s a few frames from the marker… and at tiny values (.02, etc)
AE also tells me I don’t have another marker often. That’s why I set one at the beginning and end of the layer… but it’s not a good solution apparently.
Thanks!
Alex