Finally got something of a geometry shader working. This is like my earlier post, but it takes a mesh as input. So what you're seeing on the picture is a standard Unity Sphere that is rendered with a material that outputs billboards at the vertices.
I started with this $GS example but that didn’t work out for me. The quads were rotated wrong and transformations were done twice. So here are the changed I made:
First I passed the inverse of the view-projection matrix to the material like so:
void Update () { Camera camera = Camera.main; Matrix4x4 mat = (camera.projectionMatrix * camera.worldToCameraMatrix).inverse; Material material = GetComponent().sharedMaterial; material.SetMatrix("_ViewProjectInverse", mat); }
Then, in the shader, I applied that to the up and right vectors to find what those were under the current projection:
float3 up = float3(0,1,0); float3 right = float3(1,0,0); right = normalize(mul( _ViewProjectInverse, right )); up = normalize(mul( _ViewProjectInverse, up ));
Then I noticed it didn't work with transparent bill boards. It would not compare to the z-buffer properly. Obviously the vertices my geometry shader output had the wrong z-value. That clicked for me when I read this stackoverflow comment which did the trick! Here's the code:
pIn.pos.z = pIn.pos.z/center.w;
Here, pIn is actually one of the output vertices and center is the input vertex. (I notice people mix those names up a lot in shaders because most structs are both input and output, to different stages in the pipeline).
Then, finally I had this problem and the suggested solution worked. Code:
[ContextMenu("ConvertToPointTopology")] void ConvertToPointTopology() { Mesh mesh = GetComponent().mesh; int[] indices = new int[mesh.vertices.Length]; for (int i = 0; i < indices.Length; i++) { indices[i] = i; } mesh.SetIndices(indices, MeshTopology.Points, 0); } void Start() { ConvertToPointTopology(); // it will skip the last rows of vertices if you don't do this (because it will only iterate the triangles and convert them to points) }
Hope this helps someone, I was quite surprised how hard it was to collect all this info. Took me almost a day to get this thing working.










