[SOLVED] Geometry.vertices is undefined

Hello,

I’m trying to get the world position of the vertices in my model using geometry.vertices. but it returns undefined for me.

I found this issue on SO https://stackoverflow.com/questions/11495089/how-to-get-the-absolute-position-of-a-vertex-in-three-js talking about it, but even using the method as described by WestLangley it will still return undefined.

I load my obj using the THREE.OBJLoader, and it loads correctly. I try to get the vertice position using the following code:

object.traverse( function ( child ) { // child is the mesh
            if ( child instanceof THREE.Mesh ) {
                child.material.emissive.set(0xff00ff);
                
                console.log("vertices: ", child.geometry.vertices);//returns undefined
                for(var i = 0; i < child.geometry.vertices.length; i++){//Cannot read property 'length' of undefined
                    var vector = child.geometry.vertices[i].clone();
                    vector.applyMatrix4( child.matrixWorld );
                    console.log(vector);
                }
            }

Am i missing something here?

Thanks for your time.
-Remy

1 Like

I guess your geometry if of type BufferGeometry. You can easily verify this by doing:

console.log( child.geometry.isBufferGeometry );

In this case, the following code should be correct (assuming the geometry is non-indexed)

if ( child.isMesh ) {
   
   const position = child.geometry.attributes.position;
   const vector = new THREE.Vector3();

   for ( let i = 0, l = position.count; i < l; i ++ )

      vector.fromBufferAttribute( position, i );
      vector.applyMatrix4( child.matrixWorld );
      console.log(vector);
   
   }

}
4 Likes

Thanks for the swift reply.

It was indeed the case of it being a BufferGeometry, and your solution works like a charm.

I tried using the attributes.position before which didn’t work because i had not used fromBufferAttribute.

Again thanks for your help!

Hi, gays. Three.js has updating its vertices. I try it so many times. then use code:
mesh.geometry.attributes.position.array
you can findd the geometry vetices sequences.