Three.js is not rendering my textures from my planet constructor function

Viewed 71

I have a function that takes orbital parameters and texture images from a dataset and creates a 3d object (a planet). However for some reason textures aren't being rendered. Using BasicMaterial it works normally, but not with StandardMaterial or PhongMaterial. (There is a point light in the scene), I already tested the textures outside the loop and the function, they work nice. I don´t know what else to do. I let here the code.

for(var i=0;i < results.data.length; i++){
              
 const EC = Number(results.data[i]["Eccentricity"]); //Eccentricity
 const IN = Number(results.data[i]["Inclination [Rad]"]) ; //Inclination
 const OM = Number(results.data[i]["Orbit Rotation_Y [Rad]"]) ; //Longitude of ascending node
 const W = Number(results.data[i]["Orbit Rotation_X [Rad]"]); //Argument of periapsis
 const A = Number(results.data[i]["Orbit semi-major axis [UA]"]); //Semi-major axis
 const EcRadius = Number(results.data[i]["Relative Equatorial Radius"]); //Radius
 const NAM = results.data[i]["Name"]; //Name
 const textureUrl = results.data[i]["TextureFileUrl"]; //Texture image of the planet

 const loader = new THREE.TextureLoader();
 loader.load(textureUrl, function ( texture ) {
    // Create the material when the texture is loaded
    var tex = texture.clone();
    const PlanetMaterial = new THREE.MeshBasicMaterial( {
    map: tex
    } );
    scene.add(CreatePlanet( 0,0,0, EC,IN,OM,W,A,EcRadius, NAM, 0x4E4E4E, 0.5, PlanetMaterial ));
    },
  undefined,
  function ( err ) {
     console.error( 'An error happened.');
  }
  );
} 

CreatePlanet function just creates a mesh with the material given. Like:

const geometryPlanet = new THREE.SphereBufferGeometry(EcRadius, 300, 300);
const Planet = new THREE.Mesh(geometryPlanet, PlanetMaterial);
return Planet;

Thanks beforehand for the help.

1 Answers

var tex = texture.clone();

When doing this, can you please add the following line?

tex.needsUpdate = true;

In this context, it is required otherwise the contents of the texture will never be uploaded to the GPU.

Related