我从Three.js开始。现在,我发现了一个问题,需要帮助。看起来很简单,但我找不到很好的答案。问题是:即使声明使用OrbitControls.js(CODE1),即使它显示在DOM的三棵树中(图1)。当我尝试使用构造函数(代码2)时,收到错误消息:“ TypeError:THREE.OrbitControls不是构造函数”。
CODE1 :***index.html***My first three.js app
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();
您需要包括库并将cena.js
脚本放在最后。脚本标签是同步加载的。