如何在生成过程中更改程序生成的网格内三角形的颜色



我正在使用unity,c#,我不知道如何生成具有各种不同高度颜色的景观。例如,我想要水边的沙子和山峰上的雪。但是我无法在生成网格的代码中做到这一点。

只是为了清楚起见,网格生成得非常好,但我无法更改每个单独三角形的颜色。

 void CreateMesh()
{
    planeMesh = new Mesh();
    List<Vector3> vertices = new List<Vector3>();
    List<Vector2> uvs = new List<Vector2>();
    List<int> indices = new List<int>();
    List<Color> colors = new List<Color>();
    for (int x = 0; x < gridSize + 1; x++)
    {
        for (int y = 0; y < gridSize + 1; y++)
        {
            float height = Mathf.PerlinNoise(y / (float)gridSize, x / (float)gridSize) * 15.0f;
            vertices.Add(new Vector3(y, height, x));
            if(height > 12)//mountain tops
            {
                uvs.Add(new Vector2(0, 1/3));//down to up, x first
                colors.Add(Color.white);
            }
            else if(height > 3 && height < 13)//grassy fields
            {
                uvs.Add(new Vector2(1/3,2/3));
                colors.Add(Color.green);
            }
            else if(height > 0 && height < 4)//sand
            {
                uvs.Add(new Vector2(2/3, 2/3));
                colors.Add(Color.yellow);
            }
        }
    }
    //making the indices
    for (int b = 0; b < gridSize; b++)
    {
        for (int c = 0; c < gridSize; c++)
        {
            indices.Add(b + ((gridSize + 1) * c));
            indices.Add(b + gridSize + 1 + ((gridSize + 1) * c));
            indices.Add(b + 1 + ((gridSize + 1) * c));
            indices.Add(b + gridSize + 1 + ((gridSize + 1) * c));
            indices.Add(b + gridSize + 2 + ((gridSize + 1) * c));
            indices.Add(b + 1 + ((gridSize + 1) * c));
        }
    }
    planeMesh.SetVertices(vertices);
    planeMesh.SetIndices(indices.ToArray(), MeshTopology.Triangles, 0);
    planeMesh.SetUVs(0, uvs);
    planeMesh.RecalculateNormals();
    planeMesh.colors = colors.ToArray();
    GetComponent<MeshFilter>().mesh = planeMesh;
    GetComponent<MeshFilter>().mesh.name = "Environment";
}

你们能帮我吗?

解决了!看来我用于 uv 的数字被四舍五入到 0!我必须让它们漂浮起来才能正确使用它们。