My scene is populated with various objects:
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) { //event handler for loading .obj and .mtl
obj = event.content;
var c=count(); //external counter used as i is not defined 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);
}
However, after hard refreshing the page in Chrome (F5), the objects appear in different positions. Subsequent refreshes sometimes restore the original positions. This behavior seems random.
What could be causing this issue? I've searched extensively but couldn't find a solution.
UPDATE: Attempted alternative implementation without the counter() function (still facing issues)
for (var i = 0; i < objectInfo.length; i++) {
loader=new THREE.OBJMTLLoader();
loader.addEventListener('load', function (event) { //event handler for loading .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);
}
Not only are the positions randomly changing after each refresh, but the scale and other attributes are also affected.