Christoph
Christoph

Reputation: 606

Unity: Recalculate mesh on GPU

I was searching in the web for some time, but couldn't find a good answer for my current problem: In unity a visible object consists of a mesh. In OpenGL terms, a mesh is a VertexBuffer that is transmitted, ideally once, to the GPU and then feed into the vertex shader.

And now, at some given point I want to recalculate the given mesh on the GPU. For example, if my VertexBuffer has space for 200 Vector3<float> that are part of a Cube, I want to do a call to a ComputeShader(?) that updates every Vector3<float> so that the mesh is now a sphere.

I know I could reassign the mesh using the CPU, but in my scenario thats too slow.

So the question is, how can i pass a VertexBuffer or Mesh into a ComputeShader in unity? I would like to directly modifye the VertexBuffer.

Thanks for ideas, ressources, hints!

Upvotes: 2

Views: 3704

Answers (1)

Nain
Nain

Reputation: 1222

Compute Shaders are simple to setup.

write a Compute shader like this

#pragma kernel CSMain

#define thread_group_size_x 4
#define thread_group_size_y 4

//A struct that simple holds a position
struct PositionStruct
{
    float3 pos;
};

RWStructuredBuffer<PositionStruct> output;

[numthreads(thread_group_size_x,thread_group_size_y,1)]

void CSMain (uint3 id : SV_DispatchThreadID)
{
   int idx = id.x + id.y * thread_group_size_x * 32;
   float spacing = 1.0;

   float3 pos = float3(id.x*spacing, id.y*spacing, id.z*spacing);

   pos = UpdateVertex(pos,idx);

   output[idx].pos = pos;
}

float3 UpdateVertex(float3 p, int idx)
{
    //Do your stuff here
    return p;
}

Now Create the C# file that dispatches this compute file.

public class ComputeShaderScript: MonoBehaviour
{
public Shader shader;
public ComputeShader computeShader;

private ComputeBuffer outputBuffer;
private int _kernel;
private Material material;

void Start()
{
 RunShader();
}

void RunShader()
{
  _Kernal = computeShader.FindKernal("CSMain");

  vector3 Array = new vector3[200];
  outputBuffer = new ComputeBuffer(Array.Length, 12); //Output buffer  contains      vertices (float3 = Vector3 -> 12 bytes)
  outputbuffer.SetData(Array);

  computeShader.SetBuffer(_Kernal,"output",outputBuffer);

  computeShader.Dispatch(_Kernal,array.Length,1,1);

  vector3 data= new vector3[200];
  outputbuffer.GetData(data);

  buffer.Dispose();

  material.SetPass(0);
  material.SetBuffer("buf_Points", outputBuffer);
  Graphics.DrawProcedural(MeshTopology.Points, data.Length);
}

Upvotes: 4

Related Questions