Particle System for Papervision3D

A few weeks ago I made some modifications to the Zeus Lab particle system for Actionscript made by Josh Tynjala . The Josh’s particle system is neat, simple and if you combine the particle system with the use of filter you can obtain beautiful visual effects. I wanted to continue using it while experimenting with Papervision, the only problem is that it is 2D, so I made the port of the particle system to make it usable in a 3D environment, the result was some kind of distribution of the particle system because I didn’t touch the 2D code I only extended to be usable with the display architecture and classes in PV3D.

In the following example I will explain how to use it combined with the Effects branch to obtain something like this:


Let’s see how this is done:

The first we need is a ParticleSystem3D:

pSys = new ParticleSystem3D(this.stage);
pSys.spawnRate = 4;
pSys.particleClass = SimpleParticle3D ;

ParticleSystem3D receive the stage for parameter, which is used to add a listener to the ENTER_FRAME Event.
The SpawnRate is the rate of spawn particles, this means that if this value is higher you obtain more particles spawned per second.
The particleClass property is the class or data type used by the particle system there are two types of particles for this tutorial, I used SimpleParticle3D only, the other type of particle BillBoardParticle3D is a billboard but I can’t make it work with the Effect branch yet. With the particleClass property set, the ParticleSystem3D knows which particle should create when we start it.
Finally we should start the particle system so we call its method start.

this.pSys.addEventListener( ParticleEvent.INITIALIZE_PARTICLE, initializeParticle);
this.pSys.addEventListener(ParticleEvent.UPDATE_PARTICLE, updateWaterParticle);

After initialize the particle system we must add it to the scene, ParticleSystem3D is a DisplayObject3D. We also add event listeners for the events INITIALIZE and UPDATE_PARTICLE. The INITIALIZE Event of a particle is dispatched once when the particle is created the first time and the UPDATE_PARTICLE event is dispatched every time what the particle is updated, it means in every frame. There is a third particle event that you can listen and is not used here it is DESTROY_PARTICLE, this event is dispatched when the life time of the particle ends.The following section of code do the effect tricks:

// Create a bitmap effect layer
mBfx = new BitmapEffectLayer(400, 400);
// Add a blur effect to the layer.
mBfx.addEffect(new BitmapLayerEffect(new BlurFilter(5, 5, 4)));
// add the effect layer to the viewport
// Create a draw command for the visual effect
mBfx.drawCommand = new BitmapDrawCommand(null, new ColorTransform(0.3, 0.8, 0.6, 1), BlendMode.ADD);
// Set the blend mode of the effect layer
mBfx.drawLayer.blendMode = BlendMode.OVERLAY;
// Set a little scroll in y for the effect layer.

RenderLayerManager.getInstance().sortMode = RenderLayerSortMode.Z_SORT;

I will not explain what is done here because is beyond the scope of this tutorial, if you want to understand what happens here see the Andy Zupko Tutorial here.

private function initializeParticle(event:ParticleEvent):void
 var particle:SimpleParticle3D = event.particle as SimpleParticle3D;
 particle.renderLayer = mBfx ;

	particle.size = 1.5;
 particle.lifetime = 2000;

	particle.position = new Number3D(0, Math.random() * this.stage.stageHeight / 8,0);

	var width:Number = this.stage.stageWidth / 100;
 particle.velocity = new Number3D(Math.random() * width - width / 2, this.stage.stageHeight / 8,Math.random() * width - width / 2);

This is the method added as listener of the initialize event for a particle. In this function we set the initial values for the render layer, the size, life time, position and velocity of the particle.

private function updateParticle(event:ParticleEvent):void
 var particle:SimpleParticle3D = event.particle as SimpleParticle3D;
 var age:Number = getTimer() - particle.spawnTime;

	particle.size = particle.size + 0.05;

	var vX:Number = particle.spawnVelocity.x;
 if(age / particle.lifetime >= 0.25)
 	vX = particle.spawnVelocity.x * 6;

	var vZ:Number =  particle.velocity.z ;
 if(age / particle.lifetime >= 0.25)
 	vZ = particle.spawnVelocity.z * 8;

	var vY:Number =  particle.velocity.y - 5;

	particle.velocity = new Number3D(vX, vY, vZ); 


And this is the Update function for every particle update, in this function we change the particle velocity to obtain some kind of fountain effect and thats it.You can find the source code of the example here. And you can download Zeus Particles 3D version release here.


Update: You can find a release of the particle system for PV3D 1.7  here .

Explore posts in the same categories: Actionscript, flash, Papervision3D

Tags: , , , ,

You can comment below, or link to this permanent URL from your own site.

3 Comments on “Particle System for Papervision3D”

Leave a Reply

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

You are commenting using your 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

%d bloggers like this: