Skip to content

Commit

Permalink
v2.7.2 Fix the performance issue of the color anim
Browse files Browse the repository at this point in the history
May require more investigation since the evaluate method may work with  more setup/test
  • Loading branch information
sarbian committed Feb 7, 2017
1 parent c0565d5 commit 71e4aea
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 22 deletions.
23 changes: 5 additions & 18 deletions ModelMultiShurikenPersistFX.cs
Expand Up @@ -627,27 +627,14 @@ public override void OnInitialize()
ParticleSystem.ColorOverLifetimeModule col = particleSystem.colorOverLifetime;

// This one is annoying. The old particle system animate the color over the whole % life of the particle (0 - 1)
// The new one animate it over time. So converted system may not reac the full value
// The animation is set up here but disabled and the color is manually set in the update of PersistentKSPShurikenEmitter with this gradient
// The new one animate it over time. So converted system may not reach the full value
// The color is manually set in the update of PersistentKSPShurikenEmitter
col.enabled = false;

GradientColorKey[] colorKeys = new GradientColorKey[5];
GradientAlphaKey[] alphaKeys = new GradientAlphaKey[5];

Color[] colors = childKSPParticleEmitter.colorAnimation;

float step = 1f / (colors.Length - 1);

for (int t = 0; t < colors.Length; t++)
{
colorKeys[t] = new GradientColorKey(colors[t], 1 - t * step);
alphaKeys[t] = new GradientAlphaKey(colors[t].a, 1 - t * step);
}

Gradient gradient = new Gradient();
gradient.SetKeys(colorKeys, alphaKeys);

col.color = new ParticleSystem.MinMaxGradient(gradient);

pkpe.colors = new Color[colors.Length];
Array.Copy(colors, pkpe.colors, colors.Length);
}

//try
Expand Down
35 changes: 33 additions & 2 deletions PersistentKSPShurikenEmitter.cs
Expand Up @@ -75,6 +75,7 @@ public class PersistentKSPShurikenEmitter
public Vector3 rndForce;
public Color color;
public bool doesAnimateColor;
public Color[] colors;

//private float emitterVelocityScale;
private Vector3 rndVelocity;
Expand Down Expand Up @@ -547,8 +548,38 @@ public void EmitterOnUpdate(Vector3 emitterWorldVelocity)

if (doesAnimateColor)
{
float lifePercentage = (particle.lifetime / particle.startLifetime);
particle.startColor = pe.colorOverLifetime.color.Evaluate(lifePercentage);
float lifePercentage = 1 - (particle.lifetime / particle.startLifetime);

float lerp;
Color a;
Color b;
if (lifePercentage < 0.25f)
{
a = colors[0];
b = colors[1];
lerp = lifePercentage * 4f;
}
else if (lifePercentage < 0.50f)
{
a = colors[1];
b = colors[2];
lerp = (lifePercentage - 0.25f) * 4f;
}
else if (lifePercentage < 0.75f)
{
a = colors[2];
b = colors[3];
lerp = (lifePercentage - 0.50f) * 4f;
}
else
{
a = colors[3];
b = colors[4];
lerp = (lifePercentage - 0.75f) * 4f;
}

Color c = Color.Lerp(a, b, lerp);
particle.startColor = c;
}
}
particles[j] = particle;
Expand Down
4 changes: 2 additions & 2 deletions Properties/AssemblyInfo.cs
Expand Up @@ -34,5 +34,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("2.7.1.0")]
[assembly: AssemblyFileVersion("2.7.1.0")]
[assembly: AssemblyVersion("2.7.2.0")]
[assembly: AssemblyFileVersion("2.7.2.0")]

0 comments on commit 71e4aea

Please sign in to comment.