ThreeJS bufferGeometry 位置属性在应用翻译时不更新

ThreeJS bufferGeometry position attribute not updating when translation applied

我使用 STLLoader 将 stl 加载到返回 BufferGeometry 的 threeJS 场景中。

然后我用了

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

平移几何。这有效地改变了

myMesh.position
myMesh.quaternion

场景中正在进行翻译,一切正常。 我预计

myMesh.geometry.attributes.position.array

翻译前后会有所不同 - 但它保持不变。 我想在翻译后从 buffergeometry 中提取新的顶点。 我试着打电话

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

在渲染循环中,但运气不好,因为我还没有明确更新顶点。

您想获得网格几何体的世界位置,同时考虑网格的变换矩阵,mesh.matrix。此外,您的网格几何形状为 THREE.BufferGeometry.

以下是要遵循的模式:

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