Partigen 2 AS3 & Papervision3D Demo

Desuade Partigen 2 Beta 2 has been out for about a week (silently), with an improved API, and on the heels of the DMP demo, I thought I’d see how hard it would to integrate Partigen with Papervision 3D.

Note: For the color to work, you need to download the Bleeding Edge build or checkout the latest copy from SVN/GIT (unless there’s a build available after Beta 2)

I was delighted to find out that it was actually pretty easy, with very little workaround. The workaround isn’t bad or hackish, luckily due to the design of both packages, but hopefully there will be a native renderer for PV3D for Partigen in the future.

Demo after the break, read on!

Get Adobe Flash player

The code is pretty straight-forward and commented.

package {

import flash.display.Sprite;
import flash.events.Event;

import org.papervision3d.objects.primitives.Plane;
import org.papervision3d.objects.primitives.Cube;
import org.papervision3d.materials.*;
import org.papervision3d.materials.utils.*;
import org.papervision3d.view.BasicView;

import com.desuade.partigen.*;
import com.desuade.partigen.emitters.*;
import com.desuade.partigen.particles.*;
import com.desuade.partigen.events.*;
import com.desuade.partigen.renderers.*;
import com.desuade.debugging.*;

public class ExamplePartigen3D extends BasicView {

    public function ExamplePartigen3D() {
        super();

        //loads debug codes
        Debug.load(new DebugCodesPartigen());
        //Debug.level = 60000;
        //Debug.enabled = true;

        //adds a rendere for pv3d
        stage.addEventListener(Event.ENTER_FRAME, render);

        var em:Emitter = new Emitter(); //create a Partigen emitter
        em.eps = 10; //set the emissions per second
        em.life.value = 2; //set the life
        em.particle = CubeParticle; //set the particle class to the class defined in this package
        em.renderer = new NullRenderer(); //pv3d is the renderer, so we use a NullRenderer
        var emcp = em.controllers.particle; //this ist just a shortcut to cut down on code
        emcp.addTween('cx'); //adds a tween for the particles
        emcp.cx.keyframes.end.value = -1000; //sets the end value range to -1000 to 1000
        emcp.cx.keyframes.end.spread = 1000; // ^^^^^
        emcp.addTween('cy');
        emcp.cy.keyframes.end.value = -1000;
        emcp.cy.keyframes.end.spread = 1000;
        emcp.addTween('cz');
        emcp.cz.keyframes.end.value = -1000;
        emcp.cz.keyframes.end.spread = 1000;
        emcp.addTween('rx');
        emcp.rx.keyframes.begin.spread = 200;
        emcp.rx.keyframes.end.value = 820;
        emcp.addTween('ry');
        emcp.ry.keyframes.begin.spread = 200;
        emcp.ry.keyframes.end.value = 320;
        emcp.addColorTween('fc');
        emcp.fc.keyframes.begin.value = 0xcccccc; //sets a random beginning color
        emcp.fc.keyframes.begin.spread = 0xaaaaaa;
        em.addEventListener(ParticleEvent.BORN, addcube); //since we're using pv3d to render, we need to manually add and remove
        em.addEventListener(ParticleEvent.DIED, remcube);
        em.start(); //start the emitter
    } 

    //adds the particle to the pv3d scene
    private function addcube($o):void {
        scene.addChild($o.data.particle.cuby);
    }

    //removes the particle to the pv3d scene
    private function remcube($o):void {
        scene.removeChild($o.data.particle.cuby);
    }

    //renders the scene
    private function render(event:Event):void {
        singleRender();
    }

}

}

import com.desuade.partigen.particles.; import org.papervision3d.objects.primitives.Plane; import org.papervision3d.objects.primitives.Cube; import org.papervision3d.materials.; import org.papervision3d.materials.utils.*; import org.papervision3d.view.BasicView;

dynamic class CubeParticle extends Particle {

public var cuby:Cube;
public var material:ColorMaterial;

public function CubeParticle() {
    super();

    //create a pv3d material
    material = new ColorMaterial();
    material.doubleSided = true;
    material.fillColor = 0xFF0000;
    material.fillAlpha = 1.0;
    var ml:MaterialsList = new MaterialsList();
    ml.addMaterial(material, 'all');
    cuby = new Cube(ml, 100, 100, 100); //make a new pv3d cube
}

//these are just getter setters for the PV3D object

public function get cx():Number{
    return cuby.x;
}
public function set cx(value:Number):void {
    cuby.x = value;
}

public function get cy():Number{
    return cuby.y;
}
public function set cy(value:Number):void {
    cuby.y = value;
}

public function get cz():Number{
    return cuby.z;
}
public function set cz(value:Number):void {
    cuby.z = value;
}

public function get fc():Number{
    return material.fillColor;
}
public function set fc(value:Number):void {
    material.fillColor = value;
}

public function get rx():Number{
    return cuby.rotationX;
}
public function set rx(value:Number):void {
    cuby.rotationX = value;
}

public function get ry():Number{
    return cuby.rotationY;
}
public function set ry(value:Number):void {
    cuby.rotationY = value;
}

}

Basically we’re extending a PV3D scene and creating an emitter. We configure the emitter and set up some events for dealing with PV3D and set the renderer to NullRenderer.

As an internal class (at the bottom) we extend the Partigen Particle class, and add getters/setters to deal with the PV3D cube.

And that’s a very basic example of Partigen 2 and PV3D in AS3, just introducing the power of both APIs. I’ll think of a demo that really pushes the limits, and is actually impressive… as I know spinning cubes aren’t really useful ;)


About this entry