I am facing a challenge with loading textures in a class. I want the class to be called only after all textures have been loaded successfully. While I have managed to load the textures, I am unsure about where and how to call the class at the right time for it to be returned to the main class. To demonstrate my issue clearly, I have condensed the code to focus on the essential parts.
class Environment {
constructor(params) {
this._Init(params);
}
static init(params) {
const getTextures = () => new Promise((resolve, reject)=>{
const manager = new THREE.LoadingManager(()=>resolve(textures));
const loader = new THREE.TextureLoader(manager);
const textures = [
'./resources/simplex-noise.png',
//next texture,
//next texture,
//next texture,
].map(filename=>loader.load(filename));
});
getTextures().then(tex => {
return new Environment({params, tex}); //I'm encountering an issue here.
//I need "new Environment({params, tex});" as return for "static init"
});
//return new Environment(params); //this is working but here i have no preloaded tex
}
_Init(params) {
this.params_ = params;
//....
}
}
class Main {
constructor(){
this._Initialize();
}
_Initialize() {
//...
this.OnAppStarted_();
//...
}
OnAppStarted_() {
this.LoadControllers_();
}
LoadControllers_() {
this.obj = Environment.init({
//params
});
//...
}
//...
}