Search Unity

Question VFX multiple streams in single graph, how to handle spawning?

Discussion in 'Visual Effect Graph' started by albright888, Nov 8, 2022.

  1. albright888

    albright888

    Joined:
    Nov 24, 2021
    Posts:
    5
    Hello,

    I am looking to remove the overhead of instantiating hundreds of vfx graph prefabs.

    I need to create hundreds of particle 'streams' each flowing from a different Point A -> Point B at rate X particles/s.

    My goal is to create a single vfx graph which a high particle send rate (sum of all stream rates) and simply distribute the particles to a particular stream upon initialization.
    I am close. I have used a GraphicsBuffer to send the data for N streams (startPos, endPos, particleRate), and can evenly distribute the Spawning particles (modulus on the particleId) to each stream. What I haven't been able to figure out is how to distribute the spawned particles based on a probability (and random #) to approximate the different rates in each stream.
    I see there is a Probability Sampling node, but the number of Entries can only be manually entered (and maxes at 32)

    Any brilliant ideas out there on how to distribute the spawned particles or maybe there is there a different way to spawn particles, somehow create a spawner per stream?

    Thanks!
     
  2. OrsonFavrel

    OrsonFavrel

    Unity Technologies

    Joined:
    Jul 25, 2022
    Posts:
    194
    Morning @albright888 .
    Could you explain a little bit more why do you want to use a single VFXGraph?
    The instancing Feature has come with Unity 22.2 and should allow better performances when having multiples VFXGraphs.
    Having only one VFXGraph might be more cumbersome to Edit and iterate on. Also you might gonna have some issue with particle culling.

    I'm really curious about your use case and the reason you decided to go the "Single VFXGraph" road.
     
  3. Qriva

    Qriva

    Joined:
    Jun 30, 2019
    Posts:
    1,314
    Note that not everyone can upgrade Unity to the latest version. Many people are locked with 2021, 2020 or sometimes even older versions. Also, even if you are not locked, version 2022.2 is in beta and to be honest, even after release these versions are often "broken" in multiple ways, so it is often better to wait for more final release.
    That said, creating many instances of visual effects in older versions is very performence consuming and it was even worse before dispatch fix of not playing instances. For this reason having some master vfx used to spawn things globally was the only option to keep good performence. Example use case: you have 200 fighting units and instead of spawning game object for each hit (just to spawn 10 particles) you just send event "spawn hit here" to master vfx and it's done, no pooling, no object spawning cost.
     
    OrsonFavrel likes this.
  4. albright888

    albright888

    Joined:
    Nov 24, 2021
    Posts:
    5
    I appreciate the discussion. Here's a little more light on my project. I am creating a visualization of data transmission between hundreds of network nodes, where each node can stream data to multiple neighbors. I've been able to use VFX graph with separate instances for each stream, but after reading several forum posts, and as mentioned by Qriva above, I was beginning to understand that using a single VFX graph would remove a lot of overhead of creating and managing hundreds of instances. I'm also curious if it would be better to pre-create all instances and turn them off and on, or create and destroy them as needed.
    I'm very intrigued with the 'instancing feature' in 2022.2, does it require a coding change on my side, or is it a performance boost happening behind the scenes?
     
  5. OrsonFavrel

    OrsonFavrel

    Unity Technologies

    Joined:
    Jul 25, 2022
    Posts:
    194
    I totally understand that not everyone as the option to upgrade to newer version of Unity and you're right about all the performance issues. I was not trying to dismiss this solution which can be pretty valid and sadly sometime the only and best solution..
    Just wanted to make sure that this path was taken as it was the only solution cause I know that it can be hard to manage, edit this kind of heavy master graph.
     
    Qriva likes this.
  6. fleity

    fleity

    Joined:
    Oct 13, 2015
    Posts:
    345
    could it be possible that your usecase can be simplified by providing the data differently?

    something along the lines of bake the connections between the nodes your are visualizing into a mesh, directions into vertex color and then in a single vfx sample that mesh, spawn a large amount of particles and move them along the generated mesh strips?
    Sure it's a bit of work to generate that mesh first and it's a bit annoying to have to re-bake that mesh every now and then but then it should be kind of easy?
     
  7. albright888

    albright888

    Joined:
    Nov 24, 2021
    Posts:
    5
    Yes, interesting @fleity. On first glance it seems similar to using the GraphicsBuffer as the transport of the network data (nodes, directions, colors) into the master VFX graph. I'm curious about the 'spawn a large amount of particles along the mesh strips', do you a see a way using this method to tell the VFX graph to send 2000 particles/sec along segment A, and 100 particles/sec along segment B? (coming from the spawn node generating 2100 particles/sec) I've been stumped about this uneven distribution of the particles to the different segments.
     
  8. Qriva

    Qriva

    Joined:
    Jun 30, 2019
    Posts:
    1,314
    When we talk about performence, important note here - it depends on application, and when people are talking about this, it's often about applications where this is critical to keep smooth framerate. However (I don't say it is the case) this can be no so important when you need to visualize data and that kind of stuff. Just do what makes your job done. In game you have multiple other systems running at the same time and frame budget is very limited, but if your application does not have much to do it might be completely legit way to just spawn separate instances.
    Long story short it has some constraints, but does not require any fancy stuff - it's rather behind the scenes.