Eccsx
June 3, 2020, 4:28pm
#1
If have a problem when I use a loader for one of my function.
Here’s the code I writed:
export function loadSTL(filepath, colorRGB) {
let inst;
// Load STL file
const loader = new STLLoader();
loader.load(
filepath,
(geometry) => { inst = STLToInstance(geometry, colorRGB); },
);
return inst;
}
The purpose of this is to return the var inst when the load is done.
Unfortunately, the function return null so I thought that the function return before the end of the load.
Does someone have an idea of what can I do for this?
What about this
export function loadSTL(filepath, colorRGB) {
let inst
// Load STL file
const loader = new STLLoader()
loader.load(
filepath,
(geometry) => {
inst = STLToInstance(geometry, colorRGB)
return inst;
},
)
}
Eccsx
June 3, 2020, 5:20pm
#3
The function still return undefined . I don’t find a way to “wait” until the end of the load.
Couldn’t you use the new Loader.loadAsync() method? Checkout how this method is used in the following example:
https://threejs.org/examples/webgl_tonemapping
You should be able to use it with STLLoader
, too.
3 Likes
Markus
June 3, 2020, 10:06pm
#5
Sounds like a similar problem that I had which was solved here:
Hi!
I started a new project to create a boilerplate for Webpack + Three.js to make it easier for people (including me) to get started with Three.js / WebGL.
This project is not done yet and is still a WIP.
Right now I am stuck on why the heck my 3D-model is not shown.
It is loaded with STLLoader from the examples/jsm/loaders and I’ve followed the example provided in the root dir.
It seems like the geometry returned from the loader has data so I’m curious to know what I might be missing here…
Check the comment that was marked as solution
The only way to do exactly what you’d like to do is using async/await
:
async function loadSomething() {
return new Promise((resolve) => {
const loader = new STLLoader();
loader.load(
filepath,
(geometry) => resolve(STLToInstance(geometry, colorRGB)),
() => resolve()
});
});
};
// Async / await require outer context to be an `async` function
(async function main() {
const model = await loadSomething();
console.log(model) // <- valid STL model instead of null / undefined
scene.add(model);
})();
Non-async/await
alternative that does exactly the same would be:
export function loadSTL(filepath, colorRGB) {
return new Promise((resolve) => {
const loader = new STLLoader();
loader.load(
filepath,
(geometry) => resolve(STLToInstance(geometry, colorRGB)),
() => resolve()
});
});
}
loadSTL.then((model) => {
// Continue with model loaded
});
Loader.loadAsync()
@Mugen87 mentioned above does the Promise approach.
Edit: Small fiddle with sample async/await https://jsfiddle.net/bur54veo/
2 Likes
I spent some time to find how to use this, maybe it will be useful for someone:
var gltfLoader = new THREE.GLTFLoader();
var result = await gltfLoader.loadAsync("car/scene.gltf");
...
The THREE.Loader class is never used directly, it’s a base class for other loaders. Instead:
var gltfLoader = new THREE.GLTFLoader();
var result = await loader.loadAsync("car/scene.gltf");
...