Unity URP - Trying to draw a mesh using vertices computed in compute shader without reading back to cpu to assign to mesh object

468 views Asked by At

I'm working on terrain generation using marching cubes. As of now I'm able to generate the terrain vertices in a compute shader but I can't figure out how to draw the mesh without reading the vertices back to the cpu and assigning them to a new mesh object. the marching cubes compute shader output is an AppendStructuredBuffer where the vertices are set up so every 3 consecutive vertices in the buffer makes one triangle.

output is an append buffer of triangles:

struct Triangle { float3 a, b, c; };
AppendStructuredBuffer<Triangle> triangles;

I've looked at Graphics.DrawProcedural but it looks like I need to write a custom shader for that which would take in the compute buffers and draw from them but idk how to make that work since I only have access to shadergraph in urp. Mesh.SetVertexBufferData seemed promising but it won't take a pointer to a compute buffer on the gpu, only C# arrays and lists which it then sends over.

1

There are 1 answers

0
Oarc On

So I'm looking into doing the same thing as well, and my understanding is that yes, you do need a custom shader. However, you said "... I only have access to shadergraph in urp." but you can actually write custom shaders. It's called ShaderLab (I think?).

Here are some resources for other people looking to get started:

https://docs.unity3d.com/Packages/[email protected]/manual/writing-custom-shaders-urp.html

https://www.cyanilux.com/tutorials/urp-shader-code/

And specifically, the latest unity docs give an example of a shader that reads from a graphics buffer (can be used in the same way as a compute buffer):

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

I think you could start with a full working surface shader (that supports shadows or whatever you need) and then try adding in your compute buffer?