0

Hello StackOverflow community,

I have problems adding names to THREE.Mesh() while batch loading them.

The code below is having the error:

function loadObjects () {
    var objectURL = ["cube", "birdie", "sphere"];

    for (var i = 0; i < objectURL.length; i++) {
        loader.load(
            "Ressources/models/" + objectURL[i] + ".json", 
            function (geometry, materials) {
                var material = new THREE.MeshFaceMaterial(materials);
                var mesh = new THREE.Mesh(geometry, material);
                mesh.name = String(objectURL[i]);
                scene.add(mesh);
            }
        );
    }
}

While loading a single is working fine:

function loadObject () {
    var objectURL = ["cube", "birdie", "sphere"];

    loader.load(
        "Ressources/models/" + objectURL[1] + ".json", 
        function (geometry, materials) {
            var material = new THREE.MeshFaceMaterial(materials);
            var mesh = new THREE.Mesh(geometry, material);
            mesh.name = String(objectURL[1]);
            scene.add(mesh);
        }
    );
}

All models are loaded and rendered correctly, but it is not possible to use scene.getObjectByName("birdie"); , when the objects are batch loaded.

About an answer I would be very happy. Regards Codepuree

EDIT: Found a way to batch load Objects:

var objectURL = ["cube", "birdie", "sphere"];

function loadObjects (iterator) {

    var loadMesh = new THREE.Mesh();
    loader.load(
        "Ressources/models/" + String(vegetationURL[iterator]) + ".json", 
        function (geometry, materials) {
            var material = new THREE.MeshFaceMaterial(materials);
            loadMesh = new THREE.Mesh(geometry, material);
            loadMesh.name = String(vegetationURL[iterator]);
            scene.add(loadMesh);
        }
    );

    loader.onLoadComplete = function () {
        iterator++;
        if (iterator < objectURL.length) {
            loadObjects(iterator);
        }
    }
}

loadObjects(0);

The problem was that the loader is loading asynchronously. So the load function is to slow for the for-loop. Loading it in a while loop causes and dead loop. That is why to go with the recursive way.

This works fine now and thanks for the help ther. :)

1 Answer 1

1

Problem might be that when the lambda function is called the value of the i variable is objectURL.length, since the function can be called after the for loop is terminated.

Try this instead:

function loadObjects () {
    var objectURL = ["cube", "birdie", "sphere"];

    for (var currentURLId in objectURL){
        loader.load(
            "Ressources/models/" + objectURL[currentURLId] + ".json", 
            function (geometry, materials) {
                var material = new THREE.MeshFaceMaterial(materials);
                var mesh = new THREE.Mesh(geometry, material);
                mesh.name = String(objectURL[currentURLId]);
                scene.add(mesh);
            }
        );
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

It is not working since currentURL only returns a number. So the url to the model looks like this, "Ressources/models/0.json" now.
Sorry, my mistake. Confused how foreach works in Javascript. Try the edited.
Still not working scene.getObjectByName("birdie") always returns "undefined"
Try adding a console.log(currentURLId) in after the for loop and inside the function(geometry, materials), maybe that will reveal the problem. Unfortunately I can't try the script myself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.