Here's my code, I'm trying to make the loadCone() synchronous with await/async but it isn't working.
import * as THREE from "three";
import { GLTFLoader } from "three/examples/jsm/loaders/GLTFLoader";
import conePath from "../../static/cone.glb";
import coneBaked from "../../static/coneBake.png";
import "regenerator-runtime/runtime";
export default class Cone {
constructor() {
this.cone;
const textureLoader = new THREE.TextureLoader();
const bakedTexture = textureLoader.load(coneBaked);
const bakedMaterial = new THREE.MeshBasicMaterial({
map: bakedTexture,
});
this.loadCone();
}
async loadCone() {
// GLTF loader
const gltfLoader = new GLTFLoader();
const cone = await gltfLoader.load(
conePath,
(gltf) => {
console.log("success");
console.log(gltf.scene);
this.cone = gltf.scene;
},
(progress) => {
// console.log("progress");
// console.log(progress);
},
(error) => {
console.log("error");
console.log(error);
}
);
}
getCone() {
return this.cone;
}
}
In another file I have this:
const cone = new Cone();
this.scene.add(cone.getCone());
However, because getCone() is synchronous, it executes before my loadCone() is done loading the cone, so I get undefined back instead of the cone.
How do I make my function asynch loadCone() be synchronous? I tried *.loadAsync()` but it didn't change anything.
Thanks in advance. I am getting no errors other than the fact it says "undefined is not instance of THREE.Object.3d" which I expected it to say.