Currently, I am utilizing the GLTF loader for the purpose of importing a custom model into my scene.
Within my codebase, there exists a class called Spaceship.js
that manages the loading of the model.
// Spaceship.js
import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader.js';
export default class Spaceship {
constructor() {
this.GLTFLoader = new GLTFLoader();
this.loadModel(this.GLTFLoader, './spaceship_model.gltf').then(result => {
this.model = result.scene;
});
}
loadModel(loader, url) {
return new Promise((resolve, reject) => {
loader.load(
url,
gltf => {
resolve(gltf);
},
undefined,
error => {
console.error('An error happened.', error);
reject(error);
}
);
});
}
}
Additionally, there is a class called ThreeShell.js
which serves as a container for the entire three.js scene.
import * as THREE from 'three';
import Spaceship from './Spaceship.js';
export default class ThreeShell {
constructor(container = document.body) {
this.container = container;
this.setup();
}
setup() {
...
this.spaceship = new Spaceship();
console.log(this.spaceship);
console.log(this.spaceship.model);
...
}
}
Upon inspection, I discovered that when logging this.spaceship
, an object with the model property is displayed. However, logging this.spaceship.model
yields undefined
.
https://i.sstatic.net/oHhH2.png
My suspicion is that this issue might be related to the handling of promises, an area in which I currently lack expertise. Hence, I am seeking your assistance in resolving this matter.