Jump to content

Imported mesh to BABYLON.Mesh


Aidoru
 Share

Recommended Posts

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

Link to comment
Share on other sites

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);
    });
    });






 

Link to comment
Share on other sites

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
 

Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...