Reputation: 1389
I have a weird issues with BufferGeometryUtils.mergeVertices method
I took a simple cube from a jsfiddle and performed the mergeVertices and it seems mergeVertices does not merge all identical vertices as it should. See https://jsfiddle.net/tomfree/vpdwmycn/
E.g. the vertices given in the indexGeometry returned by mergeVertices are
Index 0 = (-1, -1, 1, )
index 1 = (1, -1, 1,)
index 2= (-1, 1, 1, )
index 3= (1, 1, 1,)
index 4= (1, -1, 1)
i.e. vertices at position 1 and 4 seem to be the same but shouldn't after mergeVertices to my understanding. I am pretty sure that I am missing sth. Can someone point me into the right direction?
Cheers Tom
ps: Also posted as https://discourse.threejs.org/t/buffergeometryutils-mergevertices-seems-not-to-merge-all-identical-vertices/33890 in three.js forum
Upvotes: 5
Views: 1722
Reputation: 28482
The merge process is functioning as expected. If you look at the count of each attribute of the original geometry
you'll get 36, but if you look at the count of each attribute of the merged indexedGeo
, you'll get 24:
console.log(geometry.getAttribute("position").count); // 36
console.log(indexedGeo.getAttribute("position").count); // 24
The problem you're encountering is that the merge method looks at all attributes before merging. In your case, position, normal, uv
. The corners of a box have 3 vertices, each with its own normal pointing in 3 directions (one pointing up, one right, and one forward, for example). Since each vertex contains unique normal
data on how to render its respective face, these vertices cannot be merged. Even though their position is the same, the other attributes are different.
Upvotes: 7