#How to make the 3d-Object accessible outside the OBJLoader - function?

3 messages · Page 1 of 1 (latest)

slender plover
#

var sculpt;
const objLoader = new OBJLoader();
objLoader.load('./objects/sculpt.obj', function(l_object)
{
sculpt = l_object;
//scene.add(sculpt); //working
});
scene.add(sculpt); //not working (why?)

upper leaf
#

by not writing it like that, threejs is inherently async, you shouldn't try to confront it with callbacks. this pattern is even called "callback hell" 😬

javascript is equipped to handle async tasks with async/await and promises:

#
async function app() {
  const objLoader = new OBJLoader()
  const textureLoader = new TextureLoader()
  ...
  const [obj, texture] = await Promise.all([
    new Promise((res, rej) => objLoader.load("model.obj", res, undefined, rej),
    new Promise((res, rej) => textureLoader.load("image.png", res, undefined, rej),
    ...
  ])
  // you can acces obj & texture here