2020-08-01 21:51:54 +02:00
|
|
|
class VTUtils{static random(t,e){let a=Math.random();if(void 0===t)return a;if(void 0===e)return t instanceof Array?t[Math.floor(a*t.length)]:a*t;if(t>e){let a=t;t=e,e=a}return a*(e-t)+t}static randomInt(t,e){return Math.floor(VTUtils.random(t,e))}static normalize(t,e,a){return(t-a)/(e-a)}static distance(t,e,a,i){let s=t-a,r=e-i;return Math.sqrt(s*s+r*r)}static map(t,e,a,i,s,r){let l=(t-e)/(a-e)*(s-i)+i;return r?i<s?this.constrain(l,i,s):this.constrain(l,s,i):l}static constrain(t,e,a){return Math.max(Math.min(t,a),e)}static hsvToRgb(t,e,a){var i,s,r,l=Math.floor(6*t),n=6*t-l,o=a*(1-e),h=a*(1-n*e),d=a*(1-(1-n)*e);switch(l%6){case 0:i=a,s=d,r=o;break;case 1:i=h,s=a,r=o;break;case 2:i=o,s=a,r=d;break;case 3:i=o,s=h,r=a;break;case 4:i=d,s=o,r=a;break;case 5:i=a,s=o,r=h}return{r:i,g:s,b:r}}static peakRGB(t){return{r:t,g:1-t,b:0}}}class VTVector{constructor(t,e,a){this.x=t||0,this.y=e||0,this.z=a||0}static createRandom(t,e,a){return t=t||1,e=e||1,a=a||0,new VTVector(VTUtils.random(-t,t),VTUtils.random(-e,e),VTUtils.random(-a,a))}mult(t){this.x*=t,this.y*=t,this.z*=t}set(t){this.x=t.x,this.y=t.y,this.z=t.z}add(t){this.x=this.x+t.x,this.y=this.y+t.y,this.z=this.z+t.z}addXYZ(t,e,a){this.x+=t,this.y+=e,this.z+=a}setXYZ(t,e,a){this.x=t||0,this.y=e||0,this.z=a||0}clone(){return new VTVector(this.x,this.y,this.z)}}class TDUtils{static multiply(t,e){let a=e[0],i=e[1],s=e[2],r=e[3],l=e[4],n=e[5],o=e[6],h=e[7],d=e[8],c=e[9],u=e[10],g=e[11],p=e[12],f=e[13],y=e[14],m=e[15],S=t[0],w=t[1],x=t[2],T=t[3],v=t[4],A=t[5],P=t[6],E=t[7],L=t[8],M=t[9],R=t[10],F=t[11],D=t[12],H=t[13],I=t[14],b=t[15],U=[];return U[0]=a*S+i*v+s*L+r*D,U[1]=a*w+i*A+s*M+r*H,U[2]=a*x+i*P+s*R+r*I,U[3]=a*T+i*E+s*F+r*b,U[4]=l*S+n*v+o*L+h*D,U[5]=l*w+n*A+o*M+h*H,U[6]=l*x+n*P+o*R+h*I,U[7]=l*T+n*E+o*F+h*b,U[8]=d*S+c*v+u*L+g*D,U[9]=d*w+c*A+u*M+g*H,U[10]=d*x+c*P+u*R+g*I,U[11]=d*T+c*E+u*F+g*b,U[12]=p*S+f*v+y*L+m*D,U[13]=p*w+f*A+y*M+m*H,U[14]=p*x+f*P+y*R+m*I,U[15]=p*T+f*E+y*F+m*b,U}static xRotation(t){let e=Math.cos(t),a=Math.sin(t);return[1,0,0,0,0,e,a,0,0,-a,e,0,0,0,0,1]}static yRotation(t){let e=Math.cos(t),a=Math.sin(t);return[e,0,-a,0,0,1,0,0,a,0,e,0,0,0,0,1]}static zRotation(t){let e=Math.cos(t),a=Math.sin(t);return[e,a,0,0,-a,e,0,0,0,0,1,0,0,0,0,1]}static degToRad(t){return t*Math.PI/180}}function $(t,e){return $$(t,e)[0]}function $$(t,e){return(e=e||document).querySelectorAll(t)}function create(t,e){let a=document.createElement(t);return e&&(a.innerHTML=e),a}function append(t,e){for(let a in e)t.appendChild(e[a])}Node.prototype.addDelegatedEventListener=function(t,e,a){this.addEventListener(t,t=>{let i=t.target;if(i.matches(e))a(t,i);else{let s=i.closest(e);s&&a(t,s)}})},Node.prototype.hasClass=function(t){return this.classList.contains(t)},Node.prototype.addClass=function(t){return this.classList.add(t)},Node.prototype.removeClass=function(t){return this.classList.remove(t)};class Template{constructor(){this.tpl={}}async loadTemplate(t){let e=this;this.tpl[t]||await fetch(templateDir+t+".tpl").then(t=>t.text()).then(a=>{e.tpl[t]=a})}async loadArray(t){for(let e of t)await this.loadTemplate(e)}parseTemplate(t,e){if(!this.tpl[t])return"";let a,i=this.tpl[t];for(;null!==(a=templateEx.exec(i));){a.index===templateEx.lastIndex&&templateEx.lastIndex++;let t=a[0];i=i.replace(t,e[a[1]]||"")}return i}parseFromAPI(t,e,a){fetch(t).then(t=>t.json()).then(t=>{a(this.parseTemplate(e,t))}).catch(console.error)}}const templateEx=/\$(.*?)\$/gm,templateDir="out/tpl/";class ShaderHandler{constructor(t){this.gl=t,this.shaderNames=[],this.shaders={},this.programs={}}setGL(t){this.gl=t}async loadShader(t,e){this.shaderNames.push(t),await this.load(t,e+t+".vert",this.gl.VERTEX_SHADER),await this.load(t,e+t+".frag",this.gl.FRAGMENT_SHADER)}async load(t,e,a){let i=t+"_"+a;if(!this.shaders[i]){let t=await fetch(e),s=this.createShader(await t.text(),a);s&&(this.shaders[i]=s)}return!!this.shaders[i]}getShader(t,e){let a=t+"_"+e;return this.shaders[a]}getAllShaders(){return this.shaderNames}async createProgramForEach(t){t=t||this.shaderNames;for(let e=0;e<t.length;e++){let a=t[e];if(!await shaderHandl
|