Search Unity

ParticleSystem.GetParticles() order!

Discussion in 'General Graphics' started by adamgryu, Mar 27, 2020.

  1. adamgryu

    adamgryu

    Joined:
    Mar 1, 2014
    Posts:
    188
    Hiya! I'm using the particle trails module in ribbon mode to get a nice line that goes from the oldest particle to the newest. I want to get the particle data in that order, so I can build some procedural meshes along the trail.

    When I call ParticleSystem.GetParticles(), the order of the particles seems random. I can use a sorting algorithm to sort the particles, but it takes too long and I need to save as many milliseconds as possible! I know the information is in there somewhere (the trail is rendered along the particles) but I don't know how to access it!

    BTW I'm on Unity 2018.4, and ParticleSystem.GetTrails() doesn't exist yet (it also isn't documented so I don't even know if it will give me what I need).

    Any help or ideas would be appreciated!
     
  2. richardkettlewell

    richardkettlewell

    Unity Technologies

    Joined:
    Sep 9, 2015
    Posts:
    2,285
    Yeah sorry the order isnt reliably defined.

    If you have a constant lifetime, then I think you end up with oldest to newest order in the data, until the particles begin dying, then a new set of particles will stripe through the data.

    But if multiple particles die/are born on the same frame then once again the order may diverge quite badly.

    The trail rendering builds an index list and sorts that using the particle age. You could do the same, but it wouldn’t be terribly fast.

    If you were on Unity 2019 you could the c# job system instead of GetParticles, to make it pretty quick, but again not an option if you’re on 2018.4 :(

    Whatever you end up doing, sort indices, not the particle data. Moving those fat particle structures around will be very slow.
     
    karl_jones and adamgryu like this.
  3. adamgryu

    adamgryu

    Joined:
    Mar 1, 2014
    Posts:
    188
    Thanks for letting me know my options!

    I noticed copying the particles was super slow. If I do an in-place heapsort over the indices performance is OK enough. I'll try running with this. If I can't get it fast enough, I may have to try writing a skeleton particle system where I have control over this.
     
    richardkettlewell likes this.