Avg. Rating 5.0

Problem

If you are not embedding your Pixel Bender, you'll need to load it dynamically and apply it to a display object. This is just a sample code shows you how.

Solution

Use an URL Loader object to load the bytecode. Once it is loaded, use the bytecode to create a Shader object. Create a ShaderFilter object using the Shader object and apply it to a Flash Display object using the filter property setter.

Detailed explanation

There is a step by step walk through in part 5 of my intro tutorial at: http://www.adobe.com/devnet/pixelbender/articles/creating_effects_pt05.html

var urlRequest:URLRequest = new
URLRequest("PixelBenderToLoad.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 );
    displayObject.filters = [ shaderFilter ];
}

replace PixelBenderToLoad.pbj with the Pixel Bender pbj file that you want to load and replace displayObject with the name of the instance of the Display Object that you want to apply a filter to.


+
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.

Report abuse

Related recipes