mike chambers | about

Embedding Pixel Bender Filters within a SWF

Monday, September 8, 2008

I was just watching Lee Brimelow's excellent video introductions to creating and using Pixel Bender filters in Flash Player 10.

In his second video, he shows how to use a custom Pixel Bender filter within Flash Player 10. One issue that Lee had was that you have to load the filter at runtime in order to use it.

I have modified Lee's example to allow the filter to be embedded within the SWF, and not require it to be dynamically loaded at runtime.

package
{
    import flash.display.*;
    import flash.events.*;
    import flash.filters.*;
    import flash.net.*;
    import flash.utils.ByteArray;

    // SWF Metadata
    [SWF(width="600", height="400", backgroundColor="#000000", framerate="30")]

    public class PB extends Sprite
    {
        //the file that contains the binary bytes of the PixelBender filter
        [Embed("testfilter.pbj", mimeType="application/octet-stream")]
        private var TestFilter:Class;     

        //The image to display the filter on
        [Embed(source="image.jpg")]
        private var image:Class;

        private var im:Bitmap;

        public function PB():void
        {
            im = new image() as Bitmap;
            addChild(im);

            //Pass the loaded filter to the Shader as a ByteArray
            var shader:Shader = new Shader(new TestFilter() as ByteArray);

            shader.data.amount.value = [100];
            var filter:ShaderFilter = new ShaderFilter(shader);

            //add the filter to the image
            im.filters = [filter];
        }

    }
}

Check out the Flex Cookbook for some good information on How to embed any type of data within a SWF.

Make sure to check out Lee's video tutorial on using Pixel Bender filters in Flash Player 10.

You can see an example of the filter here.

You can find more information on Pixel Bender on Labs.

comments powered by Disqus
twitter github flickr behance rss