11 January 2010
Some familiarity with ActionScript 3.
Beginning
In this article, you will learn how to add a parameter to a kernel so you can animate a Pixel Bender filter or adjust the effects while the filter is running. You will also learn how to use Pixel Bender's vector types to make the math easier to maintain. This is the third installment in this series of articles about using the Pixel Bender Toolkit to create visual effects with bitmap images.
In the previous section, you learned how to manipulate the red, green, and blue color channels to affect the image in the preview window. This section shows you how to leverage a powerful way to control effects: parameters. By adding parameters and metadata to filters you create with the Pixel Bender Toolkit, you can control the amount of the filter effect with a slider interface.
If you have been following along since the beginning of this series, you can continue working with the same file. Otherwise, download the sample files provided. Open the Exercise3FilterA.pbk file in the Pixel Bender IDE.
The vintage tone filter that you developed in the last part looks something like this:
<languageVersion : 1.0;>
kernel Part3Filter
< namespace : "com.adobe.devnet.pixelbender";
vendor : "Kevin's Filter Factory";
version : 1;
description : "Playing around with pixels";
>
{
input image4 src;
output pixel4 dst;
void evaluatePixel()
{
dst = sampleNearest(src,outCoord());
dst.r += 0.5;
dst.b -= 0.4;
dst.g -= 0.1;
}
}
Now that you have loaded the code (either by opening the file you created in Part 2 or by opening the sample file), you are ready to edit the code to add a parameter.
Follow these steps to add a float parameter to the filter:
output pixel4 dst;
parameter float amount;
Notice that the image doesn't change. Although it is present, the slider value is not used in the Pixel Bender kernel yet. You'll hook this up in the next section.
Now that you've added the slider, the next part involves editing the code to add the parameter to your filter to make the slider adjust the settings of the effect. Follow these steps:
dst.r += 0.5;
dst.r += (0.5 * amount);
dst = sampleNearest(src,outCoord());
dst.r += (0.5*amount);
dst.b -= (0.4*amount);
dst.g -= (0.1*amount);
As you move the slider in the right-hand panel, the image changes more significantly as the slider moves. This occurs because all three channels are now modified by the parameter. When the slider is moved to the left, there is no effect applied to the image. When the slider is moved to the right, the effect is fully applied.
In this section, you'll add metadata to the new parameter you just created. The completed version of this code is called Exercise3FilterE.pbk in the sample files. Follow these steps:
parameter float amount;
parameter float amount
<
minValue: -1.0;
maxValue: 1.0;
defaultValue: 0.0;
>;
The default position of the slider in the right hand panel is now in the middle. If you move the slider to the left, you will see the filter being applied in inverse. If you move the slider to the right, the effect is applied as it was before.
Note: Adding parameter metadata is not required; the parameter will work without it. However, if you do not set the metadata, each application will use its own default minimum, maximum, and default values. It is a best practice to always set this metadata to control parameters and get consistent results.
In this section, you'll learn how to incorporate vectors into the filter. Adding vectors is easy in the Pixel Bender Toolkit:
dst.r += (0.5*amount);
dst.b -= (0.4*amount);
dst.g -= (0.1*amount);
dst += float4(0.5*amount, -0.4*amount, -0.1*amount, 0.0);
Instead of modifying the first three values of the dst vector individually, you are now creating a new vector and adding it to dst. The extra 0.0 float value at the end is because vectors have to be the same size for the arithmetic operation to be legal. Also notice that you must type the value as 0.0 and not 0 in the code. Pixel Bender will not automatically convert types for you; you always have to use the correct types or an error will occur.
In this section, you'll learn a strategy to make the math in the equation a bit more manageable. Follow these steps:
dst += float4(0.5*amount, -0.4*amount, -0.1*amount, 0.0);
dst += float4(0.5, -0.1, -0.4, 0.0) * amount;
Note: Multiplying a vector by a number is the same thing as multiplying every element of the vector by that number.
After familiarizing yourself with the Pixel Bender interface, continue with Part 4 in this series, where you'll learn how to write the code to sample multiple pixels from the same image.
Check out the following resources to learn more about working with the Pixel Bender Toolkit:
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Permissions beyond the scope of this license, pertaining to the examples of code included within this work are available at Adobe.
| 04/10/2012 | Fast Fourier Transform (FFT) in Pixel Bender? |
|---|---|
| 04/19/2012 | Shader works in toolkit, not in Flash |
| 04/12/2012 | Pixel Bender removed from AE CS6 !? |
| 03/22/2012 | Load an HDR image into Pixel Bender Toolkit |