name:
js/camera-controls.js
-rw-r--r--
4236
1/**
2 * Modified from James Baicoianu's example at http://threejs.org/examples/js/controls/FlyControls.js
3 * @author James Baicoianu / http://www.baicoianu.com/
4 */
5
6THREE.FlyControls = function ( object, domElement ) {
7 this.object = object;
8
9 this.domElement = ( domElement !== undefined ) ? domElement : document;
10 if ( domElement ) this.domElement.setAttribute( 'tabindex', -1 );
11
12 this.movementSpeed = 8.0;
13 this.rollSpeed = 0.005;
14
15 this.dragToLook = false;
16 this.autoForward = false;
17
18 this.tmpQuaternion = new THREE.Quaternion();
19
20 this.moveState = { up: 0, down: 0, left: 0, right: 0, forward: 0, back: 0, pitchUp: 0, pitchDown: 0, yawLeft: 0, yawRight: 0, rollLeft: 0, rollRight: 0 };
21 this.moveVector = new THREE.Vector3( 0, 0, 0 );
22 this.rotationVector = new THREE.Vector3( 0, 0, 0 );
23
24 this.handleEvent = function ( event ) {
25 if ( typeof this[ event.type ] == 'function' ) {
26 this[ event.type ]( event );
27 }
28 };
29
30 this.keydown = function( event ) {
31
32 if ( event.altKey ) {
33 return;
34 }
35
36 //event.preventDefault();
37
38 switch ( event.keyCode ) {
39 case 87: /*W*/ this.moveState.forward = 1; break;
40 case 83: /*S*/ this.moveState.back = 1; break;
41
42 case 65: /*A*/ this.moveState.left = 1; break;
43 case 68: /*D*/ this.moveState.right = 1; break;
44
45 case 82: /*R*/ this.moveState.up = 1; break;
46 case 70: /*F*/ this.moveState.down = 1; break;
47
48 case 38: /*up*/ this.moveState.pitchUp = 1; break;
49 case 40: /*down*/ this.moveState.pitchDown = 1; break;
50
51 case 37: /*left*/ this.moveState.yawLeft = 1; break;
52 case 39: /*right*/ this.moveState.yawRight = 1; break;
53
54 case 84: this.reset = 1; break;
55 }
56
57 this.updateMovementVector();
58 this.updateRotationVector();
59 };
60
61 this.keyup = function( event ) {
62 switch( event.keyCode ) {
63 case 87: /*W*/ this.moveState.forward = 0; break;
64 case 83: /*S*/ this.moveState.back = 0; break;
65
66 case 65: /*A*/ this.moveState.left = 0; break;
67 case 68: /*D*/ this.moveState.right = 0; break;
68
69 case 82: /*R*/ this.moveState.up = 0; break;
70 case 70: /*F*/ this.moveState.down = 0; break;
71
72 case 38: /*up*/ this.moveState.pitchUp = 0; break;
73 case 40: /*down*/ this.moveState.pitchDown = 0; break;
74
75 case 37: /*left*/ this.moveState.yawLeft = 0; break;
76 case 39: /*right*/ this.moveState.yawRight = 0; break;
77
78 case 84: this.reset = 0; break;
79 }
80
81 this.updateMovementVector();
82 this.updateRotationVector();
83 };
84
85 this.update = function( delta ) {
86 var moveMult = delta * this.movementSpeed;
87 var rotMult = delta * this.rollSpeed;
88
89 this.object.translateX( this.moveVector.x * moveMult );
90 this.object.translateZ( this.moveVector.z * moveMult );
91
92 this.object.position.y += this.moveVector.y *moveMult;
93
94 this.object.rotation.x += this.rotationVector.x * rotMult;
95 this.object.rotation.y += this.rotationVector.y * rotMult;
96 this.object.rotation.z = 0;
97 };
98
99 this.updateMovementVector = function() {
100 var forward = ( this.moveState.forward || ( this.autoForward && !this.moveState.back ) ) ? 1 : 0;
101 this.moveVector.x = ( -this.moveState.left + this.moveState.right );
102 this.moveVector.y = ( -this.moveState.down + this.moveState.up );
103 this.moveVector.z = ( -forward + this.moveState.back );
104 };
105
106 this.updateRotationVector = function() {
107 this.rotationVector.x = ( -this.moveState.pitchDown + this.moveState.pitchUp );
108 this.rotationVector.y = ( -this.moveState.yawRight + this.moveState.yawLeft );
109 };
110
111 this.getContainerDimensions = function() {
112 if ( this.domElement != document ) {
113 return {
114 size : [ this.domElement.offsetWidth, this.domElement.offsetHeight ],
115 offset : [ this.domElement.offsetLeft, this.domElement.offsetTop ]
116 };
117 } else {
118 return {
119 size : [ window.innerWidth, window.innerHeight ],
120 offset : [ 0, 0 ]
121 };
122 }
123 };
124
125 function bind( scope, fn ) {
126 return function () {
127 fn.apply( scope, arguments );
128 };
129 };
130
131 this.domElement.addEventListener( 'contextmenu', function ( event ) { event.preventDefault(); }, false );
132 this.domElement.addEventListener( 'keydown', bind( this, this.keydown ), false );
133 this.domElement.addEventListener( 'keyup', bind( this, this.keyup ), false );
134
135 this.updateMovementVector();
136 this.updateRotationVector();
137};