TypeError:THREE.OrbitControls不是构造函数

时间:2017-01-26 03:33:36

标签: javascript three.js

我从three.js开始。现在我发现了一个问题,需要帮助。看起来很简单,但我找不到一个好的答案。问题是:甚至声明使用OrbitControls.js(CODE1),即使它在DOM的三棵树中显示(图1)。当我尝试使用构造函数(CODE 2)时,我收到错误:“TypeError:THREE.OrbitControls不是构造函数”图2。

CODE1 :***index.html***
<html>
    <head>
        <title>My first three.js app</title>
        <style>
            body { margin: 0; }
            canvas { width: 100%; height: 100% }
        </style>
    </head>
    <body>

       <script src="js/libs/three.min.js"></script>
       <script src="js/cena.js"></script>
       <script src="js/libs/AxisHelper.js"></script>
       <script src="js/libs/GridHelper.js"></script>
       <script src="js/libs/OrbitControls.js"></script> 

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

THE THREE tree at DOM

CODE2***:cena.js***

var cena = new THREE.Scene();

var camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 1000 );

var renderizador = new THREE.WebGLRenderer({antialias:true});

renderizador.setSize( window.innerWidth, window.innerHeight );

document.body.appendChild( renderizador.domElement );
//----------------------------


var geometry = new THREE.BoxGeometry( 3, 1, 2 );

var material = new THREE.MeshBasicMaterial( { color: 0x00ff00 } );

var cube = new THREE.Mesh( geometry, material );

var axisHelper = new THREE.AxisHelper( 5 );
cena.add( axisHelper )
var tamanho = 10;
var elementos = 10;
var grid = new THREE.GridHelper(tamanho,elementos);
cena.add(grid);

cena.add( cube );

camera.position.z = 10;
camera.position.y = 10;
camera.position.x = 5;

var lookat_vector = new THREE.Vector3(0,0,0);
camera.lookAt(lookat_vector);
//++++++++++++++++++++++++++++++++++++++++++++++++++++++

var controle = new THREE.OrbitControls(camera, renderizador.domElement);

var render = function () {
        requestAnimationFrame( render );
        controle.update();
        cube.rotation.x += 0.01;
        cube.rotation.y +=0.01;
        cube.rotation.z +=0.03;

        renderizador.render(cena, camera);
        //controle.update();
    };

render();

Error

2 个答案:

答案 0 :(得分:3)

您需要包含lib并将cena.js脚本放在最后。 脚本标签是同步加载的。

   <script src="js/libs/three.min.js"></script>
   <script src="js/libs/AxisHelper.js"></script>
   <script src="js/libs/GridHelper.js"></script>
   <script src="js/libs/OrbitControls.js"></script>
   <script src="js/cena.js"></script>

答案 1 :(得分:2)

在创建THREE.OrbitalControls之前,您需要在脚本标记中包含OBJLoader.js。