团结 | mesh.colors 不会为我的自定义网格对象着色

Unity | mesh.colors won't color my custom mesh object

我在 Unity 中构建了一个自定义金字塔,如下所示:

    Mesh mesh = GetComponent<MeshFilter>().mesh;
    mesh.Clear();
    Vector3[] vertices = {
        new Vector3(0.0f, 0.5f, 0.0f),
        new Vector3(0.5f, 0.0f, 0.5f),
        new Vector3(-0.5f, 0.0f, 0.5f),
        new Vector3(-0.5f, 0.0f, -0.5f),
        new Vector3(0.5f, 0.0f, -0.5f),
    };

    int[] triangles = {
        1, 2, 3,
        1, 3, 4,
        1, 0, 2,
        2, 0, 3,
        3, 0, 4,
        4, 0, 1
    };


    mesh.vertices = vertices;
    mesh.triangles = triangles;

我正在尝试为我的金字塔着色,如 unity 文档中所述:

    Color[] colors = new Color[vertices.Length];

    for (int i = 0; i < vertices.Length; i++)
        colors[i] = Color.Lerp(Color.red, Color.green, vertices[i].y);

    // assign the array of colors to the Mesh.
    mesh.colors = colors;

但这不会改变任何事情..

我的对象没有材料,只有这个脚本。 有什么想法吗?

请注意 mesh.colors

中的评论

// (Note that most built-in Shaders don't display vertex colors. Use one that does, such as a Particle Shader, to see vertex colors)

因此,为了在 MeshRenderer 组件中看到这些颜色,请添加一个使用此类顶点或粒子着色器的 material。

因此

  1. Project 视图(资产)中 右键单击​​ CreateMaterial

  2. 给material一个名字

  3. 对于 Shader 从下拉菜单中找到 select 例如ParticlesStandard Unlit(或者如果你想接收闪电Standard Surface

  4. 最后将此 material 用于您的对象,方法是将其拖入 MeshRenderermaterial 或者简单地将它拖到 Scene 视图中的相应对象上(如果该对象还没有网格,后者可能不起作用)


结果:

除了

使用通用渲染管线时需要使用其他着色器:

  • Univeral Render Pipeline\Particles\Lit(带闪电)
  • Univeral Render Pipeline\Particles\Unlit(无闪电)