Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Official Flock using Custom HLSL & Global Buffer

Discussion in 'Visual Effect Graph' started by PaulDemeulenaere, Jul 21, 2023.

  1. PaulDemeulenaere

    PaulDemeulenaere

    Unity Technologies

    Joined:
    Sep 29, 2016
    Posts:
    134
    Hello,

    The Custom HLSL block has been introduced with Unity 2023.2a19 and package 16.0.3.
    I would like to share an experiment we used to validate these new blocks, the flock of spiders you can notice here is using this method.


    This sample project is available on GitHub and compatible with 2023.2b1 (if you are opening this project with an earlier version, you will encounter an already solved issue).

    The main trick about this solution consists in the usage of Shader.SetGlobalBuffer. A global data structure is allocated, bound and cleared every frame with a GridManager.

    This flock simulation is using a two steps update. The first one is responsible for updating a fixed grid of 64x64 cells, each cell can contain up to 8 instances, we are saving 2D position and velocity, see FlockUpdate. The second one will update forces based on the gathered neighbourhood data, see FlockApply.

    Since a MonoBehaviour is in charge of clearing the global data structure, this method won’t work with prewarming or scrubbing in timeline. Also, due to the limit of 8 instances per cell, if an instance can't be stored in a cell during several frames, it will be automatically killed, that’s why you can notice some instances restarting out of nowhere.

    Of course, there are better and more adaptive approaches for partitioning elements in space but this example is pretty simple and a good opportunity to familiarize with the Custom HLSL capabilities. I won’t expect to be used as it is in real-world production but it could be a starting point.

    Last but not least, an actual embedded solution for neighbor search is still on track, see ProductBoard.

    Hoping it will be inspirational, feel free to share your impressions and don’t hesitate if you have any additional questions.
     
    landonth, Vita-, florianBrn and 10 others like this.
  2. Onigiri

    Onigiri

    Joined:
    Aug 10, 2014
    Posts:
    410
    Hi, thanks for the example. Why not make it part of this repository? Just worried that all usefull vfx graph resourses are scattered across all kind of places and repos.
     
  3. OrsonFavrel

    OrsonFavrel

    Unity Technologies

    Joined:
    Jul 25, 2022
    Posts:
    128
    The Repo that you pointed is made for "Bigger" VFX examples and needs to be updated with the different version of Unity. The process involved to add content to this repository and maintain them are more time-consuming.
    So usually we try to balance between type of learning/examples resources. What if, we create a Forum page or something else to list and tracks useful VFX Graph resources? Do you think that it will be helpful ?
     
    landonth, PaulDemeulenaere and Qriva like this.