Merging BufferGeometry Objects

I have couple of meshes inside one array object.
What I am trying to do it to merge all of them to one merge with the following code:
(all mesh objects are in ‘insertedMeshes’ array)

var materials = [],
          geometries = [],
          mergedGeometry = new THREE.BufferGeometry(),
          meshMaterial,
          mergedMesh;

          insertedMeshes.forEach(function(mesh, index) {
            mesh.updateMatrix();
            geometries.push(mesh.geometry);
            meshMaterial = new THREE.MeshStandardMaterial(mesh.material);
            materials.push(meshMaterial);
          });

          
          mergedGeometry = THREE.BufferGeometryUtils.mergeBufferGeometries(geometries, false);
          mergedGeometry.groupsNeedUpdate = true;
          mergeMaterial = new THREE.MeshFaceMaterial(materials);

          mergedMesh = new THREE.Mesh(mergedGeometry, materials);
    
          mergedMesh.geometry.computeFaceNormals();
          mergedMesh.geometry.computeVertexNormals();
          mergedMesh.updateMatrixWorld();
          

     
          console.log(mergedMesh);
          scene.add(mergedMesh);

          render();

When I try to add any of the objects from insertedMeshes array to the scene it works
but mergedMesh is never added to the scene (although I see mesh in console.log).

Many thanks for your help!

It seems you want to use multiple materials with your merged geometry. In this case you have to set the second parameter of BufferGeometryUtils.mergeBufferGeometries() to true otherwise group data are not added.

Besides, MeshFaceMaterial is deprecated. It’s sufficient to just assign the array of materials to the mesh. Moreover, when working with BufferGeometry calling computeFaceNormals() has no effect (since BufferGeometry only works with vertex data).

2 Likes

Thank you!!
With this change, I was able to insert GLB file without problems.

Mirko