UNPKG

@animech-public/playcanvas

Version:
2 lines (1 loc) 4.14 kB
import{math as t}from"./math.js";import{Vec3 as s}from"./vec3.js";var h;class i{constructor(t=0,s=0,h=0,i=1){this.x=void 0,this.y=void 0,this.z=void 0,this.w=void 0,4===t.length?(this.x=t[0],this.y=t[1],this.z=t[2],this.w=t[3]):(this.x=t,this.y=s,this.z=h,this.w=i)}clone(){return new(0,this.constructor)(this.x,this.y,this.z,this.w)}conjugate(t=this){return this.x=-1*t.x,this.y=-1*t.y,this.z=-1*t.z,this.w=t.w,this}copy(t){return this.x=t.x,this.y=t.y,this.z=t.z,this.w=t.w,this}equals(t){return this.x===t.x&&this.y===t.y&&this.z===t.z&&this.w===t.w}equalsApprox(t,s=1e-6){return Math.abs(this.x-t.x)<s&&Math.abs(this.y-t.y)<s&&Math.abs(this.z-t.z)<s&&Math.abs(this.w-t.w)<s}getAxisAngle(s){let h=2*Math.acos(this.w);const i=Math.sin(h/2);return 0!==i?(s.x=this.x/i,s.y=this.y/i,s.z=this.z/i,(s.x<0||s.y<0||s.z<0)&&(s.x*=-1,s.y*=-1,s.z*=-1,h*=-1)):(s.x=1,s.y=0,s.z=0),h*t.RAD_TO_DEG}getEulerAngles(h=new s){let i,e,r;const n=this.x,a=this.y,z=this.z,x=this.w,y=2*(x*a-n*z);return y<=-.99999?(i=2*Math.atan2(n,x),e=-Math.PI/2,r=0):y>=.99999?(i=2*Math.atan2(n,x),e=Math.PI/2,r=0):(i=Math.atan2(2*(x*n+a*z),1-2*(n*n+a*a)),e=Math.asin(y),r=Math.atan2(2*(x*z+n*a),1-2*(a*a+z*z))),h.set(i,e,r).mulScalar(t.RAD_TO_DEG)}invert(t=this){return this.conjugate(t).normalize()}length(){return Math.sqrt(this.x*this.x+this.y*this.y+this.z*this.z+this.w*this.w)}lengthSq(){return this.x*this.x+this.y*this.y+this.z*this.z+this.w*this.w}mul(t){const s=this.x,h=this.y,i=this.z,e=this.w,r=t.x,n=t.y,a=t.z,z=t.w;return this.x=e*r+s*z+h*a-i*n,this.y=e*n+h*z+i*r-s*a,this.z=e*a+i*z+s*n-h*r,this.w=e*z-s*r-h*n-i*a,this}mulScalar(t,s=this){return this.x=s.x*t,this.y=s.y*t,this.z=s.z*t,this.w=s.w*t,this}mul2(t,s){const h=t.x,i=t.y,e=t.z,r=t.w,n=s.x,a=s.y,z=s.z,x=s.w;return this.x=r*n+h*x+i*z-e*a,this.y=r*a+i*x+e*n-h*z,this.z=r*z+e*x+h*a-i*n,this.w=r*x-h*n-i*a-e*z,this}normalize(t=this){let s=t.length();return 0===s?(this.x=this.y=this.z=0,this.w=1):(s=1/s,this.x=t.x*s,this.y=t.y*s,this.z=t.z*s,this.w=t.w*s),this}set(t,s,h,i){return this.x=t,this.y=s,this.z=h,this.w=i,this}setFromAxisAngle(s,h){h*=.5*t.DEG_TO_RAD;const i=Math.sin(h),e=Math.cos(h);return this.x=i*s.x,this.y=i*s.y,this.z=i*s.z,this.w=e,this}setFromEulerAngles(h,i,e){if(h instanceof s){const t=h;h=t.x,i=t.y,e=t.z}const r=.5*t.DEG_TO_RAD;h*=r,i*=r,e*=r;const n=Math.sin(h),a=Math.cos(h),z=Math.sin(i),x=Math.cos(i),y=Math.sin(e),o=Math.cos(e);return this.x=n*x*o-a*z*y,this.y=a*z*o+n*x*y,this.z=a*x*y-n*z*o,this.w=a*x*o+n*z*y,this}setFromMat4(t){const s=t.data;let h=s[0],i=s[1],e=s[2],r=s[4],n=s[5],a=s[6],z=s[8],x=s[9],y=s[10];let o;return h*(n*y-a*x)-i*(r*y-a*z)+e*(r*x-n*z)<0&&(h=-h,i=-i,e=-e),o=h*h+i*i+e*e,0===o?this.set(0,0,0,1):(o=1/Math.sqrt(o),h*=o,i*=o,e*=o,o=r*r+n*n+a*a,0===o?this.set(0,0,0,1):(o=1/Math.sqrt(o),r*=o,n*=o,a*=o,o=z*z+x*x+y*y,0===o?this.set(0,0,0,1):(o=1/Math.sqrt(o),z*=o,x*=o,y*=o,y<0?h>n?this.set(1+h-n-y,i+r,z+e,a-x):this.set(i+r,1-h+n-y,a+x,z-e):h<-n?this.set(z+e,a+x,1-h-n+y,i-r):this.set(a-x,z-e,i-r,1+h+n+y),this.mulScalar(1/this.length()))))}setFromDirections(t,s){const h=1+t.dot(s);return h<Number.EPSILON?Math.abs(t.x)>Math.abs(t.y)?(this.x=-t.z,this.y=0,this.z=t.x,this.w=0):(this.x=0,this.y=-t.z,this.z=t.y,this.w=0):(this.x=t.y*s.z-t.z*s.y,this.y=t.z*s.x-t.x*s.z,this.z=t.x*s.y-t.y*s.x,this.w=h),this.normalize()}slerp(t,s,h){const i=t.x,e=t.y,r=t.z,n=t.w;let a=s.x,z=s.y,x=s.z,y=s.w,o=n*y+i*a+e*z+r*x;if(o<0&&(y=-y,a=-a,z=-z,x=-x,o=-o),Math.abs(o)>=1)return this.w=n,this.x=i,this.y=e,this.z=r,this;const w=Math.acos(o),c=Math.sqrt(1-o*o);if(Math.abs(c)<.001)return this.w=.5*n+.5*y,this.x=.5*i+.5*a,this.y=.5*e+.5*z,this.z=.5*r+.5*x,this;const u=Math.sin((1-h)*w)/c,M=Math.sin(h*w)/c;return this.w=n*u+y*M,this.x=i*u+a*M,this.y=e*u+z*M,this.z=r*u+x*M,this}transformVector(t,h=new s){const i=t.x,e=t.y,r=t.z,n=this.x,a=this.y,z=this.z,x=this.w,y=x*i+a*r-z*e,o=x*e+z*i-n*r,w=x*r+n*e-a*i,c=-n*i-a*e-z*r;return h.x=y*x+c*-n+o*-z-w*-a,h.y=o*x+c*-a+w*-n-y*-z,h.z=w*x+c*-z+y*-a-o*-n,h}toString(){return`[${this.x}, ${this.y}, ${this.z}, ${this.w}]`}}h=i,i.IDENTITY=Object.freeze(new h(0,0,0,1)),i.ZERO=Object.freeze(new h(0,0,0,0));export{i as Quat};