I know geometry shader can generate procedural geometry by calculating it, but is it ever possible to receive some predefined mesh data to instantiate say per input vertex?
So for example if I want to instantiate a sphere mesh per every vertex input stream into geometry shader, will this be possible? ( by supplying geometry shader with predefined mesh data)
Yes, it is possible. You will have to manually fill out a buffer or array with the vertex data you need from script for the geometry shader to use though.
I can’t think of any Unity specific examples, though I’m sure people have done this somewhere. There’s not a ton of good examples for geometry shaders in general. You can try looking at using a StructuredBuffer or SetVectorArray() to copy the vertex positions and tri indices from a mesh into something a shader can get easy access to, or even store it in a texture. Then in the geometry shader it would just be a matter of reading through the tri index and outputting the appropriate tri strip geometry.
You’re probably better off looking at instancing though.
I could ofcouse hard code the mesh generation in the geometry shader, but the reason why I am asking is to be able to generate more complex objects and still follow the topology of the input mesh data from vertex shader
But now we’re kind of at a point where to get this working with a geometry shader is going to still be a lot of research and work, even though accessing the structured buffer in a normal shader uses the same syntax as a compute shader, building a mesh from a geometry shader takes a bit more finess unless you want to be really inefficient. So I’d kind of steer you back towards instancing again and use this code to copy the vertices of your input mesh and use DrawMeshInstanced() to draw them all, or get fancy and use the new DrawMeshInstancedIndirect in 5.6
The reason why I wanted to try using geometry shader is because I don’t want to perform bakedmesh call per skinned mesh per update. But I guess I will never know the difference in performance unless I try both methods.
I will look into DrawMeshInstancedIndirect as well. Thanks!
[QUOTE="But now we’re kind of at a point where to get this working with a geometry shader is going to still be a lot of research and work, … building a mesh from a geometry shader takes a bit more finess unless you want to be really inefficient. So I’d kind of steer you back towards instancing again and use this code to copy the vertices of your input mesh and use DrawMeshInstanced() to draw them all, or get fancy and use the new DrawMeshInstancedIndirect in 5.6[/QUOTE]
Thank you! I spent my whole day to figure out how to put instances of the same mesh on vertices of another. I saw “how to make grass with geometry shaders” tutorials. And l had to dive into standard 3D template, URP, HDRP, Graph Shaders, how to write a geometry shader in them etc. And I got lost and frustrated and I saw your post that mentions DrawMesh family of functions. Now I’m on the right track.