How to load Pixel Bender in Flash Player 10

May 19th, 2008

Hey so I spent most of the weekend trying to get Senocular’s tutorial on loading .pbj into flash player 10.

For some reason it wasn’t working for me so I decided to download Pixel Bender Preview Release and copy the file’s source code and export it (File > Export Pixel Bender…for Flash) and it worked so I wanted to post it. Just in case some one had the same problem.

AS FILE:

[as]package {
import flash.display.*;
import flash.events.*;
import flash.net.URLLoader;
import flash.net.URLLoaderDataFormat;
import flash.net.URLRequest;
import flash.utils.ByteArray;
import flash.utils.getTimer;

public class astro2 extends Sprite
{

// image for shader fill
private var _bitmap:Bitmap;

// shader fill shader; it will use
// the image as an input
private var _shader:Shader;

// loader to load pixel bender bytecode
// for shader instance
private var _shaderLoader:URLLoader

private var _loader:Loader

public function astro2()
{
//load image
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
_loader = new Loader();
_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onImgLoad, false, 0, true);
_loader.load(new URLRequest(”http://bp2.blogger.com/_Y8m29ZLX5ag/RarR3rcAVwI/AAAAAAAAAL4/dtA7xOMrI4g/s400/ASTRO_jpg.jpg”));

}

//once loaded load Pixel Bender File
private function onImgLoad(evt:Event):void
{
_bitmap = Bitmap(_loader.content);

_shader = new Shader();
_shaderLoader = new URLLoader();

_shaderLoader.dataFormat = URLLoaderDataFormat.BINARY;
_shaderLoader.addEventListener(Event.COMPLETE, shaderLoaded);
_shaderLoader.load(new URLRequest(”test.pbj”));
}

// shaderLoader complete handler
private function shaderLoaded(event:Event):void {

// set up shader

_shader.byteCode = _shaderLoader.data as ByteArray; // error if invalid
_shader.data.src.input = _bitmap.bitmapData; // image input
_shader.data.size.value = [100]; // constant size
_shader.data.fade.value = [.5]; // constant fade value

// draw the shader every frame
addEventListener(Event.ENTER_FRAME, drawKaleidoscope);

}

// enterframe handler
private function drawKaleidoscope(event:Event):void {
// base position on mouse location
_shader.data.position.value = [mouseX, mouseY];
// rotate constantly over time
_shader.data.angle.value = [getTimer()/500];

// draw a rectangle with the shader fill
graphics.clear();
graphics.beginShaderFill(_shader);
graphics.drawRect(0,0,300,250);
}
}
}[/as]

and the Pixel Bender code:

kernel Kaleidoscope
< namespace : “Petri Leskinen”;
version : 1;
vendor : “”;
description : “kaleidoscope -effect, rectangular”;
>
{
parameter float4 fadeColor
<
minValue: float4(0,0,0,0);
maxValue: float4(1,1,1,1);
defaultValue: float4(0,0,0,1);
description: “Fade color”;
>;
parameter float2 position
<
minValue: float2(0,0);
maxValue: float2(2880,2880);
defaultValue: float2(250,280);
description: “Center point”;
>;

parameter float size
<
minValue: float(1);
maxValue: float(500);
defaultValue: float(75);
description: “Size”;
>;

parameter float fade
<
minValue: float(0.5);
maxValue: float(1.0);
defaultValue: float(1.0);
description: “Fade”;
>;

parameter float angle
<
minValue: float(-3.14159);
maxValue: float(3.14159);
defaultValue: float(0.5);
description: “Rotation angle”;
>;

input image4 src;
output pixel4 dst;

void evaluatePixel()
{
float sina = sin(angle);
float cosa = cos(angle);

float2 newC = position – size/2.0* float2(cosa+sina, cosa-sina);

// mx matrix for rotation and scaling
// mxR reverse transformation
float2×2 mx = float2×2( cosa,sina,-sina,cosa) /size;
float2×2 mxR = float2×2( cosa,-sina,sina,cosa) *size;

// coordinate p1 on the new coordinate system,
// on the center area 0.0 < p1.x < 1.0 , 0.0 < p1.y < 1.0
float2 p1 = mx * (outCoord() -newC);

// p2 integer part, p1 only decimal part,
// which maps every pixel to the center area
float2 p2 = floor(p1);
p1 = fract(p1); // p1 -= p2;

// p3 dislocation for mirroring,
// mirrored if integer part odd
float2 p3 = 1.0-2.0*p1;
p1.x += mod (p2.x,2.0) > 0.0 ? p3.x : 0.0 ;
p1.y += mod (p2.y,2.0) > 0.0 ? p3.y : 0.0 ;

// reverting to the main coordinate system, sampling and mixing the pixel
p1 = newC + mxR*p1;
dst = mix( fadeColor, sampleLinear(src,p1) ,
pow ( fade,abs(p2.x)+abs(p2.y)) );

}
}

Entry Filed under: as3, flash

Leave a Comment

Required

Required, hidden

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Trackback this post  |  Subscribe to the comments via RSS Feed