将不同材质分配给同一物体上的不同网格

Assigning different materials to different meshes on the same object

我最初有一个函数,它在给定特定 height/width:

的情况下创建了一个矩形网格
void BoxMesh(float width, float height)
{
    MeshFilter mf = GetComponent<MeshFilter>();
    Mesh mesh = new Mesh();
    mf.mesh = mesh;

    //Verticies
    Vector3[] verticies = new Vector3[4]
    {
        new Vector3(0,0,0), new Vector3(0, height, 0), new Vector3(width, height, 0), new Vector3(width, 0, 0)
    };

    //Triangles
    int[] tri = new int[6];

    tri[0] = 0;
    tri[1] = 1;
    tri[2] = 3;

    tri[3] = 1;
    tri[4] = 2;
    tri[5] = 3;

    //normals
    
    Vector3[] normals = new Vector3[4];

    normals[0] = -Vector3.forward;
    normals[1] = -Vector3.forward;
    normals[2] = -Vector3.forward;
    normals[3] = -Vector3.forward;

    //UVs
    
    Vector2[] uv = new Vector2[4];
    
    uv[0] = new Vector2(0, 0);
    uv[1] = new Vector2(0, 1);
    uv[2] = new Vector2(1, 1);
    uv[3] = new Vector2(1, 0);
    
    

    //initialise
    mesh.vertices = verticies;
    mesh.triangles = tri;
    mesh.normals = normals;
    mesh.uv = uv;

    //setting up collider
    polyCollider.pathCount = 1;

    Vector2[] path = new Vector2[4]
    {
        new Vector2(0,0), new Vector2(0, height), new Vector2(width, height), new Vector2(width, 0)
    };

    polyCollider.SetPath(0, path);

}

然后我决定我想让这个网格有一个轮廓,所以我编辑了这个函数,现在它是:

void BoxMesh(float width, float height, float OLwidth)
{
    MeshFilter mf = GetComponent<MeshFilter>();
    Mesh mesh = new Mesh();
    mf.mesh = mesh;

    MeshFilter mfOL = GetComponent<MeshFilter>();
    Mesh meshOL = new Mesh();
    mfOL.mesh = meshOL;

    //Verticies
    Vector3[] verticies = new Vector3[4]
    {
        new Vector3(0,0,0), new Vector3(0, height, 0), new Vector3(width, height, 0), new Vector3(width, 0, 0)
    };

    //Verticies Outline
    Vector3[] verticiesOL = new Vector3[4]
    {
        new Vector3(-OLwidth,-OLwidth,0), new Vector3(-OLwidth, height + OLwidth, 0), new Vector3(width + OLwidth, height + OLwidth, 0), new Vector3(width + OLwidth, -OLwidth, 0)
    };

    //Triangles
    int[] tri = new int[6];

    tri[0] = 0;
    tri[1] = 1;
    tri[2] = 3;

    tri[3] = 1;
    tri[4] = 2;
    tri[5] = 3;

    //normals
    
    Vector3[] normals = new Vector3[4];

    normals[0] = -Vector3.forward;
    normals[1] = -Vector3.forward;
    normals[2] = -Vector3.forward;
    normals[3] = -Vector3.forward;

    //UVs
    
    Vector2[] uv = new Vector2[4];
    
    uv[0] = new Vector2(0, 0);
    uv[1] = new Vector2(0, 1);
    uv[2] = new Vector2(1, 1);
    uv[3] = new Vector2(1, 0);
    
    //initialise
    mesh.vertices = verticies;
    mesh.triangles = tri;
    mesh.normals = normals;
    mesh.uv = uv;
    
    meshOL.vertices = verticiesOL;
    meshOL.triangles = tri;
    meshOL.normals = normals;
    meshOL.uv = uv;

    //setting up collider
    polyCollider.pathCount = 1;

    Vector2[] path = new Vector2[4]
    {
        new Vector3(-OLwidth,-OLwidth,0), new Vector3(-OLwidth, height + OLwidth, 0), new Vector3(width + OLwidth, height + OLwidth, 0), new Vector3(width + OLwidth, -OLwidth, 0)
    };

    polyCollider.SetPath(0, path);

}

所以我现在有两个网格,一个在另一个外面。但是我希望外面的那个有不同的material:所以它看起来像一个轮廓。

我的网格渲染器中有这两个 material,那么如何将一个 material 分配给外部网格,另一个分配给内部网格。

您可能在这里遗漏的概念是 "SubMesh",网格的每个子网格将使用列表中给定索引处的 material 进行渲染。 要创建子网格,您可以调用

mesh.SetTriangles(int[] triangles, int submesh);

您可以根据相同的点列表指定不同的面,从而重新使用您的顶点。如果您指定的子网格多于 materials,则剩余的子网格将呈现为纯洋红色。