(function(h,l){"object"===typeof exports&&"undefined"!==typeof module?l(exports):"function"===typeof define&&define.amd?define(["exports"],l):l(h.FIK={})})(this,function(h){function l(a,b){this.x=a||0;this.y=b||0}function f(a,b,c){this.x=a||0;this.y=b||0;this.z=c||0}function y(){this.elements=[1,0,0,0,1,0,0,0,1];0a?-1:0=c?Math.PI:1<=c?0:Math.acos(c);return 0<=a.end.x*b.end.y-a.end.y*b.end.x?c:-c},clamp:function(a,b,c){a=ac?c:a},lerp:function(a,b,c){return(1-c)*a+c*b},rand:function(a,b){return a+Math.random()*(b-a)},randInt:function(a,b){return a+Math.floor(Math.random()*(b-a+1))},nearEquals:function(a,b,c){return Math.abs(a-b)<=c?!0:!1},perpendicular:function(a,b){return m.nearEquals(a.dot(b),0,.01)?!0:!1},genPerpendicularVectorQuick:function(a){var b= a.clone();return.99>Math.abs(a.y)?b.set(-a.z,0,a.x).normalize():b.set(0,a.z,-a.y).normalize()},genPerpendicularVectorFrisvad:function(a){var b=a.clone();if(-.9999999>a.z)return b.set(0,-1,0);var c=1/(1+a.z);return b.set(1-a.x*a.x*c,-a.x*a.y*c,-a.x).normalize()},rotateXDegs:function(a,b){return a.clone().rotate(b*m.toRad,"X")},rotateYDegs:function(a,b){return a.clone().rotate(b*m.toRad,"Y")},rotateZDegs:function(a,b){return a.clone().rotate(b*m.toRad,"Z")},withinManhattanDistance:function(a,b,c){return Math.abs(b.x- a.x)>c||Math.abs(b.y-a.y)>c||Math.abs(b.z-a.z)>c?!1:!0},manhattanDistanceBetween:function(a,b){return Math.abs(b.x-a.x)+Math.abs(b.x-a.x)+Math.abs(b.x-a.x)},distanceBetween:function(a,b){var c=b.x-a.x,d=b.y-a.y;a=void 0!==a.z?b.z-a.z:0;return Math.sqrt(c*c+d*d+a*a)},rotateDegs:function(a,b){return a.clone().rotate(b*m.toRad)},validateDirectionUV:function(a){0>a.length()&&p.error("vector direction unit vector cannot be zero.")},validateLength:function(a){0>a&&p.error("Length must be a greater than or equal to zero.")}}; Object.assign(l.prototype,{isVector2:!0,set:function(a,b){this.x=a||0;this.y=b||0;return this},distanceTo:function(a){return Math.sqrt(this.distanceToSquared(a))},distanceToSquared:function(a){var b=this.x-a.x;a=this.y-a.y;return b*b+a*a},multiplyScalar:function(a){this.x*=a;this.y*=a;return this},divideScalar:function(a){return this.multiplyScalar(1/a)},length:function(){return Math.sqrt(this.x*this.x+this.y*this.y)},normalize:function(){return this.divideScalar(this.length()||1)},normalised:function(){return(new l(this.x, this.y)).normalize()},lengthSq:function(){return this.x*this.x+this.y*this.y},add:function(a){this.x+=a.x;this.y+=a.y;return this},plus:function(a){return new l(this.x+a.x,this.y+a.y)},min:function(a){this.x-=a.x;this.y-=a.y;return this},minus:function(a){return new l(this.x-a.x,this.y-a.y)},divideBy:function(a){return(new l(this.x,this.y)).divideScalar(a)},times:function(a){return a.isVector2?new l(this.x*a.x,this.y*a.y):new l(this.x*a,this.y*a,this.z*a)},dot:function(a,b){return this.x*a.x+this.y* a.y},negate:function(){this.x=-this.x;this.y=-this.y;return this},negated:function(){return new l(-this.x,-this.y)},clone:function(){return new l(this.x,this.y)},copy:function(a){this.x=a.x;this.y=a.y;return this},cross:function(a){return this.x*a.y-this.y*a.x},sign:function(a){return 0<=this.cross(a)?1:-1},approximatelyEquals:function(a,b){if(0>b)return!1;var c=Math.abs(this.y-a.y);return Math.abs(this.x-a.x)=a?Math.PI:1<=a?0:Math.acos(a)},getSignedAngle:function(a){var b=this.angleTo(a);return 1===this.sign(a)?b:-b},constrainedUV:function(a,b,c){var d=a.getSignedAngle(this);d>c&&this.copy(a).rotate(c);dthis.x?-this.x:this.x,0>this.y?-this.y:this.y,0>this.z?-this.z:this.z)},dot:function(a){return this.x*a.x+this.y*a.y+this.z*a.z},length:function(){return Math.sqrt(this.x*this.x+this.y*this.y+this.z*this.z)},lengthSq:function(){return this.x*this.x+this.y*this.y+this.z*this.z},normalize:function(){return this.divideScalar(this.length()||1)},normalised:function(){return(new f(this.x, this.y,this.z)).normalize()},add:function(a){this.x+=a.x;this.y+=a.y;this.z+=a.z;return this},min:function(a){this.x-=a.x;this.y-=a.y;this.z-=a.z;return this},plus:function(a){return new f(this.x+a.x,this.y+a.y,this.z+a.z)},minus:function(a){return new f(this.x-a.x,this.y-a.y,this.z-a.z)},divideBy:function(a){return new f(this.x/a,this.y/a,this.z/a)},multiply:function(a){return new f(this.x*a,this.y*a,this.z*a)},multiplyScalar:function(a){this.x*=a;this.y*=a;this.z*=a;return this},divideScalar:function(a){return this.multiplyScalar(1/ a)},cross:function(a){return new f(this.y*a.z-this.z*a.y,this.z*a.x-this.x*a.z,this.x*a.y-this.y*a.x)},crossVectors:function(a,b){var c=a.x,d=a.y;a=a.z;var e=b.x,g=b.y;b=b.z;this.x=d*b-a*g;this.y=a*e-c*b;this.z=c*g-d*e;return this},negate:function(){this.x=-this.x;this.y=-this.y;this.z=-this.z;return this},negated:function(){return new f(-this.x,-this.y,-this.z)},clone:function(){return new f(this.x,this.y,this.z)},copy:function(a){this.x=a.x;this.y=a.y;this.z=a.z;return this},approximatelyEquals:function(a, b){if(0>b)return!1;var c=Math.abs(this.y-a.y),d=Math.abs(this.z-a.z);return Math.abs(this.x-a.x)=a?Math.PI:1<=a?0:Math.acos(a)},getSignedAngle:function(a,b){var c=this.angleTo(a);return 1===this.sign(a, b)?c:-c},constrainedUV:function(a,b,c,d,e){var g=a.getSignedAngle(this,b);g>e&&this.copy(c.rotateAboutAxis(a,e,b));gc){var d=a.normalised().cross(this).normalize();this.copy(b.rotateAboutAxis(a,c,d))}return this}});Object.assign(y.prototype,{isMatrix3:!0,set:function(a,b,c,d,e,g,k,h,t){var f=this.elements;f[0]=a;f[1]=d;f[2]=k;f[3]=b;f[4]=e;f[5]=h;f[6]=c;f[7]=g;f[8]=t;return this},identity:function(){this.set(1, 0,0,0,1,0,0,0,1);return this},setV3:function(a,b,c){var d=this.elements;d[0]=a.x;d[3]=a.y;d[6]=a.z;d[1]=b.x;d[4]=b.y;d[7]=b.z;d[2]=c.x;d[5]=c.y;d[8]=c.z;return this},transpose:function(){var a=this.elements;var b=a[1];a[1]=a[3];a[3]=b;b=a[2];a[2]=a[6];a[6]=b;b=a[5];a[5]=a[7];a[7]=b;return this},createRotationMatrix:function(a){var b=new f(1,0,0),c=new f(0,1,0);if(-.9999999>a.z)b.set(1,0,0),c.set(0,1,0);else{var d=1/(1+a.z),e=-a.x*a.y*d;b.set(1-a.x*a.x*d,e,-a.x).normalize();c.set(e,1-a.y*a.y*d,-a.y).normalize()}return this.setV3(b, c,a)},rotateAboutAxis:function(a,b,c){var d=Math.sin(b);b=Math.cos(b);var e=1-b,g=c.x*c.y*e,k=c.x*c.z*e,h=c.y*c.z*e,f=this.elements;f[0]=c.x*c.x*e+b;f[3]=g+c.z*d;f[6]=k-c.y*d;f[1]=g-c.z*d;f[4]=c.y*c.y*e+b;f[7]=h+c.x*d;f[2]=k+c.y*d;f[5]=h-c.x*d;f[8]=c.z*c.z*e+b;return a.clone().applyM3(this)}});Object.assign(u,{slerp:function(a,b,c,d){return c.copy(a).slerp(b,d)}});Object.defineProperties(u.prototype,{x:{get:function(){return this._x},set:function(a){this._x=a;this.onChangeCallback()}},y:{get:function(){return this._y}, set:function(a){this._y=a;this.onChangeCallback()}},z:{get:function(){return this._z},set:function(a){this._z=a;this.onChangeCallback()}},w:{get:function(){return this._w},set:function(a){this._w=a;this.onChangeCallback()}}});Object.assign(u.prototype,{set:function(a,b,c,d){this._x=a;this._y=b;this._z=c;this._w=d;this.onChangeCallback();return this},clone:function(){return new this.constructor(this._x,this._y,this._z,this._w)},setFromAxisAngle:function(a,b){b/=2;var c=Math.sin(b);this._x=a.x*c;this._y= a.y*c;this._z=a.z*c;this._w=Math.cos(b);this.onChangeCallback();return this},copy:function(a){this._x=a.x;this._y=a.y;this._z=a.z;this._w=a.w;this.onChangeCallback();return this},setFromRotationMatrix:function(a){var b=a.elements,c=b[0];a=b[4];var d=b[8],e=b[1],g=b[5],k=b[9],f=b[2],h=b[6];b=b[10];var l=c+g+b;0g&&c>b?(c=2*Math.sqrt(1+c-g-b),this._w=(h-k)/c,this._x=.25*c,this._y=(a+e)/c,this._z=(d+f)/c):g>b?(c= 2*Math.sqrt(1+g-c-b),this._w=(d-f)/c,this._x=(a+e)/c,this._y=.25*c,this._z=(k+h)/c):(c=2*Math.sqrt(1+b-c-g),this._w=(e-a)/c,this._x=(d+f)/c,this._y=(k+h)/c,this._z=.25*c);this.onChangeCallback();return this},setFromUnitVectors:function(){var a=new f,b;return function(c,d){void 0===a&&(a=new f);b=c.dot(d)+1;1E-6>b?(b=0,Math.abs(c.x)>Math.abs(c.z)?a.set(-c.y,c.x,0):a.set(0,-c.z,c.y)):a.crossVectors(c,d);this._x=a.x;this._y=a.y;this._z=a.z;this._w=b;return this.normalize()}}(),inverse:function(){return this.conjugate()}, conjugate:function(){this._x*=-1;this._y*=-1;this._z*=-1;this.onChangeCallback();return this},dot:function(a){return this._x*a._x+this._y*a._y+this._z*a._z+this._w*a._w},lengthSq:function(){return this._x*this._x+this._y*this._y+this._z*this._z+this._w*this._w},length:function(){return Math.sqrt(this._x*this._x+this._y*this._y+this._z*this._z+this._w*this._w)},normalize:function(){var a=this.length();0===a?(this._z=this._y=this._x=0,this._w=1):(a=1/a,this._x*=a,this._y*=a,this._z*=a,this._w*=a);this.onChangeCallback(); return this},multiply:function(a,b){return void 0!==b?(console.warn("THREE.Quaternion: .multiply() now only accepts one argument. Use .multiplyQuaternions( a, b ) instead."),this.multiplyQuaternions(a,b)):this.multiplyQuaternions(this,a)},premultiply:function(a){return this.multiplyQuaternions(a,this)},multiplyQuaternions:function(a,b){var c=a._x,d=a._y,e=a._z;a=a._w;var g=b._x,k=b._y,f=b._z;b=b._w;this._x=c*b+a*g+d*f-e*k;this._y=d*b+a*k+e*g-c*f;this._z=e*b+a*f+c*k-d*g;this._w=a*b-c*g-d*k-e*f;this.onChangeCallback(); return this},onChangeCallback:function(){}});var q=Math.PI,n=Math.PI/180,G=180/Math.PI,H=new f(1,0,0),I=new f(0,1,0),J=new f(0,0,1),K=new f(-1,0,0),L=new f(0,-1,0),M=new f(0,0,-1),F=new l(0,1),N=new l(0,-1),O=new l(-1,0),P=new l(1,0);Object.assign(v.prototype,{isJoint3D:!0,clone:function(){var a=new v;a.type=this.type;a.rotor=this.rotor;a.max=this.max;a.min=this.min;a.freeHinge=this.freeHinge;a.rotationAxisUV.copy(this.rotationAxisUV);a.referenceAxisUV.copy(this.referenceAxisUV);return a},testAngle:function(){this.freeHinge= this.max===q&&this.min===-q?!0:!1},validateAngle:function(a){a=0>a?0:a;return 180c&&(c*=-1);this.min=-this.validateAngle(c)*n;this.max=this.validateAngle(d)*n;this.testAngle();this.rotationAxisUV.copy(b).normalize();this.referenceAxisUV.copy(e).normalize()},getHingeReferenceAxis:function(){return this.referenceAxisUV},getHingeRotationAxis:function(){return this.rotationAxisUV}, setBallJointConstraintDegs:function(a){this.rotor=this.validateAngle(a)*n},setHingeClockwise:function(a){0>a&&(a*=-1);this.min=-this.validateAngle(a)*n;this.testAngle()},setHingeAnticlockwise:function(a){this.max=this.validateAngle(a)*n;this.testAngle()}});Object.assign(r.prototype,{isBone3D:!0,init:function(a,b,c,d){this.setStartLocation(a);b?(this.setEndLocation(b),this.length=this.getLength()):(this.setLength(d),this.setEndLocation(this.start.plus(c.normalised().multiplyScalar(d))))},clone:function(){var a= new r(this.start,this.end);a.joint=this.joint.clone();return a},setColor:function(a){this.color=a},setBoneConnectionPoint:function(a){this.boneConnectionPoint=a},setHingeClockwise:function(a){this.joint.setHingeClockwise(a)},setHingeAnticlockwise:function(a){this.joint.setHingeAnticlockwise(a)},setBallJointConstraintDegs:function(a){this.joint.setBallJointConstraintDegs(a)},setStartLocation:function(a){this.start.copy(a)},setEndLocation:function(a){this.end.copy(a)},setLength:function(a){0=b.length()?p.error("Hinge rotation axis cannot be zero."):0>=e.length()?p.error("Hinge reference axis cannot be zero."):m.perpendicular(b,e)?(a=a||"global",this.baseboneConstraintType="global"===a?3:5,this.baseboneConstraintUV=b.normalised(),this.bones[0].joint.setHinge("global"===a?12:11,b,c,d,e)):p.error("The hinge reference axis must be in the plane of the hinge rotation axis, that is, they must be perpendicular.")},setFreelyRotatingGlobalHingedBasebone:function(a){this.setHingeBaseboneConstraint("global", a,180,180,m.genPerpendicularVectorQuick(a))},setGlobalHingedBasebone:function(a,b,c,d){this.setHingeBaseboneConstraint("global",a,b,c,d)},setFreelyRotatingLocalHingedBasebone:function(a){this.setHingeBaseboneConstraint("local",a,180,180,m.genPerpendicularVectorQuick(a))},setLocalHingedBasebone:function(a,b,c,d){this.setHingeBaseboneConstraint("local",a,b,c,d)},setBaseLocation:function(a){this.baseLocation.copy(a)},setFixedBaseMode:function(a){if(a||-1===this.connectedChainNumber)if(2!==this.baseboneConstraintType|| a)this.fixedBaseMode=a},setMaxIterationAttempts:function(a){1>a||(this.maxIteration=a)},setMinIterationChange:function(a){0>a||(this.minIterationChange=a)},setSolveDistanceThreshold:function(a){0>a||(this.solveDistanceThreshold=a)},solveForEmbeddedTarget:function(){if(this.useEmbeddedTarget)return this.solveForTarget(this.embeddedTarget)},resetTarget:function(){this.lastBaseLocation=new f(Infinity,Infinity,Infinity);this.currentSolveDistance=Infinity},solveForTarget:function(a){this.tmpTarget.set(a.x, a.y,a.z);a=this.precision;var b=this.lastBaseLocation.approximatelyEquals(this.baseLocation,a);if(this.lastTargetLocation.approximatelyEquals(this.tmpTarget,a)&&b)return this.currentSolveDistance;a=null;b?(b=this.bones[this.numBones-1].end.distanceTo(this.tmpTarget),a=this.cloneBones()):b=Infinity;for(var c=[],d=Infinity,e=Infinity,g,k=this.maxIteration;k--;){g=this.solveIK(this.tmpTarget);if(gthis.numChains||c>this.chains[b].numBones)){a=a.clone();void 0!==f&&a.setColor(f);a.setBoneConnectionPoint("end"===d?21:20);a.setConnectedChainNumber(b);a.setConnectedBoneNumber(c);b="end"===d?this.chains[b].bones[c].end:this.chains[b].bones[c].start;a.setBaseLocation(b);a.setFixedBaseMode(!0);for(c=0;c< a.numBones;c++)a.bones[c].start.add(b),a.bones[c].end.add(b);this.add(a,e,g)}},addChainMeshs:function(a,b){this.isWithMesh=!0;b=[];for(var c=a.bones.length,d=0;da?0:a;return 180a||(this.maxIteration=a)},setMinIterationChange:function(a){0>a||(this.minIterationChange=a)},setSolveDistanceThreshold:function(a){0>a||(this.solveDistanceThreshold=a)},solveForEmbeddedTarget:function(){if(this.useEmbeddedTarget)return this.solveForTarget(this.embeddedTarget)}, resetTarget:function(){this.lastBaseLocation=new l(Infinity,Infinity);this.currentSolveDistance=Infinity},solveForTarget:function(a){this.tmpTarget.set(a.x,a.y);a=this.precision;var b=this.lastBaseLocation.approximatelyEquals(this.baseLocation,a);if(this.lastTargetLocation.approximatelyEquals(this.tmpTarget,a)&&b)return this.currentSolveDistance;a=null;b?(b=this.bones[this.numBones-1].end.distanceTo(this.tmpTarget),a=this.cloneBones()):b=Infinity;for(var c=[],d=Infinity,e=Infinity,g,f=this.maxIteration;f--;){g= this.solveIK(this.tmpTarget);if(gthis.numChains)p.error("Chain not existe !");else if(c>this.chains[b].numBones)p.error("Bone not existe !");else{a=a.clone();a.setBoneConnectionPoint("end"===d?21:20);a.setConnectedChainNumber(b);a.setConnectedBoneNumber(c);b="end"===d?this.chains[b].bones[c].end:this.chains[b].bones[c].start;a.setBaseLocation(b);a.setFixedBaseMode(!0);for(c=0;c