簡單的鍋爐旋轉立方體和帶阻尼的軌道控制
這是基本的 HTML 檔案,可以在啟動專案時用作樣板檔案。該樣板使用帶阻尼的軌道控制(可以在具有減速效果的物體周圍移動的攝像機)並建立旋轉立方體。
<!DOCTYPE html>
<html>
<head>
<title>Three.js Boilerplate</title>
<!--This is important to get a correct canvas size on mobile-->
<meta name='viewport' content='width=device-width, user-scalable=no'/>
<style>
body{
margin:0;
overflow:hidden;
}
/*
Next 2 paragraphs are a good practice.
In IE/Edge you have to provide the cursor images.
*/
canvas{
cursor:grab;
cursor:-webkit-grab;
cursor:-moz-grab;
}
canvas:active{
cursor:grabbing;
cursor:-webkit-grabbing;
cursor:-moz-grabbing;
}
</style>
</head>
<body>
<script src='three.js/build/three.js'></script>
<script src='three.js/examples/js/controls/OrbitControls.js'></script>
<script>
var scene, renderer, camera, controls, cube;
init();
function init () {
renderer = new THREE.WebGLRenderer();
//this is to get the correct pixel detail on portable devices
renderer.setPixelRatio( window.devicePixelRatio );
//and this sets the canvas' size.
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera(
70, //FOV
window.innerWidth / window.innerHeight, //aspect
1, //near clipping plane
100 //far clipping plane
);
camera.position.set( 1, 3, 5 );
controls = new THREE.OrbitControls( camera, renderer.domElement );
controls.rotateSpeed = .07;
controls.enableDamping = true;
controls.dampingFactor = .05;
window.addEventListener( 'resize', function () {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
}, false );
cube = new THREE.Mesh(
new THREE.BoxGeometry( 1, 1, 1 ),
new THREE.MeshBasicMaterial()
);
scene.add( cube );
animate();
}
function animate () {
requestAnimationFrame( animate );
controls.update();
renderer.render( scene, camera );
cube.rotation.x += 0.01;
}
</script>
</body>
</html>