@animech-public/playcanvas
Version:
PlayCanvas WebGL game engine
2 lines (1 loc) • 8.27 kB
JavaScript
import{math as t}from"./math.js";import{Vec2 as s}from"./vec2.js";import{Vec3 as a}from"./vec3.js";import{Vec4 as e}from"./vec4.js";var n;const r=new s,i=new a,h=new a,o=new a,d=new a;class c{constructor(){this.data=new Float32Array(16),this.data[0]=this.data[5]=this.data[10]=this.data[15]=1}static _getPerspectiveHalfSize(t,s,a,e,n){n?(t.x=e*Math.tan(s*Math.PI/360),t.y=t.x/a):(t.y=e*Math.tan(s*Math.PI/360),t.x=t.y*a)}add2(t,s){const a=t.data,e=s.data,n=this.data;return n[0]=a[0]+e[0],n[1]=a[1]+e[1],n[2]=a[2]+e[2],n[3]=a[3]+e[3],n[4]=a[4]+e[4],n[5]=a[5]+e[5],n[6]=a[6]+e[6],n[7]=a[7]+e[7],n[8]=a[8]+e[8],n[9]=a[9]+e[9],n[10]=a[10]+e[10],n[11]=a[11]+e[11],n[12]=a[12]+e[12],n[13]=a[13]+e[13],n[14]=a[14]+e[14],n[15]=a[15]+e[15],this}add(t){return this.add2(this,t)}clone(){return(new(0,this.constructor)).copy(this)}copy(t){const s=t.data,a=this.data;return a[0]=s[0],a[1]=s[1],a[2]=s[2],a[3]=s[3],a[4]=s[4],a[5]=s[5],a[6]=s[6],a[7]=s[7],a[8]=s[8],a[9]=s[9],a[10]=s[10],a[11]=s[11],a[12]=s[12],a[13]=s[13],a[14]=s[14],a[15]=s[15],this}equals(t){const s=this.data,a=t.data;return s[0]===a[0]&&s[1]===a[1]&&s[2]===a[2]&&s[3]===a[3]&&s[4]===a[4]&&s[5]===a[5]&&s[6]===a[6]&&s[7]===a[7]&&s[8]===a[8]&&s[9]===a[9]&&s[10]===a[10]&&s[11]===a[11]&&s[12]===a[12]&&s[13]===a[13]&&s[14]===a[14]&&s[15]===a[15]}isIdentity(){const t=this.data;return 1===t[0]&&0===t[1]&&0===t[2]&&0===t[3]&&0===t[4]&&1===t[5]&&0===t[6]&&0===t[7]&&0===t[8]&&0===t[9]&&1===t[10]&&0===t[11]&&0===t[12]&&0===t[13]&&0===t[14]&&1===t[15]}mul2(t,s){const a=t.data,e=s.data,n=this.data,r=a[0],i=a[1],h=a[2],o=a[3],d=a[4],c=a[5],u=a[6],l=a[7],y=a[8],g=a[9],m=a[10],x=a[11],z=a[12],w=a[13],M=a[14],f=a[15];let p,A,_,D;return p=e[0],A=e[1],_=e[2],D=e[3],n[0]=r*p+d*A+y*_+z*D,n[1]=i*p+c*A+g*_+w*D,n[2]=h*p+u*A+m*_+M*D,n[3]=o*p+l*A+x*_+f*D,p=e[4],A=e[5],_=e[6],D=e[7],n[4]=r*p+d*A+y*_+z*D,n[5]=i*p+c*A+g*_+w*D,n[6]=h*p+u*A+m*_+M*D,n[7]=o*p+l*A+x*_+f*D,p=e[8],A=e[9],_=e[10],D=e[11],n[8]=r*p+d*A+y*_+z*D,n[9]=i*p+c*A+g*_+w*D,n[10]=h*p+u*A+m*_+M*D,n[11]=o*p+l*A+x*_+f*D,p=e[12],A=e[13],_=e[14],D=e[15],n[12]=r*p+d*A+y*_+z*D,n[13]=i*p+c*A+g*_+w*D,n[14]=h*p+u*A+m*_+M*D,n[15]=o*p+l*A+x*_+f*D,this}mulAffine2(t,s){const a=t.data,e=s.data,n=this.data,r=a[0],i=a[1],h=a[2],o=a[4],d=a[5],c=a[6],u=a[8],l=a[9],y=a[10],g=a[12],m=a[13],x=a[14];let z,w,M;return z=e[0],w=e[1],M=e[2],n[0]=r*z+o*w+u*M,n[1]=i*z+d*w+l*M,n[2]=h*z+c*w+y*M,n[3]=0,z=e[4],w=e[5],M=e[6],n[4]=r*z+o*w+u*M,n[5]=i*z+d*w+l*M,n[6]=h*z+c*w+y*M,n[7]=0,z=e[8],w=e[9],M=e[10],n[8]=r*z+o*w+u*M,n[9]=i*z+d*w+l*M,n[10]=h*z+c*w+y*M,n[11]=0,z=e[12],w=e[13],M=e[14],n[12]=r*z+o*w+u*M+g,n[13]=i*z+d*w+l*M+m,n[14]=h*z+c*w+y*M+x,n[15]=1,this}mul(t){return this.mul2(this,t)}transformPoint(t,s=new a){const e=this.data,{x:n,y:r,z:i}=t;return s.x=n*e[0]+r*e[4]+i*e[8]+e[12],s.y=n*e[1]+r*e[5]+i*e[9]+e[13],s.z=n*e[2]+r*e[6]+i*e[10]+e[14],s}transformVector(t,s=new a){const e=this.data,{x:n,y:r,z:i}=t;return s.x=n*e[0]+r*e[4]+i*e[8],s.y=n*e[1]+r*e[5]+i*e[9],s.z=n*e[2]+r*e[6]+i*e[10],s}transformVec4(t,s=new e){const a=this.data,{x:n,y:r,z:i,w:h}=t;return s.x=n*a[0]+r*a[4]+i*a[8]+h*a[12],s.y=n*a[1]+r*a[5]+i*a[9]+h*a[13],s.z=n*a[2]+r*a[6]+i*a[10]+h*a[14],s.w=n*a[3]+r*a[7]+i*a[11]+h*a[15],s}setLookAt(t,s,a){o.sub2(t,s).normalize(),h.copy(a).normalize(),i.cross(h,o).normalize(),h.cross(o,i);const e=this.data;return e[0]=i.x,e[1]=i.y,e[2]=i.z,e[3]=0,e[4]=h.x,e[5]=h.y,e[6]=h.z,e[7]=0,e[8]=o.x,e[9]=o.y,e[10]=o.z,e[11]=0,e[12]=t.x,e[13]=t.y,e[14]=t.z,e[15]=1,this}setFrustum(t,s,a,e,n,r){const i=2*n,h=s-t,o=e-a,d=r-n,c=this.data;return c[0]=i/h,c[1]=0,c[2]=0,c[3]=0,c[4]=0,c[5]=i/o,c[6]=0,c[7]=0,c[8]=(s+t)/h,c[9]=(e+a)/o,c[10]=(-r-n)/d,c[11]=-1,c[12]=0,c[13]=0,c[14]=-i*r/d,c[15]=0,this}setPerspective(t,s,a,e,n){return c._getPerspectiveHalfSize(r,t,s,a,n),this.setFrustum(-r.x,r.x,-r.y,r.y,a,e)}setOrtho(t,s,a,e,n,r){const i=this.data;return i[0]=2/(s-t),i[1]=0,i[2]=0,i[3]=0,i[4]=0,i[5]=2/(e-a),i[6]=0,i[7]=0,i[8]=0,i[9]=0,i[10]=-2/(r-n),i[11]=0,i[12]=-(s+t)/(s-t),i[13]=-(e+a)/(e-a),i[14]=-(r+n)/(r-n),i[15]=1,this}setFromAxisAngle(s,a){a*=t.DEG_TO_RAD;const{x:e,y:n,z:r}=s,i=Math.cos(a),h=Math.sin(a),o=1-i,d=o*e,c=o*n,u=this.data;return u[0]=d*e+i,u[1]=d*n+h*r,u[2]=d*r-h*n,u[3]=0,u[4]=d*n-h*r,u[5]=c*n+i,u[6]=c*r+h*e,u[7]=0,u[8]=d*r+h*n,u[9]=c*r-e*h,u[10]=o*r*r+i,u[11]=0,u[12]=0,u[13]=0,u[14]=0,u[15]=1,this}setTranslate(t,s,a){const e=this.data;return e[0]=1,e[1]=0,e[2]=0,e[3]=0,e[4]=0,e[5]=1,e[6]=0,e[7]=0,e[8]=0,e[9]=0,e[10]=1,e[11]=0,e[12]=t,e[13]=s,e[14]=a,e[15]=1,this}setScale(t,s,a){const e=this.data;return e[0]=t,e[1]=0,e[2]=0,e[3]=0,e[4]=0,e[5]=s,e[6]=0,e[7]=0,e[8]=0,e[9]=0,e[10]=a,e[11]=0,e[12]=0,e[13]=0,e[14]=0,e[15]=1,this}setViewport(t,s,a,e){const n=this.data;return n[0]=.5*a,n[1]=0,n[2]=0,n[3]=0,n[4]=0,n[5]=.5*e,n[6]=0,n[7]=0,n[8]=0,n[9]=0,n[10]=.5,n[11]=0,n[12]=t+.5*a,n[13]=s+.5*e,n[14]=.5,n[15]=1,this}setReflection(t,s){const a=t.x,e=t.y,n=t.z,r=this.data;return r[0]=1-2*a*a,r[1]=-2*a*e,r[2]=-2*a*n,r[3]=0,r[4]=-2*a*e,r[5]=1-2*e*e,r[6]=-2*e*n,r[7]=0,r[8]=-2*a*n,r[9]=-2*e*n,r[10]=1-2*n*n,r[11]=0,r[12]=-2*a*s,r[13]=-2*e*s,r[14]=-2*n*s,r[15]=1,this}invert(t=this){const s=t.data,a=s[0],e=s[1],n=s[2],r=s[3],i=s[4],h=s[5],o=s[6],d=s[7],c=s[8],u=s[9],l=s[10],y=s[11],g=s[12],m=s[13],x=s[14],z=s[15],w=a*h-e*i,M=a*o-n*i,f=a*d-r*i,p=e*o-n*h,A=e*d-r*h,_=n*d-r*o,D=c*m-u*g,T=c*x-l*g,E=c*z-y*g,O=u*x-l*m,S=u*z-y*m,v=l*z-y*x,I=w*v-M*S+f*O+p*E-A*T+_*D;if(0===I)this.setIdentity();else{const t=1/I,s=this.data;s[0]=(h*v-o*S+d*O)*t,s[1]=(-e*v+n*S-r*O)*t,s[2]=(m*_-x*A+z*p)*t,s[3]=(-u*_+l*A-y*p)*t,s[4]=(-i*v+o*E-d*T)*t,s[5]=(a*v-n*E+r*T)*t,s[6]=(-g*_+x*f-z*M)*t,s[7]=(c*_-l*f+y*M)*t,s[8]=(i*S-h*E+d*D)*t,s[9]=(-a*S+e*E-r*D)*t,s[10]=(g*A-m*f+z*w)*t,s[11]=(-c*A+u*f-y*w)*t,s[12]=(-i*O+h*T-o*D)*t,s[13]=(a*O-e*T+n*D)*t,s[14]=(-g*p+m*M-x*w)*t,s[15]=(c*p-u*M+l*w)*t}return this}set(t){const s=this.data;return s[0]=t[0],s[1]=t[1],s[2]=t[2],s[3]=t[3],s[4]=t[4],s[5]=t[5],s[6]=t[6],s[7]=t[7],s[8]=t[8],s[9]=t[9],s[10]=t[10],s[11]=t[11],s[12]=t[12],s[13]=t[13],s[14]=t[14],s[15]=t[15],this}setIdentity(){const t=this.data;return t[0]=1,t[1]=0,t[2]=0,t[3]=0,t[4]=0,t[5]=1,t[6]=0,t[7]=0,t[8]=0,t[9]=0,t[10]=1,t[11]=0,t[12]=0,t[13]=0,t[14]=0,t[15]=1,this}setTRS(t,s,a){const e=s.x,n=s.y,r=s.z,i=s.w,h=a.x,o=a.y,d=a.z,c=e+e,u=n+n,l=r+r,y=e*c,g=e*u,m=e*l,x=n*u,z=n*l,w=r*l,M=i*c,f=i*u,p=i*l,A=this.data;return A[0]=(1-(x+w))*h,A[1]=(g+p)*h,A[2]=(m-f)*h,A[3]=0,A[4]=(g-p)*o,A[5]=(1-(y+w))*o,A[6]=(z+M)*o,A[7]=0,A[8]=(m+f)*d,A[9]=(z-M)*d,A[10]=(1-(y+x))*d,A[11]=0,A[12]=t.x,A[13]=t.y,A[14]=t.z,A[15]=1,this}transpose(t=this){const s=t.data,a=this.data;if(s===a){let t;t=s[1],a[1]=s[4],a[4]=t,t=s[2],a[2]=s[8],a[8]=t,t=s[3],a[3]=s[12],a[12]=t,t=s[6],a[6]=s[9],a[9]=t,t=s[7],a[7]=s[13],a[13]=t,t=s[11],a[11]=s[14],a[14]=t}else a[0]=s[0],a[1]=s[4],a[2]=s[8],a[3]=s[12],a[4]=s[1],a[5]=s[5],a[6]=s[9],a[7]=s[13],a[8]=s[2],a[9]=s[6],a[10]=s[10],a[11]=s[14],a[12]=s[3],a[13]=s[7],a[14]=s[11],a[15]=s[15];return this}getTranslation(t=new a){return t.set(this.data[12],this.data[13],this.data[14])}getX(t=new a){return t.set(this.data[0],this.data[1],this.data[2])}getY(t=new a){return t.set(this.data[4],this.data[5],this.data[6])}getZ(t=new a){return t.set(this.data[8],this.data[9],this.data[10])}getScale(t=new a){return this.getX(i),this.getY(h),this.getZ(o),t.set(i.length(),h.length(),o.length()),t}get scaleSign(){return this.getX(i),this.getY(h),this.getZ(o),i.cross(i,h),i.dot(o)<0?-1:1}setFromEulerAngles(s,a,e){s*=t.DEG_TO_RAD,a*=t.DEG_TO_RAD,e*=t.DEG_TO_RAD;const n=Math.sin(-s),r=Math.cos(-s),i=Math.sin(-a),h=Math.cos(-a),o=Math.sin(-e),d=Math.cos(-e),c=this.data;return c[0]=h*d,c[1]=-h*o,c[2]=i,c[3]=0,c[4]=r*o+d*n*i,c[5]=r*d-n*i*o,c[6]=-h*n,c[7]=0,c[8]=n*o-r*d*i,c[9]=d*n+r*i*o,c[10]=r*h,c[11]=0,c[12]=0,c[13]=0,c[14]=0,c[15]=1,this}getEulerAngles(s=new a){this.getScale(d);const e=d.x,n=d.y,r=d.z;if(0===e||0===n||0===r)return s.set(0,0,0);const i=this.data,h=Math.asin(-i[2]/e),o=.5*Math.PI;let c,u;return h<o?h>-o?(c=Math.atan2(i[6]/n,i[10]/r),u=Math.atan2(i[1]/e,i[0]/e)):(u=0,c=-Math.atan2(i[4]/n,i[5]/n)):(u=0,c=Math.atan2(i[4]/n,i[5]/n)),s.set(c,h,u).mulScalar(t.RAD_TO_DEG)}toString(){return`[${this.data.join(", ")}]`}}n=c,c.IDENTITY=Object.freeze(new n),c.ZERO=Object.freeze((new n).set([0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]));export{c as Mat4};