three.js:将对象添加到场景时出错

时间:2015-08-13 21:56:13

标签: javascript three.js

尝试将一个简单的对象添加到three.js场景中。我收到了错误Uncaught ReferenceError: Cube is not defined,但它似乎已经定义给我了。我在这里做错了什么?

Here's a plunker

以下是相关的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);
          } 

1 个答案:

答案 0 :(得分:1)

有两件事是错的。

  • 您应该将多维数据集网格添加到场景中。因此,而不是box = new THREE.Mesh( geometry, material );return box = new THREE.Mesh( geometry, material );

  • 在定义函数

  • 后调用createCube()