Search code examples
unity-game-enginemeshvertex-shader

How to get vertex colors working in Unity project?


I am trying to create a mesh grid programmatically and I am able to see the grid when using UV. But now I am trying to use colors on the mesh, so that every 4th line colors differently (ex. minor and major axis of a graph), but now the grid won't show up.

Things I have tried:

  • I have believe I have implemented Universal Rendering Pipeline (Universal RP) to create a vertex color shader. But that didn't work.

  • I have also tried using the built-in Particals/Standard Unit shader. And that didn't work and when I changed to universal RP I believe it broke it since the material shows up as pink in the preview pane.

  • Also copied several shaders from other stackoverflow answers that should have fixed this issue.

Here is my code for generating the grid.

    public class Grid : MonoBehaviour
    {
    
        public float thickness = 0.001f;
        public float spacing = 2f;
        public int major = 4;
        public Color majorColor;
        public Color minorColor;
    
        private Vector3[] vertices;
        private int[] triangles;
        private Color[] colors;
        
        private void Start()
        {
            transform.localScale = new Vector3(216f, 279.5f, 0.01f);
            DrawGrid();
        }
        
        public void DrawGrid()
        {
            Vector2Int numLines = new Vector2Int
            {
                x = Mathf.FloorToInt(transform.localScale.x / (spacing + thickness)),
                y = Mathf.FloorToInt(transform.localScale.y / (spacing + thickness))
            };
            int totalLines = numLines.x + numLines.y;
            int verticesPerLine = 4;
            int triangleIdxPerLine = 6;
            int numVertices = totalLines * verticesPerLine;
            int numTriangles = totalLines * triangleIdxPerLine;
            
            vertices = new Vector3[numVertices];
            triangles = new int[numTriangles];
            colors = new Color[numVertices];
            
            var minorSpacing = spacing / transform.localScale.x;
            
            int i = 0;
            for (float x = 0.0f; x < 1; x += minorSpacing + thickness, i++)
                DrawLine(i, new Vector2(x, 0.0f), new Vector2(x + thickness, 1.0f));
            
            for (float y = 0.0f; y < 1; y += minorSpacing + thickness, i++)
                DrawLine(i, new Vector2(0.0f, y), new Vector2(1.0f, y + thickness));
            print(colors.Length);
            print(majorColor);
            GetComponent<MeshFilter>().mesh = new Mesh
            {
                vertices = vertices,
                colors = colors,
                triangles = triangles
            };
        }
        
        private void DrawLine(int idx, Vector2 point1, Vector2 point2) {
            int vOffset = idx * 4;
            vertices[vOffset] = new Vector3(point1.x, point1.y);
            vertices[vOffset + 1] = new Vector3(point2.x, point2.y);
            vertices[vOffset + 2] = new Vector3(point1.x, point2.y);
            vertices[vOffset + 3] = new Vector3(point2.x, point1.y);
            
            Color color = idx % major == 0 ? majorColor : minorColor;
            colors[vOffset] = color;
            colors[vOffset + 1] = color;
            colors[vOffset + 2] = color;
            colors[vOffset + 3] = color;
            
            int tOffset = idx * 6;
            triangles[tOffset] = vOffset;
            triangles[tOffset + 1] = vOffset + 1;
            triangles[tOffset + 2] = vOffset + 2;
            triangles[tOffset + 3] = vOffset + 1;
            triangles[tOffset + 4] = vOffset;
            triangles[tOffset + 5] = vOffset + 3;
        }
    }
    

Solution

  • I figured it out with the help of this YT tutorial https://www.youtube.com/watch?v=eJEpeUH1EMg.

    I was rendering the triangles on the opposite side of where my camera was directed at.

    Merely changed the order of the triangle indexes.

        triangles[tOffset] = vOffset;
        triangles[tOffset + 1] = vOffset + 2;
        triangles[tOffset + 2] = vOffset + 3;
        triangles[tOffset + 3] = vOffset + 2;
        triangles[tOffset + 4] = vOffset + 1;
        triangles[tOffset + 5] = vOffset + 3;