Monday 28 January 2013

Extra Credit


Extra Credit

Congratulations on making it this far. The remaining
examples build on concepts covered earlier, but I have
saved them for this section because they are particularly
tricky or involve some complex math. I’m presenting them
mainly to entice you to take some time and fi gure out how
they work.

Fade as Turn Away From Camera

Let’s briefl y return to the world of layer space transforms
and examine a simple idea that requires only a short
expression, but one with a lot of complicated vector math
going on under the hood. The idea is that you want a 3D
layer to fade out as it turns away from the camera. This
needs to work not only when the layer rotates away from
the camera, but also if the camera orbits the layer. And of
course, it should still work if either the layer or the camera
happens to be the child of another layer. Take a look at an
expression for Opacity that will accomplish this:
minAngle = 20;
maxAngle = 70;
C = thisComp.activeCamera.toWorld([0,0,0]);
v1 = normalize(toWorld(transform.anchorPoint) – C);
v2 = toWorldVec([0,0,1]);
angle = radiansToDegrees(Math.acos(dot(v1, v2)));
ease(angle, minAngle, maxAngle, 100, 0)
The fi rst two lines just create two variables (minAngle and
maxAngle) that establish the range of the effect. Here you
set their values so that when the layer is within 20 degrees
of facing the camera, it will be at 100% Opacity and its
Opacity will fade from 100% to 0% as the angle increase to
70 degrees. Beyond 70 degrees, Opacity will be 0%.
Next you create a variable C that represents the position of
the comp’s active camera in world space. You’ve seen this
before, in the expression where the layer fades as it moves
away from the camera.

Now starts the vector math. Things get a little bumpy from
here. Briefl y, a vector is an entity that has a length and a
direction, but has no defi nite position in space. I like to
think of vectors as arrows that you can move around, but
they always keep the same heading. Fortunately the expression
language provides a pretty good arsenal of tools to
deal with vectors.
To fi gure out the angle between the camera and the layer
with the expression, you’re going to need two vectors. One
will be the vector that points from the center of the layer
towards the camera. The other will be a vector that points
outward from the center of the layer along the Z axis.
To calculate the fi rst vector (variable v1), convert the
layer’s anchor point to world space coordinates and
subtract from that value the location of the camera in
world space. What you’re doing is subtracting two points
in space. Remember, in After Effects, each 3D position
in space is represented by an array: [x,y,z]. The result of
subtracting two points like this gives you a vector. This
vector has a magnitude representing the distance between
the two points and a direction (in this case, the direction
from the layer to the camera). You can use normalize() to
convert the vector to what is known as a unit vector, which
maintains the direction of the original vector, but sets its
length to 1. This simplifi es the upcoming determination of
the angle between two vectors.
Next you create the second vector (variable v2). You can
create the necessary unit vector in one step this time by
using toWorldVec([0,0,1]) to create a vector of length 1
pointed along the layer’s Z axis.
Now you have your two vectors. To calculate the angle
between two vectors, you use what is known as the vector
dot product. I won’t go into great detail about how it works
(there’s a lot of information on the Internet if you’re
curious), but it turns out that if you use unit vectors, the
vector dot product will directly give you the arc cosine of
the angle between the two vectors. Luckily, the expression
language gives us a built-in function, dot(), to calculate the
dot product.


So now you can calculate the angle you need (and store
it in variable angle) in three steps. First you take the dot
product of the two vectors, producing the arc cosine of
the angle. Then you use Math.acos() to convert that to an
angle (see the “Math Object” section of the JavaScript guide
for more information). Because the result of Math.acos()
will be in radians, you need to convert it to degrees so
that it will be in the same units as the limits minAngle and
maxAngle. Fortunately, the expression language provides
radiansToDegrees() to make the conversion.
The fi nal step is to use the interpolation method ease() to
smoothly execute the fade as the angle increases.

No comments:

Post a Comment