javascript - 将网格导入到 BABYLON.Mesh

标签 javascript babylonjs

我在导入的网格体上使用 CSG 时遇到问题,这是我的代码:

var a; var b;

BABYLON.SceneLoader.ImportMesh("", "./public/Models/", "model1.stl", scene, function (newMeshes) {
    // Set the target of the camera to the first imported mesh
    camera.target = newMeshes[0];
    a = newMeshes[0];
});



BABYLON.SceneLoader.ImportMesh("", "./public/Models/", "model2.stl", scene, function (newMeshes) {
    // Set the target of the camera to the first imported mesh
    //camera.target = newMeshes[0];
    b = newMeshes[0];
});


var aCSG = BABYLON.CSG.FromMesh(a);
var bCSG = BABYLON.CSG.FromMesh(b);

“var a”和“var b”未定义,调试告诉我

 “BABYLON.CSG:网格类型错误,必须是 BABYLON.Mesh”

有什么方法可以将导入的网格转换为BABYLON.MESH吗?

非常感谢

最佳答案

这是因为 ImportMesh 是异步的,您必须在回调部分移动代码:

BABYLON.SceneLoader.ImportMesh("", "./public/Models/", "model1.stl", scene, function (newMeshes) {
    // Set the target of the camera to the first imported mesh
    camera.target = newMeshes[0];
    a = newMeshes[0];
BABYLON.SceneLoader.ImportMesh("", "./public/Models/", "model2.stl", scene, function (newMeshes) {
    // Set the target of the camera to the first imported mesh
    //camera.target = newMeshes[0];
    b = newMeshes[0];

var aCSG = BABYLON.CSG.FromMesh(a);
var bCSG = BABYLON.CSG.FromMesh(b);
});



});

关于javascript - 将网格导入到 BABYLON.Mesh,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51594372/

相关文章:

javascript - 如何在 JavaScript 中获取完全准确的值?

javascript - 基于选择性超时的事件处理 : immediate first, 接下来去抖动

html - 在一个html中加载多个babylon文件

javascript - Babylon.js,如何从场景中删除网格?

javascript - 使用 3 个向量在 Babylon.js 中创建一个平面

javascript - 类型错误 : Cannot read property 'length' of undefined react

javascript - 禁用旧按钮上的功能

javascript - app.get() 和 app.route().get() 的区别

javascript - BabylonJS 中从 AbstractMesh 到 Mesh

next.js - Next Js 中的 @Babylonjs (ES6) 因意外 token 'export' 而失败