Roland Deschain
Roland Deschain

Reputation: 2850

Unity's Mesh.CombineMeshes() seems to delete vertex information

I have a MeshFilter where I want to attach new meshes to the existing mesh, meaning I want to add additional vertecies, triangles, normals to the existing mesh.

Here is how I would do it now:

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

The first time this is called, the resulting mesh is rendered as expected, however the second time around (firstMeshFlag = false), the mesh disappears.

The application is running on an android phone and uses Google AR as well as Googles Depth API, which as far as I know, doesn't yet support instant preview. So my only means of debugging right now, is to print information to the UI.

What I found out so far: The reason the mesh disappears is, that while it holds the correct amount of vertecies, all the vertecies and normals values are set to zero.

  1. Is CombineMeshes() really the only means of adding additional vertecies, normals, etc. to an exisiting mesh?
  2. What could be the reason for the described issue?

Upvotes: 2

Views: 1320

Answers (1)

Roland Deschain
Roland Deschain

Reputation: 2850

So the answer was quite simple. I didn't add a transform matrix to the CombineInstances and the argument useMatrices in the CombineMeshes() method defaults to true.

Therefore the solution was as easy as changing use of the method to the following:

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

Upvotes: 2

Related Questions