Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

564
Visualizações
Three,js get mesh outside the loader

I am working on a project using Three.js. And, I am trying get mesh(gltf.scene) in the GLTF load from outside, but it says it is undefined if I console.log outside of the loader. I spent hours to figure this out but haven't able to solve this problem. I am guessing this is happening because it is asynchronous, but I am not so sure. This is my first attempt to use javascript class format, so I am little bit confused. Usually it worked if I declare variable on the higher scope and assign the mesh to it but does not work this time.

class mainCharacter {
  constructor(game) {
...
...
...
this.character;
this.loader = new GLTFLoader();
this.loader.load("./c.glb",(gltf)=>{
this.character= gltf.scene;
});
console.log(this.character);


}

I also tried to do async function but did not work.

class mainCharacter {
  constructor(game) {
...
...
...
this.character;
this.loadMain();
console.log(this.character);
})

async loadMain(){
    const loader = new GLTFLoader();
    this.character = await loader.loadAsync("./c.glb");
    this.character.scene.scale.set(13, 13, 13);
    this.scene.add(this.character.scene);
}

Seems like it works if I use await in front of the loadMain() and console.log(this.character), but I cannot use await in the constructor function, since it is not async function. If anyone have any idea, please help me out :'(

Thank you!

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

According to ThreeJS documentation (https://threejs.org/docs/#api/en/loaders/Loader.loadAsync), the method loadAsync does not return the loaded mesh, instead, it return a Promise event.

.loadAsync ( url : String, onProgress : Function ) : Promise

Therefore, you would need to declare a onLoad callback function like below:

this.character = await loader.loadAsync("./c.glb").then(function(value) {
  console.log(value); // "Success"
});
about 4 years ago · Juan Pablo Isaza Relatório

0

Define another static method (like load shown below) in your class and pass this as a parameter

class mainCharacter {
  constructor(game) {
    ...
    this.character;
    this.loader;
    mainCharacter.load(this);
    ...
  }
  static load(self) {
    self.loader = new GLTFLoader();
    self.loader.load(
      "./c.glb",
      (gltf) => self.character = gltf.scene
    );
  }
}

in arrow functions, this points to the owner of that function, or the object in which that particular arrow function is defined. in your case, this inside the arrow function does not point to the mainCharacter object because its actually defined in and passed on to the loader.load() function. See https://www.w3schools.com/js/js_arrow_function.asp for more details.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda