11 January 2010
Some familiarity with ActionScript 3.
Beginning
In this article, you'll learn how to modify the parameters in a Pixel Bender filter to create an interactive effect with a Slider component that allows users to change the effect at runtime. This is the sixth 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 apply a Pixel Bender filter to Flash content by adding some ActionScript code to link to the PBJ filter file. You also used the Shader and the ShaderFilter classes to create a visual effect.
In this section, you'll use the filter you created in Part 3, named Exercise3Filter.pbj. Using the provided instructions, you'll update the project to control the filter effect with a slider that appears in Flash Player 10. You'll also use the parameter metadata that you set on the filter to set the minimum, maximum, and default properties of Flash UI components.
If you didn't complete Part 3 of this article series, download the sample files provided and save them in a folder called pixel_bender on your desktop. Then launch Flash CS4 and open the exercise6A.fla file. Otherwise, you can continue working with the file named Exercise5.fla you've previously created.
In the Flash authoring environment, you should see the following ActionScript code in the Script window of the Actions panel when Frame 1 of the Timeline is selected:
var urlRequest:URLRequest = new URLRequest("Exercise2Filter.pbj");
var urlLoader:URLLoader = new URLLoader();
urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
urlLoader.addEventListener( Event.COMPLETE, applyFilter );
urlLoader.load(urlRequest);
function applyFilter( event:Event ):void {
urlLoader.removeEventListener( Event.COMPLETE, applyFilter );
var shader:Shader = new Shader( event.target.data );
var shaderFilter:ShaderFilter = new ShaderFilter( shader );
flower.filters = [ shaderFilter ];
}
The code example shown above loads a Pixel Bender bytecode file and applies it to the image that has the following instance name: flower.
Before moving on to the next section, edit the code in the first line of code. Change the filename in the URLRequest from Exercise2Filter.pbj to Exercise3Filter.pbj.
In this section, you'll add a Flash UI component to the Stage to make the movie interactive. Follow these steps:
In this section, the goal is to modify the filter that is applied to the flower image. This requires reapplying the filter by resetting it on the image. To ensure that the code continues to work, you'll create two global variables to hold the Shader and ShaderFilter objects:
applyFilter function declaration and type the following two lines of code:var shader:Shader;
var shaderFilter:ShaderFilter;
applyFilter function, remove the var keyword and the type definition as shown in the two lines below (highlighted in red):var shader:Shader = new Shader( event.target.data );
var shaderFilter:ShaderFilter = new ShaderFilter( shader );
After making these changes, the ActionScript code in Frame 1 should look like this:
var urlRequest:URLRequest = new URLRequest("Exercise3Filter.pbj");
var urlLoader:URLLoader = new URLLoader();
urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
urlLoader.addEventListener( Event.COMPLETE, applyFilter );
urlLoader.load(urlRequest);
var shader:Shader;
var shaderFilter:ShaderFilter;
function applyFilter( event:Event ):void
{
urlLoader.removeEventListener( Event.COMPLETE, applyFilter );
shader = new Shader( event.target.data );
shaderFilter = new ShaderFilter( shader );
flower.filters = [ shaderFilter ];
}
The parameter metadata that you added to the filter in Part 3 is accessible in Flash Player at runtime once the filter has been loaded. In this section, you'll use this metadata to control animation of the filter. Follow these steps:
import fl.events.SliderEvent;
applyFilter function, above the closing curly brace:amount_slider.minimum = shader.data.amount.minValue;
amount_slider.maximum = shader.data.amount.maxValue;
amount_slider.value = shader.data.amount.defaultValue;
amount_slider.snapInterval = Math.abs(amount_slider.minimum -
amount_slider.maximum) / 100;
amount_slider.liveDragging = true;
amount_slider.addEventListener( SliderEvent.CHANGE, updateFilter );
The code example above initializes the shader object and uses the parameter metadata to set its upper and lower bounds, as well as its initial value. The last line is an event listener that calls the updateFilter function when the slider changes.
Since the code shown above references the updateFilter function, the next step is to add the code for that function.
applyFilter function, add the following code:function updateFilter( event:SliderEvent ):void
{
shader.data.amount.value = [amount_slider.value];
flower.filters = [ shaderFilter ];
}
Note: In Flash, Pixel Bender parameters are always set as arrays, even if they contain a single value.
In this section, you'll begin working with the displacement filter. (In Part 7, you'll add more code to improve its capabilities.) To get started, edit the first line of code to replace the reference to the file in the URLRequest object from Exercise3Filter.pbj to Exercise4Filter.pbj:
Note: In Flash Player, when pixels are sampled off the edge of an image, the background color (white) is returned instead of transparent black. This causes the edge of the image to appear lighter, as shown in the screenshot above.
Take a moment to review your code. The ActionScript code on Frame 1 should look like this:
import fl.events.SliderEvent;
var urlRequest:URLRequest = new URLRequest("Exercise4Filter.pbj");
var urlLoader:URLLoader = new URLLoader();
urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
urlLoader.addEventListener( Event.COMPLETE, applyFilter );
urlLoader.load(urlRequest);
var shader:Shader;
var shaderFilter:ShaderFilter;
function applyFilter( event:Event ):void
{
urlLoader.removeEventListener( Event.COMPLETE, applyFilter );
shader = new Shader( event.target.data );
shaderFilter = new ShaderFilter( shader );
// set up slider
amount_slider.minimum = shader.data.amount.minValue;
amount_slider.maximum = shader.data.amount.maxValue;
amount_slider.value = shader.data.amount.defaultValue;
amount_slider.snapInterval = Math.abs(amount_slider.minimum - amount_slider.maximum) / 100;
amount_slider.liveDragging = true;
amount_slider.addEventListener( SliderEvent.CHANGE, updateFilter );
flower.filters = [ shaderFilter ];
}
function updateFilter( event:SliderEvent ):void
{
shader.data.amount.value = [amount_slider.value];
flower.filters = [ shaderFilter ];
}
After testing and reviewing the code from the Flash file, continue with Part 7 in this series, where you'll learn how to update the way the displacement filter performs and improve the resulting effect.
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 |