ThreeJS bufferGeometry position attribute not upda

2019-05-11 12:51发布

I used STLLoader to load an stl onto a threeJS scene returning a BufferGeometry.

I then used

myMesh.position.set( x,y,z ) 
myMesh.rotation.setFromQuaternion ( quaternion , 'XYZ');

to translate the geometry. This effectively changes the

myMesh.position
myMesh.quaternion

Translation is happening in the scene and all works well. I expected that the

myMesh.geometry.attributes.position.array

would be different before and after the translation - but it remained identical. I want to extract the new veritces from the buffergeometry after translation. I tried to call

myMesh.geometry.dynamic = true;
myMesh.geometry.attributes.position.needsUpdate = true;

in the render loop but no luck as I haven't updated the vertices explicity.

1条回答
可以哭但决不认输i
2楼-- · 2019-05-11 13:41

You want to get the world position of a mesh's geometry, taking into consideration the mesh's transform matrix, mesh.matrix. Also, your mesh geometry is THREE.BufferGeometry.

Here is the pattern to follow:

mesh = new THREE.Mesh( geometry, material );
mesh.position.set( 10, 10, 10 );
mesh.rotation.set( - Math.PI / 2, 0, 0 );
mesh.scale.set( 1, 1, 1 );
scene.add( mesh );

mesh.updateMatrix(); // make sure the mesh's matrix is updated

var vec = new THREE.Vector3();
var attribute = mesh.geometry.attributes.position; // we want the position data
var index = 1; // index is zero-based, so this the the 2nd vertex

vec.fromAttribute( attribute, index ); // extract the x,y,z coordinates

vec.applyMatrix4( mesh.matrix ); // apply the mesh's matrix transform

three.js r.71

查看更多
登录 后发表回答