Search code examples
javascriptimageasynchronousthree.jstextures

three.js: local texture loading works, but not remotely


I have modified an official example for the Loader object to demonstrate my issue. I want to create a mesh, with a texture map that gets loaded preferably before creating the geometry. Currently I have the issue, that local files load just fine, but not remote ones. See the behaviour in action:

Using the loader to load a locally saved .jpg the texture shows up fine on the final mesh, as can be seen in the working demo:

    <!DOCTYPE html>
<html lang="en">
<head>
  <title>three.js webgl - loaders - OBJ loader</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
</head>
<body>
  <div id="info">
    <a href="http://threejs.org" target="_blank">three.js</a> - image loader test
  </div>

  <script src="libs/three.min.js"></script>

  <script>

  var container;

  var camera, scene, renderer;

  var mouseX = 0, mouseY = 0;

  var windowHalfX = window.innerWidth / 2;
  var windowHalfY = window.innerHeight / 2;


  init();
  animate();


  function init() {

    container = document.createElement( 'div' );
    document.body.appendChild( container );

    camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 2000 );
    camera.position.z = 100;

    // scene

    scene = new THREE.Scene();

    var ambient = new THREE.AmbientLight( 0x101030 );
    scene.add( ambient );

    var directionalLight = new THREE.DirectionalLight( 0xffeedd );
    directionalLight.position.set( 0, 0, 1 );
    scene.add( directionalLight );

    // texture
    var manager = new THREE.LoadingManager();
    var texture = new THREE.Texture();

    var loader = new THREE.ImageLoader( manager );
    loader.load( 'avatar.jpg', function ( image ) {

    // uncomment next line, and comment above, to see the problem!

    //loader.load( 'http://nylkiway.net/avatar.jpg', function ( image ) {

      texture.image = image;
      console.log("debug. image size: " + image.width + " X " + image.height );
      texture.needsUpdate = true;

    } );

    // model
      var object = new THREE.Mesh( new THREE.BoxGeometry(100 , 100, 100), new THREE.MeshBasicMaterial( {map: texture}));
      object.position.y = - 80;
      scene.add( object );

    renderer = new THREE.WebGLRenderer();
    renderer.setPixelRatio( window.devicePixelRatio );
    renderer.setSize( window.innerWidth, window.innerHeight );
    container.appendChild( renderer.domElement );

    document.addEventListener( 'mousemove', onDocumentMouseMove, false );

  }

  function onDocumentMouseMove( event ) {
    mouseX = ( event.clientX - windowHalfX ) / 2;
    mouseY = ( event.clientY - windowHalfY ) / 2;
  }


  function animate() {
    requestAnimationFrame( animate );
    render();
  }

  function render() {
    camera.position.x += ( mouseX - camera.position.x ) * .05;
    camera.position.y += ( - mouseY - camera.position.y ) * .05;
    camera.lookAt( scene.position );
    renderer.render( scene, camera );
  }

</script>
</body>
</html>

However, when loading the same image from a remote server, the texture gets loaded, or atleast I think it does, because the images width etc. is accessible in the onLoad() callback. But I get errors and the mesh doesnt render. Uncomment the line to:

loader.load( 'http://nylkiway.net/avatar.jpg', function ( image ) {

I get numerous errors:

"THREE.WebGLRenderer: Texture is not power of two. Texture.minFilter is set to THREE.LinearFilter or THREE.NearestFilter. ( undefined )"

Although the image is exactly the same, and my little debug output returns the image dimensions:

console.log("debug. image size: " + image.width + " X " + image.height );

"debug. image size: 460 X 460"

I do not think this is a CORS issue, as the callback for finished image loading gets called. The images properties are readable as well. I feel, like this must be a frequent question, I searched but didn't really get a fitting answer. I don't think it has anything to do with the images dimensions not being a power of 2, as the locally loaded image shows just fine as a texture.

Any help would be appreciated!


Solution

  • It's a CORS issue, your demo has warnings:

    Uncaught SecurityError: Failed to execute 'texImage2D' on 'WebGLRenderingContext': The cross-origin image at http://i.imgur.com/Gps8L9R.jpg may not be loaded.
    

    You need to host your own image mirror and direct there, what's known as a 'reverse proxy':

    https://stackoverflow.com/a/3076439/3117360