suzanne
Version:
3D Software Renderer
2 lines • 32.4 kB
JavaScript
!function(t,s){"object"==typeof exports&&"object"==typeof module?module.exports=s():"function"==typeof define&&define.amd?define([],s):"object"==typeof exports?exports.suz=s():t.suz=s()}("undefined"!=typeof window?window:this,function(){return function(t){var s={};function i(e){if(s[e])return s[e].exports;var h=s[e]={i:e,l:!1,exports:{}};return t[e].call(h.exports,h,h.exports,i),h.l=!0,h.exports}return i.m=t,i.c=s,i.d=function(t,s,e){i.o(t,s)||Object.defineProperty(t,s,{enumerable:!0,get:e})},i.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},i.t=function(t,s){if(1&s&&(t=i(t)),8&s)return t;if(4&s&&"object"==typeof t&&t&&t.__esModule)return t;var e=Object.create(null);if(i.r(e),Object.defineProperty(e,"default",{enumerable:!0,value:t}),2&s&&"string"!=typeof t)for(var h in t)i.d(e,h,function(s){return t[s]}.bind(null,h));return e},i.n=function(t){var s=t&&t.__esModule?function(){return t.default}:function(){return t};return i.d(s,"a",s),s},i.o=function(t,s){return Object.prototype.hasOwnProperty.call(t,s)},i.p="",i(i.s=14)}([function(t,s,i){"use strict";Object.defineProperty(s,"__esModule",{value:!0});const e=i(2);class h{constructor(t=0,s=0,i=0,e=1){this.x=t,this.y=s,this.z=i,this.w=e,this.dims=4}add(t){return new h(this.x+t.x,this.y+t.y,this.z+t.z,this.w+t.w)}sub(t){return new h(this.x-t.x,this.y-t.y,this.z-t.z,this.w-t.w)}mul(t){return new h(this.x*t.x,this.y*t.y,this.z*t.z,this.w*t.w)}times(t){return new h(this.x*t,this.y*t,this.z*t,this.w*t)}div(t){return new h(this.x/t.x,this.y/t.y,this.z/t.z,this.w/t.w)}dot(t){return this.x*t.x+this.y*t.y+this.z*t.z+this.w*t.w}mag(){return Math.sqrt(Math.pow(this.x,2)+Math.pow(this.y,2)+Math.pow(this.z,2)+Math.pow(this.w,2))}normalize(){return this.times(1/this.mag())}reflect(t){return t.times(2*this.dot(t)).sub(this)}addTimes(t,s){return new h(this.x+t.x*s,this.y+t.y*s,this.z+t.z*s,this.w+t.w*s)}equals(t,s=e.EPS){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}get r(){return this.x}set r(t){this.x=t}get g(){return this.y}set g(t){this.y=t}get b(){return this.z}set b(t){this.z=t}get a(){return this.w}set a(t){this.w=t}}s.Vec4=h},function(t,s,i){"use strict";Object.defineProperty(s,"__esModule",{value:!0}),s.degrees=(t=>57.29577951308232*t%360),s.radians=(t=>.017453292519943295*t%6.283185307179586),s.clamp=((t,s,i)=>Math.max(Math.min(t,i),s)),s.parallelogramArea=function(t,s,i){return(s.x-t.x)*(i.y-t.y)-(i.x-t.x)*(s.y-t.y)},s.interpolableAdd=function(t,s){return"number"==typeof t?t+s:t.add(s)},s.interpolableTimes=function(t,s){return"number"==typeof t?t*s:t.times(s)},s.interpolableMul=function(t,s){return"number"==typeof t?t*s:t.mul(s)},s.interpolableAddTimes=function(t,s,i){return"number"==typeof t?t+s*i:t.addTimes(s,i)},s.fillSequence=function(t,s){t.set(s);const i=t.length;let e=s.length;for(;e<i;)t.copyWithin(e,0,e),e<<=1;return t}},function(t,s,i){"use strict";Object.defineProperty(s,"__esModule",{value:!0});const e=i(4);function h(t,s){if("string"!=typeof s)return;const i=t[s];if(i instanceof Function)return i;const h=s.split("");if(s.length>1&&s.length<5){const s=h.map(s=>t[s]);return 1===s.length?s[0]:e.vec(...s)}return i}s.EPS=Math.pow(10,-6),s.swizzle=function(t,s){return void 0!==s?h(t,s):new Proxy(t,{get:h})}},function(t,s,i){"use strict";Object.defineProperty(s,"__esModule",{value:!0});const e=i(9),h=i(1),r=i(0);s.Bitmap=class{constructor(t,s){this._width=t,this._height=s,this._data=new Uint8ClampedArray(this._width*this._height*4)}clear(t=e.colors.black){h.fillSequence(this.data,[255*t.r,255*t.g,255*t.b,255*t.a])}_set(t,s,i){const e=4*(t+s*this._width);this._data[e]=255*i.r,this._data[e+1]=255*i.g,this._data[e+2]=255*i.b,this._data[e+3]=255*i.a}get width(){return this._width}get height(){return this._height}resize(t,s){this._width=t,this._height=s,this._data=new Uint8ClampedArray(this._width*this._height*4)}get data(){return this._data}read(t,s){const i=4*(t+s*this._width);return new r.Vec4(this._data[i]/255,this._data[i+1]/255,this._data[i+2]/255,this._data[i+3]/255)}}},function(t,s,i){"use strict";Object.defineProperty(s,"__esModule",{value:!0});const e=i(5),h=i(6),r=i(0),_=i(10),n=i(7);s.vec2=function(...t){switch(t.length){case 0:return new e.Vec2(0,0);case 1:return"number"==typeof t[0]?new e.Vec2(t[0],t[0]):new e.Vec2(t[0].x,t[0].y);case 2:default:return new e.Vec2(t[0],t[1])}},s.vec3=function(...t){switch(t.length){case 0:return new h.Vec3(0,0,0);case 1:if("number"==typeof t[0])return new h.Vec3(t[0],t[0],t[0]);switch(t[0].dims){case 2:return new r.Vec4(t[0].x,t[0].y,0);case 3:case 4:default:return new h.Vec3(t[0].x,t[0].y,t[0].z)}case 2:return new h.Vec3(t[0],t[1],0);default:return new h.Vec3(t[0],t[1],t[2])}},s.vec4=function(...t){switch(t.length){case 0:return new r.Vec4(0,0,0,0);case 1:if("number"==typeof t[0])return new r.Vec4(t[0],t[0],t[0],t[0]);switch(t[0].dims){case 2:return new r.Vec4(t[0].x,t[0].y,t[0].x,t[0].y);case 3:return new r.Vec4(t[0].x,t[0].y,t[0].z,1);case 4:return new r.Vec4(t[0].x,t[0].y,t[0].x,t[0].y);default:if(t[0]instanceof _.VecN){const[s,i,e,h]=t[0].values;return new r.Vec4(s,i,e,h)}throw new Error(`Unexpected vec4 argument: ${t[0]}`)}case 2:if(t[0]instanceof h.Vec3){const s=t[0];return new r.Vec4(s.x,s.y,s.z,t[1])}{const[s,i]=t;return new r.Vec4(s,i,s,i)}case 3:return new r.Vec4(t[0],t[1],t[2],1);default:return new r.Vec4(t[0],t[1],t[2],t[3])}},s.vecn=((...t)=>new _.VecN(t)),s.vec=function(...t){switch(t.length){case 2:return new e.Vec2(t[0],t[1]);case 3:return new h.Vec3(t[0],t[1],t[2]);case 4:return new r.Vec4(t[0],t[1],t[2],t[3]);default:return new _.VecN(t)}},s.mat4=function(...t){if(0===t.length)return new n.Matrix4;if("number"==typeof t[0]){if(1===t.length)return n.Matrix4.scale(t[0]);if(16===t.length)return new n.Matrix4(new Float32Array(t))}if(t[0]instanceof Float32Array)return new n.Matrix4(t[0]);if(t[0]instanceof r.Vec4&&4===t.length){const[s,i,e,h]=t;return new n.Matrix4(new Float32Array([s.x,s.y,s.z,s.w,i.x,i.y,i.z,i.w,e.x,e.y,e.z,e.w,h.x,h.y,h.z,h.w]))}throw new Error(`Unexpected arguments for mat4: ${t}`)}},function(t,s,i){"use strict";Object.defineProperty(s,"__esModule",{value:!0});const e=i(2);class h{constructor(t=0,s=0){this.x=t,this.y=s,this.dims=2}add(t){return new h(this.x+t.x,this.y+t.y)}sub(t){return new h(this.x-t.x,this.y-t.y)}mul(t){return new h(this.x*t.x,this.y*t.y)}times(t){return new h(this.x*t,this.y*t)}div(t){return new h(this.x/t.x,this.y/t.y)}dot(t){return this.x*t.x+this.y*t.y}mag(){return Math.sqrt(Math.pow(this.x,2)+Math.pow(this.y,2))}normalize(){return this.times(1/this.mag())}reflect(t){return t.times(2*this.dot(t)).sub(this)}addTimes(t,s){return new h(this.x+t.x*s,this.y+t.y*s)}equals(t,s=e.EPS){return Math.abs(this.x-t.x)<s&&Math.abs(this.y-t.y)<s}}s.Vec2=h},function(t,s,i){"use strict";Object.defineProperty(s,"__esModule",{value:!0});const e=i(2);class h{constructor(t=0,s=0,i=0){this.x=t,this.y=s,this.z=i,this.dims=3}add(t){return new h(this.x+t.x,this.y+t.y,this.z+t.z)}sub(t){return new h(this.x-t.x,this.y-t.y,this.z-t.z)}mul(t){return new h(this.x*t.x,this.y*t.y,this.z*t.z)}times(t){return new h(this.x*t,this.y*t,this.z*t)}div(t){return new h(this.x/t.x,this.y/t.y,this.z/t.z)}dot(t){return this.x*t.x+this.y*t.y+this.z*t.z}mag(){return Math.sqrt(Math.pow(this.x,2)+Math.pow(this.y,2)+Math.pow(this.z,2))}normalize(){return this.times(1/this.mag())}reflect(t){return t.times(2*this.dot(t)).sub(this)}addTimes(t,s){return new h(this.x+t.x*s,this.y+t.y*s,this.z+t.z*s)}equals(t,s=e.EPS){return Math.abs(this.x-t.x)<s&&Math.abs(this.y-t.y)<s&&Math.abs(this.z-t.z)<s}get r(){return this.x}set r(t){this.x=t}get g(){return this.y}set g(t){this.y=t}get b(){return this.z}set b(t){this.z=t}}s.Vec3=h},function(t,s,i){"use strict";Object.defineProperty(s,"__esModule",{value:!0});const e=i(0);class h{constructor(t){if(void 0!==t){if(16!==t.length)throw new Error(`Matrix4 constructor requires a Float32Array with 16 elements, got ${t.length}`);this._m=t}else this._m=new Float32Array(16),this.identity()}identity(){return this._m.fill(0),this._m[0]=1,this._m[5]=1,this._m[10]=1,this._m[15]=1,this}fill(t){this._m.fill(t)}mul(t){const s=new h,i=t._m;return s._m[0]=this._m[0]*i[0]+this._m[1]*i[4]+this._m[2]*i[8]+this._m[3]*i[12],s._m[1]=this._m[0]*i[1]+this._m[1]*i[5]+this._m[2]*i[9]+this._m[3]*i[13],s._m[2]=this._m[0]*i[2]+this._m[1]*i[6]+this._m[2]*i[10]+this._m[3]*i[14],s._m[3]=this._m[0]*i[3]+this._m[1]*i[7]+this._m[2]*i[11]+this._m[3]*i[15],s._m[4]=this._m[4]*i[0]+this._m[5]*i[4]+this._m[6]*i[8]+this._m[7]*i[12],s._m[5]=this._m[4]*i[1]+this._m[5]*i[5]+this._m[6]*i[9]+this._m[7]*i[13],s._m[6]=this._m[4]*i[2]+this._m[5]*i[6]+this._m[6]*i[10]+this._m[7]*i[14],s._m[7]=this._m[4]*i[3]+this._m[5]*i[7]+this._m[6]*i[11]+this._m[7]*i[15],s._m[8]=this._m[8]*i[0]+this._m[9]*i[4]+this._m[10]*i[8]+this._m[11]*i[12],s._m[9]=this._m[8]*i[1]+this._m[9]*i[5]+this._m[10]*i[9]+this._m[11]*i[13],s._m[10]=this._m[8]*i[2]+this._m[9]*i[6]+this._m[10]*i[10]+this._m[11]*i[14],s._m[11]=this._m[8]*i[3]+this._m[9]*i[7]+this._m[10]*i[11]+this._m[11]*i[15],s._m[12]=this._m[12]*i[0]+this._m[13]*i[4]+this._m[14]*i[8]+this._m[15]*i[12],s._m[13]=this._m[12]*i[1]+this._m[13]*i[5]+this._m[14]*i[9]+this._m[15]*i[13],s._m[14]=this._m[12]*i[2]+this._m[13]*i[6]+this._m[14]*i[10]+this._m[15]*i[14],s._m[15]=this._m[12]*i[3]+this._m[13]*i[7]+this._m[14]*i[11]+this._m[15]*i[15],s}translate(t){const s=new Float32Array(this._m);return s[3]=this._m[3]+this._m[0]*t.x+this._m[1]*t.y+this._m[2]*t.z,s[7]=this._m[7]+this._m[4]*t.x+this._m[5]*t.y+this._m[6]*t.z,s[11]=this._m[11]+this._m[8]*t.x+this._m[9]*t.y+this._m[10]*t.z,s[15]=this._m[15]+this._m[12]*t.x+this._m[13]*t.y+this._m[14]*t.z,this._m=s,this}rotateX(t){const s=new Float32Array(16),i=Math.cos(t),e=Math.sin(t);return s[0]=this._m[0],s[1]=this._m[1]*i+this._m[2]*e,s[2]=this._m[2]*i-this._m[1]*e,s[3]=this._m[3],s[4]=this._m[4],s[5]=this._m[5]*i+this._m[6]*e,s[6]=this._m[6]*i-this._m[5]*e,s[7]=this._m[7],s[8]=this._m[8],s[9]=this._m[9]*i+this._m[10]*e,s[10]=this._m[10]*i-this._m[9]*e,s[11]=this._m[11],s[12]=this._m[12],s[13]=this._m[13]*i+this._m[14]*e,s[14]=this._m[14]*i-this._m[13]*e,s[15]=this._m[15],this._m=s,this}rotateY(t){const s=new Float32Array(16),i=Math.cos(t),e=Math.sin(t);return s[0]=this._m[0]*i-this._m[2]*e,s[1]=this._m[1],s[2]=this._m[2]*i+this._m[0]*e,s[3]=this._m[3],s[4]=this._m[4]*i-this._m[6]*e,s[5]=this._m[5],s[6]=this._m[6]*i+this._m[4]*e,s[7]=this._m[7],s[8]=this._m[8]*i-this._m[10]*e,s[9]=this._m[9],s[10]=this._m[10]*i+this._m[8]*e,s[11]=this._m[11],s[12]=this._m[12]*i-this._m[14]*e,s[13]=this._m[13],s[14]=this._m[14]*i+this._m[12]*e,s[15]=this._m[15],this._m=s,this}rotateZ(t){const s=new Float32Array(16),i=Math.cos(t),e=Math.sin(t);return s[0]=this._m[0]*i+this._m[1]*e,s[1]=this._m[1]*i-this._m[0]*e,s[2]=this._m[2],s[3]=this._m[3],s[4]=this._m[4]*i+this._m[5]*e,s[5]=this._m[5]*i-this._m[4]*e,s[6]=this._m[6],s[7]=this._m[7],s[8]=this._m[8]*i+this._m[9]*e,s[9]=this._m[9]*i-this._m[8]*e,s[10]=this._m[10],s[11]=this._m[11],s[12]=this._m[12]*i+this._m[13]*e,s[13]=this._m[13]*i-this._m[12]*e,s[14]=this._m[14],s[15]=this._m[15],this._m=s,this}transform(t){const s=t instanceof e.Vec4?t.w:1;return new e.Vec4(this._m[0]*t.x+this._m[1]*t.y+this._m[2]*t.z+this._m[3]*s,this._m[4]*t.x+this._m[5]*t.y+this._m[6]*t.z+this._m[7]*s,this._m[8]*t.x+this._m[9]*t.y+this._m[10]*t.z+this._m[11]*s,this._m[12]*t.x+this._m[13]*t.y+this._m[14]*t.z+this._m[15]*s)}det(){return this._m[1]*this._m[11]*this._m[14]*this._m[4]-this._m[1]*this._m[10]*this._m[15]*this._m[4]-this._m[11]*this._m[13]*this._m[2]*this._m[4]+this._m[10]*this._m[13]*this._m[3]*this._m[4]-this._m[0]*this._m[11]*this._m[14]*this._m[5]+this._m[0]*this._m[10]*this._m[15]*this._m[5]+this._m[11]*this._m[12]*this._m[2]*this._m[5]-this._m[10]*this._m[12]*this._m[3]*this._m[5]-this._m[1]*this._m[11]*this._m[12]*this._m[6]+this._m[0]*this._m[11]*this._m[13]*this._m[6]+this._m[1]*this._m[10]*this._m[12]*this._m[7]-this._m[0]*this._m[10]*this._m[13]*this._m[7]-this._m[15]*this._m[2]*this._m[5]*this._m[8]+this._m[14]*this._m[3]*this._m[5]*this._m[8]+this._m[1]*this._m[15]*this._m[6]*this._m[8]-this._m[13]*this._m[3]*this._m[6]*this._m[8]-this._m[1]*this._m[14]*this._m[7]*this._m[8]+this._m[13]*this._m[2]*this._m[7]*this._m[8]+this._m[15]*this._m[2]*this._m[4]*this._m[9]-this._m[14]*this._m[3]*this._m[4]*this._m[9]-this._m[0]*this._m[15]*this._m[6]*this._m[9]+this._m[12]*this._m[3]*this._m[6]*this._m[9]+this._m[0]*this._m[14]*this._m[7]*this._m[9]-this._m[12]*this._m[2]*this._m[7]*this._m[9]}invert(){const t=this.det(),s=new h;return s._m[0]=(-this._m[11]*this._m[14]*this._m[5]+this._m[10]*this._m[15]*this._m[5]+this._m[11]*this._m[13]*this._m[6]-this._m[10]*this._m[13]*this._m[7]-this._m[15]*this._m[6]*this._m[9]+this._m[14]*this._m[7]*this._m[9])/t,s._m[1]=(this._m[1]*this._m[11]*this._m[14]-this._m[1]*this._m[10]*this._m[15]-this._m[11]*this._m[13]*this._m[2]+this._m[10]*this._m[13]*this._m[3]+this._m[15]*this._m[2]*this._m[9]-this._m[14]*this._m[3]*this._m[9])/t,s._m[2]=(-this._m[15]*this._m[2]*this._m[5]+this._m[14]*this._m[3]*this._m[5]+this._m[1]*this._m[15]*this._m[6]-this._m[13]*this._m[3]*this._m[6]-this._m[1]*this._m[14]*this._m[7]+this._m[13]*this._m[2]*this._m[7])/t,s._m[3]=(this._m[11]*this._m[2]*this._m[5]-this._m[10]*this._m[3]*this._m[5]-this._m[1]*this._m[11]*this._m[6]+this._m[1]*this._m[10]*this._m[7]+this._m[3]*this._m[6]*this._m[9]-this._m[2]*this._m[7]*this._m[9])/t,s._m[4]=(this._m[11]*this._m[14]*this._m[4]-this._m[10]*this._m[15]*this._m[4]-this._m[11]*this._m[12]*this._m[6]+this._m[10]*this._m[12]*this._m[7]+this._m[15]*this._m[6]*this._m[8]-this._m[14]*this._m[7]*this._m[8])/t,s._m[5]=(-this._m[0]*this._m[11]*this._m[14]+this._m[0]*this._m[10]*this._m[15]+this._m[11]*this._m[12]*this._m[2]-this._m[10]*this._m[12]*this._m[3]-this._m[15]*this._m[2]*this._m[8]+this._m[14]*this._m[3]*this._m[8])/t,s._m[6]=(this._m[15]*this._m[2]*this._m[4]-this._m[14]*this._m[3]*this._m[4]-this._m[0]*this._m[15]*this._m[6]+this._m[12]*this._m[3]*this._m[6]+this._m[0]*this._m[14]*this._m[7]-this._m[12]*this._m[2]*this._m[7])/t,s._m[7]=(-this._m[11]*this._m[2]*this._m[4]+this._m[10]*this._m[3]*this._m[4]+this._m[0]*this._m[11]*this._m[6]-this._m[0]*this._m[10]*this._m[7]-this._m[3]*this._m[6]*this._m[8]+this._m[2]*this._m[7]*this._m[8])/t,s._m[8]=(-this._m[11]*this._m[13]*this._m[4]+this._m[11]*this._m[12]*this._m[5]-this._m[15]*this._m[5]*this._m[8]+this._m[13]*this._m[7]*this._m[8]+this._m[15]*this._m[4]*this._m[9]-this._m[12]*this._m[7]*this._m[9])/t,s._m[9]=(-this._m[1]*this._m[11]*this._m[12]+this._m[0]*this._m[11]*this._m[13]+this._m[1]*this._m[15]*this._m[8]-this._m[13]*this._m[3]*this._m[8]-this._m[0]*this._m[15]*this._m[9]+this._m[12]*this._m[3]*this._m[9])/t,s._m[10]=(-this._m[1]*this._m[15]*this._m[4]+this._m[13]*this._m[3]*this._m[4]+this._m[0]*this._m[15]*this._m[5]-this._m[12]*this._m[3]*this._m[5]+this._m[1]*this._m[12]*this._m[7]-this._m[0]*this._m[13]*this._m[7])/t,s._m[11]=(this._m[1]*this._m[11]*this._m[4]-this._m[0]*this._m[11]*this._m[5]+this._m[3]*this._m[5]*this._m[8]-this._m[1]*this._m[7]*this._m[8]-this._m[3]*this._m[4]*this._m[9]+this._m[0]*this._m[7]*this._m[9])/t,s._m[12]=(this._m[10]*this._m[13]*this._m[4]-this._m[10]*this._m[12]*this._m[5]+this._m[14]*this._m[5]*this._m[8]-this._m[13]*this._m[6]*this._m[8]-this._m[14]*this._m[4]*this._m[9]+this._m[12]*this._m[6]*this._m[9])/t,s._m[13]=(this._m[1]*this._m[10]*this._m[12]-this._m[0]*this._m[10]*this._m[13]-this._m[1]*this._m[14]*this._m[8]+this._m[13]*this._m[2]*this._m[8]+this._m[0]*this._m[14]*this._m[9]-this._m[12]*this._m[2]*this._m[9])/t,s._m[14]=(this._m[1]*this._m[14]*this._m[4]-this._m[13]*this._m[2]*this._m[4]-this._m[0]*this._m[14]*this._m[5]+this._m[12]*this._m[2]*this._m[5]-this._m[1]*this._m[12]*this._m[6]+this._m[0]*this._m[13]*this._m[6])/t,s._m[15]=(-this._m[1]*this._m[10]*this._m[4]+this._m[0]*this._m[10]*this._m[5]-this._m[2]*this._m[5]*this._m[8]+this._m[1]*this._m[6]*this._m[8]+this._m[2]*this._m[4]*this._m[9]-this._m[0]*this._m[6]*this._m[9])/t,s}static scale(...t){let s,i,e;1===t.length&&("number"==typeof t[0]?(i=s=t[0],e=s):(s=t[0].x,i=t[0].y,e=t[0].z));const r=new h;return r._m[0]=s,r._m[5]=i,r._m[10]=e,r._m[15]=1,r}static perspective(t,s,i,e){const r=new h,_=Math.tan(t/2),n=i-e;return r._m[0]=1/(_*s),r._m[5]=1/_,r._m[10]=(-i-e)/n,r._m[11]=2*e*i/n,r._m[14]=1,r._m[15]=0,r}static screenSpace(t,s){const i=new h,e=t/2,r=s/2;return i._m[0]=e,i._m[3]=e,i._m[5]=r,i._m[7]=r,i}static translation(t){const s=new h;return s._m[3]=t.x,s._m[7]=t.y,s._m[11]=t.z,s}}s.Matrix4=h},function(t,s,i){"use strict";Object.defineProperty(s,"__esModule",{value:!0});const e=i(1);s.VaryingInterpolator=class{constructor(t,...s){this._varyings=new Map,this._vertices=s,this._VAO=t}stepX(t){return t.current=e.interpolableAdd(t.current,t.x_step)}get varyings(){return this._varyings}get VAO(){return this._VAO}}},function(t,s,i){"use strict";Object.defineProperty(s,"__esModule",{value:!0});const e=i(0);function h(t){return new e.Vec4((t>>16&255)/255,(t>>8&255)/255,(255&t)/255,1)}s.hexToColor=h,s.colors={transparent:new e.Vec4(0,0,0,0),black:h(0),white:h(16777215),red:h(16711680),green:h(65280),blue:h(255),yellow:h(16776960),cyan:h(65535),pink:h(16711935),orange:h(16747520),rand:()=>h(Math.floor(16777216*Math.random())),css:void 0},"undefined"!=typeof window&&(s.colors.css=function(t=100){const s=document.createElement("canvas");s.width=1,s.height=1;const i=s.getContext("2d"),h=new Map;return s=>{if(h.has(s))return h.get(s);i.fillStyle=s,i.fillRect(0,0,1,1),i.fill();const[r,_,n,m]=i.getImageData(0,0,1,1).data,a=new e.Vec4(r/255,_/255,n/255,m/255);return h.size===t&&h.delete(h.keys().next().value),h.set(s,a),a}}())},function(t,s,i){"use strict";Object.defineProperty(s,"__esModule",{value:!0});const e=i(2);class h{constructor(t){if(t.length<4)throw new Error(`Cannot construct a VecN with less than 4 elements, got : ${t}`);this._values=t instanceof Float32Array?t:new Float32Array(t),this._dims=t.length}add(t){const s=new h(this._values);for(let i=0;i<this._dims;i++)s._values[i]+=t._values[i];return s}sub(t){const s=new h(this._values);for(let i=0;i<this._dims;i++)s._values[i]-=t._values[i];return s}mul(t){const s=new h(this._values);for(let i=0;i<this._dims;i++)s._values[i]*=t._values[i];return s}times(t){const s=new h(this._values);for(let i=0;i<this._dims;i++)s._values[i]*=t;return s}div(t){const s=new h(this._values);for(let i=0;i<this._dims;i++)s._values[i]/=t._values[i];return s}dot(t){let s=0;for(let i=0;i<this._dims;i++)s+=this._values[i]*t._values[i];return s}mag(){let t=0;for(let s=0;s<this._dims;s++)t+=Math.pow(this._values[s],2);return Math.sqrt(t)}normalize(){return this.times(1/this.mag())}reflect(t){return t.times(2*this.dot(t)).sub(this)}addTimes(t,s){const i=new h(this._values);for(let e=0;e<this._dims;e++)i._values[e]=i._values[e]+t._values[e]*s;return i}equals(t,s=e.EPS){if(this._dims!==t._dims)return!1;for(let i=0;i<this._dims;i++)if(Math.abs(this._values[i]-t._values[i])>s)return!1;return!0}get dims(){return this._dims}get values(){return this._values}set values(t){this._values=t,this._dims=t.length}get r(){return this._values[0]}get x(){return this._values[0]}set r(t){this._values[0]=t}set x(t){this._values[0]=t}get g(){return this._values[1]}get y(){return this._values[1]}set g(t){this._values[1]=t}set y(t){this._values[1]=t}get b(){return this._values[2]}get z(){return this._values[2]}set b(t){this._values[2]=t}set z(t){this._values[2]=t}get a(){return this._values[3]}get w(){return this._values[3]}set a(t){this._values[3]=t}set w(t){this._values[3]=t}}s.VecN=h},function(t,s,i){"use strict";Object.defineProperty(s,"__esModule",{value:!0});const e=i(1);s.Edge=class{constructor(t,s,i){this._y_start=Math.ceil(t.ndc.y),this._y_end=Math.ceil(s.ndc.y),this._interpolator=i,this._varyings=new Map,this._varying_names=[...i.varyings.keys()],this._from=t,this._to=s;const h=this._y_start-t.ndc.y;this._x_step=(t.ndc.x-s.ndc.x)/(t.ndc.y-s.ndc.y),this._x=t.ndc.x+h*this._x_step;const r=this._x-t.ndc.x;for(let s=0;s<this._varying_names.length;s++){const _=this._varying_names[s],n=i.varyings.get(_);this._varyings.set(_,{current:e.interpolableAddTimes(e.interpolableAddTimes(i.VAO.varyings.get(_)[t.index],n.x_step,r),n.y_step,h),step:e.interpolableAddTimes(n.y_step,n.x_step,this._x_step)})}}step(){this._x+=this._x_step;for(let t=0;t<this._varying_names.length;t++){const s=this._varying_names[t],i=this._varyings.get(s);i.current=e.interpolableAdd(i.current,i.step)}}get x(){return this._x}get y_start(){return this._y_start}get y_end(){return this._y_end}get from(){return this._from}get to(){return this._to}get varyings(){return this._varyings}get gradient(){return this._interpolator}}},function(t,s,i){"use strict";Object.defineProperty(s,"__esModule",{value:!0});const e=i(8);s.VaryingInterpolator2=class extends e.VaryingInterpolator{constructor(t,s,i){super(i,t,s),this._inv_dx=1/(s.ndc.x-t.ndc.x),this._inv_dy=1/(s.ndc.y-t.ndc.y),this._computeGradients()}_computeGradients(){for(const t in this._vertices[0].varyings){const s=this._VAO.varyings.get(t),i=s[this._vertices[0].index],e=s[this._vertices[1].index];"number"==typeof i?this._varyings.set(t,this._computeFloatGradient(i,e)):this._varyings.set(t,this._computeVecGradient(i,e))}}_computeFloatGradient(t,s){return{x_step:(s-t)*this._inv_dx,y_step:(s-t)*this._inv_dy}}_computeVecGradient(t,s){return{x_step:s.sub(t).times(this._inv_dx),y_step:s.sub(t).times(this._inv_dy)}}}},function(t,s,i){"use strict";Object.defineProperty(s,"__esModule",{value:!0});const e=i(1),h=i(8);s.VaryingInterpolator3=class extends h.VaryingInterpolator{constructor(t,s,i,e){super(e,t,s,i),this._inv_dx=1/((s.ndc.x-i.ndc.x)*(t.ndc.y-i.ndc.y)-(t.ndc.x-i.ndc.x)*(s.ndc.y-i.ndc.y)),this._inv_dy=-this._inv_dx,this._computeGradients()}_computeGradients(){const t=this._vertices[0].ndc.w,s=this._vertices[1].ndc.w,i=this._vertices[2].ndc.w;for(const h of this._VAO.varyingNames){const r=this._VAO.varyings.get(h);"__ndc"!==h&&(r[this._vertices[0].index]=e.interpolableTimes(r[this._vertices[0].index],t),r[this._vertices[1].index]=e.interpolableTimes(r[this._vertices[1].index],s),r[this._vertices[2].index]=e.interpolableTimes(r[this._vertices[2].index],i));let _=r[this._vertices[0].index],n=r[this._vertices[1].index],m=r[this._vertices[2].index];"number"==typeof _?this._varyings.set(h,this._computeFloatGradient(_,n,m)):this._varyings.set(h,this._computeVecGradient(_,n,m))}}_computeFloatGradient(t,s,i){return{x_step:(s-i)*(this._vertices[0].ndc.y-this._vertices[2].ndc.y)-(t-i)*(this._vertices[1].ndc.y-this._vertices[2].ndc.y)*this._inv_dx,y_step:(s-i)*(this._vertices[0].ndc.x-this._vertices[2].ndc.x)-(t-i)*(this._vertices[1].ndc.x-this._vertices[2].ndc.x)*this._inv_dy}}_computeVecGradient(t,s,i){const e=this._vertices[0].ndc.y-this._vertices[2].ndc.y,h=this._vertices[1].ndc.y-this._vertices[2].ndc.y,r=s.sub(i),_=t.sub(i),n=r.times(e).sub(_.times(h)).times(this._inv_dx),m=this._vertices[0].ndc.x-this._vertices[2].ndc.x,a=this._vertices[1].ndc.x-this._vertices[2].ndc.x;return{x_step:n,y_step:r.times(m).sub(_.times(a)).times(this._inv_dy)}}}},function(t,s,i){"use strict";function e(t){for(var i in t)s.hasOwnProperty(i)||(s[i]=t[i])}Object.defineProperty(s,"__esModule",{value:!0}),e(i(3)),e(i(15)),e(i(11)),e(i(16)),e(i(9)),e(i(1)),e(i(7)),e(i(17)),e(i(18)),e(i(5)),e(i(6)),e(i(0)),e(i(10)),e(i(4)),e(i(2)),e(i(8)),e(i(12)),e(i(13)),e(i(19))},function(t,s,i){"use strict";Object.defineProperty(s,"__esModule",{value:!0});const e=i(3),h=i(11),r=i(1),_=i(7),n=i(0),m=i(12),a=i(13);var o;!function(t){t[t.POINTS=0]="POINTS",t[t.LINES=1]="LINES",t[t.TRIANGLES=2]="TRIANGLES"}(o=s.DrawingMode||(s.DrawingMode={}));s.Context=class extends e.Bitmap{constructor(t,s,i,e){super(t,s),this._target=i,this._initOptions(e),this._screen_space_matrix=_.Matrix4.screenSpace(t,s)}initDepthBuffer(){this._options.depth_test&&(this._depth_buffer=new Float32Array(this._width*this._height),this._depth_buffer.fill(1/0))}_initOptions(t={}){this._options=Object.assign({depth_test:!0,backface_culling:!0},t),void 0!==this._target.init&&this._target.init(this._width,this._height),this.initDepthBuffer()}_perspectiveTransform(t){const s=this._program.vertex_shader(t),i=this._screen_space_matrix.transform(s),e=new n.Vec4(i.x/i.w,i.y/i.w,i.z/i.w,1/i.w);return t.varyings.__ndc=e,{ndc:e,varyings:t.varyings,index:t.index}}_shouldDrawFragment(t,s,i){if(this._options.depth_test){const e=t+s*this._width;if(i>this._depth_buffer[e])return!1;this._depth_buffer[e]=i}return!0}_scanLine(t,s,i){t=Math.ceil(t);const e=Math.ceil(s.x),h=Math.ceil(i.x),_=e-s.x;this._initVaryings(s,_),this._current_varyings={};const n=this._current_gradient.varyings.get("__ndc");let m;for(let s=e;s<h;s++){this._current_gradient.stepX(n),m=1/n.current.w;for(const t of this._vertex_array.varyingNames){if("__ndc"===t)continue;const s=this._current_gradient.varyings.get(t);this._current_varyings[t]=r.interpolableTimes(this._current_gradient.stepX(s),m)}this._shouldDrawFragment(s,t,n.current.z)&&this._set(s,t,this._program.fragment_shader(this._current_varyings,this._vertex_array.uniforms))}}_initVaryings(t,s){for(const i of this._vertex_array.varyingNames){const e=this._current_gradient.varyings.get(i);e.current=r.interpolableAdd(t.varyings.get(i).current,r.interpolableTimes(e.x_step,s))}}_strokeEdge(t){const s=t.from,i=t.to,e=Math.ceil(s.ndc.x),h=Math.ceil(s.ndc.y),r=Math.ceil(i.ndc.x),_=Math.ceil(i.ndc.y),n=Math.abs(r-e),m=Math.abs(_-h),a=e<r?1:-1,o=h<_?1:-1;let c=n-m;this._current_gradient=t.gradient;const u=e-s.ndc.x;this._initVaryings(t,u);let d=e,l=h;const y={};for(;d!==r||l!==_;){for(const[t,s]of this._current_gradient.varyings)this._current_gradient.stepX(s),y[t]=s.current;const t=2*c;t>-m&&(c-=m,d+=a),t<n&&(c+=n,l+=o)}}_drawLines(t,s){for(let i=t;i<t+s;i+=2){const t=this._perspectiveTransform(this._vertex_array.at(i)),s=this._perspectiveTransform(this._vertex_array.at(i+1));this._strokeEdge(new h.Edge(t,s,new m.VaryingInterpolator2(t,s,this._vertex_array)))}}_drawLinesIndexed(t,s){const i=this._vertex_array.indices;for(let e=t;e<t+s;e+=2){const t=this._perspectiveTransform(this._vertex_array.at(i[e])),s=this._perspectiveTransform(this._vertex_array.at(i[e+1]));this._strokeEdge(new h.Edge(t,s,new m.VaryingInterpolator2(t,s,this._vertex_array)))}}_drawTriangles(t,s,i){for(let e=t;e<t+s;e+=3){const t=this._perspectiveTransform(this._vertex_array.at(e)),s=this._perspectiveTransform(this._vertex_array.at(e+1)),h=this._perspectiveTransform(this._vertex_array.at(e+2));i?this._strokeTriangle(t,s,h):this._fillTriangle(t,s,h)}}_drawTrianglesIndexed(t,s,i){const e=this._vertex_array.indices;for(let h=t+s-1;h>=0;h-=3){const t=this._perspectiveTransform(this._vertex_array.at(e[h-2])),s=this._perspectiveTransform(this._vertex_array.at(e[h-1])),r=this._perspectiveTransform(this._vertex_array.at(e[h]));i?this._strokeTriangle(t,s,r):this._fillTriangle(t,s,r)}}_strokeTriangle(t,s,i){this._strokeEdge(new h.Edge(t,s,new m.VaryingInterpolator2(t,s,this._vertex_array))),this._strokeEdge(new h.Edge(t,i,new m.VaryingInterpolator2(t,i,this._vertex_array))),this._strokeEdge(new h.Edge(s,i,new m.VaryingInterpolator2(s,i,this._vertex_array)))}_fillTriangle(t,s,i){if(this._options.backface_culling&&r.parallelogramArea(t.ndc,s.ndc,i.ndc)<0)return;let e=!1;i.ndc.y<s.ndc.y&&([i,s]=[s,i],e=!e),s.ndc.y<t.ndc.y&&([s,t]=[t,s],e=!e),i.ndc.y<s.ndc.y&&([i,s]=[s,i],e=!e);const _=new a.VaryingInterpolator3(t,s,i,this._vertex_array);this._current_gradient=_;const n=new h.Edge(t,i,_);this._scanEdges(n,new h.Edge(t,s,_),e),this._scanEdges(n,new h.Edge(s,i,_),e)}_scanEdges(t,s,i){let e=t,h=s;const r=s.y_start,_=s.y_end;i&&([e,h]=[h,e]);for(let t=r;t<_;t++)this._scanLine(t,e,h),e.step(),h.step()}drawArrays(t,s,i,e=!1){switch(t){case o.LINES:this._drawLines(s,i);break;case o.TRIANGLES:this._drawTriangles(s,i,e);break;default:throw new Error(`Drawing mode ${t} not implemented yet`)}this.draw()}drawElements(t,s,i,e=!1){if(!this._vertex_array.indexed)throw new Error("Cannot draw elements: indices not defined in bound vertex array");switch(t){case o.LINES:this._drawLinesIndexed(s,i);break;case o.TRIANGLES:this._drawTrianglesIndexed(s,i,e);break;default:throw new Error(`Drawing mode ${t} not implemented yet`)}this.draw()}useProgram(t){this._program=t}bindVertexArray(t){this._vertex_array=t}_clearDepthBuffer(){this._options.depth_test&&this._depth_buffer.fill(1/0)}clearBuffers(){this._clearDepthBuffer()}draw(){this._target.draw(this)}get options(){return this._options}resize(t,s){super.resize(t,s),this.initDepthBuffer(),this._screen_space_matrix=_.Matrix4.screenSpace(t,s),void 0!==this._target.resize&&this._target.resize(t,s)}}},function(t,s,i){"use strict";Object.defineProperty(s,"__esModule",{value:!0});s.Clock=class{constructor(){this.last_tick=Date.now()}getDelta(){const t=Date.now(),s=t-this.last_tick;return this.last_tick=t,s}}},function(t,s,i){"use strict";Object.defineProperty(s,"__esModule",{value:!0});const e=i(5),h=i(6),r=i(4);!function(t){const s=t=>{const s=t.substr(2).split(" ").map(t=>parseFloat(t));return new h.Vec3(s[0],1-s[1],s[2])},i=t=>{const s=t.substr(3).split(" ").map(t=>parseFloat(t));return new e.Vec2(s[0],s[1])},_=t=>{const s=t.substr(3).split(" ").map(t=>parseFloat(t));return new h.Vec3(s[0],s[1],s[2])};t.parseOBJ=(t=>{const e=t.split("\n"),h=[],n=[],m=[],a=[],o=new Map;let c=0;const u={vertices:[],indices:null,attributes:{uv:[],normal:[]},bounding_box:{min:r.vec3(1/0),max:r.vec3(0)}};for(let t of e)switch((t=t.trim()).charAt(0)){case"v":switch(t.charAt(1)){case" ":const e=s(t);e.x>u.bounding_box.max.x&&(u.bounding_box.max.x=e.x),e.y>u.bounding_box.max.y&&(u.bounding_box.max.y=e.y),e.z>u.bounding_box.max.z&&(u.bounding_box.max.z=e.z),e.x<u.bounding_box.min.x&&(u.bounding_box.min.x=e.x),e.y<u.bounding_box.min.y&&(u.bounding_box.min.y=e.y),e.z<u.bounding_box.min.z&&(u.bounding_box.min.z=e.z),h.push(e);break;case"t":n.push(i(t));break;case"n":m.push(_(t))}break;case"f":const e=t.substr(2).split(" ");let r=[];for(const t of e){if(o.has(t)){r.push(o.get(t));continue}const[s,i,e]=t.split("/").map(t=>parseInt(t)-1);u.vertices.push(h[s]),void 0!==i&&u.attributes.uv.push(n[i]),void 0!==e&&u.attributes.normal.push(m[e]),o.set(t,c),r.push(c),c++}if(4===e.length){const t=r[3];r[3]=r[2],r[4]=t,r[5]=r[0]}a.push(...r)}return u.indices=c<256?new Uint8Array(a):c<65536?new Uint16Array(a):new Uint32Array(a),u})}(s.ModelLoader||(s.ModelLoader={}))},function(t,s,i){"use strict";Object.defineProperty(s,"__esModule",{value:!0});const e=i(3);s.Texture=class extends e.Bitmap{constructor(t,s){super(t,s)}at(t){return super.read(Math.floor(t.x*this._width),Math.floor(t.y*this._height))}}},function(t,s,i){"use strict";Object.defineProperty(s,"__esModule",{value:!0});class e{constructor(t){this._vertices=t,this._attributes=new Map,this._varyings=new Map,this._varying_names=[],this._uniforms={},this._current_idx=0,this._attribute_accessor=new Proxy({},{get:(t,s)=>{if("string"==typeof s&&"inspect"!==s){if(this._attributes.has(s))return this._attributes.get(s)[this._current_idx];throw new Error(`Undefined attribute: ${s}`)}}}),this._varying_setter=new Proxy({},{set:(t,s,i)=>{const e=this._varyings.get(s);if(void 0===e){const t=[];t[this._current_idx]=i,this._varyings.set(s,t),this._varying_names.push(s)}else e[this._current_idx]=i;return t[s]=i,!0}})}at(t){return this._current_idx=t,{position:this._vertices[t],attributes:this._attribute_accessor,uniforms:this._uniforms,varyings:this._varying_setter,index:t}}addAttribute(t,s){if(s.length<this._vertices.length)throw new Error(`Not enough data for attribute ${t}`);this._attributes.set(t,s)}setUniform(t,s){this._uniforms[t]=s}indexed(){return void 0!==this._indices}get indices(){return this._indices}set indices(t){this._indices=t}get varyings(){return this._varyings}get varyingNames(){return this._varying_names}get uniforms(){return this._uniforms}static fromModel(t){const s=new e(t.vertices);void 0!==t.indices&&(s.indices=t.indices);for(const i in t.attributes){const e=t.attributes[i];void 0!==e&&s.addAttribute(i,e)}return s}get vertexCount(){return this._vertices.length}}s.VertexArray=e}])});
//# sourceMappingURL=suzanne.js.map