oschakravarthi

Building NavMesh Programatically

Recommended Posts

Hi,

I am trying to build navigation mesh programatically for my flat (apartment) model

The logic I could think of, 

1. I know all the floor meshes of my model. I keep them in an array.

2. I create a empty CSG and add (union) of CSG of all floor meshes. 

3. For each non-floor mesh, I check if it is intersects with any of the floor mesh. If yes, then substract that part from my unioned CSG. (It makes a hole)

4. Build a new mesh from the unioned CSG.

 

The below is a rough implementation of this idea. Can you please suggest a better approach?

 

Thanks in advance.  

 

var buildNavMesh = function (scene, floorMeshes) {
            var navigationCSG = new BABYLON.CSG();
            floorMeshes.forEach(floorMesh => {
                var floorMeshCSG = BABYLON.CSG.FromMesh(floorMesh);
                navigationCSG.unionInPlace(floorMeshCSG);
            });

            var nonFloorMeshes = [];
            scene.meshes.forEach(mesh => {
                if (floorMeshes.indexOf(mesh) < 0) {
                        nonFloorMeshes.push(mesh);
                        mesh.position.y -= 0.2;
                    }

            });

            scene.render();

            for (var i = 0; i < floorMeshes.length; i++) {
                var floorMesh = floorMeshes;
                nonFloorMeshes.forEach(nonFloorMesh => {
                    if (nonFloorMesh.intersectsMesh(floorMesh)) {
                        var nonFloorMeshCSG = BABYLON.CSG.FromMesh(nonFloorMesh);
                        navigationCSG.subtractInPlace(nonFloorMeshCSG);    
                    }
                });
            }

            nonFloorMeshes.forEach(mesh => {
                mesh.position.y += 0.2;
            });
            var navMesh = navigationCSG.toMesh("csg4", null, scene, true);
        
            scene.removeMesh(navMesh);

            return navMesh;
        };
 

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

  • Recently Browsing   0 members

    No registered users viewing this page.