How to use custom Shaders in Astro(Flash 10)

Here is a simple example using the sample exposure shader that comes with the Adobe’s PixelBender tutorial.


(Moving the mouse from horizontally will change the exposure value)

Firstly, you need the PixelBenderToolkit(http://labs.adobe.com/wiki/index.php/Pixel_Bender_Toolkit).

I used the Exposure sample code from Adobe’s Pixel Bender tutorial (http://labs.adobe.com/wiki/index.php/Pixel_Bender_Toolkit:Tutorial)

[code lang="cpp"]

kernel NewFilter
< namespace : "your namespace";
vendor : "your vendor";
version : 1;
description : "your description";
>
{
input image4 src;
output pixel4 dst;
parameter float exposure
<
minValue:float(-0.5);
maxValue:float(0.5);
defaultValue:float(0.0);
>;
void
evaluatePixel()
{
float4 inputColor = sampleNearest(src, outCoord());
dst.rgb = pow(inputColor.rgb, float3(1.0 - exposure));
dst.a = inputColor.a;
}
}
[/code]

On PixelBender Toolkit, you can export it as Byte Code Filter for Flash from the File menu.

Exporting it, you will have a .pbj file to be loaded as ByteArray from Flash just like this.

[code lang="actionscript"]
var urlLoader:URLLoader = new URLLoader();
urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
urlLoader.addEventListener(Event.COMPLETE, urlLoaderComplete);
urlLoader.load(new URLRequest("exposure.pbj"));
[/code]

And after that, creating a Shader instance, and also the ShaderFilter to be applied on a DisplayObject.

[code lang="actionscript"]
private function urlLoaderComplete(ev:Event):void
{
shader = new Shader(ev.currentTarget.data as ByteArray);
shaderFilter = new ShaderFilter(shader);
filters = [shaderFilter];
}
[/code]

Once you have the shader instance, you can change the shader parameters by doing the following.
[code lang="actionscript"]

shader.data.exposure.value = [0.5];

filters = [shaderFilter]; //re-applying the filter

[/code]

Easy huh?

Here is all the source from this little tutorial.

Enjoy.

(update. Just because Zeh said to)

One more example using the TwirlFilter created by Elba Sobrino.
Works the same way, move the mouse along the image.

Vote in HexoSearch Vote

3 Comments »

  1. Fnk (alpha) » Other news » Filtering images with Flash 10 said,

    May 20, 2008 @ 2:31 am

    [...] Bender filters to make transformations to an image (actually working like pixel shaders). Keita has a nice post showing how it’s done, also with downloadable [...]

  2. xsvdo said,

    July 24, 2008 @ 8:42 am

    I updated flex builder 3 to work with flash player 10, loaded your code, previewed in a browser and the sunset image comes up. However, when I move my mouse over the image, I do _not_ see the exposure filter working.

    I am not getting any errors when building the swf and the sample on this web page works as expected. I just can not compile it and make it work.

    Any suggestions?

  3. xsvdo said,

    July 24, 2008 @ 8:54 am

    my bad. I was running the file locally. Once I posted to a web server, it worked (should have caught URLRequest the first time)! Thanks for the sample files.

RSS feed for comments on this post · TrackBack URI

Leave a Comment