Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

88
Vistas
load texture in a class before proceeding

I have presented a simple example to illustrate what my problem is. in functions it's easy to use await async to make sure textures are loaded before proceeding with the program. if I want to work with classes because of the cleanliness of the program, I have no idea how I can do this so that the constructor waits until the texture is loaded before the subsequent function is called.

//in my three.js init function
var sphereObject = new Sphere(100, texture);
var sphere = this.sphereObject.sphere;
scene.add(sphere); 
//------------------------


class Sphere{
  constructor(radius, preloadedtex){
 
    //this.material = this.doSomething(preloadedtex); this work fine

    const loader = new THREE.TextureLoader();
    loader.load("grass.png", function(texture){
      this.material = this.doSomething(texture);
    });    
    const geometry = new THREE.SphereGeometry( radius, 128, 64 );
    this.sphere = new THREE.Mesh( geometry, this.material); 

  }

  doSomething(texture){
    //further operations with the texture before the function returns a material

    return material;
  }

}
about 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

TextureLoader has an onLoad callback as the second argument of .load(). This is taken directly from the docs:

const loader = new THREE.TextureLoader();

// load a resource
loader.load(
    // resource URL
   'textures/land_ocean_ice_cloud_2048.jpg',

    // onLoad callback
    function ( texture ) {
        // in this example we create the material when the texture is loaded
        const material = new THREE.MeshBasicMaterial( {
            map: texture
         } );
    },

    // onProgress callback currently not supported
    undefined,

    // onError callback
    function ( err ) {
        console.error( 'An error happened.' );
    }
);
about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda