javascriptthree.jsbuffer-geometryobjloader

THREE.OBJLoader - Scale mesh and BufferGeometry vertices


I'm using THREE.OBJLoader to load a 3D model into my scene. It works fine.

However, after adding it to the scene, I need to scale it by 10, then retrieve its vertices position.

I know that THREE.OBJLoader gives me a BufferGeometry, and I know how to access its vertices using position attribute.

My problem is: the Float32Array storing these vertices doesn't seem to be updated after scaling my mesh, whether I do it before or after adding it to the scene.

I use this snippet to scale the mesh just after I got it from the loader:

myMesh = new THREE.Mesh(loadedMesh.geometry, new THREE.MeshBasicMaterial({color: 0xff0000}));
myMesh.scale.set(10, 10, 10);

Then I get the vertices using:

var myMeshVertices = myMesh.geometry.attributes.position.array;

Doing that, my mesh is added to the scene with the proper (scaled) dimensions, but its BufferGeometry doesn't want to update the vertices values.

I did a lot of tests involving myMesh.geometry.attributes.position.needsUpdate = true; or myMesh.updateMatrix();, and I've seen a lot of SO questions around this subject (here or here), but I couldn't find a solution...

Could you tell me if I misunderstand something here? (keeping in mind that I just began with Three.js and WebGL, so I most certainly lack knowledge about this).


Solution

  • The following should do the trick:

    myMesh.geometry.scale( 10, 10, 10 );
    

    Scaling an instance of Object3D and its subclasses means you apply a scale to its internal matrix and consequently its worldMatrix. This matrix is later used in the vertex shader in order to transform your vertices. If you need the transformation already on CPU side, use the respective transform methods of BufferGeometry.