Adding Pixel Bender to Papervision3D

Intro

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.

Dynamic Shading using Pixel Bender

Dynamic Shading using Pixel Bender

Demo

Source

YouTube http://www.youtube.com/watch?v=i8BnSqf0Z_g

Discussion

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
Adding Pixel Bender to PV3D

Adding Pixel Bender to PV3D

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.

Pixel Bender Filters and Algorithms

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:

lively3d Light Source for Texture Baking

lively3d Light Source for Texture Baking

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);
dst.a=1.0

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:

[Embed (source="filters/livelyLight.pbj",
mimeType="application/octet-stream")]
private var ShaderClass:Class;

[Embed (source="assets/images/gainesHouse512.jpg")]
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 = [150];
shader.data.radius.value = [100];

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

graphics.beginShaderFill(shader,_localMatrix)

That’s it! To check out the wrapper code download the source or click the more button below:

Wrapper File

package
{
import flash.display.*;
import flash.events.Event;

import org.papervision3d.cameras.Camera3D;
import org.papervision3d.lights.PointLight3D;
import org.papervision3d.materials.BitmapBendMaterial;
import org.papervision3d.materials.BitmapFileMaterial;
import org.papervision3d.materials.ColorMaterial;
import org.papervision3d.materials.shadematerials.*;
import org.papervision3d.objects.primitives.XML3DSPrimitive;
import org.papervision3d.scenes.Scene3D;
import org.papervision3d.view.Viewport3D;
import org.papervision3d.view.BasicView;

//Create WirePlane class

public class AddingPixelBender extends BasicView
{

protected var mcylinder:XML3DSPrimitive;
protected var mcylinder2:XML3DSPrimitive;

private var bitmap1:BitmapFileMaterial;
private var bitmap2:BitmapBendMaterial;

public function AddingPixelBender(){
//Call super
super(1, 1, true, false);
initPV3D();
startRendering();
}

//Initiate Papaervision
protected function initPV3D():void
{

//Set the background to black
opaqueBackground = 0;

bitmap1=new BitmapFileMaterial(“assets/images/gainesHouse512.jpg”);
bitmap2=new BitmapBendMaterial(“assets/images/gainesHouse512.jpg”);

mcylinder = new XML3DSPrimitive(“assets/data/max/gainesHouse.xml”, bitmap1);

mcylinder2 = new XML3DSPrimitive(“assets/data/max/gainesHouse.xml”, bitmap2);

mcylinder2.x=100;
mcylinder.x=-100;

scene.addChild(mcylinder);
scene.addChild(mcylinder2);
mcylinder.scale=1.5 ;
mcylinder2.scale=1.5 ;

}

// override onRenderTick so you can execute code
override protected function onRenderTick(event:Event=null):void
{
//Rotate all objects
mcylinder.yaw(1);
mcylinder2.yaw(1);

//Call the super.onRenderTick function
super.onRenderTick(event);
}}}

About these ads

2 Responses to Adding Pixel Bender to Papervision3D

  1. [...] > Adding Pixel Bender to Papervision3D « Professional Papervision3D Book [...]

  2. [...] an amazingly simple process. Just add another term to your pixel bender light equation as shown below: Adding Multiple Images to [...]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: