Featuring mix(), clamp(), and dot().

Very often I need a simple linear-gradient in After Effects, and end up sing the four-color gradient because that’s all I can find. And so, here, for your perusal, is a Pixel Bender implementation of a Linear Gradient Generator.

After that, we’ll see the code, and after that, a *quick* review of the math.

Here is the .swf. (With some improvements on the Pixel Bender viewer — you can drag any **float2** parameters as points, now.)

Isn’t that fun? Well, I’m easily amused.

### The Code

Here’s the Pixel Bender code.

kernel Gradient
{
output pixel4 dst;
parameter float2 p1<minValue:float2(0,0);maxValue:float2(400,400);defaultValue:float2(10,10);>;
parameter float2 p2<minValue:float2(0,0);maxValue:float2(400,400);defaultValue:float2(130,130);>;
parameter float3 color1<defaultValue:float3(0,0,0);>;
parameter float3 color2<defaultValue:float3(1,1,1);>;
void evaluatePixel()
{
float2 co = outCoord();
// shift everything relative to p1.
float2 uv = co - p1;
float2 xy = p2 - p1;
// the math.
float g = dot(uv,xy) / dot(xy,xy);
g = clamp(g,0.0,1.0);
// the color.
dst.a = 1.0;
dst.rgb = mix(color1,color2,g);
}
}

### The Math

I’m ok at math, but not always *fluent* in it. I can remember what a matrix is, but not necessarily exactly what a cross-product is. Maybe you’re like me. In which case this *quick* run through the math and logic behind the gradient fill will be quite useful in your own Pixel Bender bending!

And be assured, a few pages of high-school-algebra-style scribblings, and the occasional google (How do I invert a rotation again? What’s the formula for perspective?) really can lead to workable results. Good clean fun!

Let’s go.

Consider a trivial gradient, where our two control points are at (0,0) and (1,0). That would be easy! Just take the x-value of any pixel, and that’s your color. (That is, the position on the ramp between your two gradient colors.)

By the way, this picture is from Ron Avitzur’s “Graphing Calculator”. The story of its development is legendary; less well known is that it’s actually a very powerful and useful tool.

I found the easiest way to think about a general two-point gradient was to ask, “How do we rotate and scale our control points back to this trivial gradient?” And let’s assume that the first control point is always at (0,0).

Here is matrix for a basic rotation, which moves any input point by a rotation around (0,0):

[1]

A rotate-*and-scale* matrix looks like that, times a constant on each element.

We want to think of our general gradient as a transformation on the trivial gradient. Let’s say our second control point is (X,Y), and our first control point is (0,0) as mentioned above. The rotate-and-scale matrix being applied, conceptually, is:

[2]

To figure out the 0-to-1 trivial position of any of our input points, we need to invert it. A matrix inversion looks like:

[3]

So, we invert the second equation [2] by plugging it in to [3] and get:

[4]

Now, let’s call each pixel position of our output (U,V). For our gradient, we actually don’t care about the resulting Y position; just X gives us our gradient value. So we get:

[5]

Now, the definition of the dot product of (A1,A2,A3,…) and (B1,B2,B3,…) is (A1B1 + A2B2 + A3B3 + …). So, from the above, UV+XY is, conveniently, (U,V) dot (X,Y). Similarly, X^{2} + Y^{2} is (X,Y) dot (X,Y).

And now, when you read this part of the code a second time, it should be much clearer what’s going on:

void evaluatePixel() {
float2 co = outCoord();
// shift everything relative to p1.
float2 uv = co - p1;
float2 xy = p2 - p1;
// the math.
float g = dot(uv,xy) / dot(xy,xy);
g = clamp(g,0.0,1.0);
// the color.
dst.a = 1.0;
dst.rgb = mix(color1,color2,g);
}

Simple and useful!

Next up, soon, more cubes.