- Home /
Need tips for optimizing main func that loops through thousands of particles
Hi!
I'm trying to create fractal images with the following equation.
And it should look something like this but in 3D
I've got everything right, and my script is below. But, I want to run 5 - 8 particle systems at once so I can create this effect! I've ran the profiler and the CPU spikes when it's calling GenerateOrbit(); I know the main concern is the fact that I'm looping through every particle to change their positions, but I'm not really sure if there's an alternative. Perhaps there is a better way of creating 9 particle systems by using fabrication & instantiating?
#pragma strict
var particleCount : int = 4000;
function Start ()
{
GenerateOrbit();
}
function FixedUpdate(){
GenerateOrbit();
}
function GenerateOrbit(){
// Figure out which range is best for BIG fractals
var a : int = Random.Range(-1,5);
var b : int = Random.Range(-11,2);
var c : int = Random.Range(-10,20);
var myParticleSystem : ParticleSystem;
var myParticles: ParticleSystem.Particle[];
myParticleSystem = GetComponent(ParticleSystem);
myParticleSystem.startColor = new Color32(Mathf.Round(Random.Range(0,255)),Mathf.Round(Random.Range(0,255)),Mathf.Round(Random.Range(0,255)),255);
myParticleSystem.Emit(particleCount);
myParticles = new ParticleSystem.Particle[particleCount + 1];
myParticleSystem.GetParticles(myParticles);
var x : float;
var y : float;
var prevPos : Vector3;
for (var i = 0; i < particleCount; i++)
{
// Get previous particle position
prevPos = (i < 1) ? new Vector3(Random.Range(-5,10), Random.Range(-5,10)) : myParticles[i - 1].position; // Perhaps Random.value isn't the best option here?
x = prevPos.x;
y = prevPos.y;
// For the sake of optimizing by not using Mathf.sqrt & Mathf.abs
x_abs = (x > 0 ? x : -x);
var d : float;
d = (b * x + c) > 0 ? (b * x + c) : -(b * x + c);
d = Mathf.Pow(d_abs,0.5);
var x1 = y - x / x_abs * d;
var y1 = a - x;
var newPosition = new Vector3(x1, y1, 0);
myParticles[i].position = newPosition;
myParticleSystem.SetParticles(myParticles, particleCount);
}
yield WaitForSeconds(5);
myParticleSystem.Clear();
}
Ins$$anonymous$$d of doing this every fifth second, do parts of the calculation continually, and then show the results every fifth seconds. That'll spread the calculation over time, so you won't get any lag spikes.
I'm also pretty sure that you can't chear your way out of $$anonymous$$athf.Sqrt(value) by doing $$anonymous$$athf.Pow(value, 0.5f) ins$$anonymous$$d - it's got pretty much the same performance cost.
Just fill the positions of (Time.deltaTime * (4000 / 5)) particles every frame, and you'll have filled all of the 4000 particles after five seconds has passed.
Thanks! That actually seems like a very viable idea. I was thinking of putting this into the Update() function, which calls every frame. I'm having some trouble thinking about how this would work out now that I have 7 particle systems... How would I be updating the same one in each consecutive Update() call? Is it possible?
Your answer
Follow this Question
Related Questions
Custom particle system loop optimization 0 Answers
Optimize Occlusion Culling and Particle Systems in a House sized scene 0 Answers
Trying to create fractals images with particles, but getting weird pattern instead 0 Answers
Optimize Particle Emitter with Massive # of Particles 0 Answers
Particle System Optimization (.IsAlive but more efficient) 1 Answer