我正在尝试渲染基本的Three.js场景。
这是代码:
<!DOCTYPE html>
<html>
<head>
<title>Example 01.02 - First Scene</title>
<script type="text/javascript" src="../libs/three.js"></script>
<style>
body {
/* set margin to 0 and overflow to hidden, to go fullscreen */
margin: 0;
overflow: hidden;
}
</style>
</head>
<body>
<!-- Div which will hold the Output -->
<div id="WebGL-output">
</div>
<!-- Javascript code that runs our Three.js examples -->
<script type="text/javascript">
// once everything is loaded, we run our Three.js stuff.
function init() {
// create a scene, that will hold all our elements such as objects, cameras and lights.
var scene = new THREE.Scene();
// create a camera, which defines where we're looking at.
var camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 1000);
// create a render and set the size
var renderer = new THREE.WebGLRenderer();
renderer.setClearColorHex();
renderer.setClearColor(new THREE.Color(0xEEEEEE));
renderer.setSize(window.innerWidth, window.innerHeight);
// show axes in the screen
var axes = new THREE.AxisHelper(20);
scene.add(axes);
// create the ground plane
var planeGeometry = new THREE.PlaneGeometry(60, 20);
var planeMaterial = new THREE.MeshBasicMaterial({color: 0xcccccc});
var plane = new THREE.Mesh(planeGeometry, planeMaterial);
// rotate and position the plane
plane.rotation.x = -0.5 * Math.PI;
plane.position.x = 15;
plane.position.y = 0;
plane.position.z = 0;
// add the plane to the scene
scene.add(plane);
// create a cube
var cubeGeometry = new THREE.BoxGeometry(4, 4, 4);
var cubeMaterial = new THREE.MeshBasicMaterial({color: 0xff0000, wireframe: true});
var cube = new THREE.Mesh(cubeGeometry, cubeMaterial);
// position the cube
cube.position.x = -4;
cube.position.y = 3;
cube.position.z = 0;
// add the cube to the scene
scene.add(cube);
// create a sphere
var sphereGeometry = new THREE.SphereGeometry(4, 20, 20);
var sphereMaterial = new THREE.MeshBasicMaterial({color: 0x7777ff, wireframe: true});
var sphere = new THREE.Mesh(sphereGeometry, sphereMaterial);
// position the sphere
sphere.position.x = 20;
sphere.position.y = 4;
sphere.position.z = 2;
// add the sphere to the scene
scene.add(sphere);
// position and point the camera to the center of the scene
camera.position.x = -30;
camera.position.y = 40;
camera.position.z = 30;
camera.lookAt(scene.position);
// add the output of the renderer to the html element
document.getElementById("WebGL-output").appendChild(renderer.domElement);
// render the scene
renderer.render(scene, camera);
}
window.onload = init;
</script>
</body>
</html>
这是一个HTML文件,我将其拖放到浏览器中。在与该文件相同的目录中,我有一个名为libs
的文件夹,在内部有three.js
和three.mini.js
库。
我从其网站下载了three.js zip文件,里面有很多文件。但是由于这些库是代码所需的库,因此我只放置了这两个javascript文件,而忽略了zip文件中包含的所有其他文件。
[当我将html
文件拖放到浏览器上时,我唯一看到的是空白的空白图像。
编辑:JS控制台错误:
*Failed to load resource: net::ERR_FILE_NOT_FOUND
*Uncaught ReferenceError: THREE is not defined at init (test.html:29)
您是否尝试按照描述的here在本地运行事物?
我个人使用的是python版本:
如果您安装了Python,则足以从命令行(从您的工作目录)运行此命令:// Python 2.xpython -m SimpleHTTPServer// Python 3.xpython -m http.server这将为当前目录中位于8000端口下本地主机的文件提供服务,即在地址栏中键入:http://localhost:8000/
然后,您可以访问文件浏览到本地主机(由于网络浏览器上的资源访问策略发生变化,因此现在必须这样做)。>>
编辑
[在测试您的代码之后,实际上我只是删除了renderer.setClearColorHex();
第36行,并将其放置在包含three.min.js的文件夹中,当我从文件夹中打开它时,它工作正常。