Skip to content

Commit 029c745

Browse files
committed
fixed cocos2d#2144 Re-wrote cc.ParticleBatchNode
1 parent 6d51051 commit 029c745

File tree

4 files changed

+171
-136
lines changed

4 files changed

+171
-136
lines changed

cocos2d/particle_nodes/CCParticleBatchNode.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*
1+
/**
22
* Copyright (c) 2010-2012 cocos2d-x.org
33
* Copyright (C) 2009 Matt Oswald
44
* Copyright (c) 2009-2010 Ricardo Quesada
@@ -124,7 +124,7 @@ cc.ParticleBatchNode = cc.Node.extend(/** @lends cc.ParticleBatchNode# */{
124124
this.setBlendFunc(child.getBlendFunc());
125125

126126
cc.Assert(this._blendFunc.src == child.getBlendFunc().src && this._blendFunc.dst == child.getBlendFunc().dst,
127-
"Can't add a PaticleSystem that uses a differnt blending function");
127+
"Can't add a ParticleSystem that uses a different blending function");
128128

129129
//no lazy sorting, so don't call super addChild, call helper instead
130130
var pos = this._addChildHelper(child, zOrder, tag);
@@ -150,20 +150,21 @@ cc.ParticleBatchNode = cc.Node.extend(/** @lends cc.ParticleBatchNode# */{
150150
* @param {Number} index
151151
*/
152152
insertChild:function (pSystem, index) {
153+
var totalParticles = pSystem.getTotalParticles();
154+
var totalQuads = this._textureAtlas.getTotalQuads();
153155
pSystem.setAtlasIndex(index);
154-
155-
if (this._textureAtlas.getTotalQuads() + pSystem.getTotalParticles() > this._textureAtlas.getCapacity()) {
156-
this._increaseAtlasCapacityTo(this._textureAtlas.getTotalQuads() + pSystem.getTotalParticles());
156+
if (totalQuads + totalParticles > this._textureAtlas.getCapacity()) {
157+
this._increaseAtlasCapacityTo(totalQuads + totalParticles);
157158
// after a realloc empty quads of textureAtlas can be filled with gibberish (realloc doesn't perform calloc), insert empty quads to prevent it
158-
this._textureAtlas.fillWithEmptyQuadsFromIndex(this._textureAtlas.getCapacity() - pSystem.getTotalParticles(), pSystem.getTotalParticles());
159+
this._textureAtlas.fillWithEmptyQuadsFromIndex(this._textureAtlas.getCapacity() - totalParticles, totalParticles);
159160
}
160161

161162
// make room for quads, not necessary for last child
162-
if (pSystem.getAtlasIndex() + pSystem.getTotalParticles() != this._textureAtlas.getTotalQuads())
163-
this._textureAtlas.moveQuadsFromIndex(index, index + pSystem.getTotalParticles());
163+
if (pSystem.getAtlasIndex() + totalParticles != totalQuads)
164+
this._textureAtlas.moveQuadsFromIndex(index, index + totalParticles);
164165

165166
// increase totalParticles here for new particles, update method of particlesystem will fill the quads
166-
this._textureAtlas.increaseTotalQuadsWith(pSystem.getTotalParticles());
167+
this._textureAtlas.increaseTotalQuadsWith(totalParticles);
167168
this._updateAllAtlasIndexes();
168169
},
169170

@@ -333,7 +334,7 @@ cc.ParticleBatchNode = cc.Node.extend(/** @lends cc.ParticleBatchNode# */{
333334
// override visit.
334335
// Don't call visit on it's children
335336
visit:function (ctx) {
336-
if (cc.renderContextType === cc.WEBGL) {
337+
if (cc.renderContextType === cc.CANVAS) {
337338
this._super(ctx);
338339
return;
339340
}

cocos2d/particle_nodes/CCParticleSystem.js

Lines changed: 49 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -260,9 +260,6 @@ cc.ParticleSystem = cc.Node.extend(/** @lends cc.ParticleSystem# */{
260260
//! Array of particles
261261
_particles:null,
262262

263-
//particle pool
264-
_particlePool:null,
265-
266263
// color modulate
267264
// BOOL colorModulate;
268265

@@ -1195,8 +1192,6 @@ cc.ParticleSystem = cc.Node.extend(/** @lends cc.ParticleSystem# */{
11951192
this._startColorVar = new cc.Color4F(1, 1, 1, 1);
11961193
this._endColor = new cc.Color4F(1, 1, 1, 1);
11971194
this._endColorVar = new cc.Color4F(1, 1, 1, 1);
1198-
1199-
this._particlePool = [];
12001195
},
12011196

12021197
/**
@@ -1216,7 +1211,6 @@ cc.ParticleSystem = cc.Node.extend(/** @lends cc.ParticleSystem# */{
12161211
* @return {cc.ParticleSystem}
12171212
*/
12181213
initWithFile:function (plistFile) {
1219-
//TODO
12201214
this._plistFile = plistFile;
12211215
var dict = cc.FileUtils.getInstance().dictionaryWithContentsOfFileThreadSafe(this._plistFile);
12221216

@@ -1410,8 +1404,11 @@ cc.ParticleSystem = cc.Node.extend(/** @lends cc.ParticleSystem# */{
14101404
initWithTotalParticles:function (numberOfParticles) {
14111405
this._totalParticles = numberOfParticles;
14121406

1407+
var i;
14131408
this._particles = [];
1414-
this._particlePool = [];
1409+
for(i = 0; i< numberOfParticles; i++){
1410+
this._particles[i] = new cc.Particle();
1411+
}
14151412

14161413
if (!this._particles) {
14171414
cc.log("Particle system: not enough memory");
@@ -1420,7 +1417,7 @@ cc.ParticleSystem = cc.Node.extend(/** @lends cc.ParticleSystem# */{
14201417
this._allocatedParticles = numberOfParticles;
14211418

14221419
if (this._batchNode)
1423-
for (var i = 0; i < this._totalParticles; i++)
1420+
for (i = 0; i < this._totalParticles; i++)
14241421
this._particles[i].atlasIndex = i;
14251422

14261423
// default, active
@@ -1455,16 +1452,9 @@ cc.ParticleSystem = cc.Node.extend(/** @lends cc.ParticleSystem# */{
14551452
},
14561453

14571454
destroyParticleSystem:function () {
1458-
this._particlePool = null;
14591455
this.unscheduleUpdate();
14601456
},
14611457

1462-
_getParticleObject:function () {
1463-
if (this._particlePool.length > 0)
1464-
return this._particlePool.pop();
1465-
return new cc.Particle();
1466-
},
1467-
14681458
/**
14691459
* Add a particle to the emitter
14701460
* @return {Boolean}
@@ -1473,11 +1463,9 @@ cc.ParticleSystem = cc.Node.extend(/** @lends cc.ParticleSystem# */{
14731463
if (this.isFull())
14741464
return false;
14751465

1476-
var particle = this._getParticleObject();
1466+
var particle =this._particles[this._particleCount];
14771467
this.initParticle(particle);
1478-
this._particles.push(particle);
14791468
++this._particleCount;
1480-
14811469
return true;
14821470
},
14831471

@@ -1496,19 +1484,34 @@ cc.ParticleSystem = cc.Node.extend(/** @lends cc.ParticleSystem# */{
14961484
particle.pos.y = this._sourcePosition.y + this._posVar.y * cc.RANDOM_MINUS1_1();
14971485

14981486
// Color
1499-
var start = new cc.Color4F(
1500-
cc.clampf(this._startColor.r + this._startColorVar.r * cc.RANDOM_MINUS1_1(), 0, 1),
1501-
cc.clampf(this._startColor.g + this._startColorVar.g * cc.RANDOM_MINUS1_1(), 0, 1),
1502-
cc.clampf(this._startColor.b + this._startColorVar.b * cc.RANDOM_MINUS1_1(), 0, 1),
1503-
cc.clampf(this._startColor.a + this._startColorVar.a * cc.RANDOM_MINUS1_1(), 0, 1)
1504-
);
1505-
1506-
var end = new cc.Color4F(
1507-
cc.clampf(this._endColor.r + this._endColorVar.r * cc.RANDOM_MINUS1_1(), 0, 1),
1508-
cc.clampf(this._endColor.g + this._endColorVar.g * cc.RANDOM_MINUS1_1(), 0, 1),
1509-
cc.clampf(this._endColor.b + this._endColorVar.b * cc.RANDOM_MINUS1_1(), 0, 1),
1510-
cc.clampf(this._endColor.a + this._endColorVar.a * cc.RANDOM_MINUS1_1(), 0, 1)
1511-
);
1487+
var start, end;
1488+
if (cc.renderContextType === cc.CANVAS) {
1489+
start = new cc.Color4F(
1490+
cc.clampf(this._startColor.r + this._startColorVar.r * cc.RANDOM_MINUS1_1(), 0, 1),
1491+
cc.clampf(this._startColor.g + this._startColorVar.g * cc.RANDOM_MINUS1_1(), 0, 1),
1492+
cc.clampf(this._startColor.b + this._startColorVar.b * cc.RANDOM_MINUS1_1(), 0, 1),
1493+
cc.clampf(this._startColor.a + this._startColorVar.a * cc.RANDOM_MINUS1_1(), 0, 1)
1494+
);
1495+
end = new cc.Color4F(
1496+
cc.clampf(this._endColor.r + this._endColorVar.r * cc.RANDOM_MINUS1_1(), 0, 1),
1497+
cc.clampf(this._endColor.g + this._endColorVar.g * cc.RANDOM_MINUS1_1(), 0, 1),
1498+
cc.clampf(this._endColor.b + this._endColorVar.b * cc.RANDOM_MINUS1_1(), 0, 1),
1499+
cc.clampf(this._endColor.a + this._endColorVar.a * cc.RANDOM_MINUS1_1(), 0, 1)
1500+
);
1501+
} else {
1502+
start = {
1503+
r: cc.clampf(this._startColor.r + this._startColorVar.r * cc.RANDOM_MINUS1_1(), 0, 1),
1504+
g: cc.clampf(this._startColor.g + this._startColorVar.g * cc.RANDOM_MINUS1_1(), 0, 1),
1505+
b: cc.clampf(this._startColor.b + this._startColorVar.b * cc.RANDOM_MINUS1_1(), 0, 1),
1506+
a: cc.clampf(this._startColor.a + this._startColorVar.a * cc.RANDOM_MINUS1_1(), 0, 1)
1507+
};
1508+
end = {
1509+
r: cc.clampf(this._endColor.r + this._endColorVar.r * cc.RANDOM_MINUS1_1(), 0, 1),
1510+
g: cc.clampf(this._endColor.g + this._endColorVar.g * cc.RANDOM_MINUS1_1(), 0, 1),
1511+
b: cc.clampf(this._endColor.b + this._endColorVar.b * cc.RANDOM_MINUS1_1(), 0, 1),
1512+
a: cc.clampf(this._endColor.a + this._endColorVar.a * cc.RANDOM_MINUS1_1(), 0, 1)
1513+
};
1514+
}
15121515

15131516
particle.color = start;
15141517
particle.deltaColor.r = (end.r - start.r) / particle.timeToLive;
@@ -1521,8 +1524,7 @@ cc.ParticleSystem = cc.Node.extend(/** @lends cc.ParticleSystem# */{
15211524
startS = Math.max(0, startS); // No negative value
15221525

15231526
particle.size = startS;
1524-
1525-
if (this._endSize == cc.PARTICLE_START_SIZE_EQUAL_TO_END_SIZE) {
1527+
if (this._endSize === cc.PARTICLE_START_SIZE_EQUAL_TO_END_SIZE) {
15261528
particle.deltaSize = 0;
15271529
} else {
15281530
var endS = this._endSize + this._endSizeVar * cc.RANDOM_MINUS1_1();
@@ -1546,7 +1548,7 @@ cc.ParticleSystem = cc.Node.extend(/** @lends cc.ParticleSystem# */{
15461548
var a = cc.DEGREES_TO_RADIANS(this._angle + this._angleVar * cc.RANDOM_MINUS1_1());
15471549

15481550
// Mode Gravity: A
1549-
if (this._emitterMode == cc.PARTICLE_MODE_GRAVITY) {
1551+
if (this._emitterMode === cc.PARTICLE_MODE_GRAVITY) {
15501552
var v = cc.p(Math.cos(a), Math.sin(a));
15511553
var s = this.modeA.speed + this.modeA.speedVar * cc.RANDOM_MINUS1_1();
15521554

@@ -1566,8 +1568,7 @@ cc.ParticleSystem = cc.Node.extend(/** @lends cc.ParticleSystem# */{
15661568
var endRadius = this.modeB.endRadius + this.modeB.endRadiusVar * cc.RANDOM_MINUS1_1();
15671569

15681570
particle.modeB.radius = startRadius;
1569-
1570-
if (this.modeB.endRadius == cc.PARTICLE_START_RADIUS_EQUAL_TO_END_RADIUS) {
1571+
if (this.modeB.endRadius === cc.PARTICLE_START_RADIUS_EQUAL_TO_END_RADIUS) {
15711572
particle.modeB.deltaRadius = 0;
15721573
} else {
15731574
particle.modeB.deltaRadius = (endRadius - startRadius) / particle.timeToLive;
@@ -1585,8 +1586,6 @@ cc.ParticleSystem = cc.Node.extend(/** @lends cc.ParticleSystem# */{
15851586
this._isActive = false;
15861587
this._elapsed = this._duration;
15871588
this._emitCounter = 0;
1588-
1589-
this._particlePool = [];
15901589
},
15911590

15921591
/**
@@ -1595,10 +1594,8 @@ cc.ParticleSystem = cc.Node.extend(/** @lends cc.ParticleSystem# */{
15951594
resetSystem:function () {
15961595
this._isActive = true;
15971596
this._elapsed = 0;
1598-
for (this._particleIdx = 0; this._particleIdx < this._particleCount; ++this._particleIdx) {
1599-
var p = this._particles[this._particleIdx];
1600-
p.timeToLive = 0;
1601-
}
1597+
for (this._particleIdx = 0; this._particleIdx < this._particleCount; ++this._particleIdx)
1598+
this._particles[this._particleIdx].timeToLive = 0 ;
16021599
},
16031600

16041601
/**
@@ -1690,13 +1687,13 @@ cc.ParticleSystem = cc.Node.extend(/** @lends cc.ParticleSystem# */{
16901687
selParticle.pos = cc.pAdd(selParticle.pos, tmp);
16911688
} else {
16921689
// Mode B: radius movement
1693-
1690+
var selModeB = selParticle.modeB;
16941691
// Update the angle and radius of the particle.
1695-
selParticle.modeB.angle += selParticle.modeB.degreesPerSecond * dt;
1696-
selParticle.modeB.radius += selParticle.modeB.deltaRadius * dt;
1692+
selModeB.angle += selModeB.degreesPerSecond * dt;
1693+
selModeB.radius += selModeB.deltaRadius * dt;
16971694

1698-
selParticle.pos.x = -Math.cos(selParticle.modeB.angle) * selParticle.modeB.radius;
1699-
selParticle.pos.y = -Math.sin(selParticle.modeB.angle) * selParticle.modeB.radius;
1695+
selParticle.pos.x = -Math.cos(selModeB.angle) * selModeB.radius;
1696+
selParticle.pos.y = -Math.sin(selModeB.angle) * selModeB.radius;
17001697
}
17011698

17021699
// color
@@ -1745,11 +1742,11 @@ cc.ParticleSystem = cc.Node.extend(/** @lends cc.ParticleSystem# */{
17451742
} else {
17461743
// life < 0
17471744
var currentIndex = selParticle.atlasIndex;
1748-
cc.ArrayRemoveObject(this._particles, selParticle);
1749-
1750-
//cache particle to pool
1751-
this._particlePool.push(selParticle);
1752-
1745+
if(this._particleIdx !== this._particleCount -1){
1746+
var deadParticle = this._particles[this._particleIdx];
1747+
this._particles[this._particleIdx] = this._particles[this._particleCount -1];
1748+
this._particles[this._particleCount -1] = deadParticle;
1749+
}
17531750
if (this._batchNode) {
17541751
//disable the switched particle
17551752
this._batchNode.disableParticle(this._atlasIndex + currentIndex);

cocos2d/particle_nodes/CCParticleSystemQuad.js

Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,9 @@ cc.ParticleSystemQuad = cc.ParticleSystem.extend(/** @lends cc.ParticleSystemQua
116116
}
117117
}
118118

119+
if(cc.renderContextType === cc.CANVAS)
120+
return;
121+
119122
var left, bottom, right, top;
120123
if (cc.FIX_ARTIFACTS_BY_STRECHING_TEXEL) {
121124
left = (rect.origin.x * 2 + 1) / (wide * 2);
@@ -373,15 +376,38 @@ cc.ParticleSystemQuad = cc.ParticleSystem.extend(/** @lends cc.ParticleSystemQua
373376
} else
374377
quad = this._quads[this._particleIdx];
375378

376-
var color = (this._opacityModifyRGB) ?
377-
{r: 0 | (particle.color.r * particle.color.a * 255), g: 0 | (particle.color.g * particle.color.a * 255),
378-
b: 0 | (particle.color.b * particle.color.a * 255), a: 0 | (particle.color.a * 255)} :
379-
{r: 0 | (particle.color.r * 255), g: 0 | (particle.color.g * 255), b: 0 | (particle.color.b * 255), a: 0 | (particle.color.a * 255)};
379+
var r, g, b,a;
380+
if(this._opacityModifyRGB){
381+
r = 0 | (particle.color.r * particle.color.a * 255);
382+
g = 0 | (particle.color.g * particle.color.a * 255);
383+
b = 0 | (particle.color.b * particle.color.a * 255);
384+
a = 0 | (particle.color.a * 255);
385+
}else{
386+
r = 0 | (particle.color.r * 255);
387+
g = 0 | (particle.color.g * 255);
388+
b = 0 | (particle.color.b * 255);
389+
a = 0 | (particle.color.a * 255);
390+
}
391+
392+
quad.bl.colors.r = r;
393+
quad.bl.colors.g = g;
394+
quad.bl.colors.b = b;
395+
quad.bl.colors.a = a;
396+
397+
quad.br.colors.r = r;
398+
quad.br.colors.g = g;
399+
quad.br.colors.b = b;
400+
quad.br.colors.a = a;
380401

381-
quad.bl.colors = color;
382-
quad.br.colors = color;
383-
quad.tl.colors = color;
384-
quad.tr.colors = color;
402+
quad.tl.colors.r = r;
403+
quad.tl.colors.g = g;
404+
quad.tl.colors.b = b;
405+
quad.tl.colors.a = a;
406+
407+
quad.tr.colors.r = r;
408+
quad.tr.colors.g = g;
409+
quad.tr.colors.b = b;
410+
quad.tr.colors.a = a;
385411

386412
// vertices
387413
var size_2 = particle.size / 2;
@@ -591,17 +617,17 @@ cc.ParticleSystemQuad = cc.ParticleSystem.extend(/** @lends cc.ParticleSystemQua
591617
this._allocMemory();
592618
this.initIndices();
593619
this.setTexture(oldBatch.getTexture());
594-
if (cc.TEXTURE_ATLAS_USE_VAO)
595-
this._setupVBOandVAO();
596-
else
597-
this._setupVBO();
620+
//if (cc.TEXTURE_ATLAS_USE_VAO)
621+
// this._setupVBOandVAO();
622+
//else
623+
this._setupVBO();
598624
} else if (!oldBatch) {
599-
// OLD: was it self render ? cleanup
625+
// OLD: was it self render cleanup ?
600626
// copy current state to batch
601-
this._batchNode.getTextureAtlas().insertQuads(this._quads, this._atlasIndex, this._quads.length);
627+
this._batchNode.getTextureAtlas()._copyQuadsToTextureAtlas(this._quads, this._atlasIndex);
602628

603629
//delete buffer
604-
cc.renderContext.deleteBuffer(this._buffersVBO[1]);
630+
cc.renderContext.deleteBuffer(this._buffersVBO[1]); //where is re-bindBuffer code?
605631

606632
//if (cc.TEXTURE_ATLAS_USE_VAO)
607633
// glDeleteVertexArrays(1, this._VAOname);
@@ -638,12 +664,15 @@ cc.ParticleSystemQuad = cc.ParticleSystem.extend(/** @lends cc.ParticleSystemQua
638664
for (var i = 0; i < this._totalParticles; i++)
639665
this._particles[i].atlasIndex = i;
640666
}
641-
642667
this.initIndices();
643668
//if (cc.TEXTURE_ATLAS_USE_VAO)
644669
// this._setupVBOandVAO();
645670
//else
646671
this._setupVBO();
672+
673+
//set the texture coord
674+
var size = this._texture.getContentSize();
675+
this.initTexCoordsWithRect(cc.rect(0, 0, size.width, size.height));
647676
} else
648677
this._totalParticles = tp;
649678
this.resetSystem();

0 commit comments

Comments
 (0)