AE Tips and Tricks

Friday, November 30, 2007

Advanced Displacement Maps

I've recently seen a lot of questions about setting up and using displacement maps in After Effects. To help better understand how to use the effect I thought I'd bring back a sample project from 2002 using AE 5.

Basic displacement
Displacement uses the color channels of a layer to move pixels. The amount of movement is directly proportional to the pixel value. If you're working in 8 bit and in Y only then a pixel value of 0 moves the pixel you're displacing the maximum distance down while 255 moves the pixels up. Gray doesn't move pixels. The example to the right has Y displacement set to 40 pixels.

Complex disteortion
Let's say you wanted to create a spherical distortion. You could create a fancy gradient in Photoshop, but you’d need to spend a long time experimenting with the proper color values or doing some fairly complex math. There are 2 easy ways to create this gradient. The first, and most accurate, would be to use a 3D app and light a sphere using red and negative red lights on the left and right with green and negative green on the top and bottom. The second option, and the one used in the sample project, is to light a solid in AE with red and green lights in the same way you'd light the sphere in a 3D app.

Here's a displacement project that uses the color channels of a pre-comp created with the technique I just discribed. The first thing to do when you start experimenting with the project is to turn the red pair of lights on and off. In these examples I’ve set vertical displacement to green and 40 pixels. Horizontal displacement is turned off. As you can see, there is no difference in vertical displacement when I turn on the red lights. Setting the Horizontal displacement to Red and animating the colored pairs of lights gives us this keen little movie.

I hope you’ll find the project enlightening. If I get time I may try and create a more in depth video tutorial on displacement.

Wednesday, November 21, 2007

Square Wheels

There was a request recently for help rolling a square object along a floor so that the sides stayed in contact with the floor plane. I came up with a fairly simple solution by adding expressions to the anchor point and position property of a square and then rolling it along by adjusting rotation.

The approach:

The easiest way to rotate on a corner is to simply move the anchor point to the corner. The challenge is to switch corners automatically every 90º. If I divide rotation by 90 and use Math.floor(value) I can create a counter that goes from 0 to 3 in the first rotation. To make the number repeat I'll use the % 4 function. So far the expression looks like this:

count = Math.floor(rotation / 90);
num = count;
val = ( num % 4);

To keep the value from going negative I can add 100 to the number (num). This will give me 25 rotations in the negative direction before my value goes negative.

Now it's just a matter of adding a few if else statements. The final Anchor point expression looks like this:

count = Math.floor(rotation / 90);
num = count + 100;
val = ( num % 4);

if (val == 0) {
  [width, height]
else if (val == 1) {
  [width, 0]
else if (val == 2) {
  [0, 0]
  [0, height]

I used width and height instead of the values for the layer so the expression would work with any square layer of any size.

The last step is to modify the position of the X value by multiplying the rounded count by the width of the layer. The expression should be fairly easy to understand and looks like this:

val = Math.floor(rotation / 90);
x = value[0] + val * width;
y = value[1]; 
[x, y]

The only thing left to do is animate the rotation value. You'll find a project for AE CS3 here I hope you enjoy...