Three.js - TypeError: THREE.Scene 不是构造函数 [英] Three.js - TypeError: THREE.Scene is not a constructor
问题描述
我只是在尝试 Three.js,所以我可以将它实现到我的项目中,但是当我运行文档中的第一个示例时:
I am just trying out Three.js so I can implement it to a project of mine, however when I run the first example in the documentation:
<html>
<head>
<title>My first Three.js app</title>
<style>
body { margin: 0; }
canvas { width: 100%; height: 100% }
</style>
</head>
<body>
<script src="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, window.innerHeight );
document.body.appendChild( renderer.domElement );
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 );
camera.position.z = 5;
var render = function () {
requestAnimationFrame( render );
cube.rotation.x += 0.1;
cube.rotation.y += 0.1;
renderer.render(scene, camera);
};
render();
</script>
</body>
</html>
我收到此错误:
TypeError: THREE.Scene 不是构造函数
TypeError: THREE.Scene is not a constructor
我尝试添加scene.js,但它在scene.js 中引发了另一个错误,因此无法正常工作.我使用的是 r80 版本,我听说文档中有一些非工作示例,但我不确定这里有什么问题.
I tried adding scene.js, but it threw another error inside scene.js so it didn't work. I am using version r80 and I heard there was some non-working samples in the documentation, however I am not sure what's wrong here.
任何帮助将不胜感激.
推荐答案
如上面的评论中所述,问题在于页面中包含错误的 three.js
文件.您应该始终使用来自three.js 存储库的./build
文件夹中的文件,或由cdnjs 托管的版本:https://cdnjs.com/libraries/three.js/
As noted in the comments above, the problem is the wrong three.js
-file being included in the page. You should always use the file from the ./build
-folder of the three.js repository, or a version hosted by cdnjs: https://cdnjs.com/libraries/three.js/
这篇关于Three.js - TypeError: THREE.Scene 不是构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!