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

Question How can i generate new particles in a running particle system

Discussion in 'Visual Effect Graph' started by zhoupwrd, Aug 31, 2022.

  1. zhoupwrd

    zhoupwrd

    Joined:
    Jan 6, 2021
    Posts:
    12
    I want control the timing and count of particles spawn, i test some way with spawn system, but it not work, what should i do?
     
    Last edited: Aug 31, 2022
  2. Qriva

    Qriva

    Joined:
    Jun 30, 2019
    Posts:
    1,294
    Hard to help you with so poor description...

    How your graph looks like?
    What did you try?
    What do you mean by "timing and count"?
    Did you try single/periodic burst triggered by event from script?
     
  3. zhoupwrd

    zhoupwrd

    Joined:
    Jan 6, 2021
    Posts:
    12
    Sorry for so poor description.
    1, there are n predefined particle tracks
    2. Call the function (c# func) at t1 to generate m particles use Spawn rate, use "set position from map" to update the particle position, and use "set lifetime from map" to update the particle life
    3, t2, t3... Repeat operation 2. The particles generated later cannot directly cover the previous particles, and there may be several particle tracks at the same time.

    c# code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.VFX;
    5.  
    6. public class Graph0 : MonoBehaviour
    7. {
    8.     Texture2D _positions;
    9.     Texture2D _lifetimes;
    10.  
    11.     int _spawnRate;
    12.  
    13.     VisualEffect _VFX;
    14.  
    15.     float _frameTime;
    16.     List<int> _traceIds = new List<int>();
    17.     List<float> _lifes = new List<float>();
    18.  
    19.     List<List<Vector3>> _traces = new List<List<Vector3>>
    20.     {
    21.         new List<Vector3>(){ new Vector3(0, 0, 10), Vector3.zero },
    22.         new List<Vector3>(){ new Vector3(10, 0, 10), Vector3.zero },
    23.         new List<Vector3>(){ new Vector3(10, 0, 0), Vector3.zero },
    24.     };
    25.  
    26.     private void Awake()
    27.     {
    28.         _frameTime = 0;
    29.         _VFX = GetComponent<VisualEffect>();
    30.         _positions = new Texture2D(64, 64);
    31.         _lifetimes = new Texture2D(64, 64);
    32.         for (int i = 0; i < 64; i++)
    33.         {
    34.             for (int j = 0; j < 64; j++)
    35.             {
    36.                 _lifetimes.SetPixel(j, i, new Color(1, 1, 1, 1));
    37.             }
    38.         }
    39.  
    40.         _VFX.SetInt("SpawnRate", 0);
    41.         _VFX.SetTexture("Positions", _positions);
    42.         _VFX.SetFloat("PosScale", 10);
    43.         //_VFX.SetTexture("Lifetimes", _lifetimes);
    44.     }
    45.  
    46.     public void SpawnVFX(int[] traceIds)
    47.     {
    48.         Debug.Log("SpawnVFX ....");
    49.         _spawnRate = traceIds.Length;
    50.  
    51.         foreach (var traceId in traceIds)
    52.         {
    53.             _traceIds.Add(traceId);
    54.             _lifes.Add(3);
    55.         }
    56.         UpdatePos();
    57.  
    58.         _VFX.SetInt("SpawnRate", _spawnRate);
    59.         _VFX.SendEvent("Create");
    60.     }
    61.  
    62.     private void OnGUI()
    63.     {
    64.         if(GUILayout.Button("Spawn VFX"))
    65.         {
    66.             SpawnVFX(new int[] { 0,1,2 });
    67.         }
    68.     }
    69.  
    70.     void UpdatePos()
    71.     {
    72.         bool update = false;
    73.         for (int i = 0; i < 64; i++)
    74.         {
    75.             for (int j = 0; j < 64; j++)
    76.             {
    77.                 var index = i * 64 + j;
    78.                 if (index < _lifes.Count)
    79.                 {
    80.                     if (_lifes[index] > 0)
    81.                     {
    82.                         update = true;
    83.                         var traceId = _traceIds[index];
    84.                         var trace = _traces[traceId];
    85.                         var pos = Vector3.Lerp(trace[0], trace[1], (3 - _lifes[index])/3f);
    86.                         //Debug.Log($">>>{pos}");
    87.                         _positions.SetPixel(i, j, new Color(pos.x, pos.y, pos.z)/10f);
    88.                     }
    89.                     _lifes[index] -= Time.deltaTime;
    90.                 }
    91.             }
    92.         }
    93.  
    94.         if(update) _positions.Apply();
    95.  
    96.         for (int x = _lifes.Count - 1; x >= 0; x--)
    97.         {
    98.             if (_lifes[x] <= 0)
    99.             {
    100.                 _lifes.RemoveAt(x);
    101.                 _traceIds.RemoveAt(x);
    102.             }
    103.         }
    104.     }
    105.  
    106.     void Update()
    107.     {
    108.         _frameTime += Time.deltaTime;
    109.         UpdatePos();
    110.     }
    111. }
     

    Attached Files:

    Last edited: Sep 5, 2022
  4. Qriva

    Qriva

    Joined:
    Jun 30, 2019
    Posts:
    1,294
    Hello, sorry but it's still hard to help you. I am not sure I understnad what is trace, tracks and other stuff as this graph does not work and it's not complete. Also the most important information is missing - what effect you want to achieve.

    I think you want to draw some lines on grid or something and I could dig harder into your code and wild guess what is the end goal, but I think this is not how it should be :D
    By the way format your code correctly.

    In short provide some drawing or reference what you want and/or explain what should happen and what is happening.
     
  5. zhoupwrd

    zhoupwrd

    Joined:
    Jan 6, 2021
    Posts:
    12
    Sorry for the late reply,I tried to draw a schematic diagram,hope this is useful
    upload_2022-9-5_16-22-43.png
     
  6. zhoupwrd

    zhoupwrd

    Joined:
    Jan 6, 2021
    Posts:
    12
    Here is the complete graph
    upload_2022-9-5_16-33-59.png
     
  7. Qriva

    Qriva

    Joined:
    Jun 30, 2019
    Posts:
    1,294
    Let's try to describle the problem and goal first. This is what I understood:
    You have some positions in world and these positions generate some resources or something and you want to indicate this by drawing particle strip from that position to some some other "base" position.
    The question is do you want them to play all the time or just once? Should this look like people moving from point A to B or some kind of snake? Can one point generate multiple strips? There are many unknowns.

    The best possible case would be if you described the most simple case what you need and what to do, for example:
    "I want to move particles in sequence from point A to point B" or "I want to render static particle strip from point A to B"
     
  8. zhoupwrd

    zhoupwrd

    Joined:
    Jan 6, 2021
    Posts:
    12
    ok,I try to draw another diagram, and it is not played all the time or once, it is triggered.
    I want to generate a certain number of particles at a certain time, not periodically
    upload_2022-9-5_19-50-33.png
     
    Last edited: Sep 5, 2022
  9. Qriva

    Qriva

    Joined:
    Jun 30, 2019
    Posts:
    1,294
    Correct me if I am wrong - you want to spawn particle somewhere and make it move to other point, then disappear, but when you spawn another one then it should not influence movement of the previous one.
    Is "track" just line or it can be curve or something?
    Can you spawn more than 1 particle per frame in single "track"?
     
  10. Qriva

    Qriva

    Joined:
    Jun 30, 2019
    Posts:
    1,294
    One more very important thing - is it just independent visualization, so it's enough if you spawn particle and forget (it does no interact directly with gameplay) or you this is visualization layer for already working system and you must update stuff in the code and send them to vfx (like you did)?

    In the first case you can just spawn particles with target and animate with sequential line.
     
  11. zhoupwrd

    zhoupwrd

    Joined:
    Jan 6, 2021
    Posts:
    12
    "track" can be a curve depending on how the path is defined,It doesn't really matter what it's supposed to be, it doesn't matter.I want controll particle spawn,not loop and periodically. control the particle spawn first, the rest becomes easy.
    for vfx, it is dependent, need update position, but for game logic,it is independent visualization, just trigger.
     

    Attached Files:

  12. zhoupwrd

    zhoupwrd

    Joined:
    Jan 6, 2021
    Posts:
    12
    One trigger and one vfx, it is easy. But there will be lots and lots of vfx.
     
  13. Qriva

    Qriva

    Joined:
    Jun 30, 2019
    Posts:
    1,294
    For the first case where you don't need to do any updates from script, because you can determine when things happen, so visualization can run completely independently from game logic try this:
    Create simple system and replace constant rate with single burst. Don't change any other settings like loops and stuff.
    Create graphics buffer in script and every time you want to spawn particle or particles you add them to some list. To be more precise you put some struct with data like position/track or some additional things like color.
    At the end of frame in the late update you send event to vfx and fill buffer with data. In addition you set vfx property with count of elements you want to spawn (to know how many elements in buffer is used). In graph you just sample buffer and use it's data to put particles and targets or whatever in the correct place. At this point vfx does whatever you want with particle and your script does not care.

    In case you want to control directly each particle you can use similar approach, but you use buffer to update them every frame. In this case you can use direct link feature to spawn the exact amount you need.
     
  14. zhoupwrd

    zhoupwrd

    Joined:
    Jan 6, 2021
    Posts:
    12
    Thank you very much for your suggestion, I will try it