![](/img/trans.png)
[英]How can I set the position of a mesh before I add it to the scene in three.js
[英]how to add a object in scene to a mesh without changing its position in three.js
我想保留對象在場景中的位置,並想將該對象的父對象從場景更改為任何其他網格。這是示例代碼http://plnkr.co/edit/fpjsUkOA0rH6FvG4DL6Z?p=preview
在這個例子中,當我試圖將球體添加到盒子中時,它的position
正在改變。我想保持原始位置。嘗試刪除第35行中的注釋,spehere朝着盒子移動。我想要保持其位置並使球體盒子的位置子http://plnkr.co/edit/fpjsUkOA0rH6FvG4DL6Z?p=preview
如果將球體添加到組中,則只需從球體位置中減去組的位置,以使其保持在其原始世界位置即可。
var material = new THREE.MeshLambertMaterial({color: 0x00ff00});
var boxGeometry = new THREE.BoxGeometry(5, 15, 5);
var box = new THREE.Mesh(boxGeometry, material);
var sphereGeometry = new THREE.SphereGeometry( 5, 32, 32 );
var sphere = new THREE.Mesh(sphereGeometry, material);
sphere.position.set(0, 10, 0);
sphere.updateMatrix();
var group = new THREE.Object3D();
group.translateX(6);
group.updateMatrix();
// if you add sphere to group object
group.add(box);
group.add(sphere);
scene.add(group);
var m = new THREE.Matrix4().getInverse(group.matrixWorld);
sphere.applyMatrix(m);
// if you add sphere to box
group.add(box);
box.add(sphere);
scene.add(group);
var m = new THREE.Matrix4().getInverse(box.matrixWorld);
sphere.applyMatrix(m);
console.log(group.position, sphere.position);
如果問題僅取決於position
那么Brakebein的答案可能是正確的。 但是,如果您還需要還原scale
和rotation
,那么我應該做這樣的事情:
var inversionMatrix = new THREE.Matrix4();
inversionMatrix.getInverse( parentObject.matrix );
childObject.applyMatrix(inversionMatrix);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.