简体   繁体   English

三.JS,改变世界 position 一个孩子 3D object

[英]Three.JS, changing the world position of a child 3D object

So basically I have a child object3D of a group Object3D, while the child object's [x,y,z] coordinates are shown relative to the object space of the parent object, I want to change the location of the child object within the 3D space.所以基本上我有一个组 Object3D 的子 object3D,而子对象的 [x,y,z] 坐标显示相对于父 object 的 object 空间,我想在 3D 空间内更改子 object 的位置. So first I get the child object's position relative to the world space.所以首先我得到子对象相对于世界空间的 position。

var wrld_pos = childobject.matrixWorld.multiplyVector3(new THREE.Vector3);

This returns a three element vector of the child's position within the world space.这将返回世界空间中孩子的 position 的三元素向量。 Now I wish to set the position my self.现在我想自己设置 position。 So I create a three element vector.所以我创建了一个三元素向量。

var new_pos = THREE.Vector3();
new_pos.x = 1;
new_pos.y = 2;
new_pos.z = 3;

childobject.matrixWorld.setPosition(new_pos);

Provided the function definition for setPosition, it essentially it sets the last three elements of the object's world matrix to the values of the vector passed as an argument, thus changing the location of the object in world space.为 setPosition 提供 function 定义,它本质上是将对象的世界矩阵的最后三个元素设置为作为参数传递的向量的值,从而改变 object 在世界空间中的位置。 And to make sure that the matrix updates after these changes, I call the following functions.为了确保矩阵在这些更改后更新,我调用了以下函数。

childobject.matrixWorldNeedsUpdate = true;
childobject.updateMatrixWorld();

Now upon inspecting the new world matrix of the object I noticed that the setPosition function did nothing, nothing at all.现在,在检查 object 的新世界矩阵时,我注意到 setPosition function 什么也没做,什么也没做。

Why?为什么? If real code examples are needed, I will provide them.如果需要真实的代码示例,我会提供它们。 But the above should represent the application domain and syntax I used very accurately.但是上面应该很准确的代表了我使用的应用领域和语法。

In three.js, it is best just to call object.position.set( x, y, z ) , object.rotation.set( ... ) , and object.scale.set( ... ) , and not to mess with the object.matrix or object.matrixWorld directly, unless you really know what you are doing. 在three.js中,最好只调用object.position.set( x, y, z )object.rotation.set( ... )object.scale.set( ... ) ,而不是直接搞乱object.matrixobject.matrixWorld ,除非你真的知道你在做什么。

What is happening behind the scenes is this: 幕后发生的是这样的:

The default value of object.matrixAutoUpdate is true . object.matrixAutoUpdate的默认值为true

Hence, in the render() function, object.matrixWorld is being updated by the current value of object.postion , which you did not change. 因此,在render()函数中, object.matrixWorld正在由object.postion的当前值更新,您没有更改它。 So, to you, it looks like nothing happened. 所以,对你而言,似乎什么也没发生。

This answer might help with the case. 这个答案可能对案件有所帮助。 Remove child from parent, change normal position, then attach again. 从父母移除孩子,改变正常位置,然后再次附加。

If use attach for setting position in animation loop for moving objects you can get objects shaking.如果在 animation 循环中使用attach设置 position 来移动物体,您可以使物体摇晃。

For me best solution is:对我来说最好的解决方案是:

let newPosition = new THREE.Vector3(1,2,3);
let parentPosition = new THREE.Vector3();
childobject.parent.getWorldPosition(parentPosition);
childobject.position.set(
    newPosition.x-parentPosition.x,
    newPosition.y-parentPosition.y,
    newPosition.z-parentPosition.z
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM