-
Notifications
You must be signed in to change notification settings - Fork 0
/
LeapCameraControls.js
294 lines (265 loc) · 9.96 KB
/
LeapCameraControls.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
/*
* @author Torsten Sprenger / http://torstensprenger.com
*
* Leap Camera Controls (http://leapmotion.com)
*
*/
THREE.LeapCameraControls = function(camera) {
var _this = this;
this.camera = camera;
// api
this.enabled = true;
this.target = new THREE.Vector3(0, 0, 0);
this.step = (camera.position.z == 0 ? Math.pow(10, (Math.log(camera.near) + Math.log(camera.far))/Math.log(10))/10.0 : camera.position.z);
this.fingerFactor = 2;
// `...Hands` : integer or range given as an array of length 2
// `...Fingers` : integer or range given as an array of length 2
// `...RightHanded` : boolean indicating whether to use left or right hand for controlling (if number of hands > 1)
// `...HandPosition`: boolean indication whether to use palm position or finger tip position (if number of fingers == 1)
// rotation
this.rotateEnabled = true;
this.rotateSpeed = 1.0;
this.rotateHands = 1;
this.rotateFingers = [2, 3];
this.rotateRightHanded = true;
this.rotateHandPosition = true;
this.rotateMin = 0;
this.rotateMax = Math.PI;
// zoom
this.zoomEnabled = true;
this.zoomSpeed = 1.0;
this.zoomHands = 1;
this.zoomFingers = [4, 5];
this.zoomRightHanded = true;
this.zoomHandPosition = true;
this.zoomMin = _this.camera.near;
this.zoomMax = _this.camera.far;
// pan
this.panEnabled = true;
this.panSpeed = 1.0;
this.panHands = 2;
this.panFingers = [6, 12];
this.panRightHanded = true;
this.panHandPosition = true;
// internals
var _rotateXLast = null;
var _rotateYLast = null;
var _zoomZLast = null;
var _panXLast = null;
var _panYLast = null;
var _panZLast = null;
// helpers
this.transformFactor = function(action) {
switch(action) {
case 'rotate':
return _this.rotateSpeed * (_this.rotateHandPosition ? 1 : _this.fingerFactor);
case 'zoom':
return _this.zoomSpeed * (_this.zoomHandPosition ? 1 : _this.fingerFactor);
case 'pan':
return _this.panSpeed * (_this.panHandPosition ? 1 : _this.fingerFactor);
};
};
this.rotateTransform = function(delta) {
return _this.transformFactor('rotate') * THREE.Math.mapLinear(delta, -400, 400, -Math.PI, Math.PI);
};
this.zoomTransform = function(delta) {
return _this.transformFactor('zoom') * THREE.Math.mapLinear(delta, -400, 400, -_this.step, _this.step);
};
this.panTransform = function(delta) {
return _this.transformFactor('pan') * THREE.Math.mapLinear(delta, -400, 400, -_this.step, _this.step);
};
this.applyGesture = function(frame, action) {
var hl = frame.hands.length;
var fl = frame.pointables.length;
switch(action) {
case 'rotate':
if (_this.rotateHands instanceof Array) {
if (_this.rotateFingers instanceof Array) {
if (_this.rotateHands[0] <= hl && hl <= _this.rotateHands[1] && _this.rotateFingers[0] <= fl && fl <= _this.rotateFingers[1]) return true;
} else {
if (_this.rotateHands[0] <= hl && hl <= _this.rotateHands[1] && _this.rotateFingers == fl) return true;
};
} else {
if (_this.rotateFingers instanceof Array) {
if (_this.rotateHands == hl && _this.rotateFingers[0] <= fl && fl <= _this.rotateFingers[1]) return true;
} else {
if (_this.rotateHands == hl && _this.rotateFingers == fl) return true;
};
};
break;
case 'zoom':
if (_this.zoomHands instanceof Array) {
if (_this.zoomFingers instanceof Array) {
if (_this.zoomHands[0] <= hl && hl <= _this.zoomHands[1] && _this.zoomFingers[0] <= fl && fl <= _this.zoomFingers[1]) return true;
} else {
if (_this.zoomHands[0] <= hl && hl <= _this.zoomHands[1] && _this.zoomFingers == fl) return true;
};
} else {
if (_this.zoomFingers instanceof Array) {
if (_this.zoomHands == hl && _this.zoomFingers[0] <= fl && fl <= _this.zoomFingers[1]) return true;
} else {
if (_this.zoomHands == hl && _this.zoomFingers == fl) return true;
};
};
break;
case 'pan':
if (_this.panHands instanceof Array) {
if (_this.panFingers instanceof Array) {
if (_this.panHands[0] <= hl && hl <= _this.panHands[1] && _this.panFingers[0] <= fl && fl <= _this.panFingers[1]) return true;
} else {
if (_this.panHands[0] <= hl && hl <= _this.panHands[1] && _this.panFingers == fl) return true;
};
} else {
if (_this.panFingers instanceof Array) {
if (_this.panHands == hl && _this.panFingers[0] <= fl && fl <= _this.panFingers[1]) return true;
} else {
if (_this.panHands == hl && _this.panFingers == fl) return true;
};
};
break;
};
return false;
};
this.hand = function(frame, action) {
var hds = frame.hands;
if (hds.length > 0) {
if (hds.length == 1) {
return hds[0];
} else if (hds.length == 2) {
var lh, rh;
if (hds[0].palmPosition[0] < hds[1].palmPosition[0]) {
lh = hds[0];
rh = hds[1];
} else {
lh = hds[1];
rh = hds[0];
}
switch(action) {
case 'rotate':
if (_this.rotateRightHanded) {
return rh;
} else {
return lh;
};
case 'zoom':
if (_this.zoomRightHanded) {
return rh;
} else {
return lh;
};
case 'pan':
if (_this.panRightHanded) {
return rh;
} else {
return lh;
};
};
};
};
return false;
};
this.position = function(frame, action) {
// assertion: if `...HandPosition` is false, then `...Fingers` needs to be 1 or [1, 1]
var h;
switch(action) {
case 'rotate':
h = _this.hand(frame, 'rotate');
return (_this.rotateHandPosition ? h.palmPosition : frame.pointables[0].tipPosition);
case 'zoom':
h = _this.hand(frame, 'zoom');
return (_this.zoomHandPosition ? h.palmPosition : frame.pointables[0].tipPosition);
case 'pan':
h = _this.hand(frame, 'pan');
return (_this.panHandPosition ? h.palmPosition : frame.pointables[0].tipPosition);
};
};
// methods
this.rotateCamera = function(frame) {
if (_this.rotateEnabled && _this.applyGesture(frame, 'rotate')) {
// rotate around axis in xy-plane (in target coordinate system) which is orthogonal to camera vector
var y = _this.position(frame, 'rotate')[1];
if (!_rotateYLast) _rotateYLast = y;
var yDelta = y - _rotateYLast;
var t = new THREE.Vector3().subVectors(_this.camera.position, _this.target); // translate
angleDelta = _this.rotateTransform(yDelta);
newAngle = t.angleTo(new THREE.Vector3(0, 1, 0)) + angleDelta;
if (_this.rotateMin < newAngle && newAngle < _this.rotateMax) {
var n = new THREE.Vector3(t.z, 0, -t.x).normalize();
var matrixX = new THREE.Matrix4().makeRotationAxis(n, angleDelta);
_this.camera.position = t.applyMatrix4(matrixX).add(_this.target); // rotate and translate back
};
// rotate around y-axis translated by target vector
var x = _this.position(frame, 'rotate')[0];
if (!_rotateXLast) _rotateXLast = x;
var xDelta = x - _rotateXLast;
var matrixY = new THREE.Matrix4().makeRotationY(-_this.rotateTransform(xDelta));
_this.camera.position.sub(_this.target).applyMatrix4(matrixY).add(_this.target); // translate, rotate and translate back
_this.camera.lookAt(_this.target);
_rotateYLast = y;
_rotateXLast = x;
_zoomZLast = null;
_panXLast = null;
_panYLast = null;
_panZLast = null;
} else {
_rotateYLast = null;
_rotateXLast = null;
};
};
this.zoomCamera = function(frame) {
if (_this.zoomEnabled && _this.applyGesture(frame, 'zoom')) {
var z = _this.position(frame, 'zoom')[2];
if (!_zoomZLast) _zoomZLast = z;
var zDelta = z - _zoomZLast;
var t = new THREE.Vector3().subVectors(_this.camera.position, _this.target);
lengthDelta = _this.zoomTransform(zDelta);
newLength = t.length() - lengthDelta;
if (_this.zoomMin < newLength && newLength < _this.zoomMax) {
t.normalize().multiplyScalar(lengthDelta);
_this.camera.position.sub(t);
};
_zoomZLast = z;
_rotateXLast = null;
_rotateYLast = null;
_panXLast = null;
_panYLast = null;
_panZLast = null;
} else {
_zoomZLast = null;
};
};
this.panCamera = function(frame) {
if (_this.panEnabled && _this.applyGesture(frame, 'pan')) {
var x = _this.position(frame, 'pan')[0];
var y = _this.position(frame, 'pan')[1];
var z = _this.position(frame, 'pan')[2];
if (!_panXLast) _panXLast = x;
if (!_panYLast) _panYLast = y;
if (!_panZLast) _panZLast = z;
var xDelta = x - _panXLast;
var yDelta = y - _panYLast;
var zDelta = z - _panZLast;
var v = _this.camera.localToWorld(new THREE.Vector3(_this.panTransform(xDelta), _this.panTransform(yDelta), _this.panTransform(zDelta)));
v.sub(_this.camera.position);
_this.camera.position.sub(v);
_this.target.sub(v);
_panXLast = x;
_panYLast = y;
_panZLast = z;
_rotateXLast = null;
_rotateYLast = null;
_zoomZLast = null;
} else {
_panXLast = null;
_panYLast = null;
_panZLast = null;
};
};
this.update = function(frame) {
if (_this.enabled) {
_this.rotateCamera(frame);
_this.zoomCamera(frame);
_this.panCamera(frame);
};
};
};