Jump to content

GroundMesh + BABYLON.CSG => error


jdurrant
 Share

Recommended Posts

I create a groundmesh using these commands:

var groundMaterial = new BABYLON.StandardMaterial("ground", scene);groundMaterial.backFaceCulling = false;groundMaterial.diffuseTexture = new BABYLON.Texture("assets/ground/ground.png", scene);groundMaterial.specularColor = new BABYLON.Color3(0, 0, 0);var highres_ground = BABYLON.Mesh.CreateGroundFromHeightMap("highres_ground", "assets/ground/ground.png", 500, 500, 50, -100, 0, scene, false);highres_ground.material = groundMaterial;highres_ground.checkCollisions = true;

I'd like to excise just part of this groundmesh using CSG. So I run this command:

highresCSG = BABYLON.CSG.FromMesh(highres_ground);

But I get this error: TypeError: subMeshes is undefined

 

When I replace highres_ground with a simple cube, I don't get the error:

var highres_ground = BABYLON.Mesh.CreateBox("box", 20, scene);

Any suggestions on how I can convert groundmesh into a standard mesh that can be manipulated with CSG? Thanks for your help.

 

Link to comment
Share on other sites

In preparing the files to show you the "problem," I discovered the solution on my own. :) I didn't realize (though I should have) that BABYLON.Mesh.CreateGroundFromHeightMap is async. I was trying to use CSG before the groundmesh was ready. In case it's helpful to others, wrapping the CSG commands in a scene.executeWhenReady fixed the problem for me:

var cube = BABYLON.Mesh.CreateBox("box", 20, scene);cubeCSG = BABYLON.CSG.FromMesh(cube);var ground = BABYLON.Mesh.CreateGroundFromHeightMap("ground", "ground.png", 500, 500, 50, -100, 0, scene, false);scene.executeWhenReady(function () {    groundCSG = BABYLON.CSG.FromMesh(ground);        var subCSG = groundCSG.intersect(cubeCSG);    subCSG.toMesh("csg", new BABYLON.StandardMaterial("mat", scene), scene);        cube.dispose();    ground.dispose();    });

Thanks for all your incredible work on babylon.js, by the way. It's a great package.

Link to comment
Share on other sites

Another way would be to use the onReady callback in the CreateGroundFromHeightMap function :

var ground = BABYLON.Mesh.CreateGroundFromHeightMap("ground", "ground.png", 500, 500, 50, -100, 0, scene, false, function(/*groundMesh*/) {    groundCSG = BABYLON.CSG.FromMesh(ground);        var subCSG = groundCSG.intersect(cubeCSG);    subCSG.toMesh("csg", new BABYLON.StandardMaterial("mat", scene), scene);        cube.dispose();    ground.dispose();});

It was added recently (to 2.0)

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...