edap
edap

Reputation: 1114

Why the number of vertices in a merged Geometry differs from the number of vertices in the BufferedGeometry obtained from it?

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

Upvotes: 1

Views: 989

Answers (1)

Martin Schuhfu&#223;
Martin Schuhfu&#223;

Reputation: 6986

In a non-indexed BufferGeometry the length of the position attribute-array is always numberOfFaces * 3 * 3 (3 vertices per face, 3 values per vertex). That is also what the code from DirectGeometry you pointed to does: it iterates over the faces and pushes three vertices per face to the vertices-array.

A box-geometry with 4 segments in all directions has 192 faces: 4 * 4 = 16 segments per side with 2 triangles per segment makes 16 * 2 = 32 faces per side. And 32 * 6 = 192. You have 100 of these box-geometries so there are 19200 faces in total. Times 3 we get 57600 vertices.

Upvotes: 2

Related Questions