-
Notifications
You must be signed in to change notification settings - Fork 5
/
FlyControls.js
293 lines (174 loc) · 6.98 KB
/
FlyControls.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/**
* @author James Baicoianu / http://www.baicoianu.com/
*/
THREE.FlyControls = function ( object, domElement ) {
this.object = object;
this.domElement = ( domElement !== undefined ) ? domElement : document;
if ( domElement ) this.domElement.setAttribute( 'tabindex', - 1 );
// API
this.movementSpeed = 1.0;
this.rollSpeed = 0.005;
this.dragToLook = false;
this.autoForward = false;
// disable default target object behavior
// internals
this.tmpQuaternion = new THREE.Quaternion();
this.mouseStatus = 0;
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 };
this.moveVector = new THREE.Vector3( 0, 0, 0 );
this.rotationVector = new THREE.Vector3( 0, 0, 0 );
this.handleEvent = function ( event ) {
if ( typeof this[ event.type ] == 'function' ) {
this[ event.type ]( event );
}
};
this.keydown = function( event ) {
if ( event.altKey ) {
return;
}
//event.preventDefault();
switch ( event.keyCode ) {
case 16: /* shift */ this.movementSpeedMultiplier = .1; break;
case 87: /*W*/ this.moveState.forward = 1; break;
case 83: /*S*/ this.moveState.back = 1; break;
case 65: /*A*/ this.moveState.left = 1; break;
case 68: /*D*/ this.moveState.right = 1; break;
case 82: /*R*/ this.moveState.up = 1; break;
case 70: /*F*/ this.moveState.down = 1; break;
case 38: /*up*/ this.moveState.pitchUp = 1; break;
case 40: /*down*/ this.moveState.pitchDown = 1; break;
case 37: /*left*/ this.moveState.yawLeft = 1; break;
case 39: /*right*/ this.moveState.yawRight = 1; break;
case 81: /*Q*/ this.moveState.rollLeft = 1; break;
case 69: /*E*/ this.moveState.rollRight = 1; break;
}
this.updateMovementVector();
this.updateRotationVector();
};
this.keyup = function( event ) {
switch ( event.keyCode ) {
case 16: /* shift */ this.movementSpeedMultiplier = 1; break;
case 87: /*W*/ this.moveState.forward = 0; break;
case 83: /*S*/ this.moveState.back = 0; break;
case 65: /*A*/ this.moveState.left = 0; break;
case 68: /*D*/ this.moveState.right = 0; break;
case 82: /*R*/ this.moveState.up = 0; break;
case 70: /*F*/ this.moveState.down = 0; break;
case 38: /*up*/ this.moveState.pitchUp = 0; break;
case 40: /*down*/ this.moveState.pitchDown = 0; break;
case 37: /*left*/ this.moveState.yawLeft = 0; break;
case 39: /*right*/ this.moveState.yawRight = 0; break;
case 81: /*Q*/ this.moveState.rollLeft = 0; break;
case 69: /*E*/ this.moveState.rollRight = 0; break;
}
this.updateMovementVector();
this.updateRotationVector();
};
this.mousedown = function( event ) {
if ( this.domElement !== document ) {
this.domElement.focus();
}
event.preventDefault();
event.stopPropagation();
if ( this.dragToLook ) {
this.mouseStatus ++;
} else {
switch ( event.button ) {
case 0: this.moveState.forward = 1; break;
case 2: this.moveState.back = 1; break;
}
this.updateMovementVector();
}
};
this.mousemove = function( event ) {
if ( ! this.dragToLook || this.mouseStatus > 0 ) {
var container = this.getContainerDimensions();
var halfWidth = container.size[ 0 ] / 2;
var halfHeight = container.size[ 1 ] / 2;
this.moveState.yawLeft = - ( ( event.pageX - container.offset[ 0 ] ) - halfWidth ) / halfWidth;
this.moveState.pitchDown = ( ( event.pageY - container.offset[ 1 ] ) - halfHeight ) / halfHeight;
this.updateRotationVector();
}
};
this.mouseup = function( event ) {
event.preventDefault();
event.stopPropagation();
if ( this.dragToLook ) {
this.mouseStatus --;
this.moveState.yawLeft = this.moveState.pitchDown = 0;
} else {
switch ( event.button ) {
case 0: this.moveState.forward = 0; break;
case 2: this.moveState.back = 0; break;
}
this.updateMovementVector();
}
this.updateRotationVector();
};
this.update = function( delta ) {
var moveMult = delta * this.movementSpeed;
var rotMult = delta * this.rollSpeed;
this.object.translateX( this.moveVector.x * moveMult );
this.object.translateY( this.moveVector.y * moveMult );
this.object.translateZ( this.moveVector.z * moveMult );
this.tmpQuaternion.set( this.rotationVector.x * rotMult, this.rotationVector.y * rotMult, this.rotationVector.z * rotMult, 1 ).normalize();
this.object.quaternion.multiply( this.tmpQuaternion );
// expose the rotation vector for convenience
this.object.rotation.setFromQuaternion( this.object.quaternion, this.object.rotation.order );
};
this.updateMovementVector = function() {
var forward = ( this.moveState.forward || ( this.autoForward && ! this.moveState.back ) ) ? 1 : 0;
this.moveVector.x = ( - this.moveState.left + this.moveState.right );
this.moveVector.y = ( - this.moveState.down + this.moveState.up );
this.moveVector.z = ( - forward + this.moveState.back );
//console.log( 'move:', [ this.moveVector.x, this.moveVector.y, this.moveVector.z ] );
};
this.updateRotationVector = function() {
this.rotationVector.x = ( - this.moveState.pitchDown + this.moveState.pitchUp );
this.rotationVector.y = ( - this.moveState.yawRight + this.moveState.yawLeft );
this.rotationVector.z = ( - this.moveState.rollRight + this.moveState.rollLeft );
//console.log( 'rotate:', [ this.rotationVector.x, this.rotationVector.y, this.rotationVector.z ] );
};
this.getContainerDimensions = function() {
if ( this.domElement != document ) {
return {
size : [ this.domElement.offsetWidth, this.domElement.offsetHeight ],
offset : [ this.domElement.offsetLeft, this.domElement.offsetTop ]
};
} else {
return {
size : [ window.innerWidth, window.innerHeight ],
offset : [ 0, 0 ]
};
}
};
function bind( scope, fn ) {
return function () {
fn.apply( scope, arguments );
};
}
function contextmenu( event ) {
event.preventDefault();
}
this.dispose = function() {
this.domElement.removeEventListener( 'contextmenu', contextmenu, false );
this.domElement.removeEventListener( 'mousedown', _mousedown, false );
this.domElement.removeEventListener( 'mousemove', _mousemove, false );
this.domElement.removeEventListener( 'mouseup', _mouseup, false );
window.removeEventListener( 'keydown', _keydown, false );
window.removeEventListener( 'keyup', _keyup, false );
};
var _mousemove = bind( this, this.mousemove );
var _mousedown = bind( this, this.mousedown );
var _mouseup = bind( this, this.mouseup );
var _keydown = bind( this, this.keydown );
var _keyup = bind( this, this.keyup );
this.domElement.addEventListener( 'contextmenu', contextmenu, false );
this.domElement.addEventListener( 'mousemove', _mousemove, false );
this.domElement.addEventListener( 'mousedown', _mousedown, false );
this.domElement.addEventListener( 'mouseup', _mouseup, false );
window.addEventListener( 'keydown', _keydown, false );
window.addEventListener( 'keyup', _keyup, false );
this.updateMovementVector();
this.updateRotationVector();
};