Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

Vfxgraph: Strategies For Making One Graph Handle Multiple "particle Systems"?

Discussion in 'Graphics Experimental Previews' started by PhilSA, Apr 11, 2019.

  1. PhilSA

    PhilSA

    Joined:
    Jul 11, 2013
    Posts:
    1,926
    I'm trying to make an example of using only one graph for handling multiple particle systems of the same type, resulting in much less draw calls than if I had 1 graph per system. This article mentions the FPS Sample does something like this for its projectiles. I feel like this is probably very simple to pull off but I'm too new to VFXGraph to figure it out.

    So here's the example I'm working with:



    I have this fire VFXGraph that basically just spawns billboard smoke quads from a point in the world, with an upward velocity. It's more or less the same as the graph in this video. Now I want to make this graph be able to support multiple positions in the world where the particles can emanate from, so that I can have many fires without the overhead of having each individual fire be a different VFXGraph instance

    The part I'm having trouble with is:
    1. How do I pass an array of realtime-modifyable-by-script positions to the graph?
    2. How do I handle the "for each of those positions, spawn the particles from there" logic?
    For point #1, I tried storing positions in a Texture2D parameter, but the problem is that all "Vector3"s in my texture are clamped to the 0-1 range, so they can't properly represent world positions. Then I messed around with point caches, but I couldn't figure out how to modify its points by script

    For point #2, I have no idea
     
  2. ZedPo

    ZedPo

    Joined:
    Nov 27, 2018
    Posts:
    13
    Bump, I'd love to know about this type of thing too!
     
  3. Grimreaper358

    Grimreaper358

    Joined:
    Apr 8, 2013
    Posts:
    789
    This video goes over a similar topic if not exactly what's asked. Not a lot of people got to see it though

     
    ZedPo likes this.
  4. PhilSA

    PhilSA

    Joined:
    Jul 11, 2013
    Posts:
    1,926
    Unless I'm mistaken, the limitation of that approach is that they can't have one graph spawn multiple new effects per frame; they are limited to maximum one new effect per frame. Trying to call myGraph.SendEvent() multiple times per frame will only result in your graph processing the last of the events you sent (again, unless I'm mistaken)

    I did end up finding a solution to my problem though. Here's a demo where each bullet hit VFX is handled by a single VFXGraph in the scene:




    We have to find a way to not only send events to our VFXGraph like they did in the video, but potentially send a group of events to a graph all in the same update. I did this by making my graph hold 2 special parameters:
    • SCount: the number of new spawn events this frame
    • STex: a texture whose pixels contain the spawn information of each new event: pos, rot, intensity, etc...
    Then I made myself a custom "SampleTextureForSpawnData" node that makes it easy to retrieve the data of a specific event based on a particle's spawn index:


    Now in my VFXGraph, I use "SCount" to figure out how many particles to spawn, and "SampleTextureForSpawnData" for getting the spawn position/direction/intensity/etc... of each new particle, based on which event they belong to


    In code, all I do now is "serialize" all of my spawn requests to a texture (STex), pass it to the VFXGraph along with SCount, and tell the VFXGraph to do its OnPlay event. I've attached the system that does this to this post, for those who want to study it. Keep in mind this is a DOTS system but DOTS doesn't matter for this use case
     

    Attached Files:

    Last edited: Oct 3, 2021
    mk1987, MehO, NotaNaN and 1 other person like this.
  5. PhilSA

    PhilSA

    Joined:
    Jul 11, 2013
    Posts:
    1,926
    ^^ it's a terribly awkward & cumbersome workflow though

    But there are 2 things being added to VFXGraph that will help:
    1. Graphics Buffer support: I haven't had the chance to test this yet, but this allows you to upload arrays of data structures instead of having to deal with a texture. This feature should already be in 2021.2
    2. Effect Instancing: This sounds like it'll be an automagical solution to the problem, but I have no idea if it'll perform as well as a manually-handled single-graph solution. It sounds like it still involves spawning new VFXGraph gameobjects everytime, which I assume would probably be less efficient than the approach I described. Pooling might be able to mitigate this a bit
    I really just wish we could send queues of events to a graph instead. I wonder if @PaulDemeulenaere has thoughts on whether this feature could be made possible, or if effect instancing would be just as good
     
    NotaNaN and ZedPo like this.
  6. mk1987

    mk1987

    Joined:
    Mar 18, 2019
    Posts:
    53
    Hi @PhilSA the above is exactly what i was looking for and could form a really good basis for handling my visual effects going forward (particularly as im using dots and can't work out how to control point cloud interface with that).

    I have implemented a version of what you've done but i cant get it to work using "UnityEngine.Experimental.Rendering.GraphicsFormat.R32G32B32A32_SFloat", the texture generation works nicely but vfx graph doesn't generate anything when using sample texture 2d. Using RGBA32 it generates something but of course the location isn't correct, from the randomness i can see its doing the right thing.

    I notice you use a node called Sample Texture 2d Pixel, is this a custom sub graph to deal with the format above or could it be my editor version and VFX versions of 2020.3.18 and 10.6 respectively are incompatible/do not have 'sample texture 2d pixel'. Thank you for any help, it will definetely improve my games VFX handling if i can get it to work!

    EDIT: Dont worry, worked it out! It all works fine in my version when using 'Sample Attribute Map'. System looks like it works really well and i'll post some results if I can complete my vision! Thank you very much for sharing the system Phil!
     
    Last edited: Oct 25, 2021
  7. mk1987

    mk1987

    Joined:
    Mar 18, 2019
    Posts:
    53
    Old thread but i implemented your approach slightly modified @PhilSA . Ignoring the first 20 seconds or so, the thrusters, missile trails, explosions are all handled in single gameobjects with VFX graph attached with generated texture input. The texture holds, position, current velocity, size and/or engine power. Works relatively well since before i was updating VFX graphs per object. Would be good to have a native solution in 2021 unity.
     
    PhilSA likes this.
  8. Kivi97

    Kivi97

    Joined:
    Jul 28, 2022
    Posts:
    2
    VFX graph attached with generated texture input. The texture holds, position, current velocity, size, and/or engine power. Works relatively well since before i was updating VFX graphs per object. Would be good to have a native solution in 2022 unity. https://apkseagle.com/cricket-league-mod-apk/