-
Notifications
You must be signed in to change notification settings - Fork 0
/
controls_tracked.js
551 lines (374 loc) · 13 KB
/
controls_tracked.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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
function SETUP_tracked(){
var axesHolder = new THREE.Object3D();
var axisRodRad = 0.005;
var axisRodStart = 0.1;
var axisRodEnd = 0.2;
var axisMeshes = [];
var axisRots = {};
function makeAxis(label,rot){
var curAxisHolder = new THREE.Object3D();
curAxisHolder.rotation.copy(rot);
axesHolder.add(curAxisHolder);
var curAxisSplitHolder = new THREE.Object3D();
var curAxisDimMat = new THREE.MeshBasicMaterial({color:0x888888});
var axisHalves = [];
for(var i=-1; i<=1; i+=2){
var axisHalfMesh = new THREE.Mesh(
new THREE.CylinderGeometry(axisRodRad,axisRodRad,axisRodEnd-axisRodStart,8,1),
curAxisDimMat
);
axisHalfMesh.rotation.z = Math.PI/2*i;
axisHalfMesh.position.x = (axisRodEnd+axisRodStart)/2*i;
curAxisSplitHolder.add(axisHalfMesh);
axisHalves.push(axisHalfMesh);
}
var dimAxisMesh = mergeObjects(axisHalves,curAxisSplitHolder,curAxisDimMat);
curAxisHolder.add(dimAxisMesh);
var litAxisMesh = new THREE.Mesh(
new THREE.CylinderGeometry(axisRodRad,axisRodRad,axisRodEnd*2,8,1),
new THREE.MeshBasicMaterial({color:0xFFFFFF})
);
litAxisMesh.rotation.z = Math.PI/2;
curAxisHolder.add(litAxisMesh);
axisMeshes.push({
label:label,
dim:dimAxisMesh,
lit:litAxisMesh
});
axisRots[label] = rot;
}
makeAxis('x',new THREE.Euler(0,0,0));
makeAxis('y',new THREE.Euler(0,0,Math.PI/2));
makeAxis('z',new THREE.Euler(0,-Math.PI/2,0));
function getDominantHandPointWorldPos(handOffsetVec){
var domHandBone = bones[dominantHand];
if (dominantHand == "Left") {
//this assumes a fresh vector for every call!!
//which, is the case right now, but it's honestly kind of wasteful..
//so if I ever fix that -- MULTIPLY A COPY OR WHATEVER
handOffsetVec.x *= -1;
}
handOffsetVec.applyQuaternion(domHandBone.quaternion);
handOffsetVec.add(domHandBone.position);
return handOffsetVec;
}
function getDominantPalmWorldPos(){
return getDominantHandPointWorldPos(new THREE.Vector3(0,0,0.08));
}
function getDominantPalmAxisLocalPos(){
// since it's relative-only, I could probably get away with only transforming it by the rotation,
// but whatever, let's not overthink this, I don't think math is any kind of bottleneck here
axesHolder.updateMatrix();
var axesMtxInv = new THREE.Matrix4().getInverse(axesHolder.matrix);
var worldPos = getDominantPalmWorldPos();
worldPos.applyMatrix4(axesMtxInv);
return worldPos;
}
function getDominantAxisLocalQuat(){
var handQuat = bones[dominantHand].quaternion.clone();
var axesQuatInv = axesHolder.quaternion.clone().inverse();//cache this?
handQuat.premultiply(axesQuatInv);
return handQuat;
}
var dragging = false;
var grabStartVal;
var focusedAxis;
function updateAxesPos(){
scene.add(axesHolder);
axesHolder.position.copy(getDominantPalmWorldPos());
}
function updateAxesRot(){
switch(curMode){
case EDITMODES.Slide:
axesHolder.quaternion.set(0,0,0,1);
break;
case EDITMODES.Turn:
case EDITMODES.Stretch:
var copyFrom = (selectedArtMesh.artType == ARTTYPES.mannequin) ? selectedArtMesh.myHolder : selectedArtMesh ;
axesHolder.quaternion.copy( copyFrom.quaternion );
break;
}
if (selectedArtMesh.artType == ARTTYPES.costume) axesHolder.quaternion.premultiply( selectedArtMesh.myMannequinMesh.myHolder.quaternion );
}
var debugDot = new THREE.Mesh( new THREE.SphereGeometry(0.01), new THREE.MeshBasicMaterial({color:0xFF0000}) );
//axesHolder.add(debugDot);
function updateAxesSelection(){
var handQuat = getDominantAxisLocalQuat();
var handVec = new THREE.Vector3(1,0,0);
handVec.applyQuaternion(handQuat);
debugDot.position.copy(handVec);
debugDot.position.multiplyScalar(0.05);
var majAxis = 'x';
var axes = ['y','z'];
for(var i=0; i<axes.length; i++){
var curAxis = axes[i];
if (Math.abs(handVec[curAxis]) > Math.abs(handVec[majAxis])) majAxis=curAxis;
}
focusedAxis = majAxis;
for(var i=0; i<axisMeshes.length; i++){
var curMeshes = axisMeshes[i];
curMeshes.lit.visible = (curMeshes.label==focusedAxis);
curMeshes.dim.visible = !curMeshes.lit.visible;
}
}
function getAxisHandOffset(axisLabel){
var handLocalPos = getDominantPalmAxisLocalPos();
return handLocalPos[axisLabel];
}
function getAxisHandAngle(axisLabel){
var handQuat = getDominantAxisLocalQuat();
var axisQuat = new THREE.Quaternion().setFromEuler(axisRots[axisLabel]).inverse();//this could be sped up..
handQuat.premultiply(axisQuat);
var turnVec = new THREE.Vector3(0,1,0);
turnVec.applyQuaternion(handQuat);
return -Math.atan2(turnVec.y,turnVec.z);
}
function getCurrentDragAmt(){
return (curMode==EDITMODES.Turn) ? getAxisHandAngle(focusedAxis) : getAxisHandOffset(focusedAxis) ;
}
var pads;
var bones = {};
var curMode;
var GRIPINDEX = 1;
// Call immediately to indicate that we want gamepad updates
altspace.getGamepads();
function isButtonPressed(buttonIndex){
if (!pads) return false;
return pads[dominantHand].buttons[buttonIndex].pressed;
}
function isGripPressed(){
return isButtonPressed(GRIPINDEX);
}
var lastGrabMtx;
var wasGrabbing = false;
var wasDoubleGrabbing = false;
return {
label:"tracked",
isAvailable:function(){
var gamepadsList = altspace.getGamepads();//always ask regardless of anything
if (pads) return true;
if (gamepadsList.length < 2) {
//console.log("tracked unavailable because ONLY",gamepadsList.length,"PADS");
return false;
}
var padL, padR;
for(var i=0; i<gamepadsList.length; i++) {
var curPadInfo = gamepadsList[i];
switch(curPadInfo.mapping){
case "standard":
continue;
case "touch":
case "steamvr":
this.mapping = curPadInfo.mapping;
if (curPadInfo.hand == "left") {
padL = curPadInfo;
} else {
padR = curPadInfo;
}
break;
default:
console.log("UNKNOWN CONTROLLER TYPE??",curPadInfo.mapping);
break;
}
}
if (!padL || !padR || !padL.connected || !padR.connected) {
//console.log("tracked unavailable because THERE'S",gamepadsList.length,"PADS, BUT NO HANDEDNESS");
return false;
}
pads = {
Right:padR,
Left:padL
};
return true;
},
open:function(){
// NOTE: because there is no going back from tracked controls,
// I'm not going to bother actually implementing anything for open/close.
// but, if something can eventually supercede tracked controls,
// COME IN HERE AND MAKE SURE IT CLEANS UP AFTER ITSELF
},
close:function(){
},
modeSwitch:function(newMode){
switch(curMode){
case EDITMODES.Draw:
break;
case EDITMODES.Shape:
break;
case EDITMODES.Erase:
break;
case EDITMODES.Grab:
break;
case EDITMODES.Slide:
case EDITMODES.Turn:
case EDITMODES.Stretch:
scene.remove(axesHolder);
break;
case EDITMODES.Save:
case EDITMODES.Load:
case EDITMODES.Moderation:
case EDITMODES.Disabled:
break;
}
curMode = newMode;
switch(newMode){
case EDITMODES.Draw:
break;
case EDITMODES.Shape:
break;
case EDITMODES.Erase:
break;
case EDITMODES.Grab:
break;
case EDITMODES.Slide:
case EDITMODES.Turn:
case EDITMODES.Stretch:
break;
case EDITMODES.Save:
case EDITMODES.Load:
case EDITMODES.Moderation:
case EDITMODES.Disabled:
break;
}
},
frameFunc:function(){
if (!bones.Right) {
bones = {
Right:skeletonInfo.getJoint('Hand','Right',0),
Left:skeletonInfo.getJoint('Hand','Left',0)
};
}
switch(curMode){
case EDITMODES.Draw:
break;
case EDITMODES.Shape:
break;
case EDITMODES.Erase:
break;
case EDITMODES.Grab:
break;
case EDITMODES.Slide:
case EDITMODES.Turn:
case EDITMODES.Stretch:
if (selectedArtMesh) {
if (!dragging) {
updateAxesPos();
updateAxesRot();
updateAxesSelection();
if (isGripPressed()) {
latestDragAmt = grabStartVal = getCurrentDragAmt();
dragging = true;
//console.log("GRIP AXIS:",focusedAxis);
}
} else {
latestDragAmt = getCurrentDragAmt();
if (!isGripPressed()) dragging = false;
}
}
break;
case EDITMODES.Save:
case EDITMODES.Load:
case EDITMODES.Moderation:
case EDITMODES.Disabled:
break;
}
},
frameEnd:function(){
},
getDominantPointing:function(){
if (curMode == EDITMODES.Shape || !pads) return false;
var domPad = pads[dominantHand];
//this test should probably be moved into sculpt.js
if ( (curMode == EDITMODES.Draw) && (!isGripPressed()) ) return false;
return {
fingerPos:getDominantHandPointWorldPos(new THREE.Vector3(0.04,0,0.19)),
fingerQuat:new THREE.Quaternion().copy(domPad.rotation)
};
},
getBothPinching:function(){
if (!pads) return false;
if ( !(curMode == EDITMODES.Shape || curMode == EDITMODES.Save) ) return false;
if (!pads.Right.buttons[GRIPINDEX].pressed) return false;
if (!pads.Left.buttons[GRIPINDEX].pressed) return false;
var rightQuat = new THREE.Quaternion().copy(pads.Right.rotation);
var leftQuat = new THREE.Quaternion().copy(pads.Left.rotation);
var rightPinch = new THREE.Vector3().copy(pads.Right.position);
var leftPinch = new THREE.Vector3().copy(pads.Left.position);
var offX = 0.015;
var offY = -0.05;
var rightOff = new THREE.Vector3(offX,offY,0);
rightOff.applyQuaternion(rightQuat);
rightPinch.add(rightOff);
var leftOff = new THREE.Vector3(-offX,offY,0);
leftOff.applyQuaternion(leftQuat);
leftPinch.add(leftOff);
return {
rightPinch:rightPinch,
leftPinch:leftPinch,
rightQuat:rightQuat,
leftQuat:leftQuat,
spawnMargin:0.5
};
},
refreshArtSelected:function(){
},
updateEnabledness:function(){
},
getIsStretching:function(){
return dragging;
},
getTransformAxis:function(){
return focusedAxis;
},
getDragProgressAmt:function(){
return latestDragAmt - grabStartVal;
},
selectedArtTransformed:function(){
if (curMode==EDITMODES.Turn) {
var oldVal = getCurrentDragAmt();
updateAxesRot();
var newVal = getCurrentDragAmt();
grabStartVal += (newVal-oldVal);
}
},
getIsGrabbing:function(){
return pads.Right.buttons[GRIPINDEX].pressed || pads.Left.buttons[GRIPINDEX].pressed ;
},
getGrabIncrementMtx:function(){
var grabR = pads.Right.buttons[GRIPINDEX].pressed;
var grabL = pads.Left.buttons[GRIPINDEX].pressed;
if (!grabR && !grabL) return;
var isDoubleGrabbing = (grabR && grabL);
var newGrabMtx = new THREE.Matrix4();
if (isDoubleGrabbing) {//scaling grab
var middlePos = new THREE.Vector3().copy(pads.Right.position);
middlePos.lerp( pads.Left.position, 0.5 );
var middleQuat = new THREE.Quaternion().copy(pads.Right.rotation);
middleQuat.slerp( new THREE.Quaternion().copy(pads.Left.rotation), 0.5 );
var gapDist = new THREE.Vector3().copy(pads.Right.position).distanceTo( new THREE.Vector3().copy(pads.Left.position) );
gapDist = Math.max(gapDist,0.00001);//almost certainly unnecessary but no harm being cautious
newGrabMtx.compose( middlePos, middleQuat, new THREE.Vector3(gapDist,gapDist,gapDist) );
} else {//nonscaling grab
var grabbingHand = grabR ? pads.Right : pads.Left ;
newGrabMtx.compose( grabbingHand.position, grabbingHand.rotation, new THREE.Vector3(1,1,1) );
}
if (selectedArtMesh.artType == ARTTYPES.costume) {
selectedArtMesh.myMannequinMesh.myHolder.updateMatrix();//?
var holderInvMtx = new THREE.Matrix4().getInverse(selectedArtMesh.myMannequinMesh.myHolder.matrix);
newGrabMtx.premultiply(holderInvMtx);
}
if ( !wasGrabbing || (wasDoubleGrabbing != isDoubleGrabbing) ) lastGrabMtx = newGrabMtx;//could just return the identity instead, but w/e
var diffMtx = newGrabMtx.clone();
diffMtx.multiply( new THREE.Matrix4().getInverse(lastGrabMtx) );
wasGrabbing = true;
wasDoubleGrabbing = isDoubleGrabbing;
lastGrabMtx = newGrabMtx;
return diffMtx;
},
grabStopped:function(){
wasGrabbing = false;
},
strokeMinDist:0.01
};
}
fileReady();