After loading a mesh from an obj file, I attempted to normalize it. Unfortunately, the results are not what I expected. Below is the code snippet for loading and centering the mesh:
const manager = new THREE.LoadingManager();
const loader = new THREE.OBJLoader(manager);
loader.load('http://mywebsite.com/bunny.obj', function(object) {
object.traverse(function(child) {
if(child instanceof THREE.Mesh)
{
const geometry = child.geometry;
const verts = geometry.vertices;
const ctr = new THREE.Vector3(0.0, 0.0, 0.0);
for(let i = 0; i < verts.length; ++i)
ctr.add(verts[i]);
ctr.divideScalar(verts.length);
for(let i = 0; i < verts.length; ++i)
verts[i].sub(ctr);
}
});
scene.add(object);
});
The intention of this code was to center the mesh based on the average of vertex positions. However, an unexpected effect is being produced. For further details and visualization, please visit my website:
I'm currently unable to identify the cause of this issue. The ctr variable holds a valid vector, and subtracting a vector from all vertices should only reposition them.