-
Notifications
You must be signed in to change notification settings - Fork 0
/
gmatrix.min.js
1 lines (1 loc) · 15.4 KB
/
gmatrix.min.js
1
export class v2{constructor(x=0,y=0){this._x=x;this._y=y;this.isVector2=!0;return this} static create(x=0,y=0){return new this(x,y)} static of(x,y){return new this(x,y)} static add(a,b){return new this(a.x+b.x,a.y+b.y)} static sub(a,b){return new this(a.x-b.x,a.y-b.y)} static mult(a,b){return new this(a.x*b.x,a.y*b.y)} static div(a,b){return new this(a.x/b.x,a.y/b.y)} static equals(a,b){return a.x===b.x&&a.y===b.y} static distance(a,b){return Math.sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y))} static distanceSquared(a,b){return(a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)} static cross(a,b){return a.x*b.y-a.y*b.x} static inv(a){return new this(a.y,a.x)} static set(a){return new this(a.x,a.y)} static clone(a){return new this(a.x,a.y)} static get zero(){return new this(0,0)} get x(){return this._x} get y(){return this._y} set x(n){return this._x=n} set y(n){return this._y=n} get re(){return this._x} get im(){return this._y} set re(n){return this._x=n} set im(n){return this._y=n} get length(){return Math.sqrt(this._x*this._x+this._y*this._y)} get dot(){return this._x*this._x+this._y*this._y} get negative(){return new this(-this._x,-this._y)} add(v){this._x+=v.x;this._y+=v.y;return this} sub(v){this._x-=v.x;this._y-=v.y;return this} mult(v){this._x*=v.x;this._y*=v.y;return this} div(v){this._x/=v.x;this._y/=v.y;return this} equals(v){return a.x===b.x&&a.y===b.y} set(v){this._x=v.x;this._y=v.y;return this} negate(){this._x=-this._x;this._y=-this._y;return this} inv(v){this._x=v.y;this._y=v.x;return this} zero(){this._x=0;this._y=0;return this} divideScalar(s){this._x/=s;this._y/=s;return this} normalize(){return divideScalar(this.length||1)} scale(s){this._x*=s;this._y*=s;return this} limit(l=1){let m=this.dot;if(m>(l*l)){l/=Math.sqrt(m);this.scale(l)} return this} atan2(){return Math.atan2(this._x,this._y)} setLength(m){return this.divideScalar((this.length||1)/m)} toString(){return `[ ${this._x} ${this._y} ]`} copy(a){this._x=a.x;this._y=a.y} clone(){return v2.create(this._x,this._y)}} export class v3{constructor(x=0,y=0,z=0){this._x=x;this._y=y;this._z=z;Object.defineProperties(this,{0:{get(){return this._x},set(n){return this._x=n}},1:{get(){return this._y},set(n){return this._y=n}},2:{get(){return this._z},set(n){return this._z=n}}});this.isVector3=!0;return this} static create(x=0,y=0,z=0){return new this(x,y,z)} static of(x,y,z){return new this(x,y,z)} static add(a,b){return new this(a.x+b.x,a.y+b.y,a.z||0+b.z||0)} static sub(a,b){return new this(a.x-b.x,a.y-b.y,a.z||0-b.z||0)} static mult(a,b){return new this(a.x*b.x,a.y*b.y,a.z||0*b.z||0)} static div(a,b){return new this(a.x/b.x,a.y/b.y,a.z||0/b.z||1)} static equals(a,b){return a.x===b.x&&a.y===b.y&&a.z===b.z} static inv(a){return new this(a.z||0,a.y,a.x)} static normalize(a){return new this(a.x,a.y,a.z).divideScalar(a.length||1)} static set(v){return new this(v.x||v.a||v.r,v.y||v.b||v.g,v.z||v.c||v.b||0)} static distance(a,b){return Math.sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)+(a.z-b.z)*(a.z-b.z))} static distanceSquared(a,b){return(a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)+(a.z-b.z)*(a.z-b.z)} static dot(a){return a.x*a.x+a.y*a.y+squared(a.z||0)} static cross(a,b){return new this(a.y*b.z-a.z*b.y,a.z*b.x-a.x*b.z,a.x*b.y-a.y*b.x)} static clone(a){return new this(a.x,a.y,a.z||0)} static get zero(){return new this(0,0,0)} static get up(){return new this(0,1,0)} static get random(){return new this(rand.float(2)-1,rand.float(2)-1,rand.float(2)-1)} get x(){return this._x} get y(){return this._y} get z(){return this._z} get a(){return this._x} get b(){return this._y} get c(){return this_.z} get r(){return this._x} get g(){return this._y} get b(){return this_.z} set x(n){return this._x=n} set y(n){return this._y=n} set z(n){return this._z=n} set a(n){return this._x=n} set b(n){return this._y=n} set c(n){return this._z=n} set r(n){return this._x=n} set g(n){return this._y=n} set b(n){return this._z=n} set xy(vec2){this._x=vec2.x;this._y=vec2.y;return this} add(v){this._x+=v.x;this._y+=v.y;this._z+=v.z||0;return this} sub(v){this._x-=v.x;this._y-=v.y;this._z-=v.z||0;return this} mult(v){this._x*=v.x;this._y*=v.y;this._z*=v.z||0;return this} div(v){this._x/=v.x;this._y/=v.y;this._z/=v.z||1;return this} equals(v){return this._x===v.x&&this._y===v.y&&this._z===v.z} set(v){this._x=v.x;this._y=v.y;this._z=v.z||0;return this} inv(v){this._x=v.z||0;this._y=v.y;this._z=v.x;return this} zero(){this._x=0;this._y=0;this._z=0;return this} negate(){this._x=-this._x;this._y=-this._y;this._z=-this._z;return this} get negative(){return v3.create(-this._x,-this._y,-this._z)} get length(){return Math.sqrt(this._x*this._x+this._y*this._y+this._z*this._z)} get dot(){return this._x*this._x+this._y*this._y+this._z*this._z} scale(s){this._x*=s;this._y*=s;this._z*=s;return this} divideScalar(s){this._x/=s;this._y/=s;this._z/=s;return this} distanceTo(v){return Math.sqrt(square(this._x-v.x)+square(this._y-v.y)+square(this._z-v.z||0))} normalize(){return this.divideScalar(this.length||1)} cross(v){[this._x,this._y,this._z]=[this._y*v.z-this._z*v.y,this._z*v.x-this._x*v.z,this._x*v.y-this._y*v.x];return this} limit(l=1){let m=this.dot;if(m>l){l/=Math.sqrt(m);scale(l)} return this} setLength(m){return this.divideScalar((this.length||1)/m)} toString(){return `[ ${this._x} ${this._y} ${this._z} ]`} copy(v){this._x=v.x;this._y=v.y;this._z=v.z;return this} clone(){return v3.create().copy(this)}} export class v4{constructor(x=0,y=0,z=0,w=0){this._x=x;this._y=y;this._z=z;this._w=w;this.isVector4=!0;return this} static create(x=0,y=0,z=0,w=0){return new this(x,y,z,w)} static from(a){if(a.isVector4)return new this(a.x,a.y,a.z,a.w);if(a.isVector3)return new this(a.x,a.y,a.z,1);if(a.isArray)return new this(a[0],a[1],a[2],a[3])} static add(a,b){return new this(a.x+b.x,a.y+b.y,a.z+b.z,a.w||0+b.w||0)} static sub(a,b){return new this(a.x-b.x,a.y-b.y,a.z-b.z,a.w||0-b.w||0)} static mult(a,b){return new this(a.x*b.x,a.y*b.y,a.z*b.z,a.w||0*b.w||0)} static div(a,b){return new this(a.x/b.x,a.y/b.y,a.z/b.z,a.w||0/b.w||0)} static equals(a,b){return a.x===b.x&&a.y===b.y&&a.z===b.z&&a.w===b.w} static inv(a){return new this(a.w||0,a.z,a.y,a.x)} static set(v){return new this(v.x||v.a||v.r,v.y||v.b||v.g,v.z||v.c||v.b,v.w||v.d||v.a||0)} static distance(a,b){return Math.sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)+(a.z-b.z)*(a.z-b.z)+(a.w-b.w)*(a.w-b.w))} static distanceSquared(a,b){return(a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)+(a.z-b.z)*(a.z-b.z)+(a.w-b.w)*(a.w-b.w)} static dot(a){return a.x*a.x+a.y*a.y+a.z*a.z+square(a.w||0)} static clone(a){return new this(a.x,a.y,a.z,a.w||0)} static get zero(){return new this(0,0,0,0)} static get random(){return new this(rand.float(2)-1,rand.float(2)-1,rand.float(2)-1,rand.float(2)-1)} get x(){return this._x} get y(){return this._y} get z(){return this._z} get w(){return this._w} get s(){return this._x} get t(){return this._y} get u(){return this._z} get v(){return this._w} get r(){return this._x} get g(){return this._y} get b(){return this_.z} get a(){return this_.w} set x(n){return this._x=n} set y(n){return this._y=n} set z(n){return this._z=n} set w(n){return this._w=n} set s(n){return this._x=n} set t(n){return this._y=n} set u(n){return this._z=n} set v(n){return this._w=n} set r(n){return this._x=n} set g(n){return this._y=n} set b(n){return this_.z=n} set a(n){return this_.w=n} set xyz(vec3){this._x=vec3.x;this._y=vec3.y;this._z=vec3.z;return this} set rgb(vec3){this._x=vec3.r;this._y=vec3.g;this._z=vec3.b;return this} set(vec4){this._x=vec4.x;this._y=vec4.y;this._z=vec4.z;this._w=vec4.w;return this} add(v){this._x+=v.x;this._y+=v.y;this._z+=v.z;this._w+=v.w||0;return this} sub(v){this._x-=v.x;this._y-=v.y;this._z-=v.z;this._w-=v.w||0;return this} mult(v){this._x*=v.x;this._y*=v.y;this._z*=v.z;this._w*=v.w||0;return this} div(v){this._x/=v.x;this._y/=v.y;this._z/=v.z;this._w/=v.w||1;return this} equals(v){return this._x===v.x&&this._y===v.y&&this._z===v.z&&this._w===v.w} set(v){this._x=v.x;this._y=v.y;this._z=v.z;this._w=v.w||0;return this} inv(v){this._x=v.w||0;this._y=v.z;this._z=v.y;this._w=v.x;return this} zero(){this._x=0;this._y=0;this._z=0;this._w=0;return this} negate(){this._x=-this._x;this._y=-this._y;this._z=-this._z;this._w=-this._w;return this} get negative(){return v3.create(-this._x,-this._y,-this._z,-this._w)} get length(){return Math.sqrt(this._x*this._x+this._y*this._y+this._z*this._z+this._w*this._w)} get dot(){return this._x*this._x+this._y*this._y+this._z*this._z+this._w*this._w} get string(){return `[ ${this._x} ${this._y} ${this._z} ${this._w} ]`} scale(s){this._x*=s;this._y*=s;this._z*=s;this._w*=s;return this} divideScalar(s){this._x/=s;this._y/=s;this._z/=s;this._w/=s;return this} distanceTo(v){return Math.sqrt(square(this._x-v.x)+square(this._y-v.y)+square(this._z-v.z)+square(this._w-v.w||0))} normalize(){return this.divideScalar(this.length||1)} multiplyQuaternion(v){[this._x,this._y,this._z,this._w]=[this._x*v.w+this._y*v.z-this._z*v.y+this._w*v.x,-this._x*v.z+this._y*v.w+this._z*v.x+this._w*v.y,this._x*v.y-this._y*v.x+this._z*v.w+this._w*v.z,-this._x*v.x-this._y*v.y-this._z*v.z+this._w*v.w];return this} limit(l=1){let m=this.dot;if(m>l){l/=Math.sqrt(m);this.scale(l)} return this} setLength(m){return this.divideScalar((this.length||1)/m)} toString(){return `[ ${this._x} ${this._y} ${this._z} ${this._w} ]`} copy(v){this._x=v.x;this._y=v.y;this._z=v.z;this._w=v.w||this._w;return this} fromArray(a){this._x=a[0];this._y=a[1];this._z=a[2];this._w=a[3]||this._w;return this} clone(){return v4.create().copy(this)} static slerp(qa,qb,s){let l=s;if(qa.x*qb.x+qa.y*qb.y+qa.z*qb.z+qa.w*qb.w<0) l=-s;this._x=1-s*qa.x+l*qb.x;this._y=1-s*qa.y+l*qb.y;this._z=1-s*qa.z+l*qb.z;this._w=1-s*qa.w+l*qb.w;return this}} export let q4=v4;export class m4{constructor(r=1){this.a=new v4(r,0,0,0);this.b=new v4(0,r,0,0);this.c=new v4(0,0,r,0);this.d=new v4(0,0,0,r);this.isMatrix4=!0;return this} static create(){return new this()} static empty(){return{isMatrix4:!0,__proto__:m4.prototype}} static set(mat4){return new this().copy(mat4)} static translate(vec3={x:0,y:0,z:0}){return new this(1).d.xyz=vec3} static get zero(){return new this(0)} static get identity(){return new this(1)} static multiply(a,b){return new this().set(a).multiply(b)} static multiplyFaster(a,b,c){c.e0=a.e0*b.e0+a.e1*b.e4+a.e2*b.e8+a.e3*b.e12;c.e1=a.e0*b.e1+a.e1*b.e5+a.e2*b.e9+a.e3*b.e13;c.e2=a.e0*b.e2+a.e1*b.e6+a.e2*b.e10+a.e3*b.e14;c.e3=a.e0*b.e3+a.e1*b.e7+a.e2*b.e11+a.e3*b.e15;c.e4=a.e4*b.e0+a.e5*b.e4+a.e6*b.e8+a.e7*b.e12;c.e5=a.e4*b.e1+a.e5*b.e5+a.e6*b.e9+a.e7*b.e13;c.e6=a.e4*b.e2+a.e5*b.e6+a.e6*b.e10+a.e7*b.e14;c.e7=a.e4*b.e3+a.e5*b.e7+a.e6*b.e11+a.e7*b.e15;c.e8=a.e8*b.e0+a.e9*b.e4+a.e10*b.e8+a.e11*b.e12;c.e9=a.e8*b.e1+a.e9*b.e5+a.e10*b.e9+a.e11*b.e13;c.e10=a.e8*b.e2+a.e9*b.e6+a.e10*b.e10+a.e11*b.e14;c.e11=a.e8*b.e3+a.e9*b.e7+a.e10*b.e11+a.e11*b.e15;c.e12=a.e12*b.e0+a.e13*b.e4+a.e14*b.e8+a.e15*b.e12;c.e13=a.e12*b.e1+a.e13*b.e5+a.e14*b.e9+a.e15*b.e13;c.e14=a.e12*b.e2+a.e13*b.e6+a.e14*b.e10+a.e15*b.e14;c.e15=a.e12*b.e3+a.e13*b.e7+a.e14*b.e11+a.e15*b.e15;return c} static lookat(){return this.empty().lookat()} static project(){return this.empty().project()} set x(vec4){if(vec4.isVector4)return this.a.copy(vec4);return this.a.fromArray(vec4)} set y(vec4){if(vec4.isVector4)return this.b.copy(vec4);return this.b.fromArray(vec4)} set z(vec4){if(vec4.isVector4)return this.c.copy(vec4);return this.c.fromArray(vec4)} set w(vec4){if(vec4.isVector4)return this.d.copy(vec4);return this.d.fromArray(vec4)} get x(){return this.a} get y(){return this.b} get z(){return this.c} get w(){return this.d} get e0(){return this.a.x} get e1(){return this.a.y} get e2(){return this.a.z} get e3(){return this.a.w} get e4(){return this.b.x} get e5(){return this.b.y} get e6(){return this.b.z} get e7(){return this.b.w} get e8(){return this.c.x} get e9(){return this.c.y} get e10(){return this.c.z} get e11(){return this.c.w} get e12(){return this.d.x} get e13(){return this.d.y} get e14(){return this.d.z} get e15(){return this.d.w} set e0(n){return this.a.x=n} set e1(n){return this.a.y=n} set e2(n){return this.a.z=n} set e3(n){return this.a.w=n} set e4(n){return this.b.x=n} set e5(n){return this.b.y=n} set e6(n){return this.b.z=n} set e7(n){return this.b.w=n} set e8(n){return this.c.x=n} set e9(n){return this.c.y=n} set e10(n){return this.c.z=n} set e11(n){return this.c.w=n} set e12(n){return this.d.x=n} set e13(n){return this.d.y=n} set e14(n){return this.d.z=n} set e15(n){return this.d.w=n} get string(){return this.a.string+"\n"+this.b.string+"\n"+this.c.string+"\n"+this.d.string} get array(){return[this.a.x,this.a.y,this.a.z,this.a.w,this.b.x,this.b.y,this.b.z,this.b.w,this.c.x,this.c.y,this.c.z,this.c.w,this.d.x,this.d.y,this.d.z,this.d.w]} output(){console.log("%c"+this.a.string+"\n%c"+this.b.string+"\n%c"+this.c.string+"\n%c"+this.d.string,"color:#ff22bb","color:#22ff22","color:#55ffff","color:#dd99ff")} do(f,a){this.a[f](a);this.b[f](a);this.c[f](a);this.d[f](a);return this} copy(m){this.a.copy(m.x);this.b.copy(m.y);this.c.copy(m.z);m.w&&this.d.copy(m.w);return this} copyPosition(mat4){this.d.xyz=mat4.d;return this} set(mat4){this.a.set(mat4.a);this.b.set(mat4.b);this.c.set(mat4.c);this.d.set(mat4.d);return this} add(mat4){this.a.add(mat4.a);this.b.add(mat4.b);this.c.add(mat4.c);this.d.add(mat4.d);return this} sub(mat4){this.a.sub(mat4.a);this.b.sub(mat4.b);this.c.sub(mat4.c);this.d.sub(mat4.d);return this} equals(mat4){return this.a.equals(mat4.a)&&this.b.equals(mat4.b)&&this.c.equals(mat4.c)&&this.d.equals(mat4.d)} scale(x=1,y=1,z=1){[this.a.x,this.b.y,this.c.z]=[x,y,z];return this} transpose(){[this.a.y,this.a.z,this.a.w,this.b.x,this.b.z,this.b.w,this.c.x,this.c.y,this.c.w,this.d.x,this.d.y,this.d.z]=[this.b.x,this.c.x,this.d.x,this.a.y,this.c.y,this.d.y,this.a.z,this.b.z,this.d.z,this.a.w,this.b.w,this.c.w];return this} multiply(b){this.x=[this.e0*b.e0+this.e1*b.e4+this.e2*b.e8+this.e3*b.e12,this.e0*b.e1+this.e1*b.e5+this.e2*b.e9+this.e3*b.e13,this.e0*b.e2+this.e1*b.e6+this.e2*b.e10+this.e3*b.e14,this.e0*b.e3+this.e1*b.e7+this.e2*b.e11+this.e3*b.e15];this.y=[this.e4*b.e0+this.e5*b.e4+this.e6*b.e8+this.e7*b.e12,this.e4*b.e1+this.e5*b.e5+this.e6*b.e9+this.e7*b.e13,this.e4*b.e2+this.e5*b.e6+this.e6*b.e10+this.e7*b.e14,this.e4*b.e3+this.e5*b.e7+this.e6*b.e11+this.e7*b.e15];this.z=[this.e8*b.e0+this.e9*b.e4+this.e10*b.e8+this.e11*b.e12,this.e8*b.e1+this.e9*b.e5+this.e10*b.e9+this.e11*b.e13,this.e8*b.e2+this.e9*b.e6+this.e10*b.e10+this.e11*b.e14,this.e8*b.e3+this.e9*b.e7+this.e10*b.e11+this.e11*b.e15];this.w=[this.e12*b.e0+this.e13*b.e4+this.e14*b.e8+this.e15*b.e12,this.e12*b.e1+this.e13*b.e5+this.e14*b.e9+this.e15*b.e13,this.e12*b.e2+this.e13*b.e6+this.e14*b.e10+this.e15*b.e14,this.e12*b.e3+this.e13*b.e7+this.e14*b.e11+this.e15*b.e15];return this} premultiply(mat4){return m4.multiply(mat4,this)} multiplyScalar(s){this.do("scale",s);return this} divideScalar(s){this.do("divideScalar",s);return this} zero(){this.do("zero");return this} lookat(...params){let x=new v3(),y=new v3(),z=new v3(),zero=v3.zero,def_up=v3.up;return((eye,center=zero,up=def_up)=>{z.set(eye).sub(center).normalize();x.set(up).cross(z);y.set(z).cross(x);this.a=new v4(x.x,x.y,x.z,0);this.b=new v4(y.x,y.y,y.z,0);this.c=new v4(z.x,z.y,z.z,0);this.d=new v4(eye.x,eye.y,eye.z,1);return this})(...params)} project(fov=_half_pi_,aspect=1,near=1,far=1e5){let f=Math.tan(fov*0.5);let rInv=1/(near-far);this.a=new v4(f*aspect,0,0,0);this.b=new v4(0,1/f,0,0);this.c=new v4(0,0,(near+far)*rInv,-1);this.d=new v4(0,0,near*far*rInv*2,0);return this} multVec4(v){return new v4(this.e0*v.x+this.e4*v.y+this.e8*v.z+this.e12*v.w,this.e1*v.x+this.e5*v.y+this.e9*v.z+this.e13*v.w,this.e2*v.x+this.e6*v.y+this.e10*v.z+this.e14*v.w,this.e3*v.x+this.e7*v.y+this.e11*v.z+this.e15*v.w)}}