Hello, I am trying to use ComputeBuffer to draw out a TON of little geometry that I am doing some procedural animation on. I am using the shader below to do the actual drawing. I have no real experience with shaders, I have been beating my head against this problem for about a day and a half. Seems like it should be easy. Instead of drawing the vertex lines of the mesh I would like to visualize the meshes by just adding a texture and a color, pretty much the standard diffuse. It would be nice to have shadows to, I am not sure if by using ComputeBuffer I lose them.
Maybe I am going about drawing all this geometry incorrectly. CoputeBuffer just seems like a great way for the gpu to do a ton of drawing very quickly which is exactly what I need. I can provide the computeBuffer code as well if it is needed.
Thanks for any help given.
Shader "DX11/Instanced from compute buffer" {
SubShader {
Pass {
CGPROGRAM
#pragma target 5.0
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
StructuredBuffer<float3> buf_Points;
StructuredBuffer<float3> buf_Positions;
struct ps_input {
float4 pos : SV_POSITION;
};
ps_input vert (uint id : SV_VertexID, uint inst : SV_InstanceID)
{
ps_input o;
float3 worldPos = buf_Points[id] + buf_Positions[inst];
o.pos = mul (UNITY_MATRIX_VP, float4(worldPos,1.0f));
return o;
}
float4 frag (ps_input i) : COLOR
{
return float4(1,0.5f,0.5f,1);
}
ENDCG
}
}
Fallback Off
}
You can solve this problem by using RenderTexture (Unity Pro) or ComputeBuffer (Unity Free). See example (CustomComputeShader.7z) here. Both use texture to transport data to and from shader.
I am using compute buffers in my project, it is how I am getting the buf_Points(vertex points of my mesh) and buf_Positions (where to draw each mesh) information to my shader. I just have no idea how to render that mesh with a texture and color to fill it in.
Here is an example slice of my current project, you can see the meshes are just wireframes, I need to make them solid objects with shadows. Thanks for the help, I just have no idea how to accomplish this.
Well, I finally figured it out if anyone is interested. Silly me I was trying to figure out why the shader would only draw lines. It isn’t the shader at all, you have to pass a different MeshTopology to DrawProcedural.
So I tried passing MeshTopology.Triangles in my DrawProcedural call and it at least drew some triangles even though it looked nothing like the mesh I was using. So then I realized I need to pass the vertices for all the triangles in my mesh instead of just passing the vertices. I then grabbed the normals for those triangle vertices and passed those into the shader as well. Now it works and I can draw a TON of procedural geometry with very little perf hit. Of course I am dev’ing on a GTX 980 so that helps
Worst part of using DrawProcedural is it doesn’t look like you can use shadows at all which is a real bummer. Now I need help with that
I’ve been reading your posts on this topic heavily interested as I’m working on procedural terrain right now… So, yeah, interested. I was trying to figure out the best way to put in procedural detail and you’ve shown me a good way - I think… I thought I had to use a geometry shader to expand the number of triangles from the base mesh.
Although I guess what you’re using is DX11 only which is awesome, but still pretty high level hardware.
Anyway, I always feel weird answering my own posts because I feel like it might be worthless to others. Just so you know it’s appreciated.