Search Unity

Particles along polyline | Assign Point Cache Asset using C#

Discussion in 'Visual Effect Graph' started by joakim219, May 5, 2020.

  1. joakim219

    joakim219

    Joined:
    Nov 11, 2016
    Posts:
    7
    Hello,

    I'm currently attempting to spawn particles along polylines. I'm successfully generating a .pcache file of the positions where to spawn the particles, which works well if I manually assign it in my VFX Graph. However, in my project I have dozens of different lines, so creating dozens of Visual Effects is not an option.

    upload_2020-5-5_14-31-0.png

    I'm seeking for advice on what I could do to have one Visual Effect that can handle all my polylines. Is there any way I can expose a Point Cache as property, or assign it somehow using C#?

    upload_2020-5-5_14-31-53.png

    Alternatively, if there is a way to spawn along polylines in VFX graph, I would like to hear about it.

    I've tried the following:
    - Assign my generated Point Cache Asset manually in my VFX Graph. It works, but I want it to be dynamic.
    - Use Position (line) block. It does not work for polylines.
    - Use Multiple Position Binder. It only spawned particles on the vertices, not between them.
    - Generate mesh for the polyline and use Shuriken as the particle system. It works, but I want to use VFX Graph.
     
  2. VladVNeykov

    VladVNeykov

    Unity Technologies

    Joined:
    Sep 16, 2016
    Posts:
    550
    Hi @joakim219,

    Mesh sampling is on its way which would allow you to assign the polyline directly.
    In the meantime, the Multiple Position Binder approach is probably one of the easiest. To get around the particles spawning only on the vertices, you can do something like this:


    In this example, we are randomly picking a value between 0 and the position, and are sampling the position texture once for the actual position, and another time for the position + 1 (i.e. the next position). Then it's a simple matter of randomly lerping between the current and the next position. The result looks like this:


    And as a last bit, if your shapes are easily represented with math (i.e. an octagon), it might be easier to just draw them directly in the VFX graph.

    Hope this helps!
     

    Attached Files:

    florianhanke and joakim219 like this.
  3. joakim219

    joakim219

    Joined:
    Nov 11, 2016
    Posts:
    7
    Thank you very much, @VladVNeykov! With your help I was able to get my project's polylines working the way I wanted. This was my first forum post and I have to say I'm positively surprised that I received such a nice response. Especially the fact that you work at Unity enhances my view of Unity and its community.

    I have a follow-up question, though. I'm not sure if I should create a new thread for it, but its somewhat related to the solution I ended up using. The multiple position property binder to be exact.

    In attempt to make it easier for my team to deal with said polylines, I tried to automate the "Targets" array of the multiple position binder. However, I was unable to access it in C# due to VFXMultiplePositionBinder's protection level. To my understanding I could write my own property binder to solve this, but for future reference I'd like to know if there's no need to write a custom one.

    Thanks!
     
    Last edited: Jul 24, 2020
    VladVNeykov likes this.
  4. andybak

    andybak

    Joined:
    Jan 14, 2017
    Posts:
    569
    (Me and a couple of other people have argued that access control is applied over-zealously in a fair few places but we might be the heretics in this debate...)
     
  5. VladVNeykov

    VladVNeykov

    Unity Technologies

    Joined:
    Sep 16, 2016
    Posts:
    550
    Hi @joakim219 ,
    Glad this was helpful!
    Regarding this, the VFX package API is not public yet as it is subject to change and is not documented. I understand @andybak 's point on this, and it can be annoying having to resort to workarounds, but managing expectations is often kinder than potentially affecting adversely lots of user projects.

    In short, I would recommend making your own binder. If you do however want to access the API (and are ok with the fact that it can change in future versions), you can create a folder in your assets and drop an assembly definition file which links to the VFX assemblies. You can see such an *.asmdef example here. All scripts within that folder will have visibility to the VFX API. As I mentioned though, use at your own risk :)
     
    videon and joakim219 like this.