Can't combine mesh from mesh create on runtime

倾然丶 夕夏残阳落幕 提交于 2020-01-16 04:14:08

问题


I have this code :

public void BrowseColliderToCreateMesh (PolygonCollider2D polygonColliderAdded){
    //browse all path from collider
    pathCount=polygonColliderAdded.pathCount;
    CombineInstance[] combine = new CombineInstance[pathCount];

    for (int i = 0; i < pathCount; i++)
    {
        Vector2[] path = polygonColliderAdded.GetPath(i);
        Polygon2D polygon = Polygon2D.Contour(path);
        Triangulation2D triangulation = new Triangulation2D(polygon, 22.5f);
        // build a mesh from triangles in a Triangulation2D instance
        singleMesh = triangulation.Build();
        combine[i].mesh = singleMesh;
    }

    testDelaunay.GetComponent<MeshFilter>().mesh = new Mesh;
    testDelaunay.GetComponent<MeshFilter>().mesh.CombineMeshes(combine);
}

1- I have a list of point from a polygonCollider2D, divide in 3 : 2- I loop through these path to generate mesh with Delaunay. For 1 mesh it work well, but I can't find a way to combine it.

Example from unity use some other children gameobject that I don't have...

Does someone has a solution ?


回答1:


I finally find something which is not optimize but which work :

private Mesh CombineMeshes(List<Mesh> meshes)
    {
        var combine = new CombineInstance[meshes.Count];
        for (int i = 0; i < meshes.Count; i++)
        {
            combine[i].mesh = meshes[i];
            combine[i].transform = transform.localToWorldMatrix;
        }

        var mesh = new Mesh();
        mesh.CombineMeshes(combine);
        return mesh;
    }


public void BrowseColliderToCreateMesh (PolygonCollider2D polygonColliderAdded){
    pathCount=polygonColliderAdded.pathCount;
    for (int i = 0; i < pathCount; i++)
    {
        if(i==0){
        Vector2[] path = polygonColliderAdded.GetPath(i);
        Polygon2D polygon = Polygon2D.Contour(path);
        Triangulation2D triangulation = new Triangulation2D(polygon, 22.5f);
        // build a mesh from triangles in a Triangulation2D instance
        singleMesh = triangulation.Build();
        }else if (i==1){
        Vector2[] path = polygonColliderAdded.GetPath(i);
        Polygon2D polygon = Polygon2D.Contour(path);
        Triangulation2D triangulation = new Triangulation2D(polygon, 22.5f);
        // build a mesh from triangles in a Triangulation2D instance
        newMesh = triangulation.Build();
        combineMesh=CombineMeshes(new List<Mesh> { newMesh, singleMesh });
        }else if(i>1){
            Vector2[] path = polygonColliderAdded.GetPath(i);
            Polygon2D polygon = Polygon2D.Contour(path);
            Triangulation2D triangulation = new Triangulation2D(polygon, 22.5f);
            newMesh = triangulation.Build();
            combineMesh=CombineMeshes(new List<Mesh> { newMesh, combineMesh });
        }
    }
GetComponent<MeshFilter>().mesh = combineMesh;
}


来源:https://stackoverflow.com/questions/57089815/cant-combine-mesh-from-mesh-create-on-runtime

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!