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.