Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Showcase Solution for Handling Multiple SendEvent() per frame (2020.3/2021.3)

Discussion in 'Visual Effect Graph' started by peeweekVFX, Oct 23, 2022.

  1. peeweekVFX

    peeweekVFX

    Joined:
    Oct 19, 2015
    Posts:
    9
    Hi,

    I'm dropping a message here so maybe it could be useful to someone.

    upload_2022-10-23_9-12-32.png

    I've recently added two C# custom spawners to my open source VFX Graph Extras project. These blocks enable managing multiple events incoming on a spawn context at the same frame, while keeping each event's VFXEventAttribute state :

    - One is called Burst Multiplex FIFO, and handles multiple burst of particles
    - Other is called Rate Multiplex FIFO, and handles rate of particles as multiple sources.

    Of course if you don't want to install the whole package, you can get these files here:
    - https://github.com/peeweek/net.peew.../Runtime/CustomSpawners/BurstMultiplexFIFO.cs
    - https://github.com/peeweek/net.peew...r/Runtime/CustomSpawners/RateMultiplexFIFO.cs

    Disclaimer and limitations

    1) Use only for Low-Scale :These blocks does not have a goal of being a large-scale solution, and is probably less efficient than having a fully-fledged solution. While these can handle few events each frame, they are not intended for handlings hundreds/thousands of events.

    Correct built-in solutions could be -for instance- having a synchronous SpawnEvent context to be used in direct link (for instance to handle various spawn counts in particle systems on a single direct link event), or virtualize spawn contexts to handle rate correctly.

    2) Incorrect Reading of Event Attributes in Operators: The values you read in the graph from the Attributes in the spawn context will be incorrect.
    Explanation : As these blocks iterate/cycle through a list of VFXEventAttribute, they change the block's VFXEventAttribute state and thus, the values you read are always one frame late, so you don't read the value of the current event/source, but the one from the previous iteration.

    3) Do not mix with other spawn burst/blocks :The behaviour would be rather unknown. I advise you only use one Rate/Burst multiplex per context only. You can still use Set EventAttribute blocks though, it seems to work.

    How does it work (TLDR):

    upload_2022-10-23_8-56-37.png

    Burst Multiplex FIFO records any incoming event hitting the start of its spawn context. For each event, it stores both the VFXEventAttribute state, and the spawnCount attribute. Every frame, it dequeues 1 element of the queue and outputs a SpawnEvent with the correct state, and the spawnCount attribute multiplied by the Count Scale.

    That means that 30 incoming events will be processed in 30 frames.

    Hitting Stop clears the queue.

    upload_2022-10-23_8-57-46.png

    Rate Multiplex FIFO records any incoming event hitting the start of its spawn context. For each event, it creates internally a "Source" that will live for some time (defined by the Source LifeTime property). At update, every frame the system cycles through sources, updates each source internal source lifetime, and outputs based on the SpawnRate property. If the source is too old, it is removed from the alive source list.

    That means that 30 incoming events will result in 30 sources, each one processed each frame, so a full cycle of update will take 30 frames.

    Hitting Stop stops all sources.
     
    Last edited: Oct 23, 2022