Introduction to Pixel Bender and Shader for Flash Player 10

Working with bitmaps
Jean André Mas
Graphic designer converted since 2004 to coding. I play around with C++, OpenGL, Java, Javascript, AND Actionscript.
My website: ASWC
var shader:Shader;
var bitmap:Bitmap = new Bitmap(new Autumn(0,0));
addChild(bitmap);
function onLoadComplete(event:Event):void {
shader = new Shader(loader.data);
stage.addEventListener(MouseEvent.CLICK, addBender);
}
function addBender(e:MouseEvent):void{
shader.data.src.input = new Autumn(0,0);//set the input
var shaderjob:ShaderJob = new ShaderJob(shader, bitmap.bitmapData);//apply the filter on the bitmapdata of our bitmap
shaderjob.start();
}
And here is the result, click once to apply the filter:
This alters directly the BitmapData pixels. Another way is to create a ShaderFilter which you can then apply to any DisplayObject:
var shaderfilter:ShaderFilter;
function onLoadComplete(event:Event):void {
shader = new Shader(loader.data);
shaderfilter = new ShaderFilter(shader)
stage.addEventListener(MouseEvent.CLICK, addBender);
}
function addBender(e:MouseEvent):void{
bitmap.filters = [shaderfilter];
}
Here is the result:
Now that's not a lot of code. We could now tween properties using either tweens or timer or enterframe events and get quite impressive results. I'll leave that for you to experiment and come out with your own solutions. But to get to the point I wrote a little class (see zip file) that does just that so let's try on some filters here:
The code for all this is quite simple and it's about the same for all these examples, only the property being animated changes. You can just download all the files and play with all this. Just remember that the property must match the property defined in the Pixel Bender.
import ASWC.*;
var shader:ShaderExtended = new ShaderExtended("cone.pbj");
shader.addEventListener(ShaderExtended.SHADER_READY, get_filter);
var bitmap:Bitmap = new Bitmap(new Tree(0,0));
addChild(bitmap);
function get_filter(e:Event):void{
stage.addEventListener(MouseEvent.CLICK, addBender);
}
function addBender(e:MouseEvent):void{
shader.setInput({src:new Tree(0,0)});
shader.animate(30, null, { offset:[10,0]}, bitmap.bitmapData, false);
}
That's all for this introduction to Pixel Bender and Shader. Contact me if you have any questions and don't hesitate to leave a comment!
Spread The Word
Attachments
6 Responses to "Introduction to Pixel Bender and Shader for Flash Player 10" 
|
said this on 05 May 2009 4:57:05 PM CST
fantastic information, Th
|
|
said this on 22 Jul 2010 4:08:11 PM CST
A wealth of information.
|
|
said this on 09 Sep 2010 2:50:40 AM CST
very very informative, I
|
|
said this on 19 Nov 2010 8:00:06 PM CST
Your the only person to e
Thanks :)! |
|
said this on 12 Jan 2011 4:42:40 AM CST
Thanks for the graet tuto
|


Author/Admin)
