javascript 如何在three.js中获取顶点的绝对位置?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11495089/
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
How to get the absolute position of a vertex in three.js?
提问by Danny Fox
As far as I know var point = object.geometry.vertices[i];
will return with the relative position for the x
, y
and z
of the point inside the geometry of the object.
据我所知,var point = object.geometry.vertices[i];
将返回x
,y
和z
对象几何内部点的相对位置。
How to get the absolute position, if the object was moved, rotated or scaled?
如果对象被移动、旋转或缩放,如何获得绝对位置?
回答by WestLangley
First make sure the object's matrices have been updated.
首先确保对象的矩阵已更新。
object.updateMatrixWorld();
The render loop usually calls this for you.
渲染循环通常会为您调用它。
Then, do this:
然后,这样做:
var vector = object.geometry.vertices[i].clone();
vector.applyMatrix4( object.matrixWorld );
The vector will now contain the position in world coordinates.
向量现在将包含世界坐标中的位置。
You might want to read some CG reference books.
您可能想阅读一些 CG 参考书。
3D math primer for graphics and game development / by Fletcher Dunn and Ian Parberry
Essential Mathematics for Games and Interactive Applications: A Programmer's Guide James M. Van Verth and Lars M. Bishop
用于图形和游戏开发的 3D 数学入门 / Fletcher Dunn 和 Ian Parberry
游戏和交互式应用程序的基本数学:程序员指南 James M. Van Verth 和 Lars M. Bishop
three.js r69
三.js r69
回答by Lee Stemkoski
Recent versions of Three.js (v50+) provide this functionality built into the THREE.Object3D
class. In particular, to get the world coordinates of a local THREE.Vector3
instance named vector
, use the localToWorld
method:
Three.js (v50+) 的最新版本在THREE.Object3D
类中提供了这个功能。特别是,要获取THREE.Vector3
名为的本地实例的世界坐标vector
,请使用以下localToWorld
方法:
object.localToWorld( vector );
Similarly, there is a worldToLocal
methodfor converting world coordinates into local coordinates, which is slightly more complicated, mathematically speaking. To translate a world vector
into the objects local coordinate space:
同样,还有一个worldToLocal
方法,用于将世界坐标为局部坐标,这是稍微复杂一些,从数学上讲。要将世界vector
转换为对象的局部坐标空间:
object.worldToLocal( vector );