轨道控制
轨道摄像机是允许用户围绕中心点旋转但同时保持特定轴锁定的轨道摄像机。这非常受欢迎,因为它可以防止场景偏离倾斜。此版本锁定 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;