Search code examples
javascriptimagethree.jstextures

Texture in Three.js


I'm trying to apply a texture to the birds example in three.js.

  // instantiate a loader
  var loader = new THREE.TextureLoader();

  // load a resource
  loader.load('imgs/birdtexture.jpg',	function (texture) {
     var material = new THREE.MeshBasicMaterial( {
     map: texture,
     side: THREE.DoubleSide
  });

  var bird = new THREE.Mesh( new Bird(), material);
);

For some reason this doesn't work and I don't get any errors .It seems I'm missing something. What am I doing wrong?

EDIT: I got it working by using UV as describe here. Here's the code:

  var texture = new THREE.TextureLoader().load("imgs/birdtexture.jpg");
  texture.wrapS = THREE.RepeatWrapping;
  texture.wrapT = THREE.RepeatWrapping;
  texture.repeat.set( 0.05, 0.05);

  var geometry = new Bird();

  function assignUVs(geometry) {

  geometry.faceVertexUvs[0] = [];

  geometry.faces.forEach(function(face) {

    var components = ['x', 'y', 'z'].sort(function(a, b) {
        return Math.abs(face.normal[a]) > Math.abs(face.normal[b]);
    });

    var v1 = geometry.vertices[face.a];
    var v2 = geometry.vertices[face.b];
    var v3 = geometry.vertices[face.c];

    geometry.faceVertexUvs[0].push([
        new THREE.Vector2(v1[components[0]], v1[components[1]]),
        new THREE.Vector2(v2[components[0]], v2[components[1]]),
        new THREE.Vector2(v3[components[0]], v3[components[1]])
    ]);

    });

    geometry.uvsNeedUpdate = true;
    }

    assignUVs(geometry);

    var bird = new THREE.Mesh( geometry, new THREE.MeshBasicMaterial(     {map:texture, side: THREE.DoubleSide }));

Solution

  • Try this (untested):

      // instantiate a loader
      var loader = new THREE.TextureLoader();
      // Create material
      var material = new THREE.MeshBasicMaterial({side: THREE.DoubleSide});
    
      // load a resource
      loader.load('imgs/birdtexture.jpg',   function (texture) {
         marterial.map = texture;
      });
    
      var bird = new THREE.Mesh( new Bird(), material);
    );