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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Adjust Vertex Positions (and Colours) of multiple Meshes - Performance Advice [URP]

Discussion in 'Scripting' started by ZoomieRoomie, Sep 1, 2022.

  1. ZoomieRoomie

    ZoomieRoomie

    Joined:
    Apr 10, 2013
    Posts:
    12
    Hey Guys and Gals,

    I am in desperate need of advice regarding performance optimization of my current project. I have done a lot of research to the best of my capabilities but can not come to a satisfying conclusion.
    Neither in terms of actual code or at least a decision in which direction I should do more research.

    Gist:
    • I need a performant way of adjusting mesh shapes/vertices
    • I need a performant way of adjusting mesh/material colours

    Situation (see attached images):
    • I have arranged a lot of empty Gameobjects in the form of a grid
    • These Gameobjects serve as anchor points, so to speak, for a few hundred generated, triangular meshes.
    • These traingle meshes are being pooled and rearranged with the movement of the player to create a background.
    • The "Anchor Gameobjects" will ocasionally be moved around a little bit, based on a physics calculation done by unity. They are however constrained enough to never leave their general position in the grid (so there will never be any overlaps or anything like that)
    • Whenever the Anchors move, I update the positions of the vertices accordingly. More specific: The pool manager starts a Coroutine which updates the affected meshes for the next 20 seconds.
    • Every instance of a triangle gets one of four random colours from the start
    • I also lerp the colour of the triangles every now and then to white and back to its starting colour
    Problem(s):
    • Since I am currently doing all this on the CPU the downside is a lack of performance whenever a lot of the elements are in motion
    • Most notably setting the new vertex array on the mesh is the biggest bottleneck
    • From what I am understanding that is because in here we push data from and to the GPU which is super slow
    • Because I am changing the material colour of the triangles via code (C#) many draw calls have to be done as Unity creates material instances for every object
    Further Information:
    • I am already using a pooling system for the triangles themselves
    • A computeshader seems to be the way to go(?) because it allows me to change the mesh on the GPU directly. That is something completely new to me though and I do not even have any shader programming knowledge. I have tried getting into this with a few articles and tutorials but they are all very complicated and complex in comparison to what I want to achieve (which is: simply send three positions to the GPU and shove them into three vertices of an existing object).
    • Burst Jobs did not help at all the way I tried to implement them and now I understand why that is. I don't really calculate anything myself that I could then do in parallel since I simply receive three positions and use these to apply them to a mesh
    • For the colour changing I tried using property blocks only to realise that this does not seem to be an option in a URP project. So I am not able to use that
    • GPU Instancing might be possible however I found a lot of conflicting information on how to achieve this if it even is possible in an URP environment
    Question(s):
    • What is actually the best way to get the most performance out of the vertex adjustment?
      • Compute Shader?
      • Vertex Shader?
      • (Threaded) Jobs?
      • ???
    • What is actually the best way to get the most performance out of the colour/material adjustment?
      • GPU instancing?
      • PropertyBlocks?
      • Shaders?
      • ???
    I will gladly provide any and all information needed if anyone has the time and knowledge to support me in this.
    Thank you very much so far already (for reading this).

    Kind regards
     

    Attached Files:

  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    Is this just a water effect? Based on the very limited information you've given here it sounds like all of this could be done with a plain old shader (not even a compute shader). Are you aware that shaders have a vertex stage in which the vertex positions can be modified?
     
    ZoomieRoomie likes this.
  3. ZoomieRoomie

    ZoomieRoomie

    Joined:
    Apr 10, 2013
    Posts:
    12
    First of all, thank you for taking the time.
    To answer your questions.

    It is basically something like a water effect to some degree in that I display it as a background water effect.
    Under the hood at least I think it is a little more than that...
    I use a net of rigidbodies inter-connected with Unitys Joint system as the above mentioned "anchors" for my triangles. That way I created a "physically correct" jelly/stylized water effect that can be pushed, prodded and otherwise be influenced by other objects and forces if need be.
    Think "Geometry Wars" if you are familiar with that game.
    Only difference is I do not only display the lines between the corner points (anchors) but also a traingular mesh between them that I can recolour and manipulate.

    I had a suspicion that a compute shader might be overkill for what I am trying to accomplish and that is partly why I started this thread. I wanted to get some advice on how to proceed (at least) before I waste even more time on techniques that are not what I am looking for in the end etc.
    My research so far was very confusing since there is a lot of information out there that says certain techniques are not compatible with URP projects and such. For example the "Vertex Shader" you were talking about I think. A concept that I have recently become aware of and am not yet too familiar with sadly.

    I hope I have filled in a few informational gaps. Do you think a Vertex Shader would work for this? Are there limitations? Like how far away from the objects origin point are the vertices allowed to be "shaded".

    Really appreciate any help.
    Regards