1

I am currently loading in a bunch of elements of a model and adding them to a group. I then want to be able to move the group as a whole unit after they load.

My issue is, how can I run code once I have ensured that all of my models have loaded? I don't understand how to use the .onload function with the colladaLoader & its callback functions. Also, is it a bad idea to use a self executing function like I have here? I am not sure how else to go through a list and load in all the models.

My code is below. I have tried to use a hack, by using the counter "complete" but it doesn't work all of the time. Thanks!

for ( var i=0; i<object.asset.length; i++ ) {

              loader = new THREE.ColladaLoader();
              asset = furniture.asset[i];

    (function(asset) {

                    loader.load(asset["path"], function(collada, materials) {

                        //this function scales & positions model
                        var mesh = daeAttributes(collada, object, asset, newMaterial);

                        var scene = get_scene();
                        group.add( mesh );
                        complete++;

                        //when all assets have been loaded, add the group to the scene  
                        if (complete===object.asset.length-1) {

                           //want to move the group once all the models are loaded
                           group = moveModel(group, object);
                           scene.add( group );
                           render();

                        };           
                    });                       
              })(asset);

Update

So if I have a list >1, of unique objects to load in, it works. If there is only 1 object it doesn't load that one object.

Laura
  • 469
  • 1
  • 6
  • 12

1 Answers1

0

If you put everything inside of this it should work:

<body onload="myFunction()">