Search Unity

  1. Dismiss Notice
  2. All Pro and Enterprise subscribers: find helpful & inspiring creative, tech, and business know-how in the new Unity Success Hub. Sign in to stay up to date.
    Dismiss Notice

Any way to optimise this graph and avoid repetition?

Discussion in 'Visual Effect Graph' started by kayronjm, Jun 7, 2020.

  1. kayronjm

    kayronjm

    Joined:
    Jun 15, 2019
    Posts:
    32
    I've set up a pretty simple VFX graph that spawns 16 particles shot sideways through 10 small gaps in a weapon turret barrel. Since these gaps are offset by 0.08 in z, I just copied over the entire effect again and again, all of which are controlled by a single weaponFire event. The muzzle flash from the barrel front is separate because the effect is different but I feel awkward having copy-pasted an effect 10 times within the same graph. I don't know but could this not lead to loss in performance? This is the overall layout:



    That's the overall effect. The first 10 are pretty basic and are identical. It just spawns the 16 particles with random velocity between one vector and another. The only thing that changes between each adjacent effect for the first 10 is that I added 0.005 seconds delay and I added 0.08 to z position, successively. I also changed the seed for the random velocity. The rest is copy-pasted. This is a zoomed-in screenshot of the first two, as an example:



    Now, thinking as a programmer, I'd prefer having a counter that increases from 0 to 9 (or 1 to 10) and have this value used as a multiplier to add a time delay and z position offset, and therefore I'd just iterate through the same effect 10 times with this time delay and z position offset applied. That would lead me to have a single effect with this mechanism, which causes the 10 bursts. Is this possible? I'm just wondering what kinds of nodes to add for it. Thank you for any pointers you can provide!
     
  2. ThomasVFX

    ThomasVFX

    Unity Technologies

    Joined:
    Jan 14, 2016
    Posts:
    43
    Hello!

    If I get your idea correctly, you need to spawn a particle every 0.05s and shift it through a list of 10 positions (are these evenly distributed? it can help)

    A convenient solution is to use a periodic spawn of 16 particles every 0.05s in a context that loops every 10 shots (10 * 0.05s loop length) then use the Spawn Time attribute in order to modify the initial position.

    To set up the loop in your spawn context, select it, then you can edit its properties in the inspector and set it as a constant duration. Then compute its duration and set the periodic burst properties :

    upload_2020-6-12_17-7-40.png


    Next, If you want to read the spawn time attribute in an initialize, you need two things:

    - First export the time in the spawn context using the Set Spawn Time block (should be implicit in the future)
    upload_2020-6-12_16-59-54.png
    - Then Read this time in the initialize using a get SpawnTime (Source) operator
    upload_2020-6-12_17-0-43.png

    Here's the full example (the .vfx is attached in the zip file). In order to interpolate the Z i used a simple lerp between two Z values, based on the relative loop time (Source SpawnTime divided by the loop duration so the time is now between 0 and 1, which is convenient for our lerp)

    upload_2020-6-12_16-58-46.png

    Alternatively, if you use bursts of 16 particles (constant 16) you can also use the particleID attribute in an integer division by 16 ,then modulo 10, to get the index of the endpoint (between 0 and 9) so you can easily compute its position. This case could solve if you need to spawn particles in more than one source at a time (really low delays)

    This way you can factorize everything into only one system.
     

    Attached Files:

    florianhanke likes this.
  3. ThomasVFX

    ThomasVFX

    Unity Technologies

    Joined:
    Jan 14, 2016
    Posts:
    43
    If I am correct and I did understand well your need this is what it is roughly supposed to look?
     

    Attached Files:

  4. kayronjm

    kayronjm

    Joined:
    Jun 15, 2019
    Posts:
    32
    This is absolutely fantastic help Thomas, thank you so much! I understand your example and will implement it tomorrow and report back. It looks like it sorts out everything and even better, it helps me learn the VFX Graph aspect of Unity further, which is always welcome.
     
  5. kayronjm

    kayronjm

    Joined:
    Jun 15, 2019
    Posts:
    32
    Hi Thomas,

    I've set it up as suggested, as seen below:



    But I have two outstanding issues:

    1. The time delay between shots is actually 10 times smaller than you set up, so 0.005 seconds. The loop duration is therefore 0.05 seconds. While I thought nothing of it at first, it seems as if the VFX graph isn't fast enough to cope with this tiny delay and I only see 2 out of the 10 shots. If I slow it down to your values, then it appears to work.

    2. My weapon turrets have burst fire so before the particles have disappeared fully, the barrel fires off another shot. This means the effect is replayed. When I separate the effect into the 10 graphs as I showed in my original post, this displayed fine. With this optimised setup however, the effect is caught half-way and is restarted when the event is sent a second time.

    Is there any way to get around these? Thank you! :)
     
unityunity