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

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

How to get the absolute position of a vertex in three.js?

javascriptwebglthree.js

提问by Danny Fox

As far as I know var point = object.geometry.vertices[i];will return with the relative position for the x, yand zof the point inside the geometry of the object.

据我所知,var point = object.geometry.vertices[i];将返回x,yz对象几何内部点的相对位置。

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 参考书。

  1. 3D math primer for graphics and game development / by Fletcher Dunn and Ian Parberry

  2. Essential Mathematics for Games and Interactive Applications: A Programmer's Guide James M. Van Verth and Lars M. Bishop

  1. 用于图形和游戏开发的 3D 数学入门 / Fletcher Dunn 和 Ian Parberry

  2. 游戏和交互式应用程序的基本数学:程序员指南 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.Object3Dclass. In particular, to get the world coordinates of a local THREE.Vector3instance named vector, use the localToWorldmethod:

Three.js (v50+) 的最新版本THREE.Object3D类中提供了这个功能。特别是,要获取THREE.Vector3名为的本地实例的世界坐标vector,请使用以下localToWorld方法

object.localToWorld( vector );

Similarly, there is a worldToLocalmethodfor converting world coordinates into local coordinates, which is slightly more complicated, mathematically speaking. To translate a world vectorinto the objects local coordinate space:

同样,还有一个worldToLocal方法,用于将世界坐标为局部坐标,这是稍微复杂一些,从数学上讲。要将世界vector转换为对象的局部坐标空间:

object.worldToLocal( vector );