1

我有一个MeshFilter我想将新网格附加到现有网格的位置,这意味着我想向现有网格添加额外的顶点、三角形、法线。

这是我现在的做法:

    MeshFilter meshFilter = GetComponent<MeshFilter>();
    Mesh newMesh = new Mesh();
    newMesh.indexFormat = UnityEngine.Rendering.IndexFormat.UInt32;
    // information set here is calculated beforehand
    newMesh.SetVertices(MeshVertecies);
    newMesh.SetNormals(MeshNormals);
    // GenerateTriangles creates the correct triangle indices 
    newMesh.SetTriangles(GenerateTriangles(DepthSource.DepthWidth, DepthSource.DepthHeight), 0);



    if (firstMeshFlag)
    {
        meshFilter.sharedMesh = newMesh;
        firstMeshFlag = false;
    }
    else
    {
        Mesh oldMesh = meshFilter.sharedMesh;
        Mesh combinedMesh = new Mesh();

        try
        {
            combinedMesh.CombineMeshes(new CombineInstance[]
            {
                new CombineInstance
                {
                    mesh = newMesh
                },
                new CombineInstance
                {
                    mesh = oldMesh
                }
            });

            meshFilter.sharedMesh = combinedMesh;
        }
        catch (Exception e)
        {
            //This just prints debug information to the UI 
            //(Application running on Android using Google Depth API, which is why I can't debug
            DebugInformation.Instance.AppendDebugText("\ncombine err: " + e.Message);
        }
    }

第一次调用时,生成的网格按预期渲染,但是第二次在 ( firstMeshFlag = false) 附近,网格消失。

该应用程序在 Android 手机上运行,​​并使用 Google AR 以及 Google 的 Depth API,据我所知,它还不支持即时预览。所以我现在唯一的调试方法是将信息打印到 UI。

到目前为止我发现: 网格消失的原因是,虽然它拥有正确数量的顶点,但所有顶点和法线值都设置为零。

  1. 真的CombineMeshes()是向现有网格添加额外顶点、法线等的唯一方法吗?
  2. 所描述问题的原因可能是什么?
4

1 回答 1

0

所以答案很简单。我没有在 s 中添加变换矩阵,方法中CombineInstance的参数默认为 true。useMatricesCombineMeshes()

因此,解决方案就像将方法的使用更改为以下内容一样简单:

combinedMesh.CombineMeshes(new CombineInstance[]
            {
                new CombineInstance
                {
                    mesh = newMesh
                },
                new CombineInstance
                {
                    mesh = oldMesh
                }
            }, true, false);
于 2020-10-23T04:48:34.270 回答