-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathLeapControls.js
429 lines (257 loc) · 9.85 KB
/
LeapControls.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
//Almost a duplicate of THREE.RollControls
//Created by the two fine gentleman below:
/**
* @author mikael emtinger / http://gomo.se/
* @author alteredq / http://alteredqualia.com/
*/
/*
Right now, only two controls:
- If there is one pointable, move around according to its position in 3D leap space
- If there are two pointables, lock the position of the camera,
and rotate based on the first pointables X and Y position
To use this script, simply make sure
- that you call it in the html,
- initialize your THREE scene,
- add the THREE rendering function to the initLeap() function,
- call initLeap() to start the scene rendering!
*/
THREE.LeapControls = function ( object, domElement ) {
/*
Some Constants for the LEAP
If you are unhappy with the way that the camera works
this is the place to look first
*/
this.leapConstants = {
//makes rotation slower (also dependent on this.lookSpeed)
rotationConstant:100,
//when the hand leaves the field
//this is the amount the rotation will be
//divided by ever render
rotationDampening:1.01,
//Changes the 0 of the Y field
yAlignment: 200,
//Changes the 0 of the Z field
zAlignment: -50,
//Makes movement faster
movementConstant:.01,
//deceleration for when hand leaves field
movementDampening:1.05,
}
this.object = object;
this.domElement = ( domElement !== undefined ) ? domElement : document;
// API
this.mouseLook = true;
this.lookSpeed = 1;
this.movementSpeed = 1;
this.rollSpeed = 1;
this.autoForward = true;
//Added to smooth camera to a stop
this.minSpeed = 0
this.deceleration = 100
this.constrainVertical = [ -0.9, 0.9 ];
// disable default target object behavior
this.object.matrixAutoUpdate = false;
// internals
this.forward = new THREE.Vector3( 0, 0, 1 );
this.roll = 0;
var xTemp = new THREE.Vector3();
var yTemp = new THREE.Vector3();
var zTemp = new THREE.Vector3();
var rollMatrix = new THREE.Matrix4();
//Keeping keyboard control functionality
//as backup
var doRoll = false, rollDirection = 1, forwardSpeed = 0, sideSpeed = 0, upSpeed = 0;
var windowHalfX = 0;
var windowHalfY = 0;
this.handleResize = function () {
windowHalfX = window.innerWidth / 2;
windowHalfY = window.innerHeight / 2;
};
//Sets rotation contants
//so that when the hand is removed from the field, it won't automatically jump to no movement
this.leapRotateHorizontal = 0
this.leapRotateVertical = 0
this.update = function ( delta ) {
/*
LEAP MOTION CAMERA MOVEMENT / ROTATION
*/
//if the hand is within range of leap,
// use its X & Y to look horizontally and vertically
if ( window.leapPos.x !=0 && window.leapPos.y !=0) {
this.leapRotateHorizontal = window.leapPos.x/this.leapConstants.rotationConstant
this.leapRotateVertical = -( window.leapPos.y-this.leapConstants.yAlignment)/this.leapConstants.rotationConstant
}else{
this.leapRotateHorizontal = this.leapRotateHorizontal/this.leapConstants.rotationDampening
this.leapRotateVertical = this.leapRotateVertical/this.leapConstants.rotationDampening
}
var actualLookSpeed = delta * this.lookSpeed;
this.rotateHorizontally( actualLookSpeed * this.leapRotateHorizontal);
this.rotateVertically( actualLookSpeed * this.leapRotateVertical);
//If there is only one pointable, use its position in Z space to move the camera
if(window.leapPos.z!=0 && window.leapPos.stationary ==false){
this.movementSpeed += -(window.leapPos.z+this.leapConstants.zAlignment)*this.leapConstants.movementConstant
}else{
if(this.movementSpeed > 5 || this.movementSpeed < -5 ){
this.movementSpeed = this.movementSpeed/this.leapConstants.movementDampening
}else{
this.movementSpeed = 0
}
}
var actualSpeed = delta * this.movementSpeed;
var forwardOrAuto = ( forwardSpeed > 0 || ( this.autoForward && ! ( forwardSpeed < 0 ) ) ) ? 1 : forwardSpeed;
this.object.translateZ( -actualSpeed * forwardOrAuto );
this.object.translateX( actualSpeed * sideSpeed );
this.object.translateY( actualSpeed * upSpeed );
if( doRoll ) {
this.roll += this.rollSpeed * delta * rollDirection;
}
// cap forward up / down
if( this.forward.y > this.constrainVertical[ 1 ] ) {
this.forward.y = this.constrainVertical[ 1 ];
this.forward.normalize();
} else if( this.forward.y < this.constrainVertical[ 0 ] ) {
this.forward.y = this.constrainVertical[ 0 ];
this.forward.normalize();
}
// construct unrolled camera matrix
zTemp.copy( this.forward );
yTemp.set( 0, 1, 0 );
xTemp.crossVectors( yTemp, zTemp ).normalize();
yTemp.crossVectors( zTemp, xTemp ).normalize();
this.object.matrix.elements[0] = xTemp.x; this.object.matrix.elements[4] = yTemp.x; this.object.matrix.elements[8] = zTemp.x;
this.object.matrix.elements[1] = xTemp.y; this.object.matrix.elements[5] = yTemp.y; this.object.matrix.elements[9] = zTemp.y;
this.object.matrix.elements[2] = xTemp.z; this.object.matrix.elements[6] = yTemp.z; this.object.matrix.elements[10] = zTemp.z;
// calculate roll matrix
rollMatrix.identity();
rollMatrix.elements[0] = Math.cos( this.roll ); rollMatrix.elements[4] = -Math.sin( this.roll );
rollMatrix.elements[1] = Math.sin( this.roll ); rollMatrix.elements[5] = Math.cos( this.roll );
// multiply camera with roll
this.object.matrix.multiply( rollMatrix );
this.object.matrixWorldNeedsUpdate = true;
// set position
this.object.matrix.elements[12] = this.object.position.x;
this.object.matrix.elements[13] = this.object.position.y;
this.object.matrix.elements[14] = this.object.position.z;
};
this.translateX = function ( distance ) {
this.object.position.x += this.object.matrix.elements[0] * distance;
this.object.position.y += this.object.matrix.elements[1] * distance;
this.object.position.z += this.object.matrix.elements[2] * distance;
};
this.translateY = function ( distance ) {
this.object.position.x += this.object.matrix.elements[4] * distance;
this.object.position.y += this.object.matrix.elements[5] * distance;
this.object.position.z += this.object.matrix.elements[6] * distance;
};
this.translateZ = function ( distance ) {
this.object.position.x -= this.object.matrix.elements[8] * distance;
this.object.position.y -= this.object.matrix.elements[9] * distance;
this.object.position.z -= this.object.matrix.elements[10] * distance;
};
this.rotateHorizontally = function ( amount ) {
// please note that the amount is NOT degrees, but a scale value
xTemp.set( this.object.matrix.elements[0], this.object.matrix.elements[1], this.object.matrix.elements[2] );
xTemp.multiplyScalar( amount );
this.forward.sub( xTemp );
this.forward.normalize();
};
this.rotateVertically = function ( amount ) {
// please note that the amount is NOT degrees, but a scale value
yTemp.set( this.object.matrix.elements[4], this.object.matrix.elements[5], this.object.matrix.elements[6] );
yTemp.multiplyScalar( amount );
this.forward.add( yTemp );
this.forward.normalize();
};
function onKeyDown( event ) {
//event.preventDefault();
switch ( event.keyCode ) {
case 38: /*up*/
case 87: /*W*/ forwardSpeed = 1; break;
case 37: /*left*/
case 65: /*A*/ sideSpeed = -1; break;
case 40: /*down*/
case 83: /*S*/ forwardSpeed = -1; break;
case 39: /*right*/
case 68: /*D*/ sideSpeed = 1; break;
case 81: /*Q*/ doRoll = true; rollDirection = 1; break;
case 69: /*E*/ doRoll = true; rollDirection = -1; break;
case 82: /*R*/ upSpeed = 1; break;
case 70: /*F*/ upSpeed = -1; break;
}
};
function onKeyUp( event ) {
switch( event.keyCode ) {
case 38: /*up*/
case 87: /*W*/ forwardSpeed = 0; break;
case 37: /*left*/
case 65: /*A*/ sideSpeed = 0; break;
case 40: /*down*/
case 83: /*S*/ forwardSpeed = 0; break;
case 39: /*right*/
case 68: /*D*/ sideSpeed = 0; break;
case 81: /*Q*/ doRoll = false; break;
case 69: /*E*/ doRoll = false; break;
case 82: /*R*/ upSpeed = 0; break;
case 70: /*F*/ upSpeed = 0; break;
}
};
function onMouseMove( event ) {
mouseX = ( event.clientX - windowHalfX ) / window.innerWidth;
mouseY = ( event.clientY - windowHalfY ) / window.innerHeight;
};
function onMouseDown ( event ) {
event.preventDefault();
event.stopPropagation();
mouseIsDown = true
switch ( event.button ) {
case 0: forwardSpeed = 1; break;
case 2: forwardSpeed = -1; break;
}
};
function onMouseUp ( event ) {
event.preventDefault();
event.stopPropagation();
mouseIsDown = false
switch ( event.button ) {
case 0: forwardSpeed = 1; break;
case 2: forwardSpeed = -1; break;
}
};
this.domElement.addEventListener( 'contextmenu', function ( event ) { event.preventDefault(); }, false );
this.domElement.addEventListener( 'mousemove', onMouseMove, false );
this.domElement.addEventListener( 'mousedown', onMouseDown, false );
this.domElement.addEventListener( 'mouseup', onMouseUp, false );
this.domElement.addEventListener( 'keydown', onKeyDown, false );
this.domElement.addEventListener( 'keyup', onKeyUp, false );
this.handleResize();
};
var leapInit = function (){
window.leapPos={
x:0,
y:0,
z:0,
stationary:false
}
Leap.loop(function(frame) {
if(frame.pointables[0]){
window.leapPos.x = frame.pointables[0].tipPosition[0]
window.leapPos.y = frame.pointables[0].tipPosition[1]
window.leapPos.z = frame.pointables[0].tipPosition[2]
//if no pointables present, set to zero
}else{
window.leapPos.x = 0
window.leapPos.y = 0
window.leapPos.z = 0
}
//If there is a second pointable, set to stationary
if(frame.pointables[1]){
window.leapPos.stationary = true
}else{
window.leapPos.stationary = false
}
latestFrame = frame
//TODO
//Create THREE scene, and add rendering function here:
animate();
})
}