When creating a floor plan, I want to add a ground according to the walls?

Hello everyone. I want to add a ground to the ground plan creation in the Babylon.js workshop. I added Ground and I want to crop it according to the position of the walls. But I get an error that VertexData is using too much memory.

The code I used to crop

var trimGroundOutsideWalls = function (ground, walls) {
        var minX = Number.POSITIVE_INFINITY;
        var maxX = Number.NEGATIVE_INFINITY;
        var minZ = Number.POSITIVE_INFINITY;
        var maxZ = Number.NEGATIVE_INFINITY;
    
        for (var w = 0; w < walls.length; w++) {
            var corner = walls[w].corner;
            if (corner.x < minX) minX = corner.x;
            if (corner.x > maxX) maxX = corner.x;
            if (corner.z < minZ) minZ = corner.z;
            if (corner.z > maxZ) maxZ = corner.z;
        }
    
        var vertices = ground.getVerticesData(BABYLON.VertexBuffer.PositionKind);
        var indices = ground.getIndices();
        var newVertices = [];
        var newIndices = [];
    
        for (var i = 0; i < vertices.length; i += 3) {
            var x = vertices[i];
            var z = vertices[i + 2];
    
            if (x >= minX && x <= maxX && z >= minZ && z <= maxZ) {
                newVertices.push(x, vertices[i + 1], z);
            }
        }
    
        for (var i = 0; i < indices.length; i += 3) {
            var i1 = indices[i];
            var i2 = indices[i + 1];
            var i3 = indices[i + 2];
    
            var v1Inside = newVertices.indexOf(vertices[i1 * 3]) !== -1;
            var v2Inside = newVertices.indexOf(vertices[i2 * 3]) !== -1;
            var v3Inside = newVertices.indexOf(vertices[i3 * 3]) !== -1;
    
            if (v1Inside && v2Inside && v3Inside) {
                newIndices.push(i1, i2, i3);
            }
        }
    
        var vertexData = new BABYLON.VertexData();
        vertexData.positions = newVertices;
        vertexData.indices = newIndices;
    
        ground = new BABYLON.Mesh("trimmedGround", scene);
        vertexData.applyToMesh(ground, true);
    
        return ground;
    };

How can I solve this problem and is there a simpler example for cropping?

Playground:

I didn’t dive into your code (as it is quite extensive :-)) But i think the polygon mesh builder will be very helpful to you, especially if you know how to get the walls’ properties:

Polygon Mesh Builder | Babylon.js Documentation (babylonjs.com)

3 Likes

thank you very much. it’s work.

1 Like