Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Explicitly simulating particles to generate trails

Discussion in 'General Graphics' started by ferretnt, Mar 22, 2021.

  1. ferretnt

    ferretnt

    Joined:
    Apr 10, 2012
    Posts:
    412
    Suppose that we want to draw a 3D graph in the world, with lots of separate lines, using the particle system. This is possible by using a particle system and the trails module to generate a trail. However, to do so, you have to use a coroutine and:

    (0) Call ParticleSystem.Clear().
    (1) Call ParticleSystem.SetParticles() to set all particles to their start position
    (2) yield return null;
    (3) Call ParticleSystem.SetParticles to set all particles to their final position.

    That works fine, but we'd really like to be able to do this SetPosition() / SetPosition in a single frame by explicitly calling ParticleSystem.Update(). This is because we animate many different graphs using this technique (our users are actually scrubbing forward and backward with a slider through a complex 3D graph of data.) Since it takes a minimum of 2 frames to evaluate a trail, we get a weird flickering effect when doing this.

    We have tried many, many permutations of replacing the coroutine with an explicit ParticleSystem.Simulate(), but have not managed to get the trails to evaluate for any of them. It looks like there is now a PS.GetTrails() / SetTrails(), however that only helps if we know all the possible states of the graph that we want to pre-cache trail state for, and we don't.

    Is there any way to do this?

    PS. I do realize we're effectively using the particle system as a way to draw several million camera-facing 3D lines. We'd be open to do it another way, but as far as I can see the LineRenderer and TrailRenderer components only draw a single connected line, and creating a million such components isn't viable.
     
  2. richardkettlewell

    richardkettlewell

    Unity Technologies

    Joined:
    Sep 9, 2015
    Posts:
    2,281
    You’re right about line/trail Renderer not being suitable for this.

    The particle system trails rely on time passing to add points to the trails. That said, I think it should work with calls to ParticleSystem.Simulate, and no yielding.

    Another option would be to create a particle system, add a subemitter, in the subemitter, enable trails, choose ribbon mode, set split subemitter ribbons. Now, in the parent, emit particles and have them move along the desired line of the graph. Either do this with the modules and the subemitter set to birth, or via SetParticles with a call to TriggerSubEmitter after each SetParticles call. For this, set the subemitter mode to Manual.

    I’m throwing out some ideas here that may or may not work, as it’s not a use case Ive tried before. Let me know if any of it helps!

    And a final idea: try the Visual Effect Graph package. It has trails too. Maybe it will be possible to do what you want there. I’ve no idea about that though :)
     
  3. ferretnt

    ferretnt

    Joined:
    Apr 10, 2012
    Posts:
    412
    Thanks Richard. I will file a bug just so you have the repro, because in general it would be great to manually tick the ParticleSystem and get the same results as yielding, but it sounds like I should just write my own mesh generation and update code for my specific case. I was expecting a manual simulate with Time.deltaTime to do the same as a mid-function yield, but it definitely doesn't.

    The reason I haven't is that it's always more time than you think to make a well-written, optimized, continuously-camera facing thick-line renderer that does what you want on all the platforms as well as the particle system will. (There are lots of such things on the assetstore, but none of them gets anywhere near the goemetry we want.) These days having burst helps a lot though :)

    I haven't looked at VFXGraph yet, but will do. This project has to run on a range of platforms, including mobile WebGL, and I was under the impression (through ignorance) that VFXGraph is targeted more at the type of project that would use HDRP.
     
  4. richardkettlewell

    richardkettlewell

    Unity Technologies

    Joined:
    Sep 9, 2015
    Posts:
    2,281
    Yeah I agree about ticking simulate - thanks for the bug report. Please reply with the case number once you have it.

    and you’re right about vfx graph - you can’t use it on mobile webgl, as far as I’m aware.