Agent Zebra
Agent Zebra

Reputation: 4540

three.js: Error adding object to scene

Trying to add a simple object to a three.js scene. I'm getting the error Uncaught ReferenceError: Cube is not defined, but it seems to be defined to me. What am I doing wrong here?

Here's a plunker

Here's the relevant js:

// Cube
   Cube = function(){  
            var geometry = new THREE.BoxGeometry( 50, 50, 50 );

                for ( var i = 0; i < geometry.faces.length; i += 2 ) {

                    var hex = Math.random() * 0xffffff;
                    geometry.faces[ i ].color.setHex( hex );
                    geometry.faces[ i + 1 ].color.setHex( hex );

                }

          var material = new THREE.MeshBasicMaterial( { vertexColors: THREE.FaceColors, overdraw: 0.5 } );

    box = new THREE.Mesh( geometry, material );
            box.position.y = 25;

            }

        function createCube(){
          cube = new Cube();
    scene.add(cube);
          } 

Upvotes: 2

Views: 692

Answers (1)

micnil
micnil

Reputation: 4805

Two things are wrong.

  • You should be adding the cube mesh to the scene. So instead of box = new THREE.Mesh( geometry, material ); put return box = new THREE.Mesh( geometry, material );

  • call createCube() after you have defined the function

Upvotes: 1

Related Questions