One of the things which I abhor about modeling in Papervision3D is texture baking. And if you’ve been in the business as long as I have you know why…it’s a labor intensive nightmare…and you can’t ever get it right. But with pixel bender you don’t have to bake your textures anymore and you can adjust your parameters dynamically using ActionScript.
Adding Pixel Bender to Papervision3D was a three step process:
- Creation of a Pixel Bender Filter
- Creation of a Pixel Bender Bitmap File Material Class
- Creation of a Pixel Bender Bitmap Material Class
Before you start coding. Make sure you’ve switched to the Flash 10 player!!!
Step 1: In the previous post several pixel builder filters were presented.
To create a dynamic light baker filter the simple light filter was modified by throwing out all the pow and exponent methods. The big trick is that exponential behavior can be mimicked by using inverse of distance –now that’s a processor saver. The final algorithm use is shown below:
The pixel bender algorithm is easily implemented using the code below:
float2 outcoord = outCoord();
float attn = (brightness/((distance(outcoord, center)+radius)));
dst = attn* sampleNearest(src, outcoord);
In addition to the modification mentioned above the alpha component is split off and set to one. This keeps the images from losing its alpha as you reduce brightness.
Step 2: Next you must create a Pixel Bender Bitmap File Material Class. This is simply done by making a copy of PV3D’s BitmapFileMaterial class and renaming it BitmapBendMaterial class and after changing the constructor and class names extend this class by the BitmapPixelMaterial class.
BitmapBendMaterial extends BitmapPixelMaterial
You’ll now create the BitmapPixelMaterial class.
Step 3: Make a copy of the PV3D BitmapMaterial class and name it BitmapPixelMaterial. Add the appropriate import statements and the pbj and image embed methods and create a shader as shown below:
private var ShaderClass:Class;
private var myImage:Class;
private var shader:Shader
Next incorporate the code to get your shader and image working together:
shader = new Shader(new ShaderClass());
shader.data.center.value = [400, 306];
shader.data.brightness.value = ;
shader.data.radius.value = ;
var image:Bitmap = new myImage();
image.filters = [new ShaderFilter(shader)];
shader.data.src.input = image.bitmapData
Finally you must change your fill method to a shader fill method with a matrix method
That’s it! To check out the wrapper code download the source or click the more button below: