Archive for the 'Primitive' Category

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


Follow papervision2 on Twitter

RSS Feed