Monthly Archive for September, 2008

Papervision Custom Primitive Source code for use with cool particle field effect.

Papervision Particle field

Carlos Lunetta was kind enough to send me his custom primitive and vertex particle classes for this blog entry. These are similar to the ones that he used for the Terra TV website.  In this example that concept is rather simple. You create a primitive object (Plane, Cube, Sphere) and run a for loop on the verticles.

As shown here:

[as]

for (var i:int = 0; i < disc.geometry.vertices.length; i++)
{
var discVert:Vertex3D = disc.geometry.vertices[i];
}

[/as]

Link to my example

Source Code

Now that you are looping through the verticles of this primitive you are now privy to information such as the x y and z locations of each point. So what can we do with that…..

One thing we can do is attach particles to it.. such as

[as]

for (var i:int = 0; i < disc.geometry.vertices.length; i++)
{
var discVert:Vertex3D = disc.geometry.vertices[i];

var vertexParticle:VertexParticle = new VertexParticle(particalMaterial, 5, discVert);

vertexParticle.x = discVert.x;
vertexParticle.y = discVert.y;
vertexParticle.z = discVert.z;

myParticles.addParticle(vertexParticle);
}

[/as]

Doing this lends itself to some neat particle field effects such as the one shown above. Of course you can mess around with the settings to optimize it for your needs.

Here is how you put it all together:

[as]

package com.cs54.papervision
{

import flash.events.Event;

import org.papervision3d.core.geom.Particles;
import org.papervision3d.core.geom.renderables.Particle;
import org.papervision3d.core.geom.renderables.Vertex3D;
import org.papervision3d.core.geom.Vertices3D;
import org.papervision3d.materials.ColorMaterial;
import org.papervision3d.materials.special.MovieAssetParticleMaterial;
import org.papervision3d.materials.special.ParticleMaterial;
import org.papervision3d.materials.utils.MaterialsList;
import org.papervision3d.materials.WireframeMaterial;
import org.papervision3d.objects.DisplayObject3D;
import org.papervision3d.objects.primitives.Cone;
import org.papervision3d.objects.primitives.Plane;

import src.primitives.Dome;
import src.primitives.Disc;
import src.primitives.VertexParticle;

public class PV3DScene extends PaperBase
{

protected var displayContainer    :DisplayObject3D;
protected var particle            :Particle
protected var myParticles        :Particles;
protected var domeParticles        :Particles;
protected var partMat            :ParticleMaterial;
protected var colorMaterial        :ColorMaterial;
protected var disc                :Disc;
protected var particalMaterial    :ParticleMaterial;
protected var particalMaterial2    :ParticleMaterial;
protected var container            :DisplayObject3D;
protected var domeContainer        :DisplayObject3D;
protected var dome                :Dome;

override public function init(vpWidth:Number = 800, vpHeight:Number = 600):void
{
super.init(vpWidth, vpHeight);
default_camera.y = 800;
default_camera.rotationX = 20;
}

override protected function createChildren3D():void
{
super.createChildren3D();

container = new DisplayObject3D();
domeContainer = new DisplayObject3D();

default_scene.addChild(container);
default_scene.addChild(domeContainer);

default_camera.zoom                 = 50;
default_camera.z                     = -3000
colorMaterial                         = new ColorMaterial(0xFF0000, 1, true);
colorMaterial.doubleSided             = true;
particalMaterial                     = new ParticleMaterial(0xFF00CC, 100, ParticleMaterial.SHAPE_CIRCLE);
particalMaterial2                     = new ParticleMaterial(0×000000, 100, ParticleMaterial.SHAPE_CIRCLE);
myParticles                         = new Particles();
domeParticles                        = new Particles();
disc                                 = new Disc(colorMaterial, 500,15,2,3,this);
dome                                = new Dome(colorMaterial, 500, 100, 10, 5, 2);

for (var i:int = 0; i < disc.geometry.vertices.length; i++)
{
var discVert:Vertex3D = disc.geometry.vertices[i];

var vertexParticle:VertexParticle = new VertexParticle(particalMaterial2, 5, discVert);

vertexParticle.x = discVert.x;
vertexParticle.y = discVert.y;
vertexParticle.z = discVert.z;

myParticles.addParticle(vertexParticle);
}

for (var j:int = 0; j < dome.geometry.vertices.length; j++)
{
var domeVert:Vertex3D = dome.geometry.vertices[j];

var domeVertexParticle:VertexParticle = new VertexParticle(particalMaterial, 5, domeVert);

domeVertexParticle.x = domeVert.x;
domeVertexParticle.y = domeVert.y;
domeVertexParticle.z = domeVert.z;

domeParticles.addParticle(domeVertexParticle);
}

container.addChild(myParticles)
domeContainer.addChild(domeParticles)

domeContainer.y = -10
default_scene.addChild(container)
default_scene.addChild(domeContainer);
}

override protected function renderEnterFrame(ThisEvent:Event):void
{
super.renderEnterFrame(ThisEvent);

if (default_camera.z < 0)
{
default_camera.z += (0 – default_camera.z)*.01
}

default_camera.y += (50 – default_camera.y)*.01;

container.yaw(1);
domeContainer.yaw(1);
}

}

}

[/as]

Paperbase.as

[as]

package  com.cs54.papervision
{
import flash.display.Sprite;
import flash.events.Event;
import org.papervision3d.view.Viewport3D;
import org.papervision3d.cameras.*;
import org.papervision3d.scenes.Scene3D;
import org.papervision3d.render.BasicRenderEngine;

public class PaperBase extends Sprite
{

public var viewport:Viewport3D;
public var renderer:BasicRenderEngine;
public var default_scene:Scene3D;
public var default_camera:Camera3D;

public function init(vpWidth:Number = 800, vpHeight:Number = 600):void
{
initPapervision(vpWidth, vpHeight);
createChildren3D();
createChildren2D();
size();
initEvents();
}
protected function initPapervision(vpWidth:Number, vpHeight:Number):void
{
viewport = new Viewport3D(vpWidth, vpHeight);
addChild(viewport);

renderer = new BasicRenderEngine();

default_scene = new Scene3D();

default_camera = new Camera3D();
}
protected function createChildren3D():void
{
// This function should hold all of the stages needed
// to initialise everything used for papervision.
// Models, materials, cameras etc.
}
protected function createChildren2D():void
{
// This function should create all of the 2d items
// that will be overlayed on your papervision project.
// User interfaces, Heads up displays etc.
}
protected function size():void
{

}
protected function initEvents():void
{
addEventListener(Event.ENTER_FRAME, renderEnterFrame);
}
protected function processFrame():void
{
// Process any movement or animation here.
}
protected function renderEnterFrame( ThisEvent:Event ):void
{
//trace(“RENDERING”)
//We need to render the scene and update anything here.

renderer.renderScene(default_scene, default_camera, viewport);
processFrame();
}
}
}

[/as]

Here are Carlos Lunetta’s classes for the Dome,Disc and VertexParticle

Dome.as

[as]

package src.primitives
{
import org.papervision3d.core.geom.*;
import org.papervision3d.core.geom.renderables.Vertex3D;
import org.papervision3d.core.proto.*;
/**
* …
* @author clunetta
*/
public class Dome extends TriangleMesh3D
{
private var segments:uint;
private var sides:uint;
private var radius:Number
private var height:Number;
private var sideIncrement:Number;
private var currentIncrement:Number = 0;

public function Dome (material:MaterialObject3D=null, radius:Number=100, height:Number = 100, segments:uint=0, sides:uint=3, sideIncrement:Number=0, initObject:Object=null )
{
super(material, new Array(), new Array());

this.segments         = 1 + segments;
this.sides             = sides;
this.radius         = radius;
this.sideIncrement     = sideIncrement;
this.height         = 2 * height;

buildDome();
}

private function buildDome():void
{
var matInstance:MaterialObject3D = material;

var i:Number, j:Number, k:Number;

var aVertice:Array = this.geometry.vertices;

var oVtx:Vertex3D;
oVtx = new Vertex3D(0,height,0);
aVertice.push(oVtx);

for (j = 1; j < segments; j++) {

//var hRds:Number = (segments-j)* (height/segments)
var fZ:Number =  height * Math.sin((segments-(j-1)) *  (0.5*Math.PI) / segments);

//var fZ:Number = (height)*Math.sin((j-1)*(0.5*Math.PI/segments));
currentIncrement += sideIncrement;
var curSides:uint = uint(Math.floor(sides + currentIncrement));

for (i = 0; i < curSides; i++) {
var fRds:Number = j * radius / segments;
var fX:Number = fRds*Math.sin(i*(2*Math.PI/curSides));
var fY:Number = fRds*Math.cos(i*(2*Math.PI/curSides));
oVtx = new Vertex3D(fY,fZ,fX);
aVertice.push(oVtx);
}
}

this.geometry.ready = true;
}

}

}

[/as]

Disc.as

[as]

package src.primitives
{
import org.papervision3d.core.geom.*;
import org.papervision3d.core.geom.renderables.Vertex3D;
import org.papervision3d.core.proto.*;
/**
* …
* @author clunetta
*/
public class Disc extends TriangleMesh3D
{
private var segments :uint;
private var sides :uint;
private var radius :Number;
private var sideIncrement:Number;
private var currentIncrement:Number = 0;

public function Disc (material:MaterialObject3D=null, radius:Number=100, segments:uint=0, sides:uint=3, sideIncrement:Number=0, initObject:Object=null )
{
super(material, new Array(), new Array());
this.segments = 1+ segments;
this.sides = sides;
this.radius = radius;
this.sideIncrement = sideIncrement;

buildDisc();
}

private function buildDisc():void
{
var matInstance:MaterialObject3D = material;

var i:Number, j:Number, k:Number;

var aVertice:Array = this.geometry.vertices;

var fZ:Number = 0;
var oVtx:Vertex3D;
oVtx = new Vertex3D(0,0,0);
aVertice.push(oVtx);
for (j = 0; j < segments; j++) {
currentIncrement += sideIncrement;
var curSides:uint = uint(Math.floor(sides + currentIncrement));
for (i = 0; i < curSides; i++) {
var fRds:Number = (j + 1) * radius / segments;
var fX:Number = fRds*Math.sin(i*(2*Math.PI/curSides));
var fY:Number = fRds*Math.cos(i*(2*Math.PI/curSides));
oVtx = new Vertex3D(fY,fZ,fX);
aVertice.push(oVtx);
}

}

this.geometry.ready = true;
}

}

}

[/as]

VertexParticle.as

[as]

package src.primitives
{
import org.papervision3d.core.geom.renderables.Particle;
import org.papervision3d.core.geom.renderables.Vertex3D;
import org.papervision3d.materials.special.ParticleMaterial;

/**
* …
* @author clunetta
*/
public class VertexParticle extends Particle
{

public function VertexParticle(material:ParticleMaterial, size:Number, vertex:Vertex3D)
{
super(material, size);
super.vertex3D = vertex;
}

}

}

[/as]

Source Code

Papervision with 3D Physics WOW engine (Getting started)

Papervision with WOW Engine

Today I decided to create this simple example that used Papervision and the 3D physics WOW Engine. It is actually quite simple. Basically all that happens is you setup your objects for papervision (5 minutes) – setup your objects for the wow engine (5 mintues) then when you render – you just attach the movements of the WOW objects to your Papervision objects. (5 more minutes or less and you’re DONE)

So taking out 3D and Papervision all together here is what is happening. Let’s say you setup an enterframe and then assign a movieclips x and y positions to your mouse x and y position. SAME Exact thing happening here. Except you are attaching the x y and z properties of a papervision object to a wow object. Pretty simple! I’ll take corrections if I am wrong since I have spent all of 30 minutes playing around with it.

Here is a quick sample of what goes on when rendering:

[as]

sphere.x    = wowSphere.px;
sphere.y    = -wowSphere.py;
sphere.z    = wowSphere.pz;

[/as]

Not too hard right.

Here is the ActionScript code for creating a simple sphere boucing on the stage. Source code is below. (Basically a modified example shown on the WOW site)

Main.as

[as]

package
{
import com.cs54.papervision.PV3DSceneWOW;
import flash.display.Sprite;

public class Main extends Sprite
{
protected var papervisionSceneWOW:PV3DSceneWOW

public function Main():void
{
papervisionSceneWOW = new PV3DSceneWOW();
papervisionSceneWOW.init(stage.stageWidth, stage.stageHeight)
addChild(papervisionSceneWOW);
}
}
}

[/as]

PV3DSceneWOW.as:

[as]

package com.cs54.papervision
{

import flash.events.Event;
import fr.seraf.wow.core.data.WVector;
import fr.seraf.wow.core.WOWEngine;
import fr.seraf.wow.primitive.WBox;
import fr.seraf.wow.primitive.WOWPlane;
import fr.seraf.wow.primitive.WSphere;
import org.papervision3d.materials.ColorMaterial;
import org.papervision3d.materials.utils.MaterialsList;
import org.papervision3d.materials.WireframeMaterial;
import org.papervision3d.objects.DisplayObject3D;
import org.papervision3d.objects.primitives.Plane;
import org.papervision3d.objects.primitives.Sphere;

public class PV3DSceneWOW extends PaperBase
{

protected var displayContainer    :DisplayObject3D;
protected var holder            :DisplayObject3D
protected var sphere            :Sphere;
protected var wireframeMat        :WireframeMaterial;
protected var wow                :WOWEngine
protected var wowSphere            :WSphere
override public function init(vpWidth:Number = 800, vpHeight:Number = 600):void
{

super.init(vpWidth, vpHeight);
default_camera.y = 300;
}

override protected function createChildren3D():void
{
super.createChildren3D();

wireframeMat                 = new WireframeMaterial(0, 100, .2);
wireframeMat.doubleSided     = true;
sphere                         = new Sphere(wireframeMat, 100);
default_camera.zoom         = 40;

default_scene.addChild(sphere);

setupWow();
createGround();
createWOWSphere();
}

public function setupWow(): void
{
//this is the physics engine
wow=new WOWEngine(.3);
wow.collisionResponseMode = wow.STANDARD;

//setup the gravity
wow.addMasslessForce(new WVector(0,50,0));
}

public function createGround(): void
{
//we create a ground on the physics engine
var ground:WOWPlane             = new WOWPlane(0,0,0);
ground.elasticity                =.35;
ground.friction                    =2;

wow.addParticle(ground);

// we create the ground on the render engine so we start by setup the material…
var material:WireframeMaterial    = new WireframeMaterial(0,100,1)
material.doubleSided            = true

var plane:Plane                 = new Plane(material , 1000, 1000, 5, 5 );
plane.rotationX                    =-90

default_scene.addChild(plane);
}

public function createWOWSphere(): void
{
wowSphere = new WSphere(0,-900,100,100,false,0.1);
wowSphere.elasticity = 0;

wowSphere.friction=3320;
wow.addParticle(wowSphere);

sphere.x    =-wowSphere.px;
sphere.y    =-wowSphere.py;
sphere.z    =-wowSphere.pz;
}

override protected function processFrame():void
{
super.processFrame();

wow.step()

sphere.x    = wowSphere.px;
sphere.y    = -wowSphere.py;
sphere.z    = wowSphere.pz;

}
override protected function renderEnterFrame(ThisEvent:Event):void
{
super.renderEnterFrame(ThisEvent);

}

}

}

[/as]

The paper base that I reuse:
Based off of this example

[as]

package  com.cs54.papervision
{
import flash.display.Sprite;
import flash.events.Event;
import org.papervision3d.view.Viewport3D;
import org.papervision3d.cameras.*;
import org.papervision3d.scenes.Scene3D;
import org.papervision3d.render.BasicRenderEngine;

public class PaperBase extends Sprite
{

public var viewport:Viewport3D;
public var renderer:BasicRenderEngine;
public var default_scene:Scene3D;
public var default_camera:Camera3D;

public function init(vpWidth:Number = 800, vpHeight:Number = 600):void
{
initPapervision(vpWidth, vpHeight);
createChildren3D();
createChildren2D();
size();
initEvents();
}
protected function initPapervision(vpWidth:Number, vpHeight:Number):void
{
viewport = new Viewport3D(vpWidth, vpHeight);
addChild(viewport);

renderer = new BasicRenderEngine();

default_scene = new Scene3D();

default_camera = new Camera3D();
}
protected function createChildren3D():void
{
// This function should hold all of the stages needed
// to initialise everything used for papervision.
// Models, materials, cameras etc.
}
protected function createChildren2D():void
{
// This function should create all of the 2d items
// that will be overlayed on your papervision project.
// User interfaces, Heads up displays etc.
}
protected function size():void
{

}
protected function initEvents():void
{
addEventListener(Event.ENTER_FRAME, renderEnterFrame);
}
protected function processFrame():void
{
// Process any movement or animation here.
}
protected function renderEnterFrame( ThisEvent:Event ):void
{
//trace(“RENDERING”)
//We need to render the scene and update anything here.

renderer.renderScene(default_scene, default_camera, viewport);
processFrame();
}
}
}

[/as]

SOURCE CODE

Don’t forget to download the latest Papervision Great White Branch.

Papervision Double Sided Plane (err sorta) :/

Papervision double sided plane

As far as I know there are 2 options when creating a double sided plane. Double sided meaning a different material for each side.

Option 1:
Create two planes. Plane 1 with a z sorting of 1 and the other with 0 and the rotationY of one of those planes set to 180. You could then contain this in one display container.

Version 2:
Create one cube with a depth of 0. Then you just apply a material to the front and back. OK OK OK so this is not really a Plane… but I like this approach because it allows me to more easily use things like Bend Modifier.

If anyone has a better or more improved approach I would love to hear about it.

Here is what the code for Option 1:

[as]
package com.cs54.ui
{
import org.papervision3d.objects.primitives.Plane;
import org.papervision3d.materials.MovieMaterial;
import org.papervision3d.materials.utils.MaterialsList;
import org.papervision3d.objects.DisplayObject3D;
import com.everydayflash.pv3d.modifiers.Bend;
import flash.display.DisplayObject;public class Tree2Planes extends DisplayObject3D
{
[Embed(source="/assets/tree-side-1.png")]
public var TreeFront        :Class;[Embed(source="/assets/tree-side-2.png")]
public var TreeBack            :Class;

protected var treeFront        :DisplayObject;
protected var treeBack        :DisplayObject;

protected var treeFrontMat    :MovieMaterial;
protected var treeBackMat    :MovieMaterial;

protected var treeMatList    :MaterialsList;

protected var _height        :Number = 175;
protected var _width        :Number = 191;
protected var _container    :DisplayObject3D;

protected var plane1        :Plane;
protected var plane2        :Plane;

public function Tree2Planes():void
{
treeMatList     = new MaterialsList();
treeFront        = new TreeFront();
treeBack         = new TreeBack();

//Tree Materials
treeFrontMat     = new MovieMaterial(treeFront, true);
treeBackMat     = new MovieMaterial(treeBack, true);

treeFrontMat.allowAutoResize = false;

plane1        = new Plane(treeFrontMat,_width,_height);
plane2        = new Plane(treeBackMat,_width,_height);

plane1.z = 1;
plane1.rotationY = 180;

_container = new DisplayObject3D();

addChild(_container);
_container.addChild(plane1);
_container.addChild(plane2);

}

public function get height():Number
{
return _height;
}
public function get width():Number
{
return _width;
}

}

}

[/as]

Here is what the code for Option 2:

[as]

package com.cs54.ui
{
import org.papervision3d.materials.MovieMaterial;
import org.papervision3d.materials.utils.MaterialsList;
import org.papervision3d.objects.DisplayObject3D;
import org.papervision3d.objects.primitives.Cube;
import com.everydayflash.pv3d.modifiers.Bend;
import flash.display.DisplayObject;

public class Tree extends DisplayObject3D
{
[Embed(source="/assets/tree-side-1.png")]
public var TreeFront        :Class;

[Embed(source="/assets/tree-side-2.png")]
public var TreeBack            :Class;

protected var treeFront        :DisplayObject;
protected var treeBack        :DisplayObject;

protected var treeFrontMat    :MovieMaterial;
protected var treeBackMat    :MovieMaterial;

protected var treeMatList    :MaterialsList;

protected var treeCube        :Cube;
protected var _height        :Number = 175;
protected var _width        :Number = 191;
protected var _container    :DisplayObject3D;

public function Tree():void
{

treeMatList     = new MaterialsList();
treeFront        = new TreeFront();
treeBack         = new TreeBack();

//Tree Materials
treeFrontMat     = new MovieMaterial(treeFront, true);
treeBackMat     = new MovieMaterial(treeBack, true);

treeFrontMat.allowAutoResize = false;

treeFrontMat.animated = true;

treeMatList.addMaterial( treeFrontMat, “front” );
treeMatList.addMaterial( treeBackMat, “back” );

treeCube     = new Cube(treeMatList, _width, 0, _height, 6, 6, 6);

var bend:Bend = new Bend(treeCube);

bend.quickBend(1, .1);

addChild(treeCube);

}

public function get height():Number
{
return _height;
}
public function get width():Number
{
return _width;
}

}

}

[/as]

Loading MD2 Models with Papervision

Code to load MD2 files below. Anyone out there want to help create simple MD2 models that work well with Papervision – or – does anyone know of a good resource to purchase already animated models? I just wanted to get a few for some blog entry’s. Full credit would be given or course. I found several models this morning but after a few hours only 1 of them was of value. It happened to be the one tied to Clint Hannaford’s example. :/

Once you have a good model here is the super simple code to load it:

[AS]
var wireframeMat:WireframeMaterial = new WireframeMaterial(0×000000);

var md2:MD2 = new MD2(true); //if you get errors try using false – it may be a single frame file (no animation)
md2.load(“assets/test_4.md2″,wireframeMat);
//Add to your scene for rendering
[/AS]

Yeah I know.. That’s it. The hard part is finding a model that doesn’t suck.


Follow papervision2 on Twitter

RSS Feed