Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Using VFXEventAttribute and calling SendEvent multiple times per frame

Discussion in 'Visual Effect Graph' started by Fingerbob, Feb 9, 2020.

  1. Fingerbob

    Fingerbob

    Joined:
    Sep 6, 2014
    Posts:
    24
    I'm just getting to grips with the VFX system, so if I'm doing something wrong, or silly, please educate me ;)

    I'm trying to spawn a set of particles each frame (based on some audio FFT values). I've got 64 float triggers, and if any of those triggers is > 0 on a particular frame, I'd like to spawn a particle with a preset color / velocity. Currently I'm doing this around a circle.

    The result I'm seeing is that I get multiple particles spawned, but they all seem to share a single set of eventattribute data.

    I'm attempting to do this by:

    each frame, check the float trigger is > 0 for all 64 triggers. If so,

    1. Set up the color and velocity

    2. Create a VFXEventAttribute, and populate the color (as a Vector3), Velocity (as a Vector3) and the x component of targetPosition (as a Vector3)

    3. call SendEvent for my event (currently called DoSpawn), passing the VFXEventAttribute

    the IDs for the attribute names ("color", "targetPosition", "velocity", "DoSpawn") are cached using Shader.PropertyToId into va_color_id, va_velocity_id, va_targetposition_id, do_spawn_id.

    Code (CSharp):
    1.  
    2.             int bc = 64;
    3.             float bstep = 1.0f / (float)bc;
    4.  
    5.             for (int i = 0; i < bc; i++ )
    6.             {
    7.                 float v = triggers[i];
    8.                 if (v < 0.0001f) continue;
    9.  
    10.                 var va = vfx.CreateVFXEventAttribute();
    11.  
    12.                 float angle = i * bstep;
    13.                 Vector3 velocity = Quaternion.Euler(0.0f, 0.0f, 360.0f * -angle) * Vector3.up * 3.0f;
    14.  
    15.                 va.SetVector3(va_velocity_id, velocity);
    16.                 va.SetVector3(va_targetposition_id, new Vector3(angle, 0.0f, 0.0f));
    17.                 var chsv = Color.HSVToRGB(angle, 1, 1);
    18.                 Vector3 colv3 = new Vector3(chsv.r, chsv.g, chsv.b);
    19.                 va.SetVector3(va_color_id, colv3);
    20.  
    21.                 vfx.SendEvent(do_spawn_id, va);
    22.  
    23.             }
    In the effect graph, in the InitializeParticle Context node, I'm using SetVelocity (and passing it "Get Attribute: velocity (Source)"), Set Color (and passing it "Get Attribute: color (Source)") and I'm also passing the targetPosition x component into a Position (Circle) block in the arc sequencer property.

    Any clue on what I'm doing wrong here? if all 64 values are > 0, I'd expect 64 particles to be emitted around in a circle with a colour based on the hue of their angle. Instead, all I get is a stream of red particles at a near-vertical velocity.

    if I trigger a single event on any particular frame, then I get the expected colour and velocity. It appears that triggering more than one just gives me either the first or last set of attributes multiple times?
     
    Last edited: Feb 9, 2020
  2. ThomasVFX

    ThomasVFX

    Joined:
    Jan 14, 2016
    Posts:
    45
    Hello,
    Sadly, we're currently missing the feature of consuming multiple event attributes at the same frame. Here's a summary of the current state:

    * Currently, when a Spawn Context's Start input is hit, the Spawn context resets and the VFXEventAttribute payload is cached into the context so it's transmitted to output SpawnEvents when a spawn happens in this context. This means that only the last EventAttribute payload gets cached.

    * Consequently, when a spawn happens from a Spawn context, only one SpawnEvent can output at a given frame, with the same payload as the one that's cached into the Spawn context.

    Consequently, here are some solutions that you could think of right now:

    A bad solution would be using the experimental VFXSpawnerCallbacks: you could intercept any incoming SpawnEvent using the `OnPlay` method and cache all the VFXEventAttribute payloads. But as the current limitation is you can output only one spawn event with only one VFXEventAttribute per frame, you would have to defer these spawns into as many frames as you have cached payloads (which is probably not what you want as they should be spawned at the same frame)

    Another solution - that is still not ideal - could be that you cache every values into a readable property array (Texture2D) so you can set it using the vfx.SetTexture() and vfx.SetInt() for setting both the values and the array size. This way you can use only one sendEvent. But it would leave the "skipped" ones to be culled during initialize (you can do that by setting the alive attribute to false, and would have to manage these 'skipped ones' into a property array stored into a texture too.

    Regardless of that, the issue that currently prevents handling multiple events in spawn at the same frame is already identified ane we are currently working on a generic solution to solve these multiple EventAttribute issues.
     
    florianhanke likes this.
  3. Fingerbob

    Fingerbob

    Joined:
    Sep 6, 2014
    Posts:
    24
    Thanks Thomas - good to know you're looking at it. I'll try investigating the property array method, I can likely make good use of a full array each frame anyway so skipping blanks shouldn't be too much of an issue.
     
  4. glacuesta

    glacuesta

    Joined:
    Nov 30, 2017
    Posts:
    10
    I've been banging my head on the wall on this all morning -- I thought I was missing something obvious since the ability to spawn multiple particles at specific points, in a single frame, from script, would be a common requirement. Watching this thread. Thanks guys.
     
  5. BoombitLicence25

    BoombitLicence25

    Joined:
    May 22, 2019
    Posts:
    3
    Is there any update on this? Can we expect this done relatively soon or is it one of those 'don't expect it this year' issues?
     
  6. musicdeveloper

    musicdeveloper

    Joined:
    Oct 16, 2019
    Posts:
    68
    Would really appreciate an update on this issue - I develop a piano visualizer app, and seems like the only solution is going to be creating 88 SpawnEvents... Would sincerely appreciate an update on this.
     
  7. Tchjetil

    Tchjetil

    Joined:
    Mar 10, 2018
    Posts:
    3
    I’m having the same problem.

    Previously, I worked around it by having a lot of events in the graph and having a little class firing of the events by cycling thru a list of the event names.

    I only needed 20 events that time, but now I need atleast ten times as many events.

    Is it possible to add events to a graph by code in runtime?

    It there an advisable limit to how many events a graph can have?
     
  8. musicdeveloper

    musicdeveloper

    Joined:
    Oct 16, 2019
    Posts:
    68
    Truly no update on this Unity? This is a serious issue that is extremely difficult to work around. It seems like a fundamental part of VFX Graph that needs to be fixed - or a usable workaround provided.
     
  9. DylanF

    DylanF

    Joined:
    Jun 25, 2013
    Posts:
    55
    I'm also hoping for a fix on this.
     
  10. koirat

    koirat

    Joined:
    Jul 7, 2012
    Posts:
    2,068
    THIS NEEDS IMMEDIATE MANDATORY ATTENTION.
    How can you even release such a broken feature.
     
  11. Qriva

    Qriva

    Joined:
    Jun 30, 2019
    Posts:
    1,296
    We encountered the same problem today (wanted to spawn particles twice with different positions).
    It would be nice if that was possible during single frame.
     
  12. Lecks

    Lecks

    Joined:
    May 13, 2013
    Posts:
    18
    Has any progress been made on this? I haven't found anything new to help in 10.2.0.
     
  13. eggsamurai

    eggsamurai

    Joined:
    Oct 10, 2015
    Posts:
    110
    Written tons of code to manage texture2d, sigh....
     
  14. laurentlavigne

    laurentlavigne

    Joined:
    Aug 16, 2012
    Posts:
    6,225
    do share
    (I know, that was a long shot :D)
     
  15. PaulDemeulenaere

    PaulDemeulenaere

    Unity Technologies

    Joined:
    Sep 29, 2016
    Posts:
    146
    Please check out on this other thread : https://forum.unity.com/threads/new-feature-direct-link.1137253/
    We are now allowing the direct link event to an Initialize Context to bypass the Spawn Context constraint.
     
  16. m_hakozaki

    m_hakozaki

    Joined:
    May 15, 2014
    Posts:
    4
    This is an ugly solution. But it works on 10.6.0.
    Increase the number of spawn contexts and events as needed.
    Use events that have not yet been used at each frame.

    20211021_132919.jpg

    20211021_132904.jpg
     
    Shadowphax and Discipol like this.