Aidoru Posted August 2, 2018 Share Posted August 2, 2018 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 Quote Link to comment Share on other sites More sharing options...
JohnK Posted August 2, 2018 Share Posted August 2, 2018 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. Quote Link to comment Share on other sites More sharing options...
Aidoru Posted August 2, 2018 Author Share Posted August 2, 2018 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) Quote Link to comment Share on other sites More sharing options...
JohnK Posted August 2, 2018 Share Posted August 2, 2018 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. Quote Link to comment Share on other sites More sharing options...
Aidoru Posted August 2, 2018 Author Share Posted August 2, 2018 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 Quote Link to comment Share on other sites More sharing options...
JohnK Posted August 2, 2018 Share Posted August 2, 2018 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 Quote Link to comment Share on other sites More sharing options...
wuifi Posted May 6, 2019 Share Posted May 6, 2019 Hi @Aidoru Did you ever solve this problem? I am new to BabylonJS and facing the exact same issue. Neither AssetManager nor SceneLoader (https://www.babylonjs-playground.com/#XA6F3#10) is working for me. Thanks! Quote Link to comment Share on other sites More sharing options...
MarianG Posted May 21, 2019 Share Posted May 21, 2019 Hi @wuifi If you didn't do it yet, please post your question here https://forum.babylonjs.com 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.