tetorea
tetorea

Reputation: 141

textured sprite is all black in three.js

I'm starting on three.js and I'm already stuck on a trivial case : display an image on a sprite. I can only get a black plane following the lines written in the documentation:

<!DOCTYPE html>
<html>
    <head>
        <meta charset=utf-8>
        <title>TEST Sprite</title>
        <style>
            body { margin: 0; background-color:#000; }
            canvas { width: 100%; height: 100%; }
            #glRender {
                position:absolute;
                top: 50px;
                left: 200px;
                min-width: 200px;
                width: 50%;
                min-height: 200px;
                z-index: 1;
                height: 50%;
                border: 10px solid #38272C;
            }           

        </style>
    </head>
    <body>
        <div id="glRender"></div>
        <script src="http://threejs.org/build/three.js"></script>
        <script>
            var scene = new THREE.Scene();
            var camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 1000 );
            var renderer = new THREE.WebGLRenderer();
            renderer.setSize( window.innerWidth / 2, window.innerHeight / 2 );

            container = document.getElementById( 'glRender' );
            document.body.appendChild( container );
            container.appendChild( renderer.domElement );

            // A simple cube
            var geometry = new THREE.BoxGeometry( 1, 1, 1 );
            var material = new THREE.MeshBasicMaterial( { color: 0x00ff00 } );
            var cube = new THREE.Mesh( geometry, material );
            scene.add( cube );

            // completely black...
            var photoTexLoad = new THREE.TextureLoader().load( 'http://s2.favim.com/610/150817/plage-summer-Favim.com-3137863.jpg' );
            var matPhoto = new THREE.SpriteMaterial( { map: photoTexLoad, color: 0xffffff } );
            var sprite = new THREE.Sprite( matPhoto );
            sprite.scale.set( 2, 2, 2 );
            sprite.position.set( 1, 0, 0.5 );
            scene.add( sprite );

            camera.position.z = 5;

            function render() {
                requestAnimationFrame( render );
                cube.rotation.x += 0.01;
                cube.rotation.y += 0.1;
                renderer.render( scene, camera );
            }

            render();

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

I get the spinning green cube intersecting with a black plane. What did I miss to have the image displayed on the plane?

Upvotes: 0

Views: 1467

Answers (2)

Pineda
Pineda

Reputation: 7593

.TextureLoader().load() is asynchronous. Trying to apply it immediately after invoking it leads to a black plane in your code most likely because the texture has not finished loading.

You can solve this by:

Passing a callback to .load as a 2nd parameter. (It is called once the texture has finished loading):

var matPhoto,
    sprite,
    texloader = new THREE.TextureLoader();

texloader.load('http://s2.favim.com/610/150817/plage-summer-Favim.com-3137863.jpg', 
  function(tex) {
  matPhoto = new THREE.SpriteMaterial( { map: tex, color: 0xffffff } );
  sprite = new THREE.Sprite( matPhoto );
  //...etc...
  }
);

OR

set an event listener:

var texloader = new THREE.TextureLoader();

texloader.load('http://s2.favim.com/610/150817/plage-summer-Favim.com-3137863.jpg');

textureLoader.addEventListener('load', function(event){

  // event.content holds the texture---------------v
  matPhoto = new THREE.SpriteMaterial( { map: event.content, color: 0xffffff } ); = event.content;

});

Upvotes: 2

tetorea
tetorea

Reputation: 141

I just found out I got the "CORS" issue highlighted in question three.js: texture goes all black

The code is working fine but I cannot try to load an image from external website or directly on my computer, only files on my localhost.

For testing, I now use a specific instance of google chrome using a desktop shortcut launching the browser as : "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --args --user-data-dir="C:/Chrome dev session" --disable-web-security

Upvotes: 0

Related Questions