javascript 三.JS,改变子3D物体的世界位置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12547701/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 16:29:14  来源:igfitidea点击:

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

javascriptgraphicswebglthree.js

提问by sbadams

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. So first I get the child object's position relative to the world space.

所以基本上我有一个Object3D组的子object3D,而子对象的[x,y,z]坐标是相对于父对象的对象空间显示的,我想改变子对象在3D空间内的位置. 所以首先我得到子对象相对于世界空间的位置。

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

This returns a three element vector of the child's position within the world space. Now I wish to set the position my self. 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. And to make sure that the matrix updates after these changes, I call the following functions.

提供 setPosition 的函数定义,它本质上将对象世界矩阵的最后三个元素设置为作为参数传递的向量的值,从而改变对象在世界空间中的位置。为了确保矩阵在这些更改后更新,我调用了以下函数。

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.

现在在检查对象的新世界矩阵时,我注意到 setPosition 函数什么也没做,什么也没做。

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.

为什么?如果需要真实的代码示例,我会提供。但是上面应该非常准确地代表了我使用的应用程序域和语法。

回答by WestLangley

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.matrixor object.matrixWorlddirectly, 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.matrixAutoUpdateis true.

默认值object.matrixAutoUpdatetrue

Hence, in the render()function, object.matrixWorldis being updated by the current value of object.postion, which you did not change. So, to you, it looks like nothing happened.

因此,在render()函数中,object.matrixWorld正在由 的当前值更新object.postion,而您并未更改该值。所以,在你看来,好像什么都没发生。

回答by Yevhenii Bahmutskyi

This answermight help with the case. Remove child from parent, change normal position, then attach again.

这个答案可能有助于解决这个问题。从父级中移除子级,更改正常位置,然后再次附加。