Aidoru Posted July 30, 2018 Share Posted July 30, 2018 Hello, I have a problem to use CSG on imported meshes, here's my code: 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" and "var b" are undefined and Debug told me that "BABYLON.CSG: Wrong Mesh type, must be BABYLON.Mesh" Is there any method to convert imported mesh to BABYLON.MESH? Thank you very much Quote Link to comment Share on other sites More sharing options...
Guest Posted July 30, 2018 Share Posted July 30, 2018 Hello and welcome (you were not in the right sub forum (I moved the topic already)) So in your example, ImportMesh is an asynchronous function which means that a and b will be set but later when the file will be read. So you have to move to something (not tested) like that: 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); }); }); Quote Link to comment Share on other sites More sharing options...
brianzinn Posted July 30, 2018 Share Posted July 30, 2018 It looks like you want to load the meshes at the same time, based on your comment about setting the target. There may be a race condition - it's untested: var a; var b; var meshesLoaded = 0; BABYLON.SceneLoader.ImportMesh("", "./public/Models/", "model1.stl", scene, function (newMeshes) { a = newMeshes[0]; meshesLoaded++; // Set the target of the camera to the first imported mesh if (meshesLoaded === 1) { camera.target = newMeshes[0]; } else if (meshesLoaded === 2) { // CSG } }); BABYLON.SceneLoader.ImportMesh("", "./public/Models/", "model2.stl", scene, function (newMeshes) { a = newMeshes[0]; meshesLoaded++; // Set the target of the camera to the first imported mesh if (meshesLoaded === 1) { camera.target = newMeshes[0]; } else if (meshesLoaded === 2) { // CSG } }); I think a better way is with promises and it's easier to make a more generic solution. Here is a commit that loads all the materials and returns when they are all loaded. So, you could write a method that takes a list of Models and returns when they are all loaded. Look for these lines: mtlPromises.push(new Promise((resolve, reject) => {...})); and Promise.all(mtlPromises).then(() => {...}); https://github.com/BabylonJS/Babylon.js/commit/82f8addaae0c84e35adf289b122d3423dd9d59c5#diff-2209ca1e0591b2fc9b58edd4295b48eb Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.