【问题标题】:Can't combine mesh from mesh create on runtime无法在运行时从网格创建中组合网格
【发布时间】:2019-07-19 12:58:20
【问题描述】:

我有这个代码:

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- 我有一个来自 polygonCollider2D 的点列表,分为 3: 2-我遍历这些路径以使用 Delaunay 生成网格。 对于 1 个网格,它工作得很好,但我找不到组合它的方法。

unity 的示例使用我没有的其他一些子游戏对象...

有人有解决办法吗?

【问题讨论】:

    标签: c# unity3d mesh


    【解决方案1】:

    我终于找到了一些没有优化但有效的东西:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-12
      • 1970-01-01
      相关资源
      最近更新 更多