如何让顶点颜色在 Unity 项目中工作?

How to get vertex colors working in Unity project?

我正在尝试以编程方式创建网格,并且在使用 UV 时能够看到网格。但现在我正在尝试在网格上使用颜色,以便每 4 条线的颜色不同(例如图形的短轴和长轴),但现在网格不会显示。

我尝试过的事情:

这是我生成网格的代码。

    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;
        }
    }
    

我在这个 YT 教程的帮助下弄明白了 https://www.youtube.com/watch?v=eJEpeUH1EMg

我在相机对准的另一侧渲染三角形。

只是改变了三角形索引的顺序。

    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;