我在场景中加载了一些对象:
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);
}
但是当我在不更改任何内容的情况下刷新页面(F5,Chrome)时,对象处于与以前不同的位置。当我再次击中F5时,物体处于旧位置。并且这发生在时间上,并且它是随机的对象。
有什么问题? 请回答,因为我在发布之前做了很多研究。
编辑:没有counter()的替代实现:(仍然无效)
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);
}
此外,不仅位置属性随机改变,而且比例等。
答案 0 :(得分:1)
load
事件将以随机顺序触发。
- 编辑 -
我认为问题与counter
函数调用有关,因为它假定第一个调用来自第一个(在数组中)对象load
事件回调。
OBJMTLLoader有load: function ( url, mtlurl, onLoad, onProgress, onError )
您可以使用正确的索引传递onLoad
。