Search Unity

Is It Possible to Update Particle Count in Realtime?

Discussion in 'Visual Effect Graph' started by Red-Sprites, Mar 25, 2022.

  1. Red-Sprites

    Red-Sprites

    Joined:
    Jun 24, 2018
    Posts:
    8
    I'm trying to create an effect where the grid count can increase or decrease. I want this number to be controlled by a public value, but it doesn't update until I reset the effect in some way. Most specifically, if I change something in the "General" setting in the VFX inspector.

    I want to change the particle count in the Spawn system. But it doesn't change in in real-time as stated above.

    sample-clip-04-F.gif
    Screenshot 2022-03-25 182717.png
     
    Last edited: Mar 25, 2022
  2. Red-Sprites

    Red-Sprites

    Joined:
    Jun 24, 2018
    Posts:
    8
    Honestly, if it's possible to change the particle "capacity", then that'll be just as good for me.

    Screenshot 2022-03-26 104001.png
     
  3. Qriva

    Qriva

    Joined:
    Jun 30, 2019
    Posts:
    1,315
    It's not possible to change capacity, this value is needed to preallocate memory.

    I assume each segment is one particle with texIndex, you spawn X particles in shape of grid and then you would like to change the number of particles. The problem is that single burst does not care as this value is used only to spawn particles. Solution depends on your use case (you would need to describe better how your effect works), but for example you can: spawn maximum number of particles with infinite lifetime at the beginning, and then hide particles in output context with
    alive = false
    based on some property
    visibleParticleCount
    .
     
  4. Red-Sprites

    Red-Sprites

    Joined:
    Jun 24, 2018
    Posts:
    8
    Thank you for the response.

    I have to mention that I'm using Unity differently, mainly for animation. So I'm not as familiar with coding.

    The effect is done entirely with VFX graph. The effect is created using the sequential: Three Dimensional node placed in the 'Update' part of the VFX graph. You can look at the entire graph here:

    screenshot_06.png

    I've seen this 'Reinit' function, but I don't have the slightest clue how to use it. If I could get it to work, could it create the effect that I'm talking about?
     
  5. Qriva

    Qriva

    Joined:
    Jun 30, 2019
    Posts:
    1,315
    I am not sure what that means :D, but there are many effects requiring at least a bit of coding, so you might need to get some knowledge about this. In theory you could use reinit to make it work, but I think what I described above is still better and contained in single graph. Create new empty graph and do this:
    1. In spawn context create only
      Single Burst
      with amount of particles equal to system maximum capacity.
    2. In initialize context set Lifetime to any value larger than 0 and add
      Set Sequential Position
      to your liking
    3. Click update context and in inspector disable "Reap Particles" and "Age Particles", this will make your particle immortal.
    4. Create graph property "Count", this will be amount of particles to display, and then do this:
    upload_2022-3-28_13-49-4.png

    This will kill all particles with index greater than required display count, but we do it in output context, so they die only during this frame, not permanently - in result we "hide" them.
    Now if you change Count property it should display number of particles equal to this property.
     
  6. Qriva

    Qriva

    Joined:
    Jun 30, 2019
    Posts:
    1,315
    When we are already on this topic, question to @VladVNeykov or other vfx graph dev, are all particles always simulated? If there is system with capacity 10.000 and there are 100 "real" particles then only these 100 are simulated or all 10.000?
     
  7. VladVNeykov

    VladVNeykov

    Unity Technologies

    Joined:
    Sep 16, 2016
    Posts:
    550
    Hi, I'll paraphrase what @JulienF_Unity shared in a different post a while back (and hope it hasn't changed much :))

    The simulation is done on the whole buffer. There are some edge cases like if we know the maximum number on the CPU (e.g. a system with immortal particles) then the simulation is performed only on those.

    If a particle is dead, it's simply a single check and return, but you still don't want to use a system of 1 particle with a 1M capacity as we account for the worst case of all particles being alive.

    Groups with only dead particles won't cost much, but it's wasteful, and the capacity should be set close to the max expected number.

    In some scenarios you can also toggle Indirect Draw (which is implicitly on when sorting or compute culling is on) to tweak the performance based on your capacity needs. In the 100 out of 10,000 scenario, it should be more performant to have Indirect Draw on.
     

    Attached Files:

    Qriva likes this.