Skip to content

Commit

Permalink
performance optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
PrimaryFeather committed Sep 2, 2011
1 parent 769237a commit f773702
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 79 deletions.
16 changes: 8 additions & 8 deletions demo/media/drugs.pex
Expand Up @@ -2,12 +2,12 @@
<particleEmitterConfig>
<texture name="drugs_particle.png"/>
<sourcePosition x="160.00" y="211.72"/>
<sourcePositionVariance x="7.00" y="7.00"/>
<sourcePositionVariance x="30.00" y="30.00"/>
<speed value="98.00"/>
<speedVariance value="211.00"/>
<particleLifeSpan value="3.0000"/>
<particleLifespanVariance value="5.0000"/>
<angle value="357.00"/>
<particleLifeSpan value="4.0000"/>
<particleLifespanVariance value="4.0000"/>
<angle value="360.00"/>
<angleVariance value="190.00"/>
<gravity x="0.70" y="1.43"/>
<radialAcceleration value="0.00"/>
Expand All @@ -19,10 +19,10 @@
<finishColor red="0.79" green="0.85" blue="0.42" alpha="0.57"/>
<finishColorVariance red="0.45" green="0.51" blue="0.26" alpha="0.46"/>
<maxParticles value="600"/>
<startParticleSize value="49.00"/>
<startParticleSizeVariance value="60.00"/>
<finishParticleSize value="31.00"/>
<FinishParticleSizeVariance value="0.00"/>
<startParticleSize value="50.00"/>
<startParticleSizeVariance value="50.00"/>
<finishParticleSize value="30.00"/>
<FinishParticleSizeVariance value="10.00"/>
<duration value="-1.00"/>
<emitterType value="0"/>
<maxRadius value="100.00"/>
Expand Down
12 changes: 6 additions & 6 deletions demo/media/fire.pex
Expand Up @@ -2,11 +2,11 @@
<particleEmitterConfig>
<texture name="fire_particle.png"/>
<sourcePosition x="160.55" y="428.95"/>
<sourcePositionVariance x="160.00" y="0.00"/>
<sourcePositionVariance x="104.41" y="0.00"/>
<speed value="90.00"/>
<speedVariance value="30.00"/>
<particleLifeSpan value="1.5000"/>
<particleLifespanVariance value="2.8289"/>
<particleLifeSpan value="2.0000"/>
<particleLifespanVariance value="1.9000"/>
<angle value="270.37"/>
<angleVariance value="15.00"/>
<gravity x="0.00" y="0.00"/>
Expand All @@ -18,9 +18,9 @@
<startColorVariance red="0.00" green="0.00" blue="0.00" alpha="0.00"/>
<finishColor red="1.00" green="0.31" blue="0.00" alpha="0.00"/>
<finishColorVariance red="0.00" green="0.00" blue="0.00" alpha="0.00"/>
<maxParticles value="800"/>
<startParticleSize value="60.00"/>
<startParticleSizeVariance value="30.32"/>
<maxParticles value="500"/>
<startParticleSize value="70.00"/>
<startParticleSizeVariance value="49.53"/>
<finishParticleSize value="10.00"/>
<FinishParticleSizeVariance value="0.00"/>
<duration value="-1.00"/>
Expand Down
18 changes: 9 additions & 9 deletions demo/media/sun.pex
@@ -1,28 +1,28 @@
<?xml version="1.0"?>
<particleEmitterConfig>
<texture name="sun_particle.png"/>
<sourcePosition x="161.42" y="232.94"/>
<sourcePosition x="157.97" y="228.41"/>
<sourcePositionVariance x="7.00" y="7.00"/>
<speed value="225.00"/>
<speedVariance value="6.58"/>
<speed value="260.00"/>
<speedVariance value="10.00"/>
<particleLifeSpan value="1.0000"/>
<particleLifespanVariance value="0.7000"/>
<angle value="0.00"/>
<angleVariance value="360.00"/>
<gravity x="0.00" y="0.00"/>
<radialAcceleration value="-380.00"/>
<tangentialAcceleration value="-144.74"/>
<tangentialAcceleration value="-140.00"/>
<radialAccelVariance value="0.00"/>
<tangentialAccelVariance value="0.00"/>
<startColor red="1.00" green="0.00" blue="0.00" alpha="1.00"/>
<startColorVariance red="0.00" green="0.00" blue="0.00" alpha="0.00"/>
<finishColor red="1.00" green="1.00" blue="0.00" alpha="1.00"/>
<finishColorVariance red="0.00" green="0.00" blue="0.00" alpha="0.00"/>
<maxParticles value="500"/>
<startParticleSize value="45.05"/>
<startParticleSizeVariance value="29.05"/>
<finishParticleSize value="0.00"/>
<FinishParticleSizeVariance value="0.00"/>
<maxParticles value="600"/>
<startParticleSize value="60.00"/>
<startParticleSizeVariance value="40.00"/>
<finishParticleSize value="5.00"/>
<FinishParticleSizeVariance value="5.00"/>
<duration value="-1.00"/>
<emitterType value="0"/>
<maxRadius value="100.00"/>
Expand Down
4 changes: 2 additions & 2 deletions demo/src/Demo.as
Expand Up @@ -70,7 +70,7 @@ package
// create FPS label

mFrameCount = mFrameTime = 0;
mFrameLabel = new TextField(64, 16, "FPS:", "Arial", 12, 0xffffff);
mFrameLabel = new TextField(64, 16, "FPS: ?", "Arial", 12, 0xffffff);
mFrameLabel.hAlign = HAlign.LEFT;
addChild(mFrameLabel);

Expand Down Expand Up @@ -98,7 +98,7 @@ package
mFrameCount++;
mFrameTime += event.passedTime;

if (mFrameTime > 1)
if (mFrameTime > 2)
{
mFrameLabel.text = "FPS: " + int(mFrameCount / mFrameTime);
mFrameTime = mFrameCount = 0;
Expand Down
2 changes: 0 additions & 2 deletions src/starling/extensions/Particle.as
Expand Up @@ -27,7 +27,5 @@ package starling.extensions
totalTime = alpha = scale = 1.0;
color = 0xffffff;
}

public function get isComplete():Boolean { return currentTime >= totalTime; }
}
}
91 changes: 46 additions & 45 deletions src/starling/extensions/ParticleDesignerPS.as
Expand Up @@ -82,54 +82,77 @@ package starling.extensions
{
var particle:PDParticle = aParticle as PDParticle;

var lifespan:Number = getRandomVariance(mLifespan, mLifespanVariance);
// for performance reasons, the random variances are calculated inline instead
// of calling a function

var lifespan:Number = mLifespan + mLifespanVariance * (Math.random() * 2.0 - 1.0);
if (lifespan <= 0.0) return;

particle.currentTime = 0.0;
particle.totalTime = lifespan;

particle.x = getRandomVariance(mEmitterX, mEmitterXVariance);
particle.y = getRandomVariance(mEmitterY, mEmitterYVariance);
particle.x = mEmitterX + mEmitterXVariance * (Math.random() * 2.0 - 1.0);
particle.y = mEmitterY + mEmitterYVariance * (Math.random() * 2.0 - 1.0);
particle.startX = mEmitterX;
particle.startY = mEmitterY;

var angle:Number = getRandomVariance(mEmitAngle, mEmitAngleVariance);
var speed:Number = getRandomVariance(mSpeed, mSpeedVariance);
var angle:Number = mEmitAngle + mEmitAngleVariance * (Math.random() * 2.0 - 1.0);
var speed:Number = mSpeed + mSpeedVariance * (Math.random() * 2.0 - 1.0);
particle.velocityX = speed * Math.cos(angle);
particle.velocityY = speed * Math.sin(angle);

particle.radius = getRandomVariance(mMaxRadius, mMaxRadiusVariance);
particle.radius = mMaxRadius + mMaxRadiusVariance * (Math.random() * 2.0 - 1.0);
particle.radiusDelta = mMaxRadius / lifespan;
particle.rotation = getRandomVariance(mEmitAngle, mEmitAngleVariance);
particle.rotationDelta = getRandomVariance(mRotatePerSecond, mRotatePerSecondVariance);
particle.rotation = mEmitAngle + mEmitAngleVariance * (Math.random() * 2.0 - 1.0);
particle.rotationDelta = mRotatePerSecond + mRotatePerSecondVariance * (Math.random() * 2.0 - 1.0);
particle.radialAcceleration = mRadialAcceleration;
particle.tangentialAcceleration = mTangentialAcceleration;

var startSize:Number = Math.max(0.1, getRandomVariance(mStartSize, mStartSizeVariance));
var endSize:Number = Math.max(0.1, getRandomVariance(mEndSize, mEndSizeVariance));
var startSize:Number = mStartSize + mStartSizeVariance * (Math.random() * 2.0 - 1.0);
var endSize:Number = mEndSize + mEndSizeVariance * (Math.random() * 2.0 - 1.0);
if (startSize < 0.1) startSize = 0.1;
if (endSize < 0.1) endSize = 0.1;
particle.scale = startSize / texture.width;
particle.scaleDelta = ((endSize - startSize) / lifespan) / texture.width;

var startColor:ColorArgb = getRandomColorVariance(mStartColor, mStartColorVariance);
var endColor:ColorArgb = getRandomColorVariance(mEndColor, mEndColorVariance);
// colors

var startColor:ColorArgb = particle.colorArgb;
var colorDelta:ColorArgb = particle.colorArgbDelta;

startColor.red = mStartColor.red;
startColor.green = mStartColor.green;
startColor.blue = mStartColor.blue;
startColor.alpha = mStartColor.alpha;

if (mStartColorVariance.red != 0) startColor.red += mStartColorVariance.red * (Math.random() * 2.0 - 1.0);
if (mStartColorVariance.green != 0) startColor.green += mStartColorVariance.green * (Math.random() * 2.0 - 1.0);
if (mStartColorVariance.blue != 0) startColor.blue += mStartColorVariance.blue * (Math.random() * 2.0 - 1.0);
if (mStartColorVariance.alpha != 0) startColor.alpha += mStartColorVariance.alpha * (Math.random() * 2.0 - 1.0);

var colorDelta:ColorArgb = new ColorArgb();
colorDelta.red = (endColor.red - startColor.red) / lifespan;
colorDelta.green = (endColor.green - startColor.green) / lifespan;
colorDelta.blue = (endColor.blue - startColor.blue) / lifespan;
colorDelta.alpha = (endColor.alpha - startColor.alpha) / lifespan;
var endColorRed:Number = mEndColor.red;
var endColorGreen:Number = mEndColor.green;
var endColorBlue:Number = mEndColor.blue;
var endColorAlpha:Number = mEndColor.alpha;

if (mEndColorVariance.red != 0) endColorRed += mEndColorVariance.red * (Math.random() * 2.0 - 1.0);
if (mEndColorVariance.green != 0) endColorGreen += mEndColorVariance.green * (Math.random() * 2.0 - 1.0);
if (mEndColorVariance.blue != 0) endColorBlue += mEndColorVariance.blue * (Math.random() * 2.0 - 1.0);
if (mEndColorVariance.alpha != 0) endColorAlpha += mEndColorVariance.alpha * (Math.random() * 2.0 - 1.0);

particle.colorArgb = startColor;
particle.colorArgbDelta = colorDelta;
colorDelta.red = (endColorRed - startColor.red) / lifespan;
colorDelta.green = (endColorGreen - startColor.green) / lifespan;
colorDelta.blue = (endColorBlue - startColor.blue) / lifespan;
colorDelta.alpha = (endColorAlpha - startColor.alpha) / lifespan;
}

protected override function advanceParticle(aParticle:Particle, passedTime:Number):void
{
var particle:PDParticle = aParticle as PDParticle;

passedTime = Math.min(passedTime, particle.totalTime - particle.currentTime);
var restTime:Number = particle.totalTime - particle.currentTime;
passedTime = restTime > passedTime ? passedTime : restTime;
particle.currentTime += passedTime;
var timeToLive:Number = particle.totalTime - particle.currentTime;

if (mEmitterType == EMITTER_TYPE_RADIAL)
{
Expand All @@ -145,8 +168,8 @@ package starling.extensions
{
var distanceX:Number = particle.x - particle.startX;
var distanceY:Number = particle.y - particle.startY;
var distanceScalar:Number =
Math.max(0.01, Math.sqrt(distanceX * distanceX + distanceY * distanceY));
var distanceScalar:Number = Math.sqrt(distanceX*distanceX + distanceY*distanceY);
if (distanceScalar < 0.01) distanceScalar = 0.01;

var radialX:Number = distanceX / distanceScalar;
var radialY:Number = distanceY / distanceScalar;
Expand Down Expand Up @@ -248,28 +271,6 @@ package starling.extensions
}
}
}

// utility functions

private function clamp(x:Number, a:Number, b:Number):Number
{
return x < a ? a : (x > b ? b : x);
}

private function getRandomVariance(base:Number, variance:Number):Number
{
return base + variance * (Math.random() * 2.0 - 1.0);
}

private function getRandomColorVariance(base:ColorArgb, variance:ColorArgb):ColorArgb
{
var color:ColorArgb = new ColorArgb();
color.red = clamp(getRandomVariance(base.red, variance.red), 0.0, 1.0);
color.green = clamp(getRandomVariance(base.green, variance.green), 0.0, 1.0);
color.blue = clamp(getRandomVariance(base.blue, variance.blue), 0.0, 1.0);
color.alpha = clamp(getRandomVariance(base.alpha, variance.alpha), 0.0, 1.0);
return color;
}
}
}

Expand Down
16 changes: 9 additions & 7 deletions src/starling/extensions/ParticleSystem.as
Expand Up @@ -175,9 +175,9 @@ package starling.extensions

while (particleIndex < mNumParticles)
{
particle = mParticles[particleIndex];
particle = mParticles[particleIndex] as Particle;

if (!particle.isComplete)
if (particle.currentTime < particle.totalTime)
{
advanceParticle(particle, passedTime);
++particleIndex;
Expand All @@ -186,7 +186,7 @@ package starling.extensions
{
if (particleIndex != mNumParticles - 1)
{
var nextParticle:Particle = mParticles[mNumParticles - 1];
var nextParticle:Particle = mParticles[mNumParticles - 1] as Particle;
mParticles[mNumParticles-1] = particle;
mParticles[particleIndex] = nextParticle;
}
Expand All @@ -207,7 +207,7 @@ package starling.extensions
if (mNumParticles == capacity)
raiseCapacity(capacity);

particle = mParticles[mNumParticles++];
particle = mParticles[mNumParticles++] as Particle;
initParticle(particle);
advanceParticle(particle, mFrameTime);

Expand All @@ -225,17 +225,19 @@ package starling.extensions
var alpha:Number;
var x:Number, y:Number;
var xOffset:Number, yOffset:Number;
var textureWidth:Number = mTexture.width;
var textureHeight:Number = mTexture.height;

for (var i:int=0; i<mNumParticles; ++i)
{
vertexID = i << 2;
particle = mParticles[i];
particle = mParticles[i] as Particle;
color = particle.color;
alpha = particle.alpha;
x = particle.x;
y = particle.y;
xOffset = mTexture.width * particle.scale / 2;
yOffset = mTexture.height * particle.scale / 2;
xOffset = textureWidth * particle.scale / 2;
yOffset = textureHeight * particle.scale / 2;

for (var j:int=0; j<4; ++j)
mVertexData.setColor(vertexID+j, color, alpha);
Expand Down

0 comments on commit f773702

Please sign in to comment.