问题描述
我正在使用 STLLoader 将 STL 文件加载到three.js 中,我想在调用加载器以供进一步使用后获取模型的顶点(和几何图形)。 我怎样才能做到这一点? 我当前的代码如下,但在调用加载程序后我无法获取几何图形。
var loader = new THREE.STLLoader(); var myModel = new THREE.Object3D(); loader.load("myModel.stl", function (geometry) { var mat = new THREE.MeshLambertMaterial({color: 0x7777ff}); var geo = new THREE.Geometry().fromBufferGeometry(geometry); myModel = new THREE.Mesh(geo, mat); scene.add(myModel); }); console.log(myModel.geometry.vertices)
1楼
VanAlbert
0
2021-02-06 21:18:59
从three.js R125开始,推荐的方法是使用loadAsync方法,它现在是three.js的原生方法:
该方法返回一个承诺。 然后您可以使用“then”来获取 STL 的几何形状并创建网格。 您也可以使用传统的回调或 async/await 结构,但我认为下面使用原生three.js 方法的示例是最简单的方法。 该示例显示了如何在解析承诺并加载 STL 文件后将几何体获取到全局变量:
// Global variables for bounding boxes
let bbox;
const loader = new STLLoader();
const promise = loader.loadAsync('model1.stl');
promise.then(function ( geometry ) {
const material = new THREE.MeshPhongMaterial();
const mesh = new THREE.Mesh( geometry, material );
mesh.geometry.computeBoundingBox();
bbox = mesh.geometry.boundingBox;
scene.add( mesh );
buildScene();
console.log('STL file loaded!');
}).catch(failureCallback);
function failureCallback(){
console.log('Could not load STL file!');
}
function buildScene() {
console.log('STL file is loaded, so now build the scene');
// !VA bounding box of the STL mesh accessible now
console.log(bbox);
// Build the rest of your scene...
}