Sign in to follow this  
Aidoru

From AbstractMesh to Mesh

Recommended Posts

Hello everyone,
I have a little problem:
I would like to use CSG on mesh imported using assetsmanager, everythins work ok but I cant reach to have a CSG object, that I think it's because I have an abstractmesh and not a mesh,
So how to convert AbstractMesh to mesh? My bad part of code is that:

assetsManager.onFinish = function(task)
        {
            var prova = task[0].loadedMeshes[0];
            aCSG = BABYLON.CSG.FromMesh(prova);

I get "Cannot read property '0' of null", that I think because loadeMeshes return an Array<AbstractMesh> as the documentation here:  https://doc.babylonjs.com/api/classes/babylon.meshassettask#loadedmeshes
but BABYLON.CSG.FromMesh() method need a Mesh type as you can see in the documentation here: https://doc.babylonjs.com/api/classes/babylon.csg#frommesh

 

Could someone help me?

Thnak you

Share this post


Link to post
Share on other sites

Hi @Aidoru and welcome to the forum from me. Difficult to tell what you problem is without seeing other parts of your code. Here is a rough and ready playground that shows assetManager and CSG working together https://www.babylonjs-playground.com/#SAFTNA

Have a play around with it so that the code looks more like the one you are writing and get back to us.

Share this post


Link to post
Share on other sites

Thank you JohnK.

Here's my code using your (only changed the model) :

var canvas = document.getElementById("renderCanvas"); // Get the canvas element 

var engine = new BABYLON.Engine(canvas, true); // Generate the BABYLON 3D engine

var modelMesh;

/******* Add the create scene function ******/
var createScene = function () {
    var scene = new BABYLON.Scene(engine);

    //Adding a light
    var light = new BABYLON.PointLight("Omni", new BABYLON.Vector3(20, 20, 100), scene);
    var light0 = new BABYLON.HemisphericLight("Hemi0", new BABYLON.Vector3(0, 1, 0), scene);
    //Adding an Arc Rotate Camera
    var camera = new BABYLON.ArcRotateCamera("Camera", 0, 1.2, 40, BABYLON.Vector3.Zero(), scene);
    camera.attachControl(canvas, false);
    var ground = BABYLON.Mesh.CreateGround("ground1", 26, 26, 2, scene);
    ground.visibility = 0.4;
    var assetsManager = new BABYLON.AssetsManager(scene);

	var meshTask = assetsManager.addMeshTask("parte", "model1", "public/Models/", "parte.stl");
	box = BABYLON.MeshBuilder.CreateBox("", {size: 5}, scene);
    box.position.x = 1;
    box.position.z = -2;
	meshTask.onSuccess = function (task) {      
	    task.loadedMeshes[0].position = BABYLON.Vector3.Zero();
        task.loadedMeshes[0].scaling.copyFromFloats(0.03,0.03,0.03);
        fishMesh = task.loadedMeshes[0];
        fishMesh.rotationQuaternion = new BABYLON.Quaternion();
        fishMesh.position.y -= 1.2;
        
	}	

	assetsManager.onFinish = function (tasks) {
        csgFish = BABYLON.CSG.FromMesh(fishMesh);
        csgBox = BABYLON.CSG.FromMesh(box);
        csgHalfFish = csgFish.subtract(csgBox);
        fishMesh.dispose();
        box.dispose();
        csgHalfFish.toMesh("fishMesh");

	};
	
	assetsManager.load();


    return scene;
}

/******* End of the create scene function ******/

var scene = createScene(); //Call the createScene function

engine.runRenderLoop(function () { // Register a render loop to repeatedly render the scene
    scene.render();
});


window.addEventListener("resize", function () { // Watch for browser/canvas resize events
    engine.resize();
});

And here's the error code on Console in Chrome:

TypeError: Cannot read property '0' of null
    at Function.r.FromMesh (babylon.js:43)
    at i.assetsManager.onFinish (myscript_Forum.js:35)
    at i._decreaseWaitingTasksCount (babylon.js:50)
    at i (babylon.js:50)
    at i.e.onDoneCallback (babylon.js:50)
    at babylon.js:49
    at babylon.js:50
    at p (babylon.js:35)
    at babylon.js:35
    at m (babylon.js:35)

Share this post


Link to post
Share on other sites
8 minutes ago, Aidoru said:

Here's my code using your (only changed the model) :

In that case have you checked that the box and your model intersect. Actually the first step is to check your model loads. Do you get the same error messages if you only load the file?

See you are using an STL model, have you read this https://doc.babylonjs.com/how_to/stl

If you have read and done this I cannot help as I do not work very much with loaded files. Could also be an error in your file.

Share this post


Link to post
Share on other sites

Thank you John,

Yes I know SceneLoader but I would like to use AssetsManager if it is possible.

Box and my model intersect, and I get that error when using CSG.FromMesh

So I think that probably because FromMesh method need a Mesh amd I only have an AbstractMesh that I think is a Mesh parent if I look on that tree http://doc.babylonjs.com/api/classes/babylon.mesh

Share this post


Link to post
Share on other sites

My suggestion to get more help  would be edit your first post and change your title to better reflect your issue. Something like imported stl file not converting to CSG

Share this post


Link to post
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...
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.