<html> <head> <title>My first three.js app</title> <style> body { margin: 0; } canvas { width: 100%; height: 100% } </style> </head> <body> <script src="../static/three.js-master/build/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 animate = function () { requestAnimationFrame( animate ); // 循環渲染 cube.rotation.x += 0.01; cube.rotation.y += 0.01; renderer.render( scene, camera ); // 將場景和攝像機經過渲染器渲染出來 }; animate(); </script> </body> </html>
附帶three.js代碼,點擊下載html