在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
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
将点转换为世界空间。