Skip to content

Commit

Permalink
js/threex.sparks.js much cleaner
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromeetienne committed Dec 9, 2011
1 parent 3133920 commit cd00cc2
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 31 deletions.
70 changes: 40 additions & 30 deletions js/threex.sparks.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ THREEx.Sparks.Emitter = function(opts)
{
opts = opts || {};
this._maxParticles = opts.maxParticles || console.assert(false);
this._texture = opts.texture || this._buildDefaultTexture();
this._texture = opts.texture || this._buildDefaultTexture();
var counter = opts.counter || console.assert(false);

var vertexIndexPool = {
__pools: [],
Expand All @@ -29,12 +30,17 @@ THREEx.Sparks.Emitter = function(opts)


var particles = new THREE.Geometry();
var vertices = particles.vertices;
for ( i = 0; i < this._maxParticles; i++ ) {
var position = new THREE.Vector3(Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY);
particles.vertices.push(new THREE.Vertex(position));
vertices.push(new THREE.Vertex(position));
vertexIndexPool.add(i);
}

// to handle window resize
this._$onWindowResize = this._onWindowResize.bind(this);
window.addEventListener('resize', this._$onWindowResize, false);

var attributes = this._attributes = {
size : { type: 'f', value: [] },
aColor : { type: 'c', value: [] }
Expand Down Expand Up @@ -66,63 +72,67 @@ THREEx.Sparks.Emitter = function(opts)
});

this._group = new THREE.ParticleSystem( particles, material );
this._group.dynamic = true;
this._group.sortParticles = true; // TODO is this needed ?
//this._group.dynamic = true;
//this._group.sortParticles = true; // TODO is this needed ?

//// EMITTER STUFF

var setTargetParticle = function() {
// Find available vertex index
var target = vertexIndexPool.get();
valuesSize[target] = 150;

var vertexIdx = vertexIndexPool.get();
var target = {
vertexIdx : vertexIdx,
size : function(value){ valuesSize[vertexIdx] = value; },
color : function(){ return valuesColor[vertexIdx]; }
};
return target;
};

var hue = 0;
var onParticleCreated = function(particle) {
var target = particle.target;
if( !target ) return;

particles.vertices[target].position = particle.position;

hue += 0.01;
if( hue > 1 ) hue -= 1;
valuesColor[target].setHSV(hue, 0.8, 0.8);
var onParticleCreated = function(particle) {
var vertexIdx = particle.target.vertexIdx;
vertices[vertexIdx].position = particle.position;
};

var onParticleDead = function(particle) {
var target = particle.target;
if( !target ) return;
var vertexIdx = particle.target.vertexIdx;

// Hide the particle
valuesColor[target].setHSV(0, 0, 0);
particles.vertices[target].position.set(Number.POSITIVE_INFINITY,Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY);
valuesColor[vertexIdx].setHex( 0x000000 );
vertices[vertexIdx].position.set(Number.POSITIVE_INFINITY,Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY);

// Mark particle system as available by returning to pool
vertexIndexPool.add(particle.target);
vertexIndexPool.add( vertexIdx );
};

var emitter = this._emitter = new SPARKS.Emitter(new SPARKS.SteadyCounter(70));
var emitter = this._emitter = new SPARKS.Emitter(counter);

var hue = 0;
var initColorSize = function() {};
initColorSize.prototype.initialize = function( emitter, particle ){
hue += 0.01;
if( hue > 1 ) hue -= 1;
particle.target.color().setHSV(hue, 0.8, 0.8);

particle.target.size(150);
};

emitter.addInitializer(new SPARKS.Target(null, setTargetParticle));
emitter.addCallback("created" , onParticleCreated );
emitter.addCallback("dead" , onParticleDead );


emitter.addInitializer(new initColorSize());
emitter.addInitializer(new SPARKS.Position( new SPARKS.PointZone( new THREE.Vector3(0,0,0) ) ) );
emitter.addInitializer(new SPARKS.Lifetime(0,2));
emitter.addInitializer(new SPARKS.Target(null, setTargetParticle));
emitter.addInitializer(new SPARKS.Velocity(new SPARKS.PointZone(new THREE.Vector3(0,150,00))));

emitter.addAction(new SPARKS.Age());
emitter.addAction(new SPARKS.Move());
emitter.addAction(new SPARKS.RandomDrift(1000,0,1000));
emitter.addAction(new SPARKS.Accelerate(0,-100,0));

emitter.addCallback("created" , onParticleCreated );
emitter.addCallback("dead" , onParticleDead );

emitter.start();

// to handle window resize
this._$onWindowResize = this._onWindowResize.bind(this);
window.addEventListener('resize', this._$onWindowResize, false);
}


Expand Down
5 changes: 4 additions & 1 deletion js/visualFxSparks.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ Marble.VisualFxSparks = function()
this.parent.init.call(this, {});

this._emitter = new THREEx.Sparks.Emitter({
maxParticles : 10000
maxParticles : 10000,
counter : new SPARKS.SteadyCounter(70)
});



scene.add(this._emitter.container());
}

Expand Down

0 comments on commit cd00cc2

Please sign in to comment.