I want to keep the position of an object in scene and want to change the parent of that object from scene to any other mesh.here is a sample code http://plnkr.co/edit/fpjsUkOA0rH6FvG4DL6Z?p=preview
我想保持一个对象在场景中的位置,并希望将该对象的父对象从场景更改为任何其他网格。这里有一个示例代码http://plnkr.co/edit/fpjsUkOA0rH6FvG4DL6Z?p=preview
in this example when am trying to add the sphere to box it's position
is changing.i want to keep the original position .try to remove comment in line 35 ,the spehere is moving towards box.i want to keep its position and make sphere box's child http://plnkr.co/edit/fpjsUkOA0rH6FvG4DL6Z?p=preview
在这个例子中,当我试图将球体添加到盒子时,它的位置正在改变。我想保持原始位置.try删除第35行中的注释,spehere正朝着box.i想要保持其位置并制作球体盒子孩子http://plnkr.co/edit/fpjsUkOA0rH6FvG4DL6Z?p=preview
2 个解决方案
#1
0
If question relies only to position
then the answer of Brakebein may be correct. But if you need also to revert scale
and rotation
, then you should make something like this, I think:
如果问题仅依赖于位置,那么Brakebein的答案可能是正确的。但是如果你还需要恢复比例和旋转,那么你应该做这样的事情,我想:
var inversionMatrix = new THREE.Matrix4();
inversionMatrix.getInverse( parentObject.matrix );
childObject.applyMatrix(inversionMatrix);
#2
0
If you add the sphere to the group, you just need to substract the group's position from the sphere's position to keep it in its original world position.
如果将球体添加到组中,则只需要从球体的位置减去组的位置,以使其保持在原始世界位置。
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);
#1
0
If question relies only to position
then the answer of Brakebein may be correct. But if you need also to revert scale
and rotation
, then you should make something like this, I think:
如果问题仅依赖于位置,那么Brakebein的答案可能是正确的。但是如果你还需要恢复比例和旋转,那么你应该做这样的事情,我想:
var inversionMatrix = new THREE.Matrix4();
inversionMatrix.getInverse( parentObject.matrix );
childObject.applyMatrix(inversionMatrix);
#2
0
If you add the sphere to the group, you just need to substract the group's position from the sphere's position to keep it in its original world position.
如果将球体添加到组中,则只需要从球体的位置减去组的位置,以使其保持在原始世界位置。
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);