1

I am trying to write a simple shader to draw 3D line with thickness just to learn geometry shader in unity. However I am facing problem with the output from the shader when setting the input of the geometry shader to lineadj topology, which i suspect has something to do with the "weird" value of the third and fourth vertex taken in by the geometry shader.

This is how i generate my mesh from a c# script:

public static GameObject DrawShaderLine( Vector3[] posCont, float thickness, Material mat)
    {
        GameObject line = CreateObject ("Line", mat);
        line.GetComponent<Renderer>().material.SetFloat("_Thickness", thickness);

        int posContLen = posCont.Length;
        int newVerticeLen = posContLen + 2;

        Vector3[] newVertices = new Vector3[newVerticeLen];

        newVertices[0] = posCont[0] + (posCont[0]-posCont[1]);

        for(int i =0; i < posContLen; ++i)
        {
            newVertices[i+1] = posCont[i];
        }

        newVertices[newVerticeLen-1] = posCont[posContLen-1] + ( posCont[posContLen-1] - posCont[posContLen-2]);


        List<int> newIndices = new List<int>();

        for(int i = 1; i< newVerticeLen-2; ++i)
        {
            newIndices.Add(i-1);
            newIndices.Add(i);
            newIndices.Add(i+1);
            newIndices.Add(i+2);


        }

        Mesh mesh = (line.GetComponent (typeof(MeshFilter)) as MeshFilter).mesh;
        mesh.Clear ();
        mesh.vertices = newVertices;
        //mesh.triangles = newTriangles;
        mesh.SetIndices(newIndices.ToArray(), MeshTopology.LineStripe, 0);
        return line;
    }

And this is the GS that is running in the shader program

v2g vert(appdata_base v)
            {
                v2g OUT;

                OUT.pos = v.vertex;

                return OUT;
            }

            [maxvertexcount(2)]
            void geom(lineadj v2g p[4], inout LineStream<g2f> triStream)
            {

                float4x4 vp = mul(UNITY_MATRIX_MVP, _World2Object);

                g2f OUT;

                float4 pos0 = mul(vp, p[0].pos);
                float4 pos1 = mul(vp, p[1].pos);
                float4 pos2 = mul(vp, p[2].pos);
                float4 pos3 = mul(vp, p[3].pos);

                OUT.pos = pos1;
                OUT.c = half4(1,0,0,1);  
                triStream.Append(OUT);

                OUT.pos = pos2;
                OUT.c = half4(0,1,0,1);
                triStream.Append(OUT);

                triStream.RestartStrip();

            }

From my understanding, lineadj will take in 4 vertex with vertex[0] and vertex[3] being the adjacent vertexes. So by drawing vertex 1 and vertex 2 i am suppose to get my line drawn. However this is the output i get

enter image description here

This input data vertex position is (-20,0,0) and (0,-20,0) which is marked by the center 2 squares. The top left and bottom right cube are the position of the adjacent vertex generated by the c# function. As you can see the line seem to be connecting to position (0,0,0) and the lines are flickering rapidly, which make me suspect that vertex in the GS is corrupted? Start of the line is colored red and the end of the line is colored green.

If I edit the GS to output pos0 and pos1 instead of pos1 and pos2, i get this

enter image description here

with no flickering lines.

and if i plot pos2 and pos3, the result is way crazier(pos2 and pos 3 seems to be rubbish value).

I have been trying to debug this for the whole day but with no progress, so I need some help here! Thanks in advance

gin
  • 286
  • 3
  • 15
  • The imagelinks get a 504 gateway time out error – bish Jul 31 '15 at 03:56
  • Hi i have fixed the links. They are working now – gin Jul 31 '15 at 03:58
  • Adjacency information in the geometry shader seems to be unavailable in Unity at the moment: http://answers.unity3d.com/questions/374180/adjacency-information-in-geometry-shaders.html – Gnietschow Aug 01 '15 at 23:26

0 Answers0