three.js加载多个对象的异步问题 [英] three.js load multiple objects asynchronous issue
问题描述
同时将多个模型加载到场景中,但是无法加载所有模型,只能在场景中加载一个模型
am loading multiple models on the same time to a scene, but it fails to load all the models, it only loading one model on the scene
例如,在一个建筑物场景中,该建筑物内有多个对象,例如椅子,玩具等,在加载这些对象时,只有一个对象正在加载,但是如果我以某种方式在功能结束时发出警报,模型正在加载
For example am having a building scene with the multiple objects like chairs, toys and so on inside that building, while loading those objects the only one object is loading, but somehow if i do a alert on end of the function all the models are loading
Image1现在正在得到什么,Image2我实际上想要什么
Image1 what am getting now, Image2 what actually i want
我的代码如下
function load_file(floor_number,x,y,z,width,height,rotation,angle,file)
{
obj_x=x;
obj_y=y;
obj_z=z;
obj_width=width;
obj_height=height;
obj_rotation=rotation;
var object_material = new THREE.MeshBasicMaterial({
color: 0xd6d6d6,
traansparent : true,
opacity : -2.5,
side: THREE.DoubleSide
});
var loader = new THREE.JSONLoader();
loader.load("uploads/accessories/3d/code/3dfile_"+file+".js",
function(geometry, object_material)
{
var object = new THREE.Mesh(geometry, new THREE.MeshFaceMaterial(object_material));
console.log(object);
model = new THREE.Object3D();
model.add(object);
model.position.set(obj_x,obj_y,obj_z);
model.scale.set(obj_width,obj_height,obj_rotation);
model.opacity =2;
model.rotation.y = 600;
model.duration = 12000;
model.mirroredLoop = true;
model.castShadow = true;
model.receiveShadow = true;
scene.add(model);
}
);
// alert('hi'); if i remove this comment second model is loading perfectly
return true;
}
还尝试使用Object3D.getObjectById()
通过id加载对象的,这也是失败的
我知道这是关于异步问题的,但我对此无能为力,对此没有任何帮助吗?
also tried to load the object's by id using Object3D.getObjectById()
this is also fails
i know this is about the asynchronous problem, but i can't get ride of this, any help on this?
推荐答案
问题出在全局变量上.
更新
我用类似于您的代码玩了一段时间,现在我看到了可能是什么问题,但这不是真的,您可以使用如下方法:
I played around a bit with a code similar to your, and I see now what could be what it seem a problem, but it isn't really, you can use an approach like this:
/* object declaration and initialization on load completed */
var model1;
load_file('object1', function(model) {
model1 = model;
model1.position.x = -2;
});
...
function laad_file(file, on_load_complete) {
...
loader.load("http://localhost/"+file+".js", function(geometry, object_material) {
...
scene.add(model);
/* call the callback to initialize your object */
if (on_load_complete !== undefined)
on_load_complete(model);
});
...
}
render() {
...
if (model1 !== undefined)
model1.rotation.x += 0.1;
...
}
这篇关于three.js加载多个对象的异步问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!