5

I am loading multiple car models using a loop in THREE.js, but the problem is that sometime it loads all the objects but some time it does not load all the objects. For example if the loop is of 3 iteration, it will load 2 objects sometime, sometime it loads 1 and sometime it loads all three objects. I don't know why? I searched a lot but can't find any thing useful. Here is the code.

for (var k = 1; k <= myWorld.noOfEnemies(); k++) {

                myWorld.setWorldEnemyCar(k);

                loader2.load('obj/us/us_police_car.dae', function colladaReady(collada) {
                    object3 = collada.scene;

                    object3.scale.x = object3.scale.y = object3.scale.z = 2;
                    object3.updateMatrix();
                    object3.position.x = myWorld.enemyCar.position.x;
                    object3.position.y = myWorld.enemyCar.position.y;
                    object3.position.z = myWorld.enemyCar.position.z;

                    object3.rotation.x = -(Math.PI / 2);

                    object3.rotation.z = (Math.PI / 2);
                    enemyModels.push(object3);
                    //localObject.rotation.z = -(Math.PI / 2);
                    //collidableMeshList3 = localObject;
                    //console.log(collidableMeshList3);

                    // init();

                    // animate();

                });

            }

After this one more loader in which I have init() and animate() functions

loader2.load('obj/us/us_police_car.dae', function colladaReady(collada) {
                localObject = collada.scene;

                localObject.scale.x = localObject.scale.y = localObject.scale.z = 2;
                localObject.updateMatrix();
                localObject.position.x = 0;
                localObject.position.y = 0;
                localObject.position.z = 0;

                localObject.rotation.x = -(Math.PI / 2);

                localObject.rotation.z = (Math.PI / 2);

                //localObject.rotation.z = -(Math.PI / 2);
                //collidableMeshList3 = localObject;
                //console.log(collidableMeshList3);
                //scene.add(localObject);

                init();

                animate();

            });

This works fine, but can't figure out what the problem with the above one.

1
  • There seems to be a bug [github.com/mrdoob/three.js/issues/5721] with the collada loader when loading multiple collada files with a single loader instance, see my answer below. Commented Feb 27, 2015 at 11:00

5 Answers 5

2

There seems to be a known issue when re-using the same instance of the collada loader to load multiple collada files.

The following code works reliably for me (at least in chrome and firefox):

scene = new THREE.Scene();

// setup lighting etc.

load('/path/someColladaModel.dae');
load("/path/someOtherColladaModel.dae");
load("/path/yetAnotherColladaModel.dae");

function load(daeLocation){
    var manager = new THREE.LoadingManager();
    manager.onProgress = function(item, loaded, total) {
        console.log(item, loaded, total);
    };

    var loader = new THREE.ColladaLoader(manager);
    loader.options.convertUpAxis = true;

    loader.load(daeLocation, function(collada) {
            dae = collada.scene;
            dae.position.set(0, 0, 0); 
            scene.add(dae);
            render();
        }, function(progress) {
            // show some progress
    });
}

Note I am instantiating a new loader every time I load a model.

Sign up to request clarification or add additional context in comments.

Comments

1

Just make your own async loader. Your problem is that you use an async function as sync. You never know where the async function ends and the code after it runs without waiting for it to end. This is a general javascript porblem not just three.js

loader = function(files,callback){
    var i = 0;
    var objects = new Array();
    files.forEach(function(file){
        loader2.load('obj/us/us_police_car.dae', function colladaReady(collada) {
            objects[i] = collada.scene;
            (... rest of the code for each object)
            i++;
            if (i == files.length) {
                callback(objects);
            }
        } 
    });
}

1 Comment

i made a version that loads synchronously, but this does not address the problem. it is apparently not related to asynch.
0

I had the same problem. Apparently the Collada loader current cannot handle loading multiple files. I solved it by putting all objects in a single file and then when it is finished loading I locate the individual objects and use them separately. Hope this helps and that this is an option in your case.

3 Comments

can you please more elaborate about what you have done. I can't understand your idea exactly. btw: thanks
Problem is that some time it loads all the objects, some time it loads two, sometime it load one and sometime nothing. Is this loader problem or some thing with the loop or the array in the loop with name enemyModels as this is mostly have nothing in it outside the loop.
See my answer - this is bug [github.com/mrdoob/three.js/issues/5721] you can get around this by instantiating multiple collada loaders.
0

If you're loading the same model multiple times there's no need to invoke the loader for each one. You could clone the mesh:

    // queen is a mesh
    var newQueen = queen.clone();
    // make sure to re position to be able to see the new queen!
    newQueen.position.set(100,100,100); // or any other coordinates

Comments

0

I just determined that this problem is a bug in 3js collada loader, they have a new loader that supposedly doesnt have this problem but it is not released yet (dec 2015), you can get it in the dev tree. see https://github.com/mrdoob/three.js/issues/7388

Comments

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.