forked from jsantell/THREE.IK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
three-ik.js
969 lines (831 loc) · 24.8 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
594
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
(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';
/**
* A collection of utilities.
* @module utils
*/
const t1 = new three.Vector3();
const t2 = new three.Vector3();
const t3 = new three.Vector3();
const m1 = new three.Matrix4();
/**
* Returns the world position of object and sets
* it on target.
*
* @param {THREE.Object3D} object
* @param {THREE.Vector3} target
*/
function getWorldPosition(object, target) {
return target.setFromMatrixPosition(object.matrixWorld);
}
/**
* Returns the distance between two objects.
*
* @param {THREE.Object3D} obj1
* @param {THREE.Object3D} obj2
* @return {number}
*/
/**
* Sets the target to the centroid position between all passed in
* positions.
*
* @param {Array<THREE.Vector3>} positions
* @param {THREE.Vector3} target
*/
function getCentroid(positions, target) {
target.set(0, 0, 0);
for (let position of positions) {
target.add(position);
}
target.divideScalar(positions.length);
return target;
}
/**
* Takes a direction vector and an up vector and sets
* `target` quaternion to the rotation. Similar to THREE.Matrix4's
* `lookAt` function, except rather than taking two Vector3 points,
* we've already calculaeld the direction earlier so skip the first half.
*
* @param {THREE.Vector3} direction
* @param {THREE.Vector3} up
* @param {THREE.Quaternion} target
*/
function setQuaternionFromDirection(direction, up, target) {
const x = t1;
const y = t2;
const z = t3;
const m = m1;
const el = m1.elements;
z.copy(direction);
x.crossVectors(up, z);
if (x.lengthSq() === 0) {
// parallel
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);
}
/**
* Implementation of Unity's Transform.transformPoint, which is similar
* to three's Vector3.transformDirection, except we want to take scale into account,
* as we're not transforming a direction. Function taken from BabylonJS.
*
* From BabylonJS's `Vector3.transformCoordinates`:
* Sets the passed vector coordinates with the result of the transformation by the
* passed matrix of the passed vector. This method computes tranformed coordinates only,
* not transformed direction vectors (ie. it takes translation in account)
*
* @see https://docs.unity3d.com/ScriptReference/Transform.TransformPoint.html
* @see https://github.com/BabylonJS/Babylon.js/blob/6050288da37623088d5f613ca2d85aef877c5cd5/src/Math/babylon.math.ts#L1936
* @param {THREE.Vector3} vector
* @param {THREE.Matrix4} matrix
* @param {THREE.Vector3} target
*/
function transformPoint(vector, matrix, target) {
const e = matrix.elements;
const x = (vector.x * e[0]) + (vector.y * e[4]) + (vector.z * e[8]) + e[12];
const y = (vector.x * e[1]) + (vector.y * e[5]) + (vector.z * e[9]) + e[13];
const z = (vector.x * e[2]) + (vector.y * e[6]) + (vector.z * e[10]) + e[14];
const w = (vector.x * e[3]) + (vector.y * e[7]) + (vector.z * e[11]) + e[15];
target.set(x / w, y / w, z / w);
}
const Z_AXIS = new three.Vector3(0, 0, 1);
const { DEG2RAD, RAD2DEG } = three.MathUtils;
/**
* A class for a constraint.
*/
class IKBallConstraint {
/**
* Pass in an angle value in degrees.
*
* @param {number} angle
*/
constructor(angle) {
this.angle = angle;
}
/**
* Applies a constraint to passed in IKJoint, updating
* its direction if necessary. Returns a boolean indicating
* if the constraint was applied or not.
*
* @param {IKJoint} joint
* @private
* @return {boolean}
*/
_apply(joint) {
// Get direction of joint and parent in world space
const direction = new three.Vector3().copy(joint._getDirection());
const parentDirection = joint._localToWorldDirection(new three.Vector3().copy(Z_AXIS)).normalize();
// Find the current angle between them
const currentAngle = direction.angleTo(parentDirection) * RAD2DEG;
if ((this.angle / 2) < currentAngle) {
direction.normalize();
// Find the correction axis and rotate around that point to the
// largest allowed angle
const correctionAxis = new three.Vector3().crossVectors(parentDirection, direction).normalize();
parentDirection.applyAxisAngle(correctionAxis, this.angle * DEG2RAD * 0.5);
joint._setDirection(parentDirection);
return true;
}
return false;
}
}
const Y_AXIS = new three.Vector3(0, 1, 0);
/**
* A class for a joint.
*/
class IKJoint {
/**
* @param {THREE.Bone} bone
* @param {Object} config
* @param {Array<IKConstraint>} [config.constraints]
*/
constructor(bone, { constraints } = {}) {
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();
}
/**
* @private
*/
_setIsSubBase() {
this._isSubBase = true;
this._subBasePositions = [];
}
/**
* Consumes the stored sub base positions and apply it as this
* joint's world position, clearing the sub base positions.
*
* @private
*/
_applySubBasePositions() {
if (this._subBasePositions.length === 0) {
return;
}
getCentroid(this._subBasePositions, this._worldPosition);
this._subBasePositions.length = 0;
}
/**
* @private
*/
_applyConstraints() {
if (!this.constraints) {
return;
}
let constraintApplied = false;
for (let constraint of this.constraints) {
if (constraint && constraint._apply) {
let applied = constraint._apply(this);
constraintApplied = constraintApplied || applied;
}
}
return constraintApplied;
}
/**
* Set the distance.
* @private
* @param {number} distance
*/
_setDistance(distance) {
this.distance = distance;
}
/**
* @private
*/
_getDirection() {
return this._direction;
}
/**
* @private
*/
_setDirection(direction) {
this._direction.copy(direction);
}
/**
* Gets the distance.
* @private
* @return {THREE.Vector3}
*/
_getDistance() {
return this.distance;
}
/**
* @private
*/
_updateMatrixWorld() {
this.bone.updateMatrixWorld(true);
}
/**
* @private
* @return {THREE.Vector3}
*/
_getWorldPosition() {
return this._worldPosition;
}
/**
* @private
*/
_getWorldDirection(joint) {
return new three.Vector3().subVectors(this._getWorldPosition(), joint._getWorldPosition()).normalize();
}
/**
* @private
*/
_updateWorldPosition() {
getWorldPosition(this.bone, this._worldPosition);
}
/**
* @private
*/
_setWorldPosition(position) {
this._worldPosition.copy(position);
}
/**
* @private
*/
_localToWorldDirection(direction) {
if (this.bone.parent) {
const parent = this.bone.parent.matrixWorld;
direction.transformDirection(parent);
}
return direction;
}
/**
* @private
*/
_worldToLocalDirection(direction) {
if (this.bone.parent) {
const inverseParent = new three.Matrix4().copy(this.bone.parent.matrixWorld).invert();
direction.transformDirection(inverseParent);
}
return direction;
}
/**
* @private
*/
_applyWorldPosition() {
let direction = new three.Vector3().copy(this._direction);
let position = new three.Vector3().copy(this._getWorldPosition());
const parent = this.bone.parent;
if (parent) {
this._updateMatrixWorld();
let 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);
}
// Update the world matrix so the next joint can properly transform
// with this world matrix
this.bone.updateMatrix();
this._updateMatrixWorld();
}
/**
* @param {IKJoint|THREE.Vector3}
* @private
* @return {THREE.Vector3}
*/
_getWorldDistance(joint) {
return this._worldPosition.distanceTo(joint.isIKJoint ? joint._getWorldPosition() : getWorldPosition(joint, new three.Vector3()));
}
}
/**
* Class representing an IK chain, comprising multiple IKJoints.
*/
class IKChain {
/**
* Create an IKChain.
*/
constructor() {
this.isIKChain = true;
this.totalLengths = 0;
this.base = null;
this.effector = null;
this.effectorIndex = null;
this.chains = new Map();
/* THREE.Vector3 world position of base node */
this.origin = null;
this.iterations = 100;
this.tolerance = 0.01;
this._depth = -1;
this._targetPosition = new three.Vector3();
}
/**
* Add an IKJoint to the end of this chain.
*
* @param {IKJoint} joint
* @param {Object} config
* @param {THREE.Object3D} [config.target]
*/
add(joint, { 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 is the first joint, set as base.
if (this.joints.length === 1) {
this.base = this.joints[0];
this.origin = new three.Vector3().copy(this.base._getWorldPosition());
}
// Otherwise, calculate the distance for the previous joint,
// and update the total length.
else {
const previousJoint = this.joints[this.joints.length - 2];
previousJoint._updateMatrixWorld();
previousJoint._updateWorldPosition();
joint._updateWorldPosition();
const distance = previousJoint._getWorldDistance(joint);
if (distance === 0) {
throw new Error('bone with 0 distance between adjacent bone found');
}
joint._setDistance(distance);
joint._updateWorldPosition();
const 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;
}
/**
* Returns a boolean indicating whether or not this chain has an end effector.
*
* @private
* @return {boolean}
*/
_hasEffector() {
return !!this.effector;
}
/**
* Returns the distance from the end effector to the target. Returns -1 if
* this chain does not have an end effector.
*
* @private
* @return {number}
*/
_getDistanceFromTarget() {
return this._hasEffector() ? this.effector._getWorldDistance(this.target) : -1;
}
/**
* Connects another IKChain to this chain. The additional chain's root
* joint must be a member of this chain.
*
* @param {IKChain} chain
*/
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.');
}
const index = this.joints.indexOf(chain.base);
// If we're connecting to the last joint in the chain, ensure we don't
// already have an effector.
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();
let chains = this.chains.get(index);
if (!chains) {
chains = [];
this.chains.set(index, chains);
}
chains.push(chain);
return this;
}
/**
* Update joint world positions for this chain.
*
* @private
*/
_updateJointWorldPositions() {
for (let joint of this.joints) {
joint._updateWorldPosition();
}
}
/**
* Runs the forward pass of the FABRIK algorithm.
*
* @private
*/
_forward() {
// Copy the origin so the forward step can use before `_backward()`
// modifies it.
this.origin.copy(this.base._getWorldPosition());
// Set the effector's position to the target's position.
if (this.target) {
this._targetPosition.setFromMatrixPosition(this.target.matrixWorld);
this.effector._setWorldPosition(this._targetPosition);
}
else if (!this.joints[this.joints.length - 1]._isSubBase) {
// If this chain doesn't have additional chains or a target,
// not much to do here.
return;
}
// Apply sub base positions for all joints except the base,
// as we want to possibly write to the base's sub base positions,
// not read from it.
for (let i = 1; i < this.joints.length; i++) {
const joint = this.joints[i];
if (joint._isSubBase) {
joint._applySubBasePositions();
}
}
for (let i = this.joints.length - 1; i > 0; i--) {
const joint = this.joints[i];
const prevJoint = this.joints[i - 1];
const direction = prevJoint._getWorldDirection(joint);
const worldPosition = direction.multiplyScalar(joint.distance).add(joint._getWorldPosition());
// If this chain's base is a sub base, set it's position in
// `_subBaseValues` so that the forward step of the parent chain
// can calculate the centroid and clear the values.
// @TODO Could this have an issue if a subchain `x`'s base
// also had its own subchain `y`, rather than subchain `x`'s
// parent also being subchain `y`'s parent?
if (prevJoint === this.base && this.base._isSubBase) {
this.base._subBasePositions.push(worldPosition);
} else {
prevJoint._setWorldPosition(worldPosition);
}
}
}
/**
* Runs the backward pass of the FABRIK algorithm.
*
* @private
*/
_backward() {
// If base joint is a sub base, don't reset it's position back
// to the origin, but leave it where the parent chain left it.
if (!this.base._isSubBase) {
this.base._setWorldPosition(this.origin);
}
for (let i = 0; i < this.joints.length - 1; i++) {
const joint = this.joints[i];
const nextJoint = this.joints[i + 1];
const jointWorldPosition = joint._getWorldPosition();
const direction = nextJoint._getWorldDirection(joint);
joint._setDirection(direction);
joint._applyConstraints();
direction.copy(joint._direction);
// Now apply the world position to the three.js matrices. We need
// to do this before the next joint iterates so it can generate rotations
// in local space from its parent's matrixWorld.
// If this is a chain sub base, let the parent chain apply the world position
if (!(this.base === joint && joint._isSubBase)) {
joint._applyWorldPosition();
}
nextJoint._setWorldPosition(direction.multiplyScalar(nextJoint.distance).add(jointWorldPosition));
// Since we don't iterate over the last joint, handle the applying of
// the world position. If it's also a non-effector, then we must orient
// it to its parent rotation since otherwise it has nowhere to point to.
if (i === this.joints.length - 2) {
if (nextJoint !== this.effector) {
nextJoint._setDirection(direction);
}
nextJoint._applyWorldPosition();
}
}
return this._getDistanceFromTarget();
}
}
/**
* Class representing IK structure.
*/
class IK {
/**
* Create an IK structure.
*
*/
constructor() {
this.chains = [];
this._needsRecalculated = true;
this.isIK = true;
// this.iterations = 1;
// this.tolerance = 0.05;
/**
* An array of root chains for this IK system, each containing
* an array of all subchains, including the root chain, for that
* root chain, in descending-depth order.
* @private
*/
this._orderedChains = null;
}
/**
* Adds an IKChain to the IK system.
*
* @param {IKChain} chain
*/
add(chain) {
if (!chain.isIKChain) {
throw new Error('Argument is not an IKChain.');
}
this.chains.push(chain);
}
/**
* Called if there's been any changes to an IK structure.
* Called internally. Not sure if this should be supported externally.
* @private
*/
recalculate() {
this._orderedChains = [];
for (let rootChain of this.chains) {
const orderedChains = [];
this._orderedChains.push(orderedChains);
const chainsToSave = [rootChain];
while (chainsToSave.length) {
const chain = chainsToSave.shift();
orderedChains.push(chain);
for (let subChains of chain.chains.values()) {
for (let subChain of subChains) {
if (chainsToSave.indexOf(subChain) !== -1) {
throw new Error('Recursive chain structure detected.');
}
chainsToSave.push(subChain);
}
}
}
}
}
/**
* Performs the IK solution and updates bones.
*/
solve() {
// If we don't have a depth-sorted array of chains, generate it.
// This is from the first `update()` call after creating.
if (!this._orderedChains) {
this.recalculate();
}
for (let subChains of this._orderedChains) {
// Hardcode to one for now
let iterations = 1; // this.iterations;
while (iterations > 0) {
for (let i = subChains.length - 1; i >= 0; i--) {
subChains[i]._updateJointWorldPositions();
}
// Run the chain's forward step starting with the deepest chains.
for (let i = subChains.length - 1; i >= 0; i--) {
subChains[i]._forward();
}
// Run the chain's backward step starting with the root chain.
let withinTolerance = true;
for (let i = 0; i < subChains.length; i++) {
const distanceFromTarget = subChains[i]._backward();
if (distanceFromTarget > this.tolerance) {
withinTolerance = false;
}
}
if (withinTolerance) {
break;
}
iterations--;
// Get the root chain's base and randomize the rotation, maybe
// we'll get a better change at reaching our goal
// @TODO
}
}
}
/**
* Returns the root bone of this structure. Currently
* only returns the first root chain's bone.
*
* @return {THREE.Bone}
*/
getRootBone() {
return this.chains[0].base.bone;
}
}
/**
* Mesh for representing an IKJoint.
* @private
* @extends {THREE.Object3d}
*/
class BoneHelper extends three.Object3D {
/**
* @param {number} height
* @param {number?} boneSize
* @param {number?} axesSize
*/
constructor(height, boneSize, axesSize) {
super();
// If our bone has 0 height (like an end effector),
// use a dummy Object3D instead, otherwise the ConeGeometry
// will fall back to its default and not use 0 height.
if (height !== 0) {
const geo = new three.ConeGeometry(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();
}
// Offset the bone so that its rotation point is at the base of the bone
this.boneMesh.position.z = height / 2;
this.add(this.boneMesh);
this.axesHelper = new three.AxesHelper(axesSize);
this.add(this.axesHelper);
}
}
/**
* Class for visualizing an IK system.
* @extends {THREE.Object3d}
*/
class IKHelper extends three.Object3D {
/**
* Creates a visualization for an IK.
*
* @param {IK} ik
* @param {Object} config
* @param {THREE.Color} [config.color]
* @param {boolean} [config.showBones]
* @param {boolean} [config.showAxes]
* @param {boolean} [config.wireframe]
* @param {number} [config.axesSize]
* @param {number} [config.boneSize]
*/
constructor(ik, { color, showBones, boneSize, showAxes, axesSize, wireframe } = {}) {
super();
boneSize = boneSize || 0.1;
axesSize = axesSize || 0.2;
if (!ik.isIK) {
throw new Error('IKHelper must receive an IK instance.');
}
this.ik = ik;
this._meshes = new Map();
for (let rootChain of this.ik.chains) {
const chainsToMeshify = [rootChain];
while (chainsToMeshify.length) {
const chain = chainsToMeshify.shift();
for (let i = 0; i < chain.joints.length; i++) {
const joint = chain.joints[i];
const nextJoint = chain.joints[i+1];
const distance = nextJoint ? nextJoint.distance : 0;
// If a sub base, don't make another bone
if (chain.base === joint && chain !== rootChain) {
continue;
}
const mesh = new BoneHelper(distance, boneSize, axesSize);
mesh.matrixAutoUpdate = false;
this._meshes.set(joint, mesh);
this.add(mesh);
}
for (let subChains of chain.chains.values()) {
for (let subChain of subChains) {
chainsToMeshify.push(subChain);
}
}
}
}
/**
* Whether this IKHelper's bones are visible or not.
*
* @name IKHelper#showBones
* @type boolean
* @default true
*/
this.showBones = showBones !== undefined ? showBones : true;
/**
* Whether this IKHelper's axes are visible or not.
*
* @name IKHelper#showAxes
* @type boolean
* @default true
*/
this.showAxes = showAxes !== undefined ? showAxes : true;
/**
* Whether this IKHelper should be rendered as wireframes or not.
*
* @name IKHelper#wireframe
* @type boolean
* @default true
*/
this.wireframe = wireframe !== undefined ? wireframe : true;
/**
* The color of this IKHelper's bones.
*
* @name IKHelper#color
* @type THREE.Color
* @default new THREE.Color(0xff0077)
*/
this.color = color || new three.Color(0xff0077);
}
get showBones() { return this._showBones; }
set showBones(showBones) {
if (showBones === this._showBones) {
return;
}
for (let [joint, mesh] of this._meshes) {
if (showBones) {
mesh.add(mesh.boneMesh);
} else {
mesh.remove(mesh.boneMesh);
}
}
this._showBones = showBones;
}
get showAxes() { return this._showAxes; }
set showAxes(showAxes) {
if (showAxes === this._showAxes) {
return;
}
for (let [joint, mesh] of this._meshes) {
if (showAxes) {
mesh.add(mesh.axesHelper);
} else {
mesh.remove(mesh.axesHelper);
}
}
this._showAxes = showAxes;
}
get wireframe() { return this._wireframe; }
set wireframe(wireframe) {
if (wireframe === this._wireframe) {
return;
}
for (let [joint, mesh] of this._meshes) {
if (mesh.boneMesh.material) {
mesh.boneMesh.material.wireframe = wireframe;
}
}
this._wireframe = wireframe;
}
get color() { return this._color; }
set color(color) {
if (this._color && this._color.equals(color)) {
return;
}
color = (color && color.isColor) ? color : new three.Color(color);
for (let [joint, mesh] of this._meshes) {
if (mesh.boneMesh.material) {
mesh.boneMesh.material.color = color;
}
}
this._color = color;
}
updateMatrixWorld(force) {
for (let [joint, mesh] of this._meshes) {
mesh.matrix.copy(joint.bone.matrixWorld);
}
super.updateMatrixWorld(force);
}
}
// If this is being included via script tag and using THREE
// globals, attach our exports to THREE.
if (typeof window !== 'undefined' && typeof window.THREE === 'object') {
window.THREE.IK = IK;
window.THREE.IKChain = IKChain;
window.THREE.IKJoint = IKJoint;
window.THREE.IKBallConstraint = IKBallConstraint;
window.THREE.IKHelper = IKHelper;
}
exports.IK = IK;
exports.IKChain = IKChain;
exports.IKJoint = IKJoint;
exports.IKBallConstraint = IKBallConstraint;
exports.IKHelper = IKHelper;
Object.defineProperty(exports, '__esModule', { value: true });
})));