-
Notifications
You must be signed in to change notification settings - Fork 54
/
three-ik.js
1183 lines (1107 loc) · 35.6 KB
/
three-ik.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
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
863
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('three')) :
typeof define === 'function' && define.amd ? define(['exports', 'three'], factory) :
(factory((global.IK = {}),global.THREE));
}(this, (function (exports,three) { 'use strict';
var t1 = new three.Vector3();
var t2 = new three.Vector3();
var t3 = new three.Vector3();
var m1 = new three.Matrix4();
function getWorldPosition(object, target) {
return target.setFromMatrixPosition(object.matrixWorld);
}
function getCentroid(positions, target) {
target.set(0, 0, 0);
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;
try {
for (var _iterator = positions[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var position = _step.value;
target.add(position);
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
target.divideScalar(positions.length);
return target;
}
function setQuaternionFromDirection(direction, up, target) {
var x = t1;
var y = t2;
var z = t3;
var m = m1;
var el = m1.elements;
z.copy(direction);
x.crossVectors(up, z);
if (x.lengthSq() === 0) {
if (Math.abs(up.z) === 1) {
z.x = 0.0001;
} else {
z.z = 0.0001;
}
z.normalize();
x.crossVectors(up, z);
}
x.normalize();
y.crossVectors(z, x);
el[0] = x.x;el[4] = y.x;el[8] = z.x;
el[1] = x.y;el[5] = y.y;el[9] = z.y;
el[2] = x.z;el[6] = y.z;el[10] = z.z;
target.setFromRotationMatrix(m);
}
function transformPoint(vector, matrix, target) {
var e = matrix.elements;
var x = vector.x * e[0] vector.y * e[4] vector.z * e[8] e[12];
var y = vector.x * e[1] vector.y * e[5] vector.z * e[9] e[13];
var z = vector.x * e[2] vector.y * e[6] vector.z * e[10] e[14];
var w = vector.x * e[3] vector.y * e[7] vector.z * e[11] e[15];
target.set(x / w, y / w, z / w);
}
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) {
return typeof obj;
} : function (obj) {
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
};
var asyncGenerator = function () {
function AwaitValue(value) {
this.value = value;
}
function AsyncGenerator(gen) {
var front, back;
function send(key, arg) {
return new Promise(function (resolve, reject) {
var request = {
key: key,
arg: arg,
resolve: resolve,
reject: reject,
next: null
};
if (back) {
back = back.next = request;
} else {
front = back = request;
resume(key, arg);
}
});
}
function resume(key, arg) {
try {
var result = gen[key](arg);
var value = result.value;
if (value instanceof AwaitValue) {
Promise.resolve(value.value).then(function (arg) {
resume("next", arg);
}, function (arg) {
resume("throw", arg);
});
} else {
settle(result.done ? "return" : "normal", result.value);
}
} catch (err) {
settle("throw", err);
}
}
function settle(type, value) {
switch (type) {
case "return":
front.resolve({
value: value,
done: true
});
break;
case "throw":
front.reject(value);
break;
default:
front.resolve({
value: value,
done: false
});
break;
}
front = front.next;
if (front) {
resume(front.key, front.arg);
} else {
back = null;
}
}
this._invoke = send;
if (typeof gen.return !== "function") {
this.return = undefined;
}
}
if (typeof Symbol === "function" && Symbol.asyncIterator) {
AsyncGenerator.prototype[Symbol.asyncIterator] = function () {
return this;
};
}
AsyncGenerator.prototype.next = function (arg) {
return this._invoke("next", arg);
};
AsyncGenerator.prototype.throw = function (arg) {
return this._invoke("throw", arg);
};
AsyncGenerator.prototype.return = function (arg) {
return this._invoke("return", arg);
};
return {
wrap: function (fn) {
return function () {
return new AsyncGenerator(fn.apply(this, arguments));
};
},
await: function (value) {
return new AwaitValue(value);
}
};
}();
var classCallCheck = function (instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a function");
}
};
var createClass = function () {
function defineProperties(target, props) {
for (var i = 0; i < props.length; i ) {
var descriptor = props[i];
descriptor.enumerable = descriptor.enumerable || false;
descriptor.configurable = true;
if ("value" in descriptor) descriptor.writable = true;
Object.defineProperty(target, descriptor.key, descriptor);
}
}
return function (Constructor, protoProps, staticProps) {
if (protoProps) defineProperties(Constructor.prototype, protoProps);
if (staticProps) defineProperties(Constructor, staticProps);
return Constructor;
};
}();
var get = function get(object, property, receiver) {
if (object === null) object = Function.prototype;
var desc = Object.getOwnPropertyDescriptor(object, property);
if (desc === undefined) {
var parent = Object.getPrototypeOf(object);
if (parent === null) {
return undefined;
} else {
return get(parent, property, receiver);
}
} else if ("value" in desc) {
return desc.value;
} else {
var getter = desc.get;
if (getter === undefined) {
return undefined;
}
return getter.call(receiver);
}
};
var inherits = function (subClass, superClass) {
if (typeof superClass !== "function" && superClass !== null) {
throw new TypeError("Super expression must either be null or a function, not " typeof superClass);
}
subClass.prototype = Object.create(superClass && superClass.prototype, {
constructor: {
value: subClass,
enumerable: false,
writable: true,
configurable: true
}
});
if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass;
};
var possibleConstructorReturn = function (self, call) {
if (!self) {
throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
}
return call && (typeof call === "object" || typeof call === "function") ? call : self;
};
var slicedToArray = function () {
function sliceIterator(arr, i) {
var _arr = [];
var _n = true;
var _d = false;
var _e = undefined;
try {
for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) {
_arr.push(_s.value);
if (i && _arr.length === i) break;
}
} catch (err) {
_d = true;
_e = err;
} finally {
try {
if (!_n && _i["return"]) _i["return"]();
} finally {
if (_d) throw _e;
}
}
return _arr;
}
return function (arr, i) {
if (Array.isArray(arr)) {
return arr;
} else if (Symbol.iterator in Object(arr)) {
return sliceIterator(arr, i);
} else {
throw new TypeError("Invalid attempt to destructure non-iterable instance");
}
};
}();
var Z_AXIS = new three.Vector3(0, 0, 1);
var DEG2RAD = three.Math.DEG2RAD;
var RAD2DEG = three.Math.RAD2DEG;
var IKBallConstraint = function () {
function IKBallConstraint(angle) {
classCallCheck(this, IKBallConstraint);
this.angle = angle;
}
createClass(IKBallConstraint, [{
key: '_apply',
value: function _apply(joint) {
var direction = new three.Vector3().copy(joint._getDirection());
var parentDirection = joint._localToWorldDirection(new three.Vector3().copy(Z_AXIS)).normalize();
var currentAngle = direction.angleTo(parentDirection) * RAD2DEG;
if (this.angle / 2 < currentAngle) {
direction.normalize();
var correctionAxis = new three.Vector3().crossVectors(parentDirection, direction).normalize();
parentDirection.applyAxisAngle(correctionAxis, this.angle * DEG2RAD * 0.5);
joint._setDirection(parentDirection);
return true;
}
return false;
}
}]);
return IKBallConstraint;
}();
var Y_AXIS = new three.Vector3(0, 1, 0);
var IKJoint = function () {
function IKJoint(bone) {
var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
constraints = _ref.constraints;
classCallCheck(this, IKJoint);
this.constraints = constraints || [];
this.bone = bone;
this.distance = 0;
this._originalDirection = new three.Vector3();
this._direction = new three.Vector3();
this._worldPosition = new three.Vector3();
this._isSubBase = false;
this._subBasePositions = null;
this.isIKJoint = true;
this._updateWorldPosition();
}
createClass(IKJoint, [{
key: '_setIsSubBase',
value: function _setIsSubBase() {
this._isSubBase = true;
this._subBasePositions = [];
}
}, {
key: '_applySubBasePositions',
value: function _applySubBasePositions() {
if (this._subBasePositions.length === 0) {
return;
}
getCentroid(this._subBasePositions, this._worldPosition);
this._subBasePositions.length = 0;
}
}, {
key: '_applyConstraints',
value: function _applyConstraints() {
if (!this.constraints) {
return;
}
var constraintApplied = false;
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;
try {
for (var _iterator = this.constraints[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var constraint = _step.value;
if (constraint && constraint._apply) {
var applied = constraint._apply(this);
constraintApplied = constraintApplied || applied;
}
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
return constraintApplied;
}
}, {
key: '_setDistance',
value: function _setDistance(distance) {
this.distance = distance;
}
}, {
key: '_getDirection',
value: function _getDirection() {
return this._direction;
}
}, {
key: '_setDirection',
value: function _setDirection(direction) {
this._direction.copy(direction);
}
}, {
key: '_getDistance',
value: function _getDistance() {
return this.distance;
}
}, {
key: '_updateMatrixWorld',
value: function _updateMatrixWorld() {
this.bone.updateMatrixWorld(true);
}
}, {
key: '_getWorldPosition',
value: function _getWorldPosition() {
return this._worldPosition;
}
}, {
key: '_getWorldDirection',
value: function _getWorldDirection(joint) {
return new three.Vector3().subVectors(this._getWorldPosition(), joint._getWorldPosition()).normalize();
}
}, {
key: '_updateWorldPosition',
value: function _updateWorldPosition() {
getWorldPosition(this.bone, this._worldPosition);
}
}, {
key: '_setWorldPosition',
value: function _setWorldPosition(position) {
this._worldPosition.copy(position);
}
}, {
key: '_localToWorldDirection',
value: function _localToWorldDirection(direction) {
if (this.bone.parent) {
var parent = this.bone.parent.matrixWorld;
direction.transformDirection(parent);
}
return direction;
}
}, {
key: '_worldToLocalDirection',
value: function _worldToLocalDirection(direction) {
if (this.bone.parent) {
var inverseParent = new three.Matrix4().copy(this.bone.parent.matrixWorld).invert();
direction.transformDirection(inverseParent);
}
return direction;
}
}, {
key: '_applyWorldPosition',
value: function _applyWorldPosition() {
var direction = new three.Vector3().copy(this._direction);
var position = new three.Vector3().copy(this._getWorldPosition());
var parent = this.bone.parent;
if (parent) {
this._updateMatrixWorld();
var inverseParent = new three.Matrix4().copy(this.bone.parent.matrixWorld).invert();
transformPoint(position, inverseParent, position);
this.bone.position.copy(position);
this._updateMatrixWorld();
this._worldToLocalDirection(direction);
setQuaternionFromDirection(direction, Y_AXIS, this.bone.quaternion);
} else {
this.bone.position.copy(position);
}
this.bone.updateMatrix();
this._updateMatrixWorld();
}
}, {
key: '_getWorldDistance',
value: function _getWorldDistance(joint) {
return this._worldPosition.distanceTo(joint.isIKJoint ? joint._getWorldPosition() : getWorldPosition(joint, new three.Vector3()));
}
}]);
return IKJoint;
}();
var IKChain = function () {
function IKChain() {
classCallCheck(this, IKChain);
this.isIKChain = true;
this.totalLengths = 0;
this.base = null;
this.effector = null;
this.effectorIndex = null;
this.chains = new Map();
this.origin = null;
this.iterations = 100;
this.tolerance = 0.01;
this._depth = -1;
this._targetPosition = new three.Vector3();
}
createClass(IKChain, [{
key: 'add',
value: function add(joint) {
var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
target = _ref.target;
if (this.effector) {
throw new Error('Cannot add additional joints to a chain with an end effector.');
}
if (!joint.isIKJoint) {
if (joint.isBone) {
joint = new IKJoint(joint);
} else {
throw new Error('Invalid joint in an IKChain. Must be an IKJoint or a THREE.Bone.');
}
}
this.joints = this.joints || [];
this.joints.push(joint);
if (this.joints.length === 1) {
this.base = this.joints[0];
this.origin = new three.Vector3().copy(this.base._getWorldPosition());
}
else {
var previousJoint = this.joints[this.joints.length - 2];
previousJoint._updateMatrixWorld();
previousJoint._updateWorldPosition();
joint._updateWorldPosition();
var distance = previousJoint._getWorldDistance(joint);
if (distance === 0) {
throw new Error('bone with 0 distance between adjacent bone found');
}
joint._setDistance(distance);
joint._updateWorldPosition();
var direction = previousJoint._getWorldDirection(joint);
previousJoint._originalDirection = new three.Vector3().copy(direction);
joint._originalDirection = new three.Vector3().copy(direction);
this.totalLengths = distance;
}
if (target) {
this.effector = joint;
this.effectorIndex = joint;
this.target = target;
}
return this;
}
}, {
key: '_hasEffector',
value: function _hasEffector() {
return !!this.effector;
}
}, {
key: '_getDistanceFromTarget',
value: function _getDistanceFromTarget() {
return this._hasEffector() ? this.effector._getWorldDistance(this.target) : -1;
}
}, {
key: 'connect',
value: function connect(chain) {
if (!chain.isIKChain) {
throw new Error('Invalid connection in an IKChain. Must be an IKChain.');
}
if (!chain.base.isIKJoint) {
throw new Error('Connecting chain does not have a base joint.');
}
var index = this.joints.indexOf(chain.base);
if (this.target && index === this.joints.length - 1) {
throw new Error('Cannot append a chain to an end joint in a chain with a target.');
}
if (index === -1) {
throw new Error('Cannot connect chain that does not have a base joint in parent chain.');
}
this.joints[index]._setIsSubBase();
var chains = this.chains.get(index);
if (!chains) {
chains = [];
this.chains.set(index, chains);
}
chains.push(chain);
return this;
}
}, {
key: '_updateJointWorldPositions',
value: function _updateJointWorldPositions() {
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;
try {
for (var _iterator = this.joints[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var joint = _step.value;
joint._updateWorldPosition();
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
}
}, {
key: '_forward',
value: function _forward() {
this.origin.copy(this.base._getWorldPosition());
if (this.target) {
this._targetPosition.setFromMatrixPosition(this.target.matrixWorld);
this.effector._setWorldPosition(this._targetPosition);
} else if (!this.joints[this.joints.length - 1]._isSubBase) {
return;
}
for (var i = 1; i < this.joints.length; i ) {
var joint = this.joints[i];
if (joint._isSubBase) {
joint._applySubBasePositions();
}
}
for (var _i = this.joints.length - 1; _i > 0; _i--) {
var _joint = this.joints[_i];
var prevJoint = this.joints[_i - 1];
var direction = prevJoint._getWorldDirection(_joint);
var worldPosition = direction.multiplyScalar(_joint.distance).add(_joint._getWorldPosition());
if (prevJoint === this.base && this.base._isSubBase) {
this.base._subBasePositions.push(worldPosition);
} else {
prevJoint._setWorldPosition(worldPosition);
}
}
}
}, {
key: '_backward',
value: function _backward() {
if (!this.base._isSubBase) {
this.base._setWorldPosition(this.origin);
}
for (var i = 0; i < this.joints.length - 1; i ) {
var joint = this.joints[i];
var nextJoint = this.joints[i 1];
var jointWorldPosition = joint._getWorldPosition();
var direction = nextJoint._getWorldDirection(joint);
joint._setDirection(direction);
joint._applyConstraints();
direction.copy(joint._direction);
if (!(this.base === joint && joint._isSubBase)) {
joint._applyWorldPosition();
}
nextJoint._setWorldPosition(direction.multiplyScalar(nextJoint.distance).add(jointWorldPosition));
if (i === this.joints.length - 2) {
if (nextJoint !== this.effector) {
nextJoint._setDirection(direction);
}
nextJoint._applyWorldPosition();
}
}
return this._getDistanceFromTarget();
}
}]);
return IKChain;
}();
var IK = function () {
function IK() {
classCallCheck(this, IK);
this.chains = [];
this._needsRecalculated = true;
this.isIK = true;
this._orderedChains = null;
}
createClass(IK, [{
key: 'add',
value: function add(chain) {
if (!chain.isIKChain) {
throw new Error('Argument is not an IKChain.');
}
this.chains.push(chain);
}
}, {
key: 'recalculate',
value: function recalculate() {
this._orderedChains = [];
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;
try {
for (var _iterator = this.chains[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var rootChain = _step.value;
var orderedChains = [];
this._orderedChains.push(orderedChains);
var chainsToSave = [rootChain];
while (chainsToSave.length) {
var chain = chainsToSave.shift();
orderedChains.push(chain);
var _iteratorNormalCompletion2 = true;
var _didIteratorError2 = false;
var _iteratorError2 = undefined;
try {
for (var _iterator2 = chain.chains.values()[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
var subChains = _step2.value;
var _iteratorNormalCompletion3 = true;
var _didIteratorError3 = false;
var _iteratorError3 = undefined;
try {
for (var _iterator3 = subChains[Symbol.iterator](), _step3; !(_iteratorNormalCompletion3 = (_step3 = _iterator3.next()).done); _iteratorNormalCompletion3 = true) {
var subChain = _step3.value;
if (chainsToSave.indexOf(subChain) !== -1) {
throw new Error('Recursive chain structure detected.');
}
chainsToSave.push(subChain);
}
} catch (err) {
_didIteratorError3 = true;
_iteratorError3 = err;
} finally {
try {
if (!_iteratorNormalCompletion3 && _iterator3.return) {
_iterator3.return();
}
} finally {
if (_didIteratorError3) {
throw _iteratorError3;
}
}
}
}
} catch (err) {
_didIteratorError2 = true;
_iteratorError2 = err;
} finally {
try {
if (!_iteratorNormalCompletion2 && _iterator2.return) {
_iterator2.return();
}
} finally {
if (_didIteratorError2) {
throw _iteratorError2;
}
}
}
}
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
}
}, {
key: 'solve',
value: function solve() {
if (!this._orderedChains) {
this.recalculate();
}
var _iteratorNormalCompletion4 = true;
var _didIteratorError4 = false;
var _iteratorError4 = undefined;
try {
for (var _iterator4 = this._orderedChains[Symbol.iterator](), _step4; !(_iteratorNormalCompletion4 = (_step4 = _iterator4.next()).done); _iteratorNormalCompletion4 = true) {
var subChains = _step4.value;
var iterations = 1;
while (iterations > 0) {
for (var i = subChains.length - 1; i >= 0; i--) {
subChains[i]._updateJointWorldPositions();
}
for (var _i = subChains.length - 1; _i >= 0; _i--) {
subChains[_i]._forward();
}
var withinTolerance = true;
for (var _i2 = 0; _i2 < subChains.length; _i2 ) {
var distanceFromTarget = subChains[_i2]._backward();
if (distanceFromTarget > this.tolerance) {
withinTolerance = false;
}
}
if (withinTolerance) {
break;
}
iterations--;
}
}
} catch (err) {
_didIteratorError4 = true;
_iteratorError4 = err;
} finally {
try {
if (!_iteratorNormalCompletion4 && _iterator4.return) {
_iterator4.return();
}
} finally {
if (_didIteratorError4) {
throw _iteratorError4;
}
}
}
}
}, {
key: 'getRootBone',
value: function getRootBone() {
return this.chains[0].base.bone;
}
}]);
return IK;
}();
var BoneHelper = function (_Object3D) {
inherits(BoneHelper, _Object3D);
function BoneHelper(height, boneSize, axesSize) {
classCallCheck(this, BoneHelper);
var _this = possibleConstructorReturn(this, (BoneHelper.__proto__ || Object.getPrototypeOf(BoneHelper)).call(this));
if (height !== 0) {
var geo = new three.ConeBufferGeometry(boneSize, height, 4);
geo.applyMatrix4(new three.Matrix4().makeRotationAxis(new three.Vector3(1, 0, 0), Math.PI / 2));
_this.boneMesh = new three.Mesh(geo, new three.MeshBasicMaterial({
color: 0xff0000,
wireframe: true,
depthTest: false,
depthWrite: false
}));
} else {
_this.boneMesh = new three.Object3D();
}
_this.boneMesh.position.z = height / 2;
_this.add(_this.boneMesh);
_this.axesHelper = new three.AxesHelper(axesSize);
_this.add(_this.axesHelper);
return _this;
}
return BoneHelper;
}(three.Object3D);
var IKHelper = function (_Object3D2) {
inherits(IKHelper, _Object3D2);
function IKHelper(ik) {
var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
color = _ref.color,
showBones = _ref.showBones,
boneSize = _ref.boneSize,
showAxes = _ref.showAxes,
axesSize = _ref.axesSize,
wireframe = _ref.wireframe;
classCallCheck(this, IKHelper);
var _this2 = possibleConstructorReturn(this, (IKHelper.__proto__ || Object.getPrototypeOf(IKHelper)).call(this));
boneSize = boneSize || 0.1;
axesSize = axesSize || 0.2;
if (!ik.isIK) {
throw new Error('IKHelper must receive an IK instance.');
}
_this2.ik = ik;
_this2._meshes = new Map();
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;
try {
for (var _iterator = _this2.ik.chains[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var rootChain = _step.value;
var chainsToMeshify = [rootChain];
while (chainsToMeshify.length) {
var chain = chainsToMeshify.shift();
for (var i = 0; i < chain.joints.length; i ) {
var joint = chain.joints[i];
var nextJoint = chain.joints[i 1];
var distance = nextJoint ? nextJoint.distance : 0;
if (chain.base === joint && chain !== rootChain) {
continue;
}
var mesh = new BoneHelper(distance, boneSize, axesSize);
mesh.matrixAutoUpdate = false;
_this2._meshes.set(joint, mesh);
_this2.add(mesh);
}
var _iteratorNormalCompletion2 = true;
var _didIteratorError2 = false;
var _iteratorError2 = undefined;
try {
for (var _iterator2 = chain.chains.values()[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
var subChains = _step2.value;
var _iteratorNormalCompletion3 = true;
var _didIteratorError3 = false;
var _iteratorError3 = undefined;
try {
for (var _iterator3 = subChains[Symbol.iterator](), _step3; !(_iteratorNormalCompletion3 = (_step3 = _iterator3.next()).done); _iteratorNormalCompletion3 = true) {
var subChain = _step3.value;
chainsToMeshify.push(subChain);
}
} catch (err) {
_didIteratorError3 = true;
_iteratorError3 = err;
} finally {
try {
if (!_iteratorNormalCompletion3 && _iterator3.return) {
_iterator3.return();
}
} finally {
if (_didIteratorError3) {
throw _iteratorError3;
}
}
}
}
} catch (err) {
_didIteratorError2 = true;
_iteratorError2 = err;
} finally {
try {
if (!_iteratorNormalCompletion2 && _iterator2.return) {
_iterator2.return();
}
} finally {
if (_didIteratorError2) {
throw _iteratorError2;
}
}
}
}
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
_this2.showBones = showBones !== undefined ? showBones : true;
_this2.showAxes = showAxes !== undefined ? showAxes : true;
_this2.wireframe = wireframe !== undefined ? wireframe : true;
_this2.color = color || new three.Color(0xff0077);
return _this2;
}
createClass(IKHelper, [{
key: 'updateMatrixWorld',
value: function updateMatrixWorld(force) {
var _iteratorNormalCompletion4 = true;
var _didIteratorError4 = false;
var _iteratorError4 = undefined;
try {
for (var _iterator4 = this._meshes[Symbol.iterator](), _step4; !(_iteratorNormalCompletion4 = (_step4 = _iterator4.next()).done); _iteratorNormalCompletion4 = true) {
var _ref2 = _step4.value;
var _ref3 = slicedToArray(_ref2, 2);
var joint = _ref3[0];
var mesh = _ref3[1];
mesh.matrix.copy(joint.bone.matrixWorld);
}
} catch (err) {
_didIteratorError4 = true;
_iteratorError4 = err;
} finally {
try {
if (!_iteratorNormalCompletion4 && _iterator4.return) {
_iterator4.return();