Accessibility

Table of Contents

Pixel Bender basics for Flash

Exporting and loading a kernel

Use the Export Kernel Filter for Flash Player command from the Pixel Bender Toolkit to compile and export the kernel for use in Flash Player. Kernels are exported with a file extension .pbj (see Figure 1).

Pixel Bender Toolkit File menu
Figure 1. Exporting the kernal from the Pixel Bender Toolkit

To load a Pixel Bender kernel in Flash Player, you must either embed or load the compiled kernel.

The Embed tag, newly supported in Flash CS4 Professional, instructs the ActionScript compiler to embed the Pixel Bender kernel when it creates the SWF file. The Embed tag is used with a variable definition of type, Class, as shown in the following example:

[Embed(source="channelscrambler.pbj", mimeType="application/octet-stream")]
var ChannelScramblerKernel:Class;
          

To use the kernel, create an instance of the class, in this case, ChannelScramblerFilter. The following code uses an embedded kernel to create new Shader and ShaderFilter objects, which are applied it to a MovieClip instance on the Stage:

var camellia_mc:MovieClip;


//Embed the PixelBender kernel in the output SWF
[Embed(source="channelscrambler.pbj", mimeType="application/octet-stream")]
var ChannelScramblerKernel:Class;

var shader:Shader = new Shader(new ChannelScramblerKernel() );
var shaderFilter:ShaderFilter = new ShaderFilter( shader );
camellia_mc.filters = [ shaderFilter ];
          

Using the Embed tag is typically the simplest method of loading Pixel Bender kernels, but you can also load kernels at runtime. The following example uses the URLLoader class to load a kernel:

var camellia_mc:MovieClip;

var urlRequest:URLRequest = new URLRequest( "channelscrambler.pbj" );
var urlLoader:URLLoader = new URLLoader();
urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
urlLoader.addEventListener( Event.COMPLETE, applyFilter );
urlLoader.load( urlRequest );

function applyFilter( event:Event ):void
{
	trace("apply");
	urlLoader.removeEventListener( Event.COMPLETE, applyFilter );
	var shader:Shader = new Shader( event.target.data );
	var shaderFilter:ShaderFilter = new ShaderFilter( shader );
	camellia_mc.filters = [ shaderFilter ];
}
          

Note: When you use the Embed tag, Flash CS4 Professional uses the Flex.swc library from the Flex SDK. This SDK is installed with Flash CS4, typically in the Common/Configuration/ActionScript 3.0/libs/flex_sdk_3 subdirectory. The first time you test or publish a movie using the Embed tag, you will be asked to confirm the location of the Flex SDK. You can click OK to use the Flex SDK installed with Flash CS4, or you can change the path if you prefer to use a different version of Flex. The setting used for a project can be changed later on the Advanced ActionScript 3.0 Settings dialog box, under the Library path tab. (Access the Advanced ActionScript 3.0 Settings dialog box from the Flash tab of the Publish Settings by clicking the Settings button next to the Script drop-down menu.)