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.
  2. Dismiss Notice

Bug [14.0.8] C# events are sometimes not processed in the right order

Discussion in 'Visual Effect Graph' started by Marc-Ducret, Aug 3, 2023.

  1. Marc-Ducret

    Marc-Ducret

    Joined:
    Apr 24, 2015
    Posts:
    6
    I am trying to create trails for moving objects.

    The trails exist in a unique visual effect instance and therefore the particles are created from C# through custom events.

    The position of the particles being computed from C# means that if the particles are not spawned within the particle strip with the correct order, it would lead to problems; and indeed this seems to be happening:

    upload_2023-8-3_17-39-48.png
    • The particle color is purple if spawnIndexInStrip is equal to the event index (written by C# in the event); and otherwise it is white.
    • The particle size is proportional to spawnIndexInStrip.
    Is spawnIndexInStrip meant to be coherent with the order of events since for most of the time it seems to be that way?

    If needed I can also make a minimal project to reproduce the issue.
     
  2. gabriel-delacruz

    gabriel-delacruz

    Unity Technologies

    Joined:
    May 19, 2021
    Posts:
    31
    Hi!
    It seems worth investigating, and I'm currently working on strips, so I can take a look.
    A minimal repro would be perfect, thanks!
     
  3. Marc-Ducret

    Marc-Ducret

    Joined:
    Apr 24, 2015
    Posts:
    6
    gabriel-delacruz likes this.
  4. gabriel-delacruz

    gabriel-delacruz

    Unity Technologies

    Joined:
    May 19, 2021
    Posts:
    31
    Hi Marc,

    I tested on some different versions and I think I cant repro the issue.

    This is the result I get (with different colors). Nothing in white:
    upload_2023-8-4_14-24-5.png


    Just playing the scene at the moment, is there anything else I have to do to reproduce?
     
  5. Marc-Ducret

    Marc-Ducret

    Joined:
    Apr 24, 2015
    Posts:
    6
    On my end I have the issue almost every time after clicking play (although it is bit random and some times I get the same result as you, but clearly less than 20% of the time).
    Maybe your computer is faster or something and this changes the odds. If that is the case, I have the intuition that increasing the Spawn Count field of the EventStrips object should increase the odds of the problem happening.
    See on the following screenshot: upload_2023-8-4_16-32-27.png
     
    Qriva likes this.
  6. gabriel-delacruz

    gabriel-delacruz

    Unity Technologies

    Joined:
    May 19, 2021
    Posts:
    31
    Ok, that helps.
    Actually, just setting the value at 65 instead of 64 makes it what seems to be 100% reproducible, when it was 0% with 64 (probably related to the number of threads in the compute).
    Will investigate further next week.
    Thanks for the good repro!
     
    Marc-Ducret likes this.
  7. gabriel-delacruz

    gabriel-delacruz

    Unity Technologies

    Joined:
    May 19, 2021
    Posts:
    31
    Hi Marc,

    I had some time to look at your issue. As expected, the behavior comes from the fact that spawning new particles is done in parallel, and it is not trivial to enforce a specific order. I would not consider it a bug, as it is like that by design.

    There is one case when this is trivial though, which is when the strip index is constant for all the spawning particles in this frame. In that case, because we know they will all end up in the same strip, we can assign them an order beforehand. This is true for your repro case, and it might be also true for your real scenario. We can change that, but it may take some time to make sure it is handled properly. I will create a task and see how it fits with our current priorities.

    As a workaround, and as an alternative for more complex cases, I suggest that you queue the event data yourself and provide it to VFX graph as a graphics buffer. Then, use spawnIndexInStrip (and stripIndex, if you have more than one) to load the data from the graphics buffer. I included an updated graph and script as a reference.

    Let me know if you have any questions :)

    upload_2023-8-10_13-26-29.png
     

    Attached Files:

    Vita- likes this.
  8. Marc-Ducret

    Marc-Ducret

    Joined:
    Apr 24, 2015
    Posts:
    6
    Hello,

    Thanks a lot for the detailed reply.
    In my real case multiple strip indexes can be used to add new particules in the same frame (the strips are tracers for bullets, and they all live in a single visual effect to simplify interoperability with ECS). Therefore I will try to use this graphics buffer approach.

    Also, one quick idea would be the option to disable parallel spawning.
     
  9. gabriel-delacruz

    gabriel-delacruz

    Unity Technologies

    Joined:
    May 19, 2021
    Posts:
    31
    Hi,
    I'm afraid disabling parallel spawning is not really possible, as everything is handled on the GPU. But it shows an interesting use case to consider in the future for a CPU backend.
    Thanks!
     
    Marc-Ducret likes this.