【发布时间】:2020-05-22 11:20:41
【问题描述】:
我正在使用 GLTF 加载器在我的场景中加载自定义模型。
我有一个类Spaceship.js 负责加载模型。
// 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);
}
);
});
}
}
还有一个类ThreeShell.js充当整个三个场景的外壳
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);
...
}
}
不知何故,在记录this.spaceship 时,我得到了一个具有模型属性的对象。
但是当登录this.spaceship.model 时,我得到undefined。
我想这可能与承诺有关,我目前对此感到不舒服。这就是我向您寻求帮助的原因。
【问题讨论】:
标签: javascript promise three.js this gltf