11 January 2010
Some familiarity with ActionScript 3.
Beginning
In this article, you'll add a type of interactivity that animates a Pixel Bender filter to correspond to changing mouse positions. This is the eighth 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 update the displacement filter to enable it to move in all directions. You also updated the parameters to make the displacement filter effect more dramatic when you moved the slider controls.
In this section, you'll update the ActionScript code in the Flash file to control the displacement filter parameter with changing positions of the mouse.
Before you begin, if you'd like to continue experimenting with the previous version of the project, you can add a second slider to control the float2 parameter that you just added to the Displacement filter. That process is fairly straightforward and it is always a good idea to practice when learning a new concept.
In this part of the series, we'll discuss adding another type of interactivity to control the filter effect; this time you'll track the placement of the cursor and use it to animate the displacement filter.
Before updating the ActionScript, set the file up in Flash in preparation for the code edits. Follow these steps:
(If you are just beginning to follow along with this section and didn't complete the files in the previous parts of this series, download the sample files provided. Uncompress the ZIP file and save the contents into a folder named pixel_bender that you create on your desktop. Open the file named exercise8A.fla to begin following along at this point.)
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 ];
}
Follow these steps to remove the slider from the Flash file:
import fl.events.SliderEvent;
applyFilter function. After making these changes, the applyFilter function should look like this:function applyFilter( event:Event ):void
{
urlLoader.removeEventListener( Event.COMPLETE, applyFilter );
shader = new Shader( event.target.data );
shaderFilter = new ShaderFilter( shader );
flower.filters = [ shaderFilter ];
}
updateFilter function:function updateFilter( event:SliderEvent ):void
function updateFilter( event:Event ):void
amount parameter in the Pixel Bender filter. You need to disable the code, but don't delete this line; you'll re-enable it later:shader.data.amount.value = [amount_slider.value];
Instead, just type two forward slashes before the line to comment it out:
// shader.data.amount.value = [amount_slider.value];
After making these changes, your ActionScript code should look like this:
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 );
flower.filters = [ shaderFilter ];
}
function updateFilter( event:Event ):void
{
// shader.data.amount.value = [amount_slider.value];
flower.filters = [ shaderFilter ];
}
Now that you've removed the slider control, the next task involves adding code to control the filter effects by tracking the cursor. This step allows the filter to be reapplied and creates a more interactive experience for the user:
applyFilter function, before the closing curly brace:flower.addEventListener( Event.ENTER_FRAME, updateFilter );
updateFilter function:// shader.data.amount.value = [amount_slider.value];
flower.filters = [ shaderFilter ];
updateFilter function to look like this:var xOffset:Number = flower.width/2 - flower.mouseX;
var yOffset:Number = flower.height/2 - flower.mouseY;
shader.data.amount.value = [ xOffset, yOffset ];
flower.filters = [ shaderFilter ];
The code above retrieves the current mouse position, determines its offset (based on the center of the image), and uses the value to set the offset as the parameter value to the Pixel Bender filter.
Note: Because Flash player does not enforce the minValue or maxValues on the filter, it is a best practice to take these values into account in your ActionScript code.
In this section, you'll update the code to enable the filter to be constrained by the minimum and maximum values that are set in the Pixel Bender metadata. Follow these steps:
updateFilter function, to match the example below:function updateFilter( event:Event ):void
{
var xOffset:Number = flower.width/2 - flower.mouseX;
var yOffset:Number = flower.height/2 - flower.mouseY;
xOffset = Math.min( xOffset, shader.data.amount.maxValue[0] );
xOffset = Math.max( xOffset, shader.data.amount.minValue[0] );
yOffset = Math.min( yOffset, shader.data.amount.maxValue[1] );
yOffset = Math.max( yOffset, shader.data.amount.minValue[1] );
shader.data.amount.value = [ xOffset, yOffset ];
flower.filters = [ shaderFilter ];
}
This code forces the parameter values set by the ActionScript code to stay within the ranges specified by the Pixel Bender code.
After testing and reviewing the code from the SWF, continue with Part 9 in this series, where you'll learn how to create a Pixel Bender kernel that integrates values from multiple image sources.
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 |