Jump to content

Can't remove all targeted meshes via iteration


Yazheirx
 Share

Recommended Posts

function resetPack(scene) {
    console.log(scene.meshes.length);
    for (currentMesh = 0; currentMesh < scene.meshes.length; currentMesh++) {
        // console.log(scene.meshes[currentMesh].geometry.getTotalVertices());
        if (scene.meshes[currentMesh].geometry.getTotalVertices() == 24) {
            console.log(currentMesh + ") Removing " + scene.meshes[currentMesh].name);
            index = scene.removeMesh(scene.meshes[currentMesh], true);
            console.log(index + " Removed");
        } else {
            console.log(currentMesh + ") Not removing (" + scene.meshes[currentMesh].name + ") with (" + scene.meshes[currentMesh].geometry.getTotalVertices() + ") geometry");
        }
    }
}

In the above code, I am trying to remove all cuboid forms from my scene.  All of my cuboid meshes are made with the same code:

function createPackObject(shapeName, shape, dimensionsJSON, diffuseColor, offsetX, offsetY, alpha, scene) {
    // console.log('Creating ' + shapeName);
    switch (shape) {
        case "cuboid":
            // console.log('Creating ' + shapeName + ' as a cuboid');
            var packObject = BABYLON.MeshBuilder.CreateBox(shapeName, {
                width: dimensionsJSON[0].value,
                depth: dimensionsJSON[1].value,
                height: dimensionsJSON[2].value
            }, scene);
            packObject.position = new BABYLON.Vector3(offsetX, ((dimensionsJSON[2].value / 2) + offsetY), 0);
            packObject.material = new BABYLON.StandardMaterial(shapeName + "Material", scene);
            packObject.material.diffuseColor = diffuseColor;
            packObject.material.alpha = alpha;
            packObject.material.wireframe = false;
            break;
        default:
            console.log('Shape "' + solutionSet.containers[container].spaceToFill.shape + '" not handled');
    }
}

function loadSolution(scene, camera, solutionID) {
     // ...
// create shipping containers
            for (currentContainer = 0; currentContainer < solutionSet.containers.length; currentContainer++) {
                // ...
                createPackObject(containerShapeName + "I", containerInnerShape, containerInnerDimensions, new BABYLON.Color3(1, 0.87, 0.386), containerOffset, wallThickness, 0.55, scene);
                createPackObject(containerShapeName + "O", containerOuterShape, containerOuterDimensions, new BABYLON.Color3(1, 0.87, 0.386), containerOffset, 0, 0.55, scene);
                // ...
                for (currentItem = 0; currentItem < solutionSet.containers[currentContainer].items.length; currentItem++) {
                    createPackObject(itemShapeName, itemOuterShape, orientedDimensions, itemColor, itemOffset, 0, 1, scene);
                }
     }
}

For some reason when I get to a mesh created for the containers the next item mesh I try to remove exits the function.  Does anyone have any idea what might be happening, or if there is a better way to remove a particular type of mesh from a scene?

Link to comment
Share on other sites

Well...it is never a good idea to update an array while browsing it ;)

try that:

function resetPack(scene) {
    console.log(scene.meshes.length);
    for (currentMesh = 0; currentMesh < scene.meshes.length; currentMesh++) {
        // console.log(scene.meshes[currentMesh].geometry.getTotalVertices());
        if (scene.meshes[currentMesh].geometry.getTotalVertices() == 24) {
            console.log(currentMesh + ") Removing " + scene.meshes[currentMesh].name);
            index = scene.removeMesh(scene.meshes[currentMesh], true);
            console.log(index + " Removed");
currentMesh--;
        } else {
            console.log(currentMesh + ") Not removing (" + scene.meshes[currentMesh].name + ") with (" + scene.meshes[currentMesh].geometry.getTotalVertices() + ") geometry");
        }
    }
}

 

Link to comment
Share on other sites

Oh, wait, I think I understand.  Would it work better if iterated from the tail of the mesh array?

for(currentMesh = scene.meshes.length-1; currentMesh > -1; currentMesh --) {
        if (scene.meshes[currentMesh].geometry.getTotalVertices() == 24) {
            console.log(currentMesh + ") Removing " + scene.meshes[currentMesh].name);
            index = scene.removeMesh(scene.meshes[currentMesh], true);
            console.log(index + " Removed");
        } else {
            console.log(currentMesh + ") Not removing (" + scene.meshes[currentMesh].name + ") with (" + scene.meshes[currentMesh].geometry.getTotalVertices() + ") geometry");
        }
}

If I am understanding correctly, by removing a node from the array as I am 'climbing' it I drive it out of bounds?

Edited by Yazheirx
forgot that pesky "-1" on length
Link to comment
Share on other sites

SOLVED: all cleaned up now:

function resetPack(scene) {
    // iterate from the end of the mesh array to the beginning, removing any boxes found
    for (currentMesh = scene.meshes.length - 1; currentMesh > -1; currentMesh--) {
        // only remove a mesh if it has 24 Vertices - meaning it is a box
        if (scene.meshes[currentMesh].geometry.getTotalVertices() == 24) {
            index = scene.removeMesh(scene.meshes[currentMesh], true);
        }
    }
}

 

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