Currently, I am loading multiple gltf models using an async function. After that, I go through the results using .then()
and add the models to my scene. However, everything works perfectly fine until I decide to remove one of the models from the source. Specifically, when the model I want to remove is larger than the rest. Upon removing the model, it appears as though the promise resolution isn't waiting for all promises to resolve before calling .then()
. Here is the loader function, can you spot what I might be doing wrong?
The 'source' mentioned here is simply an array of objects containing paths to various model files.
The issue seems to lie within the .then()
block. The error message displayed in the console states: 'variant.components is not iterable' whenever a large-sized model is removed from the 'source' array.
async function modelLoader(source, loadingManager) {
const gltfLoader = new GLTFLoader(loadingManager);
const promises = [];
for (let i = 0; i < source.length; i++) {
promises.push(
new Promise((resolve) => {
const model = {}
gltfLoader.load(source[i].file, (gltf) => {
const children = [...gltf.scene.children];
console.log(children);
model.components = children;
resolve(model);
});
if (source[i].type === 'baseModel' || (source[i].type === 'variant' && source[i].viewPoints.file !== 'none')) {
gltfLoader.load(source[i].viewPoints.file, (gltf) => {
const viewPoints = [...gltf.scene.children];
model.viewPoints = viewPoints;
resolve(model);
});
} else if (source[i].type === 'baseModel' || (source[i].type === 'variant' && source[i].viewPoints.file === 'none')) {
model.viewPoints = 'none';
} else if (source[i].type === 'land') {
gltfLoader.load(source[i].buildingLine, (gltf) => {
const buildingLine = [...gltf.scene.children]
model.buildingLine = buildingLine
resolve(model)
})
}
})
);
}
return await Promise.all(promises);
}
const allModels = modelLoader(source, loadingManager)
allModels.then(result => {
for (const variant of result) {
if (variant.type === 'baseModel') {
const house = new THREE.Group()
for (const components of variant.components) {
house.add(components)
}
}
}
})