Search Unity

Compute shader to change mesh data?

Discussion in 'Shaders' started by james_unity988, May 27, 2019.

  1. james_unity988

    james_unity988

    Joined:
    Aug 10, 2018
    Posts:
    71
    Hi!

    I would like to get started with using compute shaders. Specifically, I would like to change mesh data. I want to do this because I have a large mesh with many frequent updates and I don't want to re-upload the entire mesh every time an update happens. Some of the operations I would need to perform include:
    1. Removing vertices/faces from the mesh
    2. Adding new vertices/faces to the mesh
    3. Changing color, UV coordinates, and other vertex attributes
    So my question is: Is this a good idea, can it be done using compute shaders? If so, can you provide some resource that would help me do this? If not, can you recommend an alternative approach?

    Thanks so much for your help.
     
  2. Spacejunkk

    Spacejunkk

    Joined:
    Sep 8, 2012
    Posts:
    7
    Yes, it's possible. The general idea is you have a few buffers on the graphics device (vertex, possibly index, optionally vertex attributes) modify them with a compute shader then bind them as shader variables and use Graphics.DrawProcedural. For your purposes you want DrawProceduralIndirect so you can control the number of vertices drawn from the graphics device without readback.

    https://docs.unity3d.com/ScriptReference/Graphics.DrawProceduralIndirect.html

    It's pretty easy to get it set up, but be aware that some operations (adding/removing faces) are very tricky to get right. Best of luck with your project!
     
    Last edited: May 28, 2019
  3. tclancey

    tclancey

    Joined:
    May 19, 2017
    Posts:
    143
    Sorry to raise an old thread. I need to do something similar, I'm moving verts after collisions, or in certain circumstances just over time. I can get it all working but I have to rebuild the mesh collider, this is a killer. Is it possible to do this on the gpu, or is this resident in the cpu only?
     
  4. bdeniz1997

    bdeniz1997

    Joined:
    Jan 26, 2021
    Posts:
    17
    can you just like, change the vertex and lets say its 457th vertex on the array. can't you just update the 457th vertex of the shared mesh of the collider? would not that work?