如何在Three.JS中获取obj模型对象的顶点?

问题描述 投票:0回答:1

在Three.js中加载.obj模型后,我无法找到顶点数据。根据this answer

的建议,需要顶点数据来进行碰撞检测
var loader = new THREE.OBJLoader();
loader.load('models/wall.obj', function ( object ) {
    object.traverse( function ( node ) {
       if ( node.isMesh ) {
         console.log(node);
       }
    }); 
    scene.add( object );                
});

在网格中有geometry.attributes.position.array,但我无法在对象的任何地方找到“顶点”。

现在尝试将position.array数据转换为顶点,但是下面的代码不起作用,this answer正确指出了问题,但我无法使用它来解决问题:

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
three.js vertices wavefront objloader
1个回答
0
投票

geometry.attributes.position.array IS顶点。每三个值组成一个顶点。您还需要查看index属性(geometry.index),因为这是position数组的索引列表,定义了组成形状的顶点。在Mesh定义为单个三角形的情况下,每三个索引组成一个三角形。 (Tri-strip数据略有不同,但是通过索引引用顶点值的概念是相同的。)

您可以交替使用属性便捷功能:

这些功能将索引考虑在内。因此,如果您想要第一个三角形的第一个顶点(索引= 0):

let pos = geometry.attributes.position;
let vertex = new THREE.Vector3( pos.getX(0), pos.getY(0), pos.getZ(0) );

有了顶点,然后,您可以使用mesh.localToWorld将点转换为世界空间。

© www.soinside.com 2019 - 2024. All rights reserved.