获取网格的顶点的最佳方法 Three.js

Ant*_*mes 3 javascript three.js

我是 Three.js 的新手,所以也许我不会以最佳方式接近这个,

我有如下创建的几何图形,

const geo = new THREE.PlaneBufferGeometry(10,0);
Run Code Online (Sandbox Code Playgroud)

然后我对其应用旋转

geo.applyMatrix( new THREE.Matrix4().makeRotationX( Math.PI * 0.5 ) );
Run Code Online (Sandbox Code Playgroud)

然后我从中创建一个网格

const open = new THREE.Mesh( geo, materialNormal);
Run Code Online (Sandbox Code Playgroud)

然后我对网格应用一系列操作以正确定位它,如下所示:

open.position.copy(v2(10,20);
open.position.z = 0.5*10

open.position.x -= 20
open.position.y -= 10
open.rotation.z = angle;
Run Code Online (Sandbox Code Playgroud)

现在在位置改变之前和之后获取网格顶点的最佳方法是什么?我很惊讶地发现网格的顶点没有内置到three.js 中。

任何提示和代码示例将不胜感激。

The*_*m01 6

我认为你被一些关于 Three.js 对象的语义绊倒了。

1) AMesh没有顶点。AMesh包含对Geometry/BufferGeometryMaterial(s) 的引用。顶点包含在Meshgeometry属性/对象中。

2)您正在使用PlaneBufferGeometry,这意味着一个BufferGeometry对象的实现。BufferGeometry将其顶点保留在position属性 ( mesh.geometry.attributes.position) 中。请记住,顶点顺序可能会受到index属性 ( mesh.geometry.index) 的影响。

现在,对于您的问题,几何原点也是其 parentMesh的原点,因此您的“网格变换之前”顶点位置与创建网格时完全相同。只需按原样读出它们。

要获得“网格变换后”顶点位置,您需要获取每个顶点,并将其从Mesh的局部空间转换为世界空间。幸运的是,three.js 有一个方便的函数来做到这一点:

var tempVertex = new THREE.Vector3();
// set tempVertex based on information from mesh.geometry.attributes.position

mesh.localToWorld(tempVertex);
// tempVertex is converted from local coordinates into world coordinates,
// which is its "after mesh transformation" position
Run Code Online (Sandbox Code Playgroud)