【问题标题】:How to get vertex colors working in Unity project?如何在 Unity 项目中获得顶点颜色?
【发布时间】:2021-01-11 04:54:12
【问题描述】:

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

我尝试过的事情:

  • 我相信我已经实现了通用渲染管道 (Universal RP) 来创建顶点颜色着色器。但这没有用。

  • 我也尝试过使用内置的 Particals/Standard Unit 着色器。这没有用,当我更改为通用 RP 时,我相信它会破坏它,因为材料在预览窗格中显示为粉红色。

  • 还从其他应该解决此问题的 stackoverflow 答案中复制了几个着色器。

这是我生成网格的代码。

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

【问题讨论】:

标签: unity3d mesh vertex-shader


【解决方案1】:

我在这个 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;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多