Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

86
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!