I load some objects in my scene:
var objectInfo = [
{"objURL":"3D/Barrel/barrel.obj", "mtlURL":"3D/Barrel/barrel.mtl","xPOS":0,"yPOS":-2,"zPOS":-260,"xROT":0,"yROT":0,"zROT":0,"scaleX":0.6,"scaleY":0.6,"scaleZ":0.6},
{"objURL":"3D/Sofa/sofa.obj", "mtlURL":"3D/Sofa/sofa.mtl","xPOS":0,"yPOS":0,"zPOS":0,"xROT":0,"yROT":4,"zROT":0,"scaleX":1,"scaleY":1,"scaleZ":1},
{"objURL":"3D/Lamp/lamp.obj", "mtlURL":"3D/Lamp/lamp.mtl","xPOS":210,"yPOS":-20,"zPOS":10,"xROT":0,"yROT":50,"zROT":0,"scaleX":1,"scaleY":1,"scaleZ":1},
];
for (var i = 0; i < objectInfo.length; i++) {
loader=new THREE.OBJMTLLoader();
loader.addEventListener('load', function (event) { //callback f. to load .obj and .mtl
obj = event.content;
var c=count(); //just an external counter because i is not defined in here
obj.position.set(objectInfo[c].xPOS,objectInfo[c].yPOS,objectInfo[c].zPOS);
obj.rotation.x=objectInfo[c].xROT;
obj.rotation.y=objectInfo[c].yROT;
obj.rotation.z=objectInfo[c].zROT;
obj.scale.set(objectInfo[c].scaleX, objectInfo[c].scaleY, objectInfo[c].scaleZ);
scene.add(obj);
OBJS[c]=obj;
});
loader.load(objectInfo[i].objURL,objectInfo[i].mtlURL);
}
But when I hard refresh the page (F5, Chrome) without having changed anything , the objects are in different position than before. When I hit again the F5, the objects are in their old position. ANd this happens alla the time and it is random where the objects will be.
What is the problem? Please answer because I have made much research before posting.
EDIT: alternative implementation without counter() : (still not working)
for (var i = 0; i < objectInfo.length; i++) {
loader=new THREE.OBJMTLLoader();
loader.addEventListener('load', function (event) { //callbac f. to load .obj and .mtl
obj = event.content;
var objectInfo2=objectInfo.pop();
obj.position.set(objectInfo2.xPOS,objectInfo2.yPOS,objectInfo2.zPOS);
obj.rotation.x=objectInfo2.xROT;
obj.rotation.y=objectInfo2.yROT;
obj.rotation.z=objectInfo2.zROT;
obj.scale.set(objectInfo2.scaleX, objectInfo2.scaleY, objectInfo2.scaleZ);
scene.add(obj);
// OBJS[c]=obj;
});
loader.load(objectInfo[i].objURL,objectInfo[i].mtlURL);
}
Also, not only the position attributes are randomly changed but also scale etc.
load
events will fire in the random order.
--edit--
I think the problem is related to the counter
function call because it assumes that the first call is from the first(in the array) object load
event callback.
OBJMTLLoader has load: function ( url, mtlurl, onLoad, onProgress, onError )
you can pass onLoad
with the right index.