網站搜尋

javascript - 當應用平移時,ThreeJS bufferGeometry 的 position 屬性未更新


我使用 STLLoader 將 STL 載入到 threeJS 場景中,返回了一個 BufferGeometry。

然後我使用了

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;

在渲染循環中,但沒有成功,因為我沒有明確更新頂點。

答案#1

你想要取得一個網格幾何的世界位置,同時考慮網格的變換矩陣 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.fromBufferAttribute( attribute, index ); // extract the x,y,z coordinates

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

three.js r157