-
Notifications
You must be signed in to change notification settings - Fork 0
/
LeapObjectControls.js
292 lines (264 loc) · 9.7 KB
/
LeapObjectControls.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
/*
* @author Torsten Sprenger / http://torstensprenger.com
*
* Leap Object Controls (http://leapmotion.com)
*
*/
THREE.LeapObjectControls = function(camera, object) {
var _this = this;
this.camera = camera;
this.object = object;
// api
this.enabled = true;
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 = 4.0;
this.rotateHands = 1;
this.rotateFingers = [2, 3];
this.rotateRightHanded = true;
this.rotateHandPosition = true;
this.rotateMin = 0;
this.rotateMax = Math.PI;
// scale
this.scaleEnabled = true;
this.scaleSpeed = 1.0;
this.scaleHands = 1;
this.scaleFingers = [4, 5];
this.scaleRightHanded = true;
this.scaleHandPosition = true;
this.scaleMin = 0.1;
this.scaleMax = 10;
// 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 _scaleZLast = 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 'scale':
return _this.scaleSpeed * (_this.scaleHandPosition ? 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.scaleTransform = function(delta) {
return _this.transformFactor('scale') * THREE.Math.mapLinear(delta, -400, 400, -2, 2);
};
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 'scale':
if (_this.scaleHands instanceof Array) {
if (_this.scaleFingers instanceof Array) {
if (_this.scaleHands[0] <= hl && hl <= _this.scaleHands[1] && _this.scaleFingers[0] <= fl && fl <= _this.scaleFingers[1]) return true;
} else {
if (_this.scaleHands[0] <= hl && hl <= _this.scaleHands[1] && _this.scaleFingers == fl) return true;
};
} else {
if (_this.scaleFingers instanceof Array) {
if (_this.scaleHands == hl && _this.scaleFingers[0] <= fl && fl <= _this.scaleFingers[1]) return true;
} else {
if (_this.scaleHands == hl && _this.scaleFingers == 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 'scale':
if (_this.scaleRightHanded) {
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 'scale':
h = _this.hand(frame, 'scale');
return (_this.scaleHandPosition ? h.palmPosition : frame.pointables[0].tipPosition);
case 'pan':
h = _this.hand(frame, 'pan');
return (_this.panHandPosition ? h.palmPosition : frame.pointables[0].tipPosition);
};
};
// methods
this.rotateObject = function(frame) {
if (_this.rotateEnabled && _this.applyGesture(frame, 'rotate')) {
// rotate around axis in xy-plane which is orthogonal to camera vector
var y = _this.position(frame, 'rotate')[1];
if (!_rotateYLast) _rotateYLast = y;
var yDelta = y - _rotateYLast;
// TODO: apply correct rotation to object
//
// var t = new THREE.Vector3().subVectors(_this.camera.position, _this.object.position);
// 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 rotationMatrix = new THREE.Matrix4();
// rotationMatrix.makeRotationAxis(n, angleDelta);
// rotationMatrix.multiply(_this.object.matrix);
// _this.object.rotation.setFromRotationMatrix(rotationMatrix, _this.camera.rotation.order);
// };
// rotate around y-axis
var x = _this.position(frame, 'rotate')[0];
if (!_rotateXLast) _rotateXLast = x;
var xDelta = x - _rotateXLast;
_this.object.rotation.y += _this.rotateTransform(xDelta);
_rotateYLast = y;
_rotateXLast = x;
_scaleZLast = null;
_panXLast = null;
_panYLast = null;
_panZLast = null;
} else {
_rotateYLast = null;
_rotateXLast = null;
};
};
this.scaleObject = function(frame) {
if (_this.scaleEnabled && _this.applyGesture(frame, 'scale')) {
var z = _this.position(frame, 'scale')[2];
if (!_scaleZLast) _scaleZLast = z;
var zDelta = z - _scaleZLast;
scaleDelta = _this.scaleTransform(zDelta);
var newScale = _this.object.scale.x + scaleDelta;
if (_this.scaleMin < newScale && newScale < _this.scaleMax) {
_this.object.scale = new THREE.Vector3(newScale, newScale, newScale);
};
_scaleZLast = z;
_rotateXLast = null;
_rotateYLast = null;
_panXLast = null;
_panYLast = null;
_panZLast = null;
} else {
_scaleZLast = null;
};
};
this.panObject = 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.object.position.add(v);
_panXLast = x;
_panYLast = y;
_panZLast = z;
_rotateXLast = null;
_rotateYLast = null;
_scaleZLast = null;
} else {
_panXLast = null;
_panYLast = null;
_panZLast = null;
};
};
this.update = function(frame) {
if (_this.enabled) {
_this.rotateObject(frame);
_this.scaleObject(frame);
_this.panObject(frame);
};
};
};