将不同的材质指定给同一对象上的不同网格

时间:2018-05-31 16:38:59

标签: c# unity3d mesh

我最初有一个函数创建了一个给定特定高度/宽度的矩形网格:

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

}

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

我的网格渲染器中有这两种材质,所以如何将一种材质分配给外部网格,另一种材质分配给内部网格。

1 个答案:

答案 0 :(得分:1)

这里可能缺少的概念是“SubMesh”,网格的每个子网格都将使用给定索引处的列表中的材质进行渲染。 要创建子网格,您可以调用

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

您可以通过基于相同的点列表指定不同的面来重复使用顶点。如果指定的子网格多于材质,则剩余的子网格将呈现为纯洋红色。