I'm merging more objects together to create a BufferGeometry. During the merging, i want to fullfil a buffer that i will use later to add an attribute to my BufferGeometry. Therefore, I need to know the size of the buffer before the BufferedGeometry is created.
To calculate the size of the buffer I'm counting the number of vertices in that geometry added to the number of the faces multiplied by 3. As this code says https://github.com/mrdoob/three.js/blob/master/src/core/DirectGeometry.js#L148 and as this answer suggests Does converting a Geometry to a BufferGeometry in Three.js increase the number of vertices?
But doing like this, the number of vertices in the buffer geometry is 57600, that one in calculated by me is 67400. I'm doing something wrong but I do not understand what exactly.
This is the code that I'm using:
let tot_objects = 100;
let material = new THREE.MeshStandardMaterial( {color: 0x00ff00} );
let geometry = new THREE.BoxGeometry(5, 5, 5, 4, 4, 4);
let objs = populateGroup(geometry, material, tot_objects);
//let's merge all the objects in one geometry
let mergedGeometry = new THREE.Geometry();
for (let i = 0; i < objs.length; i++){
let mesh = objs[i];
mesh.updateMatrix();
mergedGeometry.merge(mesh.geometry, mesh.matrix);
}
let bufGeometry = new THREE.BufferGeometry().fromGeometry(mergedGeometry);
let totVerticesMergedGeometry = (mergedGeometry.vertices.length ) + (mergedGeometry.faces.length * 3);
console.log(bufGeometry.attributes.position.count); // 57600
console.log(totVerticesMergedGeometry); // it returns 67400 !!!
scene.add(new THREE.Mesh(bufGeometry, material));
function populateGroup(selected_geometry, selected_material, tot_objects) {
let objects = [];
for (var i = 0; i< tot_objects; i++) {
let coord = {x:i, y:i, z:i};
let object = new THREE.Mesh(selected_geometry, selected_material);
object.position.set(coord.x, coord.y, coord.z);
object.rotateY( (90 + 40 + i * 100/tot_objects) * -Math.PI/180.0 );
objects.push(object);
}
return objects;
}