軌道控制
軌道攝像機是允許使用者圍繞中心點旋轉但同時保持特定軸鎖定的軌道攝像機。這非常受歡迎,因為它可以防止場景偏離傾斜。此版本鎖定 Y(垂直)軸,並允許使用者使用滑鼠左鍵,中鍵和右鍵(或特定觸控事件)進行軌道,縮放和平移。
index.html
<html>
<head>
<title>Three.js Orbit Controller Example</title>
<script src="/javascripts/three.js"></script>
<script src="/javascripts/OrbitControls.js"></script>
</head>
<body>
<script src="javascripts/scene.js"></script>
</body>
</html>
scene.js
var scene, renderer, camera;
var cube;
var controls;
init();
animate();
function init()
{
renderer = new THREE.WebGLRenderer( {antialias:true} );
var width = window.innerWidth;
var height = window.innerHeight;
renderer.setSize (width, height);
document.body.appendChild (renderer.domElement);
scene = new THREE.Scene();
var cubeGeometry = new THREE.BoxGeometry (10,10,10);
var cubeMaterial = new THREE.MeshBasicMaterial ({color: 0x1ec876});
cube = new THREE.Mesh (cubeGeometry, cubeMaterial);
cube.position.set (0, 0, 0);
scene.add (cube);
camera = new THREE.PerspectiveCamera (45, width/height, 1, 10000);
camera.position.y = 160;
camera.position.z = 400;
camera.lookAt (new THREE.Vector3(0,0,0));
controls = new THREE.OrbitControls (camera, renderer.domElement);
var gridXZ = new THREE.GridHelper(100, 10);
gridXZ.setColors( new THREE.Color(0xff0000), new THREE.Color(0xffffff) );
scene.add(gridXZ);
}
function animate()
{
controls.update();
requestAnimationFrame ( animate );
renderer.render (scene, camera);
}
OrbitControls 指令碼有幾個可以修改的設定。程式碼有詳細記錄,因此請檢視 OrbitControls.js 以檢視這些程式碼。作為示例,這裡是一個程式碼片段,顯示了一些在新的 OrbitControls 物件上被修改的程式碼片段。
controls = new THREE.OrbitControls( camera, renderer.domElement );
controls.enableDamping = true;
controls.dampingFactor = 0.25;
controls.enableZoom = true;
controls.autoRotate = true;