Search Unity

Bug Calling VisualEffect.Play(attribute) several times per frame only plays ONCE

Discussion in 'Visual Effect Graph' started by cubrman, Dec 10, 2022.

  1. cubrman

    cubrman

    Joined:
    Jun 18, 2016
    Posts:
    412
    Code (CSharp):
    1. [ExecuteInEditMode]
    2. public class Test : MonoBehaviour
    3. {
    4.     VisualEffect VisualEffect;
    5.  
    6.     private void Awake()
    7.     {
    8.         VisualEffect = GetComponent<VisualEffect>();
    9.     }
    10.  
    11.     private void OnEnable()
    12.     {
    13.         var attr = VisualEffect.CreateVFXEventAttribute();
    14.         attr.SetVector3("position", new Vector3(1, 1, 3));
    15.         VisualEffect.Play(attr);
    16.         attr = VisualEffect.CreateVFXEventAttribute();
    17.         attr.SetVector3("position", new Vector3(3, 1, 2));
    18.         VisualEffect.Play(attr);
    19.     }
    20. }
    Expected result: two effects are created, one at (1, 1, 3) and one at (3, 1, 2).
    Actual result: one effect is created at (3, 1, 2).

    Bug?
     
  2. Qriva

    Qriva

    Joined:
    Jun 30, 2019
    Posts:
    1,315
    This is expected behaviour.

    Also there is no need to create new vfx attribute every time you call event as it's copied (cache in awake).
    I think most of this stuff should be already described somewhere in documentation.
     
  3. cubrman

    cubrman

    Joined:
    Jun 18, 2016
    Posts:
    412
    Well, if it is, I don't know how to find it. Can't say it's an intuitive behavior. You are playing an event and passing your arguments as parameters. MAYBE if I was setting global variables, like VisualEffect.SetVector3(), MAYBE then it would be intuitive (not for me thought). But having a dedicated structure called EventAttributes and being able to pass it to Play() screams that it should launch different events every time you call Play().

    In any case @Qriva do you know how to send an array to a VisualEffect? Are we forced to use textures?
     
  4. Qriva

    Qriva

    Joined:
    Jun 30, 2019
    Posts:
    1,315
    I can't say it is intuitive, but this is how it works. You can use textures, but in my opinion buffer is more flexible.
    In general there are two ways:
    1. Send data in form of texture or buffer and read it inside the graph, you also need to pass number of elements so you can correctly sample it. Just you know you do it via component API, not event attributes.
    2. Use direct link to send multiple events, but you need to manage spawning.
     
    PaulDemeulenaere and cubrman like this.
  5. cubrman

    cubrman

    Joined:
    Jun 18, 2016
    Posts:
    412
    Jesus. Your single post expanded my knowledge of VFX graph tenfold! Where do you people get all this info from? VFX graph info is incredibly hard to find for me.
     
  6. PaulDemeulenaere

    PaulDemeulenaere

    Unity Technologies

    Joined:
    Sep 29, 2016
    Posts:
    154
    There is a really basic sample of code in SendEvent, it's true it's lacking on the cache capabilities of this attribute object.

    Thanks, it's a really documented answer! About sending data through buffer, we can potentially add this reference.
    There is another alternative which has been suggested on the forum delaying received events among several frames using a custom spawner.

    We are aware the package documentation isn't always easy to navigate. The forum is our main public channel of discussion and support (with the discord server). You did well asking here, it improves the visibility of common issues users can encounter.

    By the way, we also recently released an ebook based on 2021.3 set of features.
     
    cubrman and Qriva like this.
  7. cubrman

    cubrman

    Joined:
    Jun 18, 2016
    Posts:
    412
    Well I google, I'm a frequent at Unity blog and I donwloaded all the github samples I could find - never before I saw anything like SendEvent. But, at the same time, some samples are way too difficult and they frighten you by their mere existence) So I might have been inattentive. Still, the book link is VERY useful and I DID NOT see it before. Just now I checked and saw that it was indeed a post on Unity blog and I missed it, such a shame. Oh well, I did find it thanks to you and that's great. TY!

    Btw VFX is one of the toughest parts of IT at the moment. I guess because of lack of info. When I worked for a sizeable mobile casino they had real struggle finding a Unity VFX artist, even though regular Unity c# devs were in abundance.
     
  8. cubrman

    cubrman

    Joined:
    Jun 18, 2016
    Posts:
    412
    @PaulDemeulenaere one more thing: I don't know if you can pass this to the appropriate team in Unity but SRP custom passes require a LOT of love in terms of documentation and examples. In their case asking on the forums is WAAAAY less useful and usually my (not too difficult) unique cases are, sadly, left unaswered.