How to use mergeBufferGeometries()?

I’m having a hard time getting BufferGeometryUtils.mergeBufferGeometries() to work. I isolated the error to the following example. When I try to merge 3 simple geometries, like a torus, box and sphere, the method works as expected. However, when I substitute the sphere with an icosahedron, the method returns null.

const geom1 = new THREE.TorusBufferGeometry( 3, 1, 16, 100 );
const geom2 = new THREE.BoxBufferGeometry(5, 5, 5);
const geom3 = new THREE.SphereBufferGeometry(3, 32, 32);
const geom4 = new THREE.IcosahedronBufferGeometry(7, 0);

const geomA = THREE.BufferGeometryUtils.mergeBufferGeometries([geom1, geom2, geom3], false);
const geomB = THREE.BufferGeometryUtils.mergeBufferGeometries([geom1, geom2, geom4], false);
console.log("geomA: ", geomA); // <- this logs the merged geometry
console.log("geomB: ", geomB); // <- this logs null

Why is geomB null? I thought that as long as all 3 attributes matched (position, uv, & normal), it should work.

Here’s a live example, you can see in the console it logs null for the second one. What can I do to solve this? This is just a simplified demo, but I’m getting the same behavior with more complex imported geometry as well.

It’s unfortunate that it’s such a silent error but there are some conditions in which the geometries are not merged. In this case it’s because not every geometry has an index buffer:

Specifically geom4 does not have one:

console.log( ! ! geom1.index, ! ! geom2.index, ! ! geom4.index );
// true true false

You can fix this by generating an index for geom4. Maybe it’s worth adding a warning log or something if it cannot be merged?

2 Likes

I think it’d be good to add warning logs for each case where merging fails, yes. It didn’t make sense with the original implementation of the method, but I can’t think of any reason not to now.

1 Like

@donmccurdy I could open a ticket on Github tomorrow to track this if you’d like.

2 Likes