'use strict';(function(n){function T(){return Array.prototype.slice.call(this)}function N(g,e){if(g.constructor===e)return g;if(g.constructor!==Array)return e=e||Float32Array,new e(g);e=e||Float32Array;for(var a=g[0].length,b=new e(g.length*a),c=0;c<g.length;++c)for(var d=0;d<a;++d)b[c*a+d]=g[c][d];return b}function G(g,e,a,b,c){g=g||1024;h.debug&&console.log("GL.Mesh created");null!==c&&(this.gl=c=c||n.gl);this._context_id=c.context_id;this.vertexBuffers={};this.indexBuffers={};this.info={groups:[]};
this._bounding=BBox.create();this.resize(g)}function y(g,e,a,b){this.gl=b=b||n.gl;this._context_id=b.context_id;if(g&&g.constructor!==Array)throw"FBO textures must be an Array";this.handler=null;this.height=this.width=-1;this.color_textures=[];this.depth_texture=null;this.stencil=!!a;this._stencil_enabled=!1;this._num_binded_textures=0;(g&&g.length||e)&&this.setTextures(g,e);this._old_fbo_handler=null;this._old_viewport=new Float32Array(4)}var h=n.GL={};if("undefined"==typeof glMatrix)throw"litegl.js requires gl-matrix to work. It must be included before litegl.";
if(!n.vec2)throw"litegl.js does not support gl-matrix 3.0, download 2.8 https://github.com/toji/gl-matrix/releases/tag/v2.8.1";n.requestAnimationFrame=n.requestAnimationFrame||n.mozRequestAnimationFrame||n.webkitRequestAnimationFrame||function(g){setTimeout(g,1E3/60)};h.blockable_keys={Up:!0,Down:!0,Left:!0,Right:!0};h.reverse=null;h.LEFT_MOUSE_BUTTON=0;h.MIDDLE_MOUSE_BUTTON=1;h.RIGHT_MOUSE_BUTTON=2;h.LEFT_MOUSE_BUTTON_MASK=1;h.RIGHT_MOUSE_BUTTON_MASK=2;h.MIDDLE_MOUSE_BUTTON_MASK=4;h.last_context_id=
0;h.COLOR_BUFFER_BIT=16384;h.DEPTH_BUFFER_BIT=256;h.STENCIL_BUFFER_BIT=1024;h.TEXTURE_2D=3553;h.TEXTURE_CUBE_MAP=34067;h.TEXTURE_3D=32879;h.TEXTURE_MAG_FILTER=10240;h.TEXTURE_MIN_FILTER=10241;h.TEXTURE_WRAP_S=10242;h.TEXTURE_WRAP_T=10243;h.BYTE=5120;h.UNSIGNED_BYTE=5121;h.SHORT=5122;h.UNSIGNED_SHORT=5123;h.INT=5124;h.UNSIGNED_INT=5125;h.FLOAT=5126;h.HALF_FLOAT_OES=36193;h.HALF_FLOAT=5131;h.DEPTH_COMPONENT16=33189;h.DEPTH_COMPONENT24=33190;h.DEPTH_COMPONENT32F=36012;h.FLOAT_VEC2=35664;h.FLOAT_VEC3=
35665;h.FLOAT_VEC4=35666;h.INT_VEC2=35667;h.INT_VEC3=35668;h.INT_VEC4=35669;h.BOOL=35670;h.BOOL_VEC2=35671;h.BOOL_VEC3=35672;h.BOOL_VEC4=35673;h.FLOAT_MAT2=35674;h.FLOAT_MAT3=35675;h.FLOAT_MAT4=35676;h.TYPE_LENGTH={};h.TYPE_LENGTH[h.FLOAT]=h.TYPE_LENGTH[h.INT]=h.TYPE_LENGTH[h.BYTE]=h.TYPE_LENGTH[h.BOOL]=1;h.TYPE_LENGTH[h.FLOAT_VEC2]=h.TYPE_LENGTH[h.INT_VEC2]=h.TYPE_LENGTH[h.BOOL_VEC2]=2;h.TYPE_LENGTH[h.FLOAT_VEC3]=h.TYPE_LENGTH[h.INT_VEC3]=h.TYPE_LENGTH[h.BOOL_VEC3]=3;h.TYPE_LENGTH[h.FLOAT_VEC4]=
h.TYPE_LENGTH[h.INT_VEC4]=h.TYPE_LENGTH[h.BOOL_VEC4]=4;h.TYPE_LENGTH[h.FLOAT_MAT3]=9;h.TYPE_LENGTH[h.FLOAT_MAT4]=16;h.SAMPLER_2D=35678;h.SAMPLER_3D=35679;h.SAMPLER_CUBE=35680;h.DEPTH_COMPONENT=6402;h.ALPHA=6406;h.RGB=6407;h.RGBA=6408;h.LUMINANCE=6409;h.LUMINANCE_ALPHA=6410;h.DEPTH_STENCIL=34041;h.UNSIGNED_INT_24_8_WEBGL=34042;h.R8=33321;h.R16F=33325;h.R32F=33326;h.R8UI=33330;h.RG8=33323;h.RG16F=33327;h.RG32F=33328;h.RGB8=32849;h.SRGB8=35905;h.RGB565=36194;h.R11F_G11F_B10F=35898;h.RGB9_E5=35901;h.RGB16F=
34843;h.RGB32F=34837;h.RGB8UI=36221;h.RGBA8=32856;h.RGB5_A1=32855;h.RGBA16F=34842;h.RGBA32F=34836;h.RGBA8UI=36220;h.RGBA16I=36232;h.RGBA16UI=36214;h.RGBA32I=36226;h.RGBA32UI=36208;h.NEAREST=9728;h.LINEAR=9729;h.NEAREST_MIPMAP_NEAREST=9984;h.LINEAR_MIPMAP_NEAREST=9985;h.NEAREST_MIPMAP_LINEAR=9986;h.LINEAR_MIPMAP_LINEAR=9987;h.REPEAT=10497;h.CLAMP_TO_EDGE=33071;h.MIRRORED_REPEAT=33648;h.ZERO=0;h.ONE=1;h.SRC_COLOR=768;h.ONE_MINUS_SRC_COLOR=769;h.SRC_ALPHA=770;h.ONE_MINUS_SRC_ALPHA=771;h.DST_ALPHA=772;
h.ONE_MINUS_DST_ALPHA=773;h.DST_COLOR=774;h.ONE_MINUS_DST_COLOR=775;h.SRC_ALPHA_SATURATE=776;h.CONSTANT_COLOR=32769;h.ONE_MINUS_CONSTANT_COLOR=32770;h.CONSTANT_ALPHA=32771;h.ONE_MINUS_CONSTANT_ALPHA=32772;h.VERTEX_SHADER=35633;h.FRAGMENT_SHADER=35632;h.FRONT=1028;h.BACK=1029;h.FRONT_AND_BACK=1032;h.NEVER=512;h.LESS=513;h.EQUAL=514;h.LEQUAL=515;h.GREATER=516;h.NOTEQUAL=517;h.GEQUAL=518;h.ALWAYS=519;h.KEEP=7680;h.REPLACE=7681;h.INCR=7682;h.DECR=7683;h.INCR_WRAP=34055;h.DECR_WRAP=34056;h.INVERT=5386;
h.STREAM_DRAW=35040;h.STATIC_DRAW=35044;h.DYNAMIC_DRAW=35048;h.ARRAY_BUFFER=34962;h.ELEMENT_ARRAY_BUFFER=34963;h.POINTS=0;h.LINES=1;h.LINE_LOOP=2;h.LINE_STRIP=3;h.TRIANGLES=4;h.TRIANGLE_STRIP=5;h.TRIANGLE_FAN=6;h.CW=2304;h.CCW=2305;h.CULL_FACE=2884;h.DEPTH_TEST=2929;h.BLEND=3042;h.temp_vec3=vec3.create();h.temp2_vec3=vec3.create();h.temp_vec4=vec4.create();h.temp_quat=quat.create();h.temp_mat3=mat3.create();h.temp_mat4=mat4.create();n.DEG2RAD=0.0174532925;n.RAD2DEG=57.295779578552306;n.EPSILON=1E-6;
n.isPowerOfTwo=h.isPowerOfTwo=function(g){return 0==Math.log(g)/Math.log(2)%1};n.nearestPowerOfTwo=h.nearestPowerOfTwo=function(g){return Math.pow(2,Math.round(Math.log(g)/Math.log(2)))};n.polarToCartesian=function(g,e,a,b){g=g||vec3.create();g[0]=b*Math.sin(a)*Math.cos(e);g[1]=b*Math.cos(a);g[2]=b*Math.sin(a)*Math.sin(e);return g};n.cartesianToPolar=function(g,e,a,b){g=g||vec3.create();g[2]=Math.sqrt(e*e+a*a+b*b);g[0]=Math.atan2(e,b);g[1]=Math.acos(b/g[2]);return g};[Uint8Array,Int8Array,Uint16Array,
Int16Array,Uint32Array,Int32Array,Float32Array,Float64Array].forEach(function(g){g.prototype.toJSON||Object.defineProperty(g.prototype,"toJSON",{value:T,enumerable:!1})});n.getTime="undefined"!=typeof performance?performance.now.bind(performance):Date.now.bind(Date);h.getTime=n.getTime;n.isFunction=function(g){return!!(g&&g.constructor&&g.call&&g.apply)};n.isArray=function(g){return g&&g.constructor===Array};n.isNumber=function(g){return null!=g&&g.constructor===Number};n.getClassName=function(g){if(g){if(g.name)return g.name;
if(g.toString&&(g=g.toString().match(/function\s*(\w+)/))&&2==g.length)return g[1]}};n.cloneObject=h.cloneObject=function(g,e){if(g.constructor!==Object)throw"cloneObject only can clone pure javascript objects, not classes";e=e||{};for(var a in g){var b=g[a];if(null===b)e[a]=null;else switch(b.constructor){case Int8Array:case Uint8Array:case Int16Array:case Uint16Array:case Int32Array:case Uint32Array:case Float32Array:case Float64Array:e[a]=new b.constructor(b);break;case Boolean:case Number:case String:e[a]=
b;break;case Array:e[a]=b.concat();break;case Object:e[a]=h.cloneObject(b)}}return e};n.regexMap=function(g,e,a){for(var b;null!=(b=g.exec(e));)a(b)};n.createCanvas=h.createCanvas=function(g,e){var a=document.createElement("canvas");a.width=g;a.height=e;return a};n.cloneCanvas=h.cloneCanvas=function(g){var e=document.createElement("canvas");e.width=g.width;e.height=g.height;e.getContext("2d").drawImage(g,0,0);return e};"undefined"!=typeof Image&&(Image.prototype.getPixels=function(){var g=document.createElement("canvas");
g.width=this.width;g.height=this.height;g=g.getContext("2d");g.drawImage(this,0,0);return g.getImageData(0,0,this.width,this.height).data});String.prototype.hasOwnProperty("replaceAll")||Object.defineProperty(String.prototype,"replaceAll",{value:function(g){var e=this,a;for(a in g)e=e.split(a).join(g[a]);return e},enumerable:!1});String.prototype.hasOwnProperty("hashCode")||Object.defineProperty(String.prototype,"hashCode",{value:function(){var g=0,e,a,b;if(0==this.length)return g;e=0;for(b=this.length;e<
b;++e)a=this.charCodeAt(e),g=(g<<5)-g+a,g|=0;return g},enumerable:!1});Array.prototype.hasOwnProperty("clone")||Object.defineProperty(Array.prototype,"clone",{value:Array.prototype.concat,enumerable:!1});Float32Array.prototype.hasOwnProperty("clone")||Object.defineProperty(Float32Array.prototype,"clone",{value:function(){return new Float32Array(this)},enumerable:!1});n.wipeObject=function(g){for(var e in g)g.hasOwnProperty(e)&&delete g[e]};n.extendClass=h.extendClass=function(g,e){for(var a in e)g.hasOwnProperty(a)||
(g[a]=e[a]);if(e.prototype){var b=Object.getOwnPropertyNames(e.prototype);for(a=0;a<b.length;++a){var c=b[a];g.prototype.hasOwnProperty(c)||(e.prototype.__lookupGetter__(c)?g.prototype.__defineGetter__(c,e.prototype.__lookupGetter__(c)):g.prototype[c]=e.prototype[c],e.prototype.__lookupSetter__(c)&&g.prototype.__defineSetter__(c,e.prototype.__lookupSetter__(c)))}}g.hasOwnProperty("superclass")||Object.defineProperty(g,"superclass",{get:function(){return e},enumerable:!1})};n.HttpRequest=h.request=
function(g,e,a,b,c){var d=!0;c&&void 0!==c.async&&(d=c.async);if(e){var f=null,f=[],k;for(k in e)f.push(k+"="+e[k]);f=f.join("&");g=g+"?"+f}var h=new XMLHttpRequest;h.open("GET",g,d);h.onload=function(c){this.getResponseHeader("Content-Type");200!=this.status?(v.trigger(h,"fail",this.status),b&&b(this.status)):(v.trigger(h,"done",this.response),a&&a(this.response))};h.onerror=function(a){v.trigger(h,"fail",a)};if(c){for(k in c)h[k]=c[k];c.binary&&(h.responseType="arraybuffer")}h.send();return h};
n.XMLHttpRequest&&(XMLHttpRequest.prototype.hasOwnProperty("done")||Object.defineProperty(XMLHttpRequest.prototype,"done",{enumerable:!1,value:function(g){v.bind(this,"done",function(e,a){g(a)});return this}}),XMLHttpRequest.prototype.hasOwnProperty("fail")||Object.defineProperty(XMLHttpRequest.prototype,"fail",{enumerable:!1,value:function(g){v.bind(this,"fail",function(e,a){g(a)});return this}}));n.getFileExtension=function(g){var e=g.indexOf("?");-1!=e&&(g=g.substr(0,e));e=g.lastIndexOf(".");return-1==
e?"":g.substr(e+1).toLowerCase()};n.loadFileAtlas=h.loadFileAtlas=function(g,e,a){var b=null;HttpRequest(g,null,function(a){a=h.processFileAtlas(a);e&&e(a);b&&b(a)},alert,a);return{done:function(a){b=a}}};n.processFileAtlas=h.processFileAtlas=function(g,e){for(var a=g.split("\n"),b={},c=[],d="",f=0,k=a.length;f<k;f++){var h=e?a[f]:a[f].trim();h.length&&("\\"!=h[0]?c.push(h):(c.length&&(b[d]=c.join("\n")),c.length=0,d=h.substr(1)))}c.length&&(b[d]=c.join("\n"));return b};n.typedArrayToArray=function(g){var e=
[];e.length=g.length;for(var a=0;a<g.length;a++)e[a]=g[a];return e};n.RGBToHex=function(g,e,a){g=Math.min(255,255*g)|0;e=Math.min(255,255*e)|0;a=Math.min(255,255*a)|0;return"#"+(16777216+(g<<16)+(e<<8)+a).toString(16).slice(1)};n.HUEToRGB=function(g,e,a){0>a&&(a+=1);1<a&&(a-=1);return a<1/6?g+6*(e-g)*a:0.5>a?e:a<2/3?g+(e-g)*(2/3-a)*6:g};n.HSLToRGB=function(g,e,a,b){b=b||vec3.create();if(0==e)a=e=g=a;else{var c=0.5>a?a*(1+e):a+e-a*e,d=2*a-c;a=HUEToRGB(d,c,g+1/3);e=HUEToRGB(d,c,g);g=HUEToRGB(d,c,g-
1/3)}b[0]=a;b[1]=e;b[2]=g;return b};n.hexColorToRGBA=function(){var g={white:[1,1,1],black:[0,0,0],gray:[0.501960813999176,0.501960813999176,0.501960813999176],red:[1,0,0],orange:[1,0.6470588445663452,0],pink:[1,0.7529411911964417,0.7960784435272217],green:[0,0.501960813999176,0],lime:[0,1,0],blue:[0,0,1],violet:[0.9333333373069763,0.5098039507865906,0.9333333373069763],magenta:[1,0,1],cyan:[0,1,1],yellow:[1,1,0],brown:[0.6470588445663452,0.16470588743686676,0.16470588743686676],silver:[0.7529411911964417,
0.7529411911964417,0.7529411911964417],gold:[1,0.843137264251709,0],transparent:[0,0,0,0]};return function(e,a,b){b=void 0===b?1:b;a=a||new Float32Array(4);a[3]=b;if("string"!=typeof e)return a;var c=g[e];if(void 0!==c)return a.set(c),a[3]=3==a.length?b:a[3]*b,a;c=e.indexOf("rgba(");if(-1!=c)return e=e.substr(5,e.length-2),e=e.split(","),a[0]=parseInt(e[0])/255,a[1]=parseInt(e[1])/255,a[2]=parseInt(e[2])/255,a[3]=parseFloat(e[3])*b,a;c=e.indexOf("hsla(");if(-1!=c)return e=e.substr(5,e.length-2),e=
e.split(","),HSLToRGB(parseInt(e[0])/360,parseInt(e[1])/100,parseInt(e[2])/100,a),a[3]=parseFloat(e[3])*b,a;a[3]=b;c=e.indexOf("rgb(");if(-1!=c)return e=e.substr(4,e.length-2),e=e.split(","),a[0]=parseInt(e[0])/255,a[1]=parseInt(e[1])/255,a[2]=parseInt(e[2])/255,a;c=e.indexOf("hsl(");if(-1!=c)return e=e.substr(4,e.length-2),e=e.split(","),HSLToRGB(parseInt(e[0])/360,parseInt(e[1])/100,parseInt(e[2])/100,a),a;e=e.replace(/^#?([a-f\d])([a-f\d])([a-f\d])$/i,function(a,b,c,e){return b+b+c+c+e+e});b=/^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(e);
if(!b)return a;a[0]=parseInt(b[1],16)/255;a[1]=parseInt(b[2],16)/255;a[2]=parseInt(b[3],16)/255;return a}}();var P=function(){function g(a){return a.charCodeAt(0)+(a.charCodeAt(1)<<8)+(a.charCodeAt(2)<<16)+(a.charCodeAt(3)<<24)}function e(a,b,c,d){var e=new Uint16Array(4),f=new Uint16Array(c*d),g=0,k=0,l=0,h=k=g=0,s=0,m=0,t=0,p=c/4;d/=4;for(var q=0;q<d;q++)for(var r=0;r<p;r++)l=b+4*(q*p+r),e[0]=a[l],e[1]=a[l+1],g=e[0]&31,k=e[0]&2016,h=e[0]&63488,s=e[1]&31,m=e[1]&2016,t=e[1]&63488,e[2]=5*g+3*s>>3|
5*k+3*m>>3&2016|5*h+3*t>>3&63488,e[3]=5*s+3*g>>3|5*m+3*k>>3&2016|5*t+3*h>>3&63488,g=a[l+2],k=4*q*c+4*r,f[k]=e[g&3],f[k+1]=e[g>>2&3],f[k+2]=e[g>>4&3],f[k+3]=e[g>>6&3],k+=c,f[k]=e[g>>8&3],f[k+1]=e[g>>10&3],f[k+2]=e[g>>12&3],f[k+3]=e[g>>14],g=a[l+3],k+=c,f[k]=e[g&3],f[k+1]=e[g>>2&3],f[k+2]=e[g>>4&3],f[k+3]=e[g>>6&3],k+=c,f[k]=e[g>>8&3],f[k+1]=e[g>>10&3],f[k+2]=e[g>>12&3],f[k+3]=e[g>>14];return f}function a(a){for(var b=0,c=a.length,d=0;b<c;b+=4)d=a[b],a[b]=a[b+2],a[b+2]=d}function b(b,c,d,g){var K=new Int32Array(d,
0,r),I,A,C,E,D,v,J,B,y;if(K[w]!=f)return console.error("Invalid magic number in DDS header"),0;if(!K[z]&l)return console.error("Unsupported format, must contain a FourCC code"),0;I=K[n];switch(I){case m:A=8;C=c?c.COMPRESSED_RGB_S3TC_DXT1_EXT:null;break;case p:A=16;C=c?c.COMPRESSED_RGBA_S3TC_DXT3_EXT:null;break;case q:A=16;C=c?c.COMPRESSED_RGBA_S3TC_DXT5_EXT:null;break;default:A=4,I=null,C=b.RGBA}J=1;K[t]&k&&!1!==g&&(J=Math.max(1,K[L]));E=K[F];D=K[x];v=K[u]+4;if(K[O+1]&h)for(y=0;6>y;++y)for(E=K[F],
D=K[x],B=0;B<J;++B)I?(g=Math.max(4,E)/4*Math.max(4,D)/4*A,c=new Uint8Array(d,v,g),b.compressedTexImage2D(b.TEXTURE_CUBE_MAP_POSITIVE_X+y,B,C,E,D,0,c)):(b.pixelStorei(b.UNPACK_FLIP_Y_WEBGL,!1),g=E*D*A,c=new Uint8Array(d,v,g),a(c),b.texImage2D(b.TEXTURE_CUBE_MAP_POSITIVE_X+y,B,C,E,D,0,b.RGBA,b.UNSIGNED_BYTE,c)),v+=g,E*=0.5,D*=0.5;else if(c)for(b.pixelStorei(b.UNPACK_FLIP_Y_WEBGL,!0),B=0;B<J;++B)I?(g=Math.max(4,E)/4*Math.max(4,D)/4*A,c=new Uint8Array(d,v,g),b.compressedTexImage2D(b.TEXTURE_2D,B,C,E,
D,0,c)):(g=E*D*A,c=new Uint8Array(d,v,g),a(c),b.texImage2D(b.TEXTURE_2D,B,C,E,D,0,C,b.UNSIGNED_BYTE,c)),v+=g,E*=0.5,D*=0.5;else if(I==m)Math.max(4,E),Math.max(4,D),c=new Uint16Array(d),d=e(c,v/2,E,D),b.texImage2D(b.TEXTURE_2D,0,b.RGB,E,D,0,b.RGB,b.UNSIGNED_SHORT_5_6_5,d),g&&b.generateMipmap(b.TEXTURE_2D);else return console.error("No manual decoder for",String.fromCharCode(I&255,I>>8&255,I>>16&255,I>>24&255),"and no native support"),0;return J}function c(b,c){var d=new Int32Array(b,0,r),g,v,I,A,C,
E,D,y,J,B,H;if(d[w]!=f)return console.error("Invalid magic number in DDS header"),0;if(!d[z]&l)return console.error("Unsupported format, must contain a FourCC code"),0;g=d[n];switch(g){case m:v=8;I="COMPRESSED_RGB_S3TC_DXT1_EXT";break;case p:v=16;I="COMPRESSED_RGBA_S3TC_DXT3_EXT";break;case q:v=16;I="COMPRESSED_RGBA_S3TC_DXT5_EXT";break;default:v=4,I="RGBA"}J=1;d[t]&k&&!1!==loadMipmaps&&(J=Math.max(1,d[L]));A=d[F];C=d[x];D=d[u]+4;var G=[];if(d[O+1]&h)for(H=0;6>H;++H)for(A=d[F],C=d[x],B=0;B<J;++B)g?
(E=Math.max(4,A)/4*Math.max(4,C)/4*v,new Uint8Array(b,D,E),G.push({tex:"TEXTURE_CUBE_MAP",face:H,mipmap:B,internalFormat:I,width:A,height:C,offset:0,dataOffset:D,dataLength:E})):(E=A*C*v,y=new Uint8Array(b,D,E),a(y),G.push({tex:"TEXTURE_CUBE_MAP",face:H,mipmap:B,internalFormat:I,width:A,height:C,offset:0,type:"UNSIGNED_BYTE",dataOffset:D,dataLength:E})),D+=E,A*=0.5,C*=0.5;else if(c)if(g==m)Math.max(4,A),Math.max(4,C),y=new Uint16Array(b),d=e(y,D/2,A,C),G.push({tex:"TEXTURE_2D",mipmap:0,internalFormat:"RGB",
width:A,height:C,offset:0,format:"RGB",type:"UNSIGNED_SHORT_5_6_5",data:d});else return console.error("No manual decoder for",String.fromCharCode(g&255,g>>8&255,g>>16&255,g>>24&255),"and no native support"),0;else for(B=0;B<J;++B)E=Math.max(4,A)/4*Math.max(4,C)/4*v,new Uint8Array(b,D,E),G.push({tex:"TEXTURE_2D",mipmap:B,internalFormat:I,width:A,height:C,offset:0,type:"UNSIGNED_BYTE",dataOffset:D,dataLength:E}),D+=E,A*=0.5,C*=0.5;return G}function d(a,c,d,e,f,g){var k=new XMLHttpRequest;k.open("GET",
d,!0);k.responseType="arraybuffer";k.onload=function(){if(200==this.status){var d=new Int32Array(this.response,0,r),k=d[O+1]&h?a.TEXTURE_CUBE_MAP:a.TEXTURE_2D;a.bindTexture(k,e);var l=b(a,c,this.response,f);a.texParameteri(k,a.TEXTURE_MAG_FILTER,a.LINEAR);a.texParameteri(k,a.TEXTURE_MIN_FILTER,1<l?a.LINEAR_MIPMAP_LINEAR:a.LINEAR);a.bindTexture(k,null);e.texture_type=k;e.width=d[F];e.height=d[x]}g&&g(e)};k.send(null);return e}var f=542327876,k=131072,h=512,l=4,m=g("DXT1"),p=g("DXT3"),q=g("DXT5"),r=
31,w=0,u=1,t=2,x=3,F=4,L=7,z=20,n=21,O=27;return{dxtToRgb565:e,uploadDDSLevels:b,loadDDSTextureEx:d,loadDDSTexture:function(a,b,c,e){var f=a.createTexture();b=a.getExtension("WEBGL_compressed_texture_s3tc");d(a,b,c,f,!0,e);return f},loadDDSTextureFromMemoryEx:function(a,c,d,e,f){var g=new Int32Array(d,0,r),k=!!(g[O+1]&h),l=k?a.TEXTURE_CUBE_MAP:a.TEXTURE_2D;a.bindTexture(l,e.handler);c=b(a,c,d,f);a.texParameteri(l,a.TEXTURE_MAG_FILTER,a.LINEAR);a.texParameteri(l,a.TEXTURE_MIN_FILTER,1<c?a.LINEAR_MIPMAP_LINEAR:
a.LINEAR);k&&(a.texParameteri(l,a.TEXTURE_WRAP_S,a.CLAMP_TO_EDGE),a.texParameteri(l,a.TEXTURE_WRAP_T,a.CLAMP_TO_EDGE));a.bindTexture(l,null);e.handler&&(e.texture_type=l,e.width=g[F],e.height=g[x]);return e},getDDSTextureFromMemoryEx:function(a){var b=new Int32Array(a,0,r),d=b[O+1]&h?"TEXTURE_CUBE_MAP":"TEXTURE_2D",e=c(a);return{type:d,buffers:e,data:a,width:b[F],height:b[x]}}}}();"undefined"!=typeof n&&(n.DDS=P);if("undefined"==typeof glMatrix)throw"You must include glMatrix on your project";Math.clamp=
function(g,e,a){return e>g?e:a<g?a:g};vec3.ZERO=vec3.fromValues(0,0,0);vec3.FRONT=vec3.fromValues(0,0,-1);vec3.UP=vec3.fromValues(0,1,0);vec3.RIGHT=vec3.fromValues(1,0,0);vec2.rotate=function(g,e,a){var b=e[0];e=e[1];var c=Math.cos(a);a=Math.sin(a);g[0]=b*c-e*a;g[1]=b*a+e*c;return g};vec3.zero=function(g){g[0]=g[1]=0;return g};vec2.perpdot=function(g,e){return g[1]*e[0]+-g[0]*e[1]};vec2.computeSignedAngle=function(g,e){return Math.atan2(vec2.perpdot(g,e),vec2.dot(g,e))};vec2.random=function(g,e){e=
e||1;g[0]=Math.random()*e;g[1]=Math.random()*e;return g};vec3.zero=function(g){g[0]=g[1]=g[2]=0;return g};vec3.minValue=function(g){return g[0]<g[1]&&g[0]<g[2]?g[0]:g[1]<g[2]?g[1]:g[2]};vec3.maxValue=function(g){return g[0]>g[1]&&g[0]>g[2]?g[0]:g[1]>g[2]?g[1]:g[2]};vec3.minValue=function(g){return g[0]<g[1]&&g[0]<g[2]?g[0]:g[1]<g[2]?g[1]:g[2]};vec3.addValue=function(g,e,a){g[0]=e[0]+a;g[1]=e[1]+a;g[2]=e[2]+a};vec3.subValue=function(g,e,a){g[0]=e[0]-a;g[1]=e[1]-a;g[2]=e[2]-a};vec3.toArray=function(g){return[g[0],
g[1],g[2]]};vec3.rotateX=function(g,e,a){var b=e[1],c=e[2],d=Math.cos(a);a=Math.sin(a);g[0]=e[0];g[1]=b*d-c*a;g[2]=b*a+c*d;return g};vec3.rotateY=function(g,e,a){var b=e[0],c=e[2],d=Math.cos(a);a=Math.sin(a);g[0]=b*d-c*a;g[1]=e[1];g[2]=b*a+c*d;return g};vec3.rotateZ=function(g,e,a){var b=e[0],c=e[1],d=Math.cos(a);a=Math.sin(a);g[0]=b*d-c*a;g[1]=b*a+c*d;g[2]=e[2];return g};vec3.angle=function(g,e){return Math.acos(vec3.dot(g,e))};vec3.random=function(g,e){e=e||1;g[0]=Math.random()*e;g[1]=Math.random()*
e;g[2]=Math.random()*e;return g};vec3.polarToCartesian=function(g,e){var a=e[0],b=e[1],c=e[2];g[0]=a*Math.cos(b)*Math.sin(c);g[1]=a*Math.sin(b);g[2]=a*Math.cos(b)*Math.cos(c);return g};vec3.reflect=function(g,e,a){var b=e[0],c=e[1],d=e[2];vec3.scale(g,a,-2*vec3.dot(e,a));g[0]+=b;g[1]+=c;g[2]+=d;return g};vec4.random=function(g,e){e=e||1;g[0]=Math.random()*e;g[1]=Math.random()*e;g[2]=Math.random()*e;g[3]=Math.random()*e;return g};vec4.toArray=function(g){return[g[0],g[1],g[2],g[3]]};mat3.IDENTITY=
mat3.create();mat4.IDENTITY=mat4.create();mat4.toArray=function(g){return[g[0],g[1],g[2],g[3],g[4],g[5],g[6],g[7],g[8],g[9],g[10],g[11],g[12],g[13],g[14],g[15]]};mat4.setUpAndOrthonormalize=function(g,e,a){e!=g&&mat4.copy(g,e);e=g.subarray(0,3);vec3.normalize(g.subarray(4,7),a);g=g.subarray(8,11);vec3.cross(e,a,g);vec3.normalize(e,e);vec3.cross(g,e,a);vec3.normalize(g,g)};mat4.multiplyVec3=function(g,e,a){var b=a[0],c=a[1];a=a[2];g[0]=e[0]*b+e[4]*c+e[8]*a+e[12];g[1]=e[1]*b+e[5]*c+e[9]*a+e[13];g[2]=
e[2]*b+e[6]*c+e[10]*a+e[14];return g};mat4.projectVec3=function(g,e,a){var b=a[0],c=a[1];a=a[2];var d=e[1]*b+e[5]*c+e[9]*a+e[13],f=e[2]*b+e[6]*c+e[10]*a+e[14],k=e[3]*b+e[7]*c+e[11]*a+e[15];g[0]=((e[0]*b+e[4]*c+e[8]*a+e[12])/k+1)/2;g[1]=(d/k+1)/2;g[2]=(f/k+1)/2;return g};vec3.project=function(g,e,a,b){b=b||gl.viewport_data;var c=e[0],d=e[1];e=e[2];var f=a[3]*c+a[7]*d+a[11]*e+a[15],k=1-((a[1]*c+a[5]*d+a[9]*e+a[13])/f+1)/2,h=((a[2]*c+a[6]*d+a[10]*e+a[14])/f+1)/2;g[0]=((a[0]*c+a[4]*d+a[8]*e+a[12])/f+
1)/2*b[2]+b[0];g[1]=k*b[3]+b[1];g[2]=h;return g};var R=mat4.create(),H=vec4.create();vec3.unproject=function(g,e,a,b){H[0]=2*(e[0]-b[0])/b[2]-1;H[1]=2*(e[1]-b[1])/b[3]-1;H[2]=2*e[2]-1;H[3]=1;if(!mat4.invert(R,a))return null;vec4.transformMat4(H,H,R);if(0===H[3])return null;g[0]=H[0]/H[3];g[1]=H[1]/H[3];g[2]=H[2]/H[3];return g};mat4.rotateVec3=function(g,e,a){var b=a[0],c=a[1];a=a[2];g[0]=e[0]*b+e[4]*c+e[8]*a;g[1]=e[1]*b+e[5]*c+e[9]*a;g[2]=e[2]*b+e[6]*c+e[10]*a;return g};mat4.fromTranslationFrontTop=
function(g,e,a,b){vec3.cross(g.subarray(0,3),a,b);g.set(b,4);g.set(a,8);g.set(e,12);return g};mat4.translationMatrix=function(g){var e=mat4.create();e[12]=g[0];e[13]=g[1];e[14]=g[2];return e};mat4.setTranslation=function(g,e){g[12]=e[0];g[13]=e[1];g[14]=e[2];return g};mat4.getTranslation=function(g,e){g[0]=e[12];g[1]=e[13];g[2]=e[14];return g};mat4.toRotationMat4=function(g,e){mat4.copy(g,e);g[12]=g[13]=g[14]=0;return g};mat4.swapRows=function(g,e,a,b){if(g!=e)return mat4.copy(g,e),g[4*a]=e[4*b],
g[4*a+1]=e[4*b+1],g[4*a+2]=e[4*b+2],g[4*a+3]=e[4*b+3],g[4*b]=e[4*a],g[4*b+1]=e[4*a+1],g[4*b+2]=e[4*a+2],g[4*b+3]=e[4*a+3],g;e=new Float32Array(matrix.subarray(4*a,5*a));matrix.set(matrix.subarray(4*b,5*b),4*a);matrix.set(e,4*b);return g};mat4.scaleAndAdd=function(g,e,a,b){g[0]=e[0]+a[0]*b;g[1]=e[1]+a[1]*b;g[2]=e[2]+a[2]*b;g[3]=e[3]+a[3]*b;g[4]=e[4]+a[4]*b;g[5]=e[5]+a[5]*b;g[6]=e[6]+a[6]*b;g[7]=e[7]+a[7]*b;g[8]=e[8]+a[8]*b;g[9]=e[9]+a[9]*b;g[10]=e[10]+a[10]*b;g[11]=e[11]+a[11]*b;g[12]=e[12]+a[12]*
b;g[13]=e[13]+a[13]*b;g[14]=e[14]+a[14]*b;g[15]=e[15]+a[15]*b;return g};quat.fromAxisAngle=function(g,e){var a=quat.create();e*=0.5;var b=Math.sin(e);a[0]=b*g[0];a[1]=b*g[1];a[2]=b*g[2];a[3]=Math.cos(e);return a};quat.toEuler=function(g,e){var a=Math.atan2(2*e[1]*e[3]-2*e[0]*e[2],1-2*e[1]*e[1]-2*e[2]*e[2]),b=Math.asin(2*e[0]*e[1]+2*e[2]*e[3]),c=Math.atan2(2*e[0]*e[3]-2*e[1]*e[2],1-2*e[0]*e[0]-2*e[2]*e[2]);g||(g=vec3.create());vec3.set(g,a,b,c);return g};quat.fromEuler=function(g,e){var a=e[0],b=e[1],
c=e[2],d=Math.cos(a),f=Math.cos(b),k=Math.cos(c),a=Math.sin(a),b=Math.sin(b),c=Math.sin(c),h=0.5*Math.sqrt(1+d*f+d*k-a*b*c+f*k);0==h&&(h=1E-6);quat.set(g,(f*c+d*c+a*b*k)/(4*h),(a*f+a*k+d*b*c)/(4*h),(-a*c+d*b*k+b)/(4*h),h);quat.normalize(g,g);return g};quat.fromMat4=function(g,e){var a=e[0]+e[5]+e[10];if(0<a){var b=Math.sqrt(a+1);g[3]=0.5*b;b=0.5/b;g[0]=(e[9]-e[6])*b;g[1]=(e[2]-e[8])*b;g[2]=(e[4]-e[1])*b}else{a=0;e[5]>e[0]&&(a=1);e[10]>e[4*a+a]&&(a=2);var c=(a+1)%3,d=(c+1)%3,b=Math.sqrt(e[4*a+a]-e[4*
c+c]-e[4*d+d]+1);g[a]=0.5*b;b=0.5/b;g[3]=(e[4*d+c]-e[4*c+d])*b;g[c]=(e[4*c+a]+e[4*a+c])*b;g[d]=(e[4*d+a]+e[4*a+d])*b}quat.normalize(g,g)};vec3.getMat3Column=function(g,e,a){g[0]=e[3*a];g[1]=e[3*a+1];g[2]=e[3*a+2];return g};mat3.setColumn=function(g,e,a){g[3*a]=e[0];g[3*a+1]=e[1];g[3*a+2]=e[2];return g};quat.fromMat3AndQuat=function(){var g=mat3.create(),e=quat.create(),a=vec3.create(),b=vec3.create(),c=vec3.create(),d=vec3.create(),f=vec3.create(),k=vec3.create(),h=vec3.create(),l=vec3.create(),m=
vec3.create(),p=vec3.create();mat3.create();return function(q,r,w){w=w||25;for(var u=0;u<w;++u){var t=mat3.fromQuat(g,q);vec3.getMat3Column(a,t,0);vec3.getMat3Column(b,t,1);vec3.getMat3Column(c,t,2);vec3.getMat3Column(d,r,0);vec3.getMat3Column(f,r,1);vec3.getMat3Column(k,r,2);vec3.cross(h,a,d);vec3.cross(l,b,f);vec3.cross(m,c,k);vec3.add(p,h,l);vec3.add(p,p,m);t=1/Math.abs(vec3.dot(a,d)+vec3.dot(b,f)+vec3.dot(c,k))+1E-9;vec3.scale(p,p,t);t=vec3.length(p);if(1E-9>t)break;vec3.scale(p,p,1/t);quat.setAxisAngle(e,
p,t);quat.mul(q,e,q);quat.normalize(q,q)}return q}}();quat.rotateToFrom=function(){var g=vec3.create();return function(e,a,b){e=e||quat.create();var c=vec3.cross(g,a,b);a=vec3.dot(a,b);if(-0.99>a)return e[0]=0,e[1]=1,e[2]=0,e[3]=0,e;e[0]=0.5*c[0];e[1]=0.5*c[1];e[2]=0.5*c[2];e[3]=0.5*(1+a);quat.normalize(e,e);return e}}();quat.lookAt=function(){var g=vec3.create();return function(e,a,b){b=vec3.dot(vec3.FRONT,a);if(1E-6>Math.abs(b- -1))return e.set(vec3.UP),e[3]=Math.PI,e;if(1E-6>Math.abs(b-1))return quat.identity(e);
b=Math.acos(b);vec3.cross(g,vec3.FRONT,a);vec3.normalize(g,g);quat.setAxisAngle(e,g,b);return e}}();h.Indexer=function(){this.unique=[];this.indices=[];this.map={}};h.Indexer.prototype={add:function(g){var e=JSON.stringify(g);e in this.map||(this.map[e]=this.unique.length,this.unique.push(g));return this.map[e]}};h.Buffer=function(g,e,a,b,c){h.debug&&console.log("GL.Buffer created");null!==c&&(c=c||n.gl);this.gl=c;this.buffer=null;this.target=g;this.attribute=null;this.data=e;this.spacing=a||3;this.data&&
this.gl&&this.upload(b)};h.Buffer.prototype.bind=function(g,e){e=e||this.gl;e.bindBuffer(e.ARRAY_BUFFER,this.buffer);e.enableVertexAttribArray(g);e.vertexAttribPointer(g,this.spacing,this.buffer.gl_type,!1,0,0)};h.Buffer.prototype.unbind=function(g,e){e=e||this.gl;e.disableVertexAttribArray(g)};h.Buffer.prototype.forEach=function(g){for(var e=this.data,a=0,b=this.spacing,c=e.length;a<c;a+=b)g(e.subarray(a,a+b),a);return this};h.Buffer.prototype.applyTransform=function(g){for(var e=this.data,a=0,b=
this.spacing,c=e.length;a<c;a+=b){var d=e.subarray(a,a+b);vec3.transformMat4(d,d,g)}return this};h.Buffer.prototype.upload=function(g){var e=this.spacing||3,a=this.gl;if(a){if(!this.data)throw"No data supplied";var b=this.data;if(!b.buffer)throw"Buffers must be typed arrays";if(this.buffer=this.buffer||a.createBuffer()){this.buffer.length=b.length;this.buffer.spacing=e;switch(b.constructor){case Int8Array:this.buffer.gl_type=a.BYTE;break;case Uint8ClampedArray:case Uint8Array:this.buffer.gl_type=
a.UNSIGNED_BYTE;break;case Int16Array:this.buffer.gl_type=a.SHORT;break;case Uint16Array:this.buffer.gl_type=a.UNSIGNED_SHORT;break;case Int32Array:this.buffer.gl_type=a.INT;break;case Uint32Array:this.buffer.gl_type=a.UNSIGNED_INT;break;case Float32Array:this.buffer.gl_type=a.FLOAT;break;default:throw"unsupported buffer type";}this.target!=a.ARRAY_BUFFER||this.buffer.gl_type!=a.INT&&this.buffer.gl_type!=a.UNSIGNED_INT||(console.warn("WebGL does not support UINT32 or INT32 as vertex buffer types, converting to FLOAT"),
this.buffer.gl_type=a.FLOAT,b=new Float32Array(b));a.bindBuffer(this.target,this.buffer);a.bufferData(this.target,b,g||this.stream_type||a.STATIC_DRAW)}}};h.Buffer.prototype.compile=h.Buffer.prototype.upload;h.Buffer.prototype.setData=function(g,e){if(!g.buffer)throw"Data must be typed array";e=e||0;if(this.data){if(this.data.length<g.length)throw"buffer is not big enough, you cannot set data to a smaller buffer";if(this.data!=g)if(this.data.length==g.length)this.data.set(g),this.upload();else{var a=
new Uint8Array(g.buffer,g.buffer.byteOffset,g.buffer.byteLength);(new Uint8Array(this.data.buffer)).set(a,e);this.uploadRange(e,a.length)}}else this.data=g,this.upload()};h.Buffer.prototype.uploadRange=function(g,e){if(!this.data)throw"No data stored in this buffer";if(!this.data.buffer)throw"Buffers must be typed arrays";var a=new Uint8Array(this.data.buffer,g,e),b=this.gl;b.bindBuffer(this.target,this.buffer);b.bufferSubData(this.target,g,a)};h.Buffer.prototype.clone=function(g){var e=new h.Buffer;
if(g)for(var a in this)e[a]=this[a];else this.target&&(e.target=this.target),this.gl&&(e.gl=this.gl),this.spacing&&(e.spacing=this.spacing),this.data&&(e.data=new n[this.data.constructor](this.data),e.upload());return e};h.Buffer.prototype.toJSON=function(){return this.data?{data_type:getClassName(this.data),data:this.data.toJSON(),target:this.target,attribute:this.attribute,spacing:this.spacing}:(console.error("cannot serialize a mesh without data"),null)};h.Buffer.prototype.fromJSON=function(g){this.data=
new (n[g.data_type]||Float32Array)(g.data);this.target=g.target;this.spacing=g.spacing||3;this.attribute=g.attribute;this.upload(h.STATIC_DRAW)};h.Buffer.prototype.delete=function(){this.gl.deleteBuffer(this.buffer);this.buffer=null};n.Mesh=h.Mesh=function(g,e,a,b){h.debug&&console.log("GL.Mesh created");null!==b&&(this.gl=b=b||n.gl);this._context_id=b.context_id;this.vertexBuffers={};this.indexBuffers={};this.info={groups:[]};this._bounding=BBox.create();(g||e)&&this.addBuffers(g,e,a?a.stream_type:
null);if(a)for(var c in a)this[c]=a[c]};Mesh.common_buffers={vertices:{spacing:3,attribute:"a_vertex"},vertices2D:{spacing:2,attribute:"a_vertex2D"},normals:{spacing:3,attribute:"a_normal"},coords:{spacing:2,attribute:"a_coord"},coords1:{spacing:2,attribute:"a_coord1"},coords2:{spacing:2,attribute:"a_coord2"},colors:{spacing:4,attribute:"a_color"},tangents:{spacing:3,attribute:"a_tangent"},bone_indices:{spacing:4,attribute:"a_bone_indices",type:Uint8Array},weights:{spacing:4,attribute:"a_weights"},
extra:{spacing:1,attribute:"a_extra"},extra2:{spacing:2,attribute:"a_extra2"},extra3:{spacing:3,attribute:"a_extra3"},extra4:{spacing:4,attribute:"a_extra4"}};Mesh.default_datatype=Float32Array;Object.defineProperty(Mesh.prototype,"bounding",{set:function(g){if(g){if(13>g.length)throw"Bounding must use the BBox bounding format of 13 floats: center, halfsize, min, max, radius";this._bounding.set(g)}},get:function(){return this._bounding}});Mesh.prototype.addBuffer=function(g,e){e.target==gl.ARRAY_BUFFER?
this.vertexBuffers[g]=e:this.indexBuffers[g]=e;if(!e.attribute){var a=h.Mesh.common_buffers[g];a&&(e.attribute=a.attribute)}};Mesh.prototype.addBuffers=function(g,e,a){var b=0;this.vertexBuffers.vertices&&(b=this.vertexBuffers.vertices.data.length/3);for(var c in g){var d=g[c];if(d){if(d.constructor==h.Buffer)d=d.data;else if("number"!=typeof d[0]){for(var f=[],k=0,s=1E4;k<d.length;k+=s)f=Array.prototype.concat.apply(f,d.slice(k,k+s));d=f}f=h.Mesh.common_buffers[c];d.constructor===Array&&(k=h.Mesh.default_datatype,
f&&f.type&&(k=f.type),d=new k(d));"vertices"==c&&(b=d.length/3);k=d.length/b;f&&f.spacing&&(k=f.spacing);s="a_"+c;f&&f.attribute&&(s=f.attribute);this.vertexBuffers[c]?this.updateVertexBuffer(c,s,k,d,a):this.createVertexBuffer(c,s,k,d,a)}}if(e)for(c in e)if(d=e[c]){d.constructor==h.Buffer&&(d=d.data);if("number"!=typeof d[0]){f=[];c=0;for(s=1E4;c<d.length;c+=s)f=Array.prototype.concat.apply(f,d.slice(c,c+s));d=f}d.constructor===Array&&(k=Uint16Array,65536<b&&(k=Uint32Array),d=new k(d));this.createIndexBuffer(c,
d)}};Mesh.prototype.createVertexBuffer=function(g,e,a,b,c){var d=h.Mesh.common_buffers[g];!e&&d&&(e=d.attribute);if(!e)throw"Buffer added to mesh without attribute name";!a&&d&&(a=d&&d.spacing?d.spacing:3);if(!b){b=this.getNumVertices();if(!b)throw"Cannot create an empty buffer in a mesh without vertices (vertices are needed to know the size)";b=new h.Mesh.default_datatype(b*a)}if(!b.buffer)throw"Buffer data MUST be typed array";a=this.vertexBuffers[g]=new h.Buffer(gl.ARRAY_BUFFER,b,a,c,this.gl);
a.name=g;a.attribute=e;return a};Mesh.prototype.updateVertexBuffer=function(g,e,a,b,c){var d=this.vertexBuffers[g];d?b.length&&(d.attribute=e,d.spacing=a,d.data=b,d.upload(c)):console.log("buffer not found: ",g)};Mesh.prototype.removeVertexBuffer=function(g,e){var a=this.vertexBuffers[g];a&&(e&&a.delete(),delete this.vertexBuffers[g])};Mesh.prototype.getVertexBuffer=function(g){return this.vertexBuffers[g]};Mesh.prototype.createIndexBuffer=function(g,e,a){if(e.constructor===Array){var b=Uint16Array,
c=this.vertexBuffers.vertices;c&&(65536<c.data.length/3&&(b=Uint32Array),e=new b(e))}return this.indexBuffers[g]=new h.Buffer(gl.ELEMENT_ARRAY_BUFFER,e,0,a,this.gl)};Mesh.prototype.getBuffer=function(g){return this.vertexBuffers[g]};Mesh.prototype.getIndexBuffer=function(g){return this.indexBuffers[g]};Mesh.prototype.removeIndexBuffer=function(g,e){var a=this.indexBuffers[g];a&&(e&&a.delete(),delete this.indexBuffers[g])};Mesh.prototype.upload=function(g){for(var e in this.vertexBuffers){var a=this.vertexBuffers[e];
a.upload(g)}for(var b in this.indexBuffers)a=this.indexBuffers[b],a.upload()};Mesh.prototype.compile=Mesh.prototype.upload;Mesh.prototype.deleteBuffers=function(){for(var g in this.vertexBuffers){var e=this.vertexBuffers[g];e.delete()}this.vertexBuffers={};for(g in this.indexBuffers)e=this.indexBuffers[g],e.delete();this.indexBuffers={}};Mesh.prototype.delete=Mesh.prototype.deleteBuffers;Mesh.prototype.bindBuffers=function(g){for(var e in this.vertexBuffers){var a=this.vertexBuffers[e],b=g.attributes[a.attribute||
e];null!=b&&a.buffer&&(gl.bindBuffer(gl.ARRAY_BUFFER,a.buffer),gl.enableVertexAttribArray(b),gl.vertexAttribPointer(b,a.buffer.spacing,a.buffer.gl_type,!1,0,0))}};Mesh.prototype.unbindBuffers=function(g){for(var e in this.vertexBuffers){var a=this.vertexBuffers[e],b=a.attribute||e;null!=g.attributes[b]&&a.buffer&&gl.disableVertexAttribArray(g.attributes[b])}};Mesh.prototype.clone=function(g){g=g||n.gl;var e={},a={},b;for(b in this.vertexBuffers){var c=this.vertexBuffers[b];e[b]=new c.data.constructor(c.data)}for(b in this.indexBuffers)c=
this.indexBuffers[b],a[b]=new c.data.constructor(c.data);return new h.Mesh(e,a,void 0,g)};Mesh.prototype.cloneShared=function(g){g=g||n.gl;return new h.Mesh(this.vertexBuffers,this.indexBuffers,void 0,g)};Mesh.prototype.toObject=function(){var g={},e={},a;for(a in this.vertexBuffers){var b=this.vertexBuffers[a];g[a]={spacing:b.spacing,data:new b.data.constructor(b.data)}}for(a in this.indexBuffers)b=this.indexBuffers[a],e[a]={data:new b.data.constructor(b.data)};return{vertexBuffers:g,indexBuffers:e,
info:this.info?cloneObject(this.info):null,bounding:this._bounding.toJSON()}};Mesh.prototype.toJSON=function(){var g={vertexBuffers:{},indexBuffers:{},info:this.info?cloneObject(this.info):null,bounding:this._bounding.toJSON()},e;for(e in this.vertexBuffers)g.vertexBuffers[e]=this.vertexBuffers[e].toJSON();for(e in this.indexBuffers)g.indexBuffers[e]=this.indexBuffers[e].toJSON();return g};Mesh.prototype.fromJSON=function(g){this.vertexBuffers={};this.indexBuffers={};for(var e in g.vertexBuffers)if(g.vertexBuffers[e]){var a=
new h.Buffer;a.fromJSON(g.vertexBuffers[e]);!a.attribute&&h.Mesh.common_buffers[e]&&(a.attribute=h.Mesh.common_buffers[e].attribute);this.vertexBuffers[e]=a}for(e in g.indexBuffers)g.indexBuffers[e]&&(a=new h.Buffer,a.fromJSON(g.indexBuffers[e]),this.indexBuffers[e]=a);g.info&&(this.info=cloneObject(g.info));g.bounding&&(this.bounding=g.bounding)};Mesh.prototype.generateMetadata=function(){var g={},e=this.vertexBuffers.vertices.data,a=this.indexBuffers.triangles.data;g.vertices=e.length/3;g.faces=
a?a.length/3:e.length/9;g.indexed=!!this.metadata.faces;this.metadata=g};Mesh.prototype.computeWireframe=function(){var g=this.indexBuffers.triangles,e=this.vertexBuffers.vertices.data.length/3;if(g){for(var a=g.data,b=new h.Indexer,g=0;g<a.length;g+=3)for(var c=a.subarray(g,g+3),d=0;d<c.length;d++){var f=c[d],k=c[(d+1)%c.length];b.add([Math.min(f,k),Math.max(f,k)])}a=b.unique;b=65536<e?new Uint32Array(2*a.length):new Uint16Array(2*a.length);g=0;for(e=a.length;g<e;++g)b.set(a[g],2*g)}else for(g=e/
3,b=65536<e?new Uint32Array(6*g):new Uint16Array(6*g),g=0;g<e;g+=3)b[2*g]=g,b[2*g+1]=g+1,b[2*g+2]=g+1,b[2*g+3]=g+2,b[2*g+4]=g+2,b[2*g+5]=g;this.createIndexBuffer("wireframe",b);return this};Mesh.prototype.flipNormals=function(g){var e=this.vertexBuffers.normals;if(e){for(var a=e.data,b=a.length,c=0;c<b;++c)a[c]*=-1;e.upload(g);this.indexBuffers.triangles||this.computeIndices();e=this.indexBuffers.triangles;a=e.data;b=a.length;for(c=0;c<b;c+=3){var d=a[c];a[c]=a[c+1];a[c+1]=d}e.upload(g)}};Mesh.prototype.computeIndices=
function(){var g=[],e=[],a=[],b=[],c=this.vertexBuffers.normals,d=this.vertexBuffers.coords,f=this.vertexBuffers.vertices.data,k=null;c&&(k=c.data);c=null;d&&(c=d.data);for(var d={},s=f.length/3,l=0;l<s;++l){var m=f.subarray(3*l,3*(l+1)),p=1E3*m[0]|0,q=0,r=d[p];if(r)for(var w=r.length;q<w;q++)if(0.01>vec3.sqrDist(m,g[r[q]])){b.push(q);break}r&&q!=w||(g.push(m),d[p]?d[p].push(q):d[p]=[q],k&&e.push(k.subarray(3*l,3*(l+1))),c&&a.push(c.subarray(2*l,2*(l+1))),b.push(q))}this.vertexBuffers={};this.createVertexBuffer("vertices",
h.Mesh.common_buffers.vertices.attribute,3,N(g));k&&this.createVertexBuffer("normals",h.Mesh.common_buffers.normals.attribute,3,N(e));c&&this.createVertexBuffer("coords",h.Mesh.common_buffers.coords.attribute,2,N(a));this.createIndexBuffer("triangles",b)};Mesh.prototype.explodeIndices=function(g){g=g||"triangles";var e=this.getIndexBuffer(g);if(e){var a=e.data,e={},b;for(b in this.vertexBuffers){var c=h.Mesh.common_buffers[b];e[b]=new (c.type||Float32Array)(c.spacing*a.length)}b=0;for(var d=a.length;b<
d;++b){var f=a[b],k;for(k in this.vertexBuffers){var s=this.vertexBuffers[k],c=h.Mesh.common_buffers[k],c=s.spacing||c.spacing;e[k].set(s.data.subarray(f*c,f*c+c),b*c)}}for(b in e)k=this.vertexBuffers[b],this.createVertexBuffer(b,k.attribute,k.spacing,e[b]);delete this.indexBuffers[g]}};Mesh.prototype.computeNormals=function(g){if(!this.vertexBuffers.vertices)return console.error("Cannot compute normals of a mesh without vertices");var e=this.vertexBuffers.vertices.data,a=new Float32Array(e.length),
b=null;this.indexBuffers.triangles&&(b=this.indexBuffers.triangles.data);for(var c=h.temp_vec3,d=h.temp2_vec3,f,k,s,l,m,p,q=b?b.length:e.length,r=0;r<q;r+=3)b?(f=b[r],k=b[r+1],s=b[r+2],l=e.subarray(3*f,3*f+3),m=e.subarray(3*k,3*k+3),p=e.subarray(3*s,3*s+3),f=a.subarray(3*f,3*f+3),k=a.subarray(3*k,3*k+3),s=a.subarray(3*s,3*s+3)):(l=e.subarray(3*r,3*r+3),m=e.subarray(3*r+3,3*r+6),p=e.subarray(3*r+6,3*r+9),f=a.subarray(3*r,3*r+3),k=a.subarray(3*r+3,3*r+6),s=a.subarray(3*r+6,3*r+9)),vec3.sub(c,m,l),vec3.sub(d,
p,l),vec3.cross(c,c,d),vec3.normalize(c,c),vec3.add(f,f,c),vec3.add(k,k,c),vec3.add(s,s,c);if(b)for(r=0,q=a.length;r<q;r+=3)e=a.subarray(r,r+3),vec3.normalize(e,e);if(q=this.vertexBuffers.normals)q.data=a,q.upload(g);else return this.createVertexBuffer("normals",h.Mesh.common_buffers.normals.attribute,3,a);return q};Mesh.prototype.computeTangents=function(){var g=this.vertexBuffers.vertices;if(!g)return console.error("Cannot compute tangents of a mesh without vertices");var e=this.vertexBuffers.normals;
if(!e)return console.error("Cannot compute tangents of a mesh without normals");var a=this.vertexBuffers.coords;if(!a)return console.error("Cannot compute tangents of a mesh without uvs");var b=this.indexBuffers.triangles;if(!b)return console.error("Cannot compute tangents of a mesh without indices");var g=g.data,e=e.data,c=a.data,d=b.data;if(g&&e&&c){var f=g.length/3,b=new Float32Array(4*f),a=new Float32Array(6*f),f=a.subarray(3*f),k,h,l=vec3.create(),m=vec3.create(),p=vec3.create(),q=vec3.create();
k=0;for(h=d.length;k<h;k+=3){var r=d[k],w=d[k+1],u=d[k+2],t=g.subarray(3*r,3*r+3),x=g.subarray(3*w,3*w+3),F=g.subarray(3*u,3*u+3),L=c.subarray(2*r,2*r+2),z=c.subarray(2*w,2*w+2),n=c.subarray(2*u,2*u+2),O=x[0]-t[0],v=F[0]-t[0],y=x[1]-t[1],H=F[1]-t[1],x=x[2]-t[2],t=F[2]-t[2],F=z[0]-L[0],G=n[0]-L[0],z=z[1]-L[1],L=n[1]-L[1],n=F*L-G*z,n=1E-9>Math.abs(n)?0:1/n;vec3.copy(l,[(L*O-z*v)*n,(L*y-z*H)*n,(L*x-z*t)*n]);vec3.copy(m,[(F*v-G*O)*n,(F*H-G*y)*n,(F*t-G*x)*n]);vec3.add(a.subarray(3*r,3*r+3),a.subarray(3*
r,3*r+3),l);vec3.add(a.subarray(3*w,3*w+3),a.subarray(3*w,3*w+3),l);vec3.add(a.subarray(3*u,3*u+3),a.subarray(3*u,3*u+3),l);vec3.add(f.subarray(3*r,3*r+3),f.subarray(3*r,3*r+3),m);vec3.add(f.subarray(3*w,3*w+3),f.subarray(3*w,3*w+3),m);vec3.add(f.subarray(3*u,3*u+3),f.subarray(3*u,3*u+3),m)}k=0;for(h=g.length;k<h;k+=3)g=e.subarray(k,k+3),c=a.subarray(k,k+3),vec3.subtract(p,c,vec3.scale(p,g,vec3.dot(g,c))),vec3.normalize(p,p),g=0>vec3.dot(vec3.cross(q,g,c),f.subarray(k,k+3))?-1:1,b.set([p[0],p[1],
p[2],g],k/3*4);this.createVertexBuffer("tangents",Mesh.common_buffers.tangents.attribute,4,b)}};Mesh.prototype.computeTextureCoordinates=function(g){var e=this.vertexBuffers.vertices;if(!e)return console.error("Cannot compute uvs of a mesh without vertices");this.explodeIndices("triangles");var e=e.data,a=this.vertexBuffers.coords,b=new Float32Array(e.length/3*2),c=this.indexBuffers.triangles,d=null;c&&(d=c.data);var c=vec3.create(),f=vec3.create(),k=vec3.create(),h=this.getBoundingBox(),l=BBox.getCenter(h),
m=vec3.create();m.set(BBox.getHalfsize(h));vec3.scale(m,m,2);for(var h=d?d.length:e.length/3,p=0;p<h;p+=3){if(d)var q=d[p],r=d[p+1],w=d[p+2],n=e.subarray(3*q,3*q+3),t=e.subarray(3*r,3*r+3),x=e.subarray(3*w,3*w+3),q=b.subarray(2*q,2*q+2),r=b.subarray(2*r,2*r+2),w=b.subarray(2*w,2*w+2);else n=e.subarray(3*p,3*p+3),t=e.subarray(3*(p+1),3*(p+1)+3),x=e.subarray(3*(p+2),3*(p+2)+3),q=b.subarray(2*p,2*p+2),r=b.subarray(2*(p+1),2*(p+1)+2),w=b.subarray(2*(p+2),2*(p+2)+2);vec3.sub(f,n,t);vec3.sub(k,n,x);vec3.cross(c,
f,k);c[0]=Math.abs(c[0]);c[1]=Math.abs(c[1]);c[2]=Math.abs(c[2]);c[0]>c[1]&&c[0]>c[2]?(q[0]=(n[2]-l[2])/m[2],q[1]=(n[1]-l[1])/m[1],r[0]=(t[2]-l[2])/m[2],r[1]=(t[1]-l[1])/m[1],w[0]=(x[2]-l[2])/m[2],w[1]=(x[1]-l[1])/m[1]):c[1]>c[2]?(q[0]=(n[0]-l[0])/m[0],q[1]=(n[2]-l[2])/m[2],r[0]=(t[0]-l[0])/m[0],r[1]=(t[2]-l[2])/m[2],w[0]=(x[0]-l[0])/m[0],w[1]=(x[2]-l[2])/m[2]):(q[0]=(n[0]-l[0])/m[0],q[1]=(n[1]-l[1])/m[1],r[0]=(t[0]-l[0])/m[0],r[1]=(t[1]-l[1])/m[1],w[0]=(x[0]-l[0])/m[0],w[1]=(x[1]-l[1])/m[1])}a?(a.data=
b,a.upload(g)):this.createVertexBuffer("coords",Mesh.common_buffers.coords.attribute,2,b)};Mesh.prototype.getNumVertices=function(){var g=this.vertexBuffers.vertices;return g?g.data.length/g.spacing:0};Mesh.prototype.getNumTriangles=function(){var g=this.getIndexBuffer("triangles");return g?g.data.length/3:this.getNumVertices()/3};Mesh.computeBoundingBox=function(g,e,a){if(g){var b=0;if(a){for(var c=0;c<a.length;++c)if(a[c]){b=c;break}if(b==a.length){console.warn("mask contains only zeros, no vertices marked");
return}}for(var d=vec3.clone(g.subarray(3*b,3*b+3)),f=vec3.clone(g.subarray(3*b,3*b+3)),c=3*b;c<g.length;c+=3)if(!a||a[c/3])b=g.subarray(c,c+3),vec3.min(d,b,d),vec3.max(f,b,f);if(isNaN(d[0])||isNaN(d[1])||isNaN(d[2])||isNaN(f[0])||isNaN(f[1])||isNaN(f[2]))d[0]=d[1]=d[2]=0,f[0]=f[1]=f[2]=0,console.warn("Warning: GL.Mesh has NaN values in vertices");g=vec3.add(vec3.create(),d,f);vec3.scale(g,g,0.5);f=vec3.subtract(vec3.create(),f,g);return BBox.setCenterHalfsize(e||BBox.create(),g,f)}};Mesh.prototype.getBoundingBox=
function(){if(this._bounding)return this._bounding;this.updateBoundingBox();return this._bounding};Mesh.prototype.updateBoundingBox=function(){var g=this.vertexBuffers.vertices;g&&(h.Mesh.computeBoundingBox(g.data,this._bounding),this.info&&this.info.groups&&this.info.groups.length&&this.computeGroupsBoundingBoxes())};Mesh.prototype.computeGroupsBoundingBoxes=function(){var g=null,e=this.getIndexBuffer("triangles");e&&(g=e.data);e=this.getVertexBuffer("vertices");if(!e)return!1;e=e.data;if(!e.length)return!1;
for(var a=this.info.groups,b=0;b<a.length;++b){var c=a[b];c.bounding=c.bounding||BBox.create();var d=null;if(g){for(var d=new Uint8Array(e.length/3),f=c.start,k=0,s=c.length;k<s;k+=3)d[g[f+k]]=1,d[g[f+k+1]]=1,d[g[f+k+2]]=1;h.Mesh.computeBoundingBox(e,c.bounding,d)}else d=e.subarray(3*c.start,3*(c.start+c.length)),h.Mesh.computeBoundingBox(d,c.bounding)}return!0};Mesh.prototype.setBoundingBox=function(g,e){BBox.setCenterHalfsize(this._bounding,g,e)};Mesh.prototype.freeData=function(){for(var g in this.vertexBuffers)this.vertexBuffers[g].data=
null,delete this[this.vertexBuffers[g].name];for(var e in this.indexBuffers)this.indexBuffers[e].data=null,delete this[this.indexBuffers[e].name]};Mesh.prototype.configure=function(g,e){var a={},b={};e=e||{};for(var c in g)if(g[c])if("vertexBuffers"==c||"vertex_buffers"==c)for(d in g[c])a[d]=g[c][d];else if("indexBuffers"==c||"index_buffers"==c)for(d in g[c])b[d]=g[c][d];else"indices"==c||"lines"==c||"wireframe"==c||"triangles"==c?b[c]=g[c]:h.Mesh.common_buffers[c]?a[c]=g[c]:e[c]=g[c];this.addBuffers(a,
b,e.stream_type);for(var d in e)this[d]=e[d];e.bounding||this.updateBoundingBox()};Mesh.prototype.totalMemory=function(){var g=0,e;for(e in this.vertexBuffers)g+=this.vertexBuffers[e].data.buffer.byteLength;for(e in this.indexBuffers)g+=this.indexBuffers[e].data.buffer.byteLength;return g};Mesh.prototype.slice=function(g,e){var a={},b=this.indexBuffers.triangles;if(!b)return console.warn("splice in not indexed not supported yet"),null;var b=b.data,c=[],d=new Int32Array(b.length);d.fill(-1);var f=
g+e;f>=b.length&&(f=b.length);for(var k=0,s=g;s<f;++s){var l=b[s];if(-1!=d[l])c.push(d[l]);else{var m=k++;d[l]=m;c.push(m);for(var p in this.vertexBuffers){var q=this.vertexBuffers[p],m=q.data,q=q.spacing;a[p]||(a[p]=[]);for(var r=a[p],w=0;w<q;++w)r.push(m[w+l*q])}}}a=new h.Mesh(a,{triangles:c},null,gl);a.updateBoundingBox();return a};Mesh.prototype.simplify=function(){var g=this.getBoundingBox(),e=BBox.getMin(g),g=BBox.getHalfsize(g),g=vec3.scale(vec3.create(),g,2),a=new h.Mesh,b=vec3.create(),c;
for(c in this.vertexBuffers){var d=this.vertexBuffers[c].data,f=new Float32Array(d.length);if("vertices"==c)for(var k=0,s=d.length;k<s;k+=3){var l=d.subarray(k,k+3);vec3.sub(b,l,e);vec3.div(b,b,g);b[0]=Math.round(256*b[0])/256;b[1]=Math.round(256*b[1])/256;b[2]=Math.round(256*b[2])/256;vec3.mul(b,b,g);vec3.add(b,b,e);f.set(b,k)}a.addBuffer()}};Mesh.load=function(g,e,a,b){e=e||{};e.no_gl&&(b=null);a=a||new h.Mesh(null,null,null,b);a.configure(g,e);return a};Mesh.mergeMeshes=function(g,e){function a(a,
b,c,d){for(c=b+c;b<c;b+=3){var e=a.subarray(b,b+3);vec3.transformMat4(e,e,d)}}function b(a,b,c,d){for(c=b+c;b<c;b+=2){var e=a.subarray(b,b+2);vec2.transformMat3(e,e,d)}}function c(a,b,c,d){for(c=b+c;b<c;++b)a[b]+=d}e=e||{};for(var d={},f={},k={},s=[],l=0,m=[],p=0;p<g.length;++p){var q=g[p],r=q.mesh,w=l;s.push(w);var n=r.vertexBuffers.vertices.data.length/3,l=l+n,t;for(t in r.vertexBuffers)d[t]=d[t]?d[t]+r.vertexBuffers[t].data.length:r.vertexBuffers[t].data.length;for(t in r.indexBuffers)f[t]=f[t]?
f[t]+r.indexBuffers[t].data.length:r.indexBuffers[t].data.length;m.push({name:"mesh_"+p,start:w,length:n,material:""})}for(t in d)p=e[t],null===p?delete d[t]:(p||(p=Float32Array),d[t]=new p(d[t]),k[t]=0);for(t in f)f[t]=new Uint32Array(f[t]),k[t]=0;for(p=0;p<g.length;++p){q=g[p];r=q.mesh;n=w=0;for(t in r.vertexBuffers)d[t]&&("vertices"==t&&(n=r.vertexBuffers[t].data.length/3),d[t].set(r.vertexBuffers[t].data,k[t]),q[t+"_matrix"]&&(l=q[t+"_matrix"],16==l.length?a(d[t],k[t],r.vertexBuffers[t].data.length,
l):9==l.length&&b(d[t],k[t],r.vertexBuffers[t].data.length,l)),k[t]+=r.vertexBuffers[t].data.length);for(t in r.indexBuffers)f[t].set(r.indexBuffers[t].data,k[t]),c(f[t],k[t],r.indexBuffers[t].data.length,s[p]),k[t]+=r.indexBuffers[t].data.length}k={info:{groups:m}};return"undefined"!=typeof gl||e.only_data?new h.Mesh(d,f,k):{vertexBuffers:d,indexBuffers:f,info:{groups:m}}};Mesh.parsers={};Mesh.encoders={};Mesh.binary_file_formats={};Mesh.compressors={};Mesh.decompressors={};Mesh.fromURL=function(g,
e,a,b){b=b||{};a=a||n.gl;var c=new h.Mesh(void 0,void 0,void 0,a);c.ready=!1;a=g.lastIndexOf(".");var d=g.substr(a+1).toLowerCase();b.binary=Mesh.binary_file_formats[d];HttpRequest(g,null,function(a){c.parse(a,d);delete c.ready;e&&e.call(c,c,g)},function(a){e&&e(null)},b);return c};Mesh.prototype.parse=function(g,e){e=e.toLowerCase();var a=h.Mesh.parsers[e];if(a)return a.call(null,g,{mesh:this});throw"GL.Mesh.parse: no parser found for format "+e;};Mesh.prototype.encode=function(g,e){g=g.toLowerCase();
var a=h.Mesh.encoders[g];if(a)return a.call(null,this,e);throw"GL.Mesh.encode: no encoder found for format "+g;};Mesh.getScreenQuad=function(g){g=g||n.gl;var e=g.meshes[":screen_quad"];if(e)return e;var e=new Float32Array([0,0,0,1,1,0,0,1,0,0,0,0,1,0,0,1,1,0]),a=new Float32Array([0,0,1,1,0,1,0,0,1,0,1,1]),e=new h.Mesh({vertices:e,coords:a},void 0,void 0,g);return g.meshes[":screen_quad"]=e};h.linearizeArray=N;h.Mesh.EXTENSION="wbin";h.Mesh.enable_wbin_compression=!0;G.DEFAULT_NORMAL=vec3.fromValues(0,
1,0);G.DEFAULT_COORD=vec2.fromValues(0.5,0.5);G.DEFAULT_COLOR=vec4.fromValues(1,1,1,1);G.prototype.resize=function(g){var e={};this._vertex_data=new Float32Array(3*g);e.vertices=this._vertex_data;normals&&(e.normals=this._normal_data=new Float32Array(3*g));coords&&(e.coords=this._coord_data=new Float32Array(2*g));colors&&(e.colors=this._color_data=new Float32Array(4*g));this.addBuffers(e);this.current_pos=0;this.max_size=g;this._must_update=!0};G.prototype.clear=function(){this.current_pos=0};G.prototype.addPoint=
function(g,e,a,b){if(c>=this.max_size)return console.warn("DynamicMesh: not enough space, reserve more"),!1;var c=this.current_pos++;this._vertex_data.set(g,3*c);this._normal_data&&this._normal_data.set(e||G.DEFAULT_NORMAL,3*c);this._coord_data&&this._coord_data.set(a||G.DEFAULT_COORD,2*c);this._color_data&&this._color_data.set(b||G.DEFAULT_COLOR,4*c);return this._must_update=!0};G.prototype.update=function(g){if(!this._must_update&&!g)return this.current_pos;this._must_update=!1;this.getBuffer("vertices").upload(gl.STREAM_DRAW);
this._normal_data&&this.getBuffer("normal").upload(gl.STREAM_DRAW);this._coord_data&&this.getBuffer("coord").upload(gl.STREAM_DRAW);this._color_data&&this.getBuffer("color").upload(gl.STREAM_DRAW);return this.current_pos};extendClass(G,Mesh);Mesh.plane=function(g,e){g=g||{};g.triangles=[];var a=g.detailX||g.detail||1,b=g.detailY||g.detail||1,c=g.width||g.size||1,d=g.height||g.size||1,f=g.xz,c=0.5*c,d=0.5*d,k=[],s=[],l=[],m=[],p=vec3.fromValues(0,0,1);f&&p.set([0,1,0]);for(var q=0;q<=b;q++)for(var r=
q/b,w=0;w<=a;w++){var n=w/a;f?s.push((2*n-1)*c,0,-(2*r-1)*d):s.push((2*n-1)*c,(2*r-1)*d,0);l.push(n,r);m.push(p[0],p[1],p[2]);w<a&&q<b&&(n=w+q*(a+1),f?(k.push(n+1,n+a+1,n),k.push(n+1,n+a+2,n+a+1)):(k.push(n,n+1,n+a+1),k.push(n+a+1,n+1,n+a+2)))}a=BBox.fromCenterHalfsize([0,0,0],f?[c,0,d]:[c,d,0]);return h.Mesh.load({vertices:s,normals:m,coords:l,triangles:k},{bounding:a},e)};Mesh.plane2D=function(g,e){var a=new Float32Array([-1,1,1,-1,1,1,-1,1,-1,-1,1,-1]),b=new Float32Array([0,1,1,0,1,1,0,1,0,0,1,
0]);if(g&&g.size)for(var c=0.5*g.size,d=0;d<a.length;++d)a[d]*=c;return new h.Mesh({vertices2D:a,coords:b},null,e)};Mesh.point=function(g){return new h.Mesh({vertices:[0,0,0]})};Mesh.cube=function(g,e){g=g||{};var a=0.5*(g.size||1),b={};b.vertices=new Float32Array([-1,1,-1,-1,-1,1,-1,1,1,-1,1,-1,-1,-1,-1,-1,-1,1,1,1,-1,1,1,1,1,-1,1,1,1,-1,1,-1,1,1,-1,-1,-1,1,1,1,-1,1,1,1,1,-1,1,1,-1,-1,1,1,-1,1,-1,1,-1,1,1,-1,1,-1,-1,-1,1,-1,1,-1,-1,-1,-1,-1,-1,1,-1,1,1,1,1,1,-1,-1,1,-1,-1,1,1,1,1,1,-1,-1,-1,1,-1,
-1,1,-1,1,-1,-1,-1,1,-1,1,-1,-1,1]);for(var c=0,d=b.vertices.length;c<d;++c)b.vertices[c]*=a;b.normals=new Float32Array([-1,0,0,-1,0,0,-1,0,0,-1,0,0,-1,0,0,-1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,-1,0,0,-1,0,0,-1,0,0,-1,0,0,-1,0,0,-1,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,-1,0,0,-1,0,0,-1,0,0,-1,0,0,-1,0,0,-1,0]);b.coords=new Float32Array([0,1,1,0,1,1,0,1,0,0,1,0,1,1,0,1,0,0,1,1,0,0,1,0,0,1,1,0,1,1,0,1,0,0,1,0,1,1,0,1,0,0,1,1,0,0,1,0,0,1,1,0,1,1,0,1,0,0,1,
0,1,1,0,1,0,0,1,1,0,0,1,0]);g.wireframe&&(b.wireframe=new Uint16Array([0,2,2,5,5,4,4,0,6,7,7,10,10,11,11,6,0,6,2,7,5,10,4,11]));g.bounding=BBox.fromCenterHalfsize([0,0,0],[a,a,a]);return h.Mesh.load(b,g,e)};Mesh.box=function(g,e){g=g||{};var a=g.sizex||1,b=g.sizey||1,c=g.sizez||1,a=0.5*a,b=0.5*b,c=0.5*c,d={};d.vertices=new Float32Array([-1,1,-1,-1,-1,1,-1,1,1,-1,1,-1,-1,-1,-1,-1,-1,1,1,1,-1,1,1,1,1,-1,1,1,1,-1,1,-1,1,1,-1,-1,-1,1,1,1,-1,1,1,1,1,-1,1,1,-1,-1,1,1,-1,1,-1,1,-1,1,1,-1,1,-1,-1,-1,1,-1,
1,-1,-1,-1,-1,-1,-1,1,-1,1,1,1,1,1,-1,-1,1,-1,-1,1,1,1,1,1,-1,-1,-1,1,-1,-1,1,-1,1,-1,-1,-1,1,-1,1,-1,-1,1]);for(var f=0,k=d.vertices.length;f<k;f+=3)d.vertices[f]*=a,d.vertices[f+1]*=b,d.vertices[f+2]*=c;d.normals=new Float32Array([-1,0,0,-1,0,0,-1,0,0,-1,0,0,-1,0,0,-1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,-1,0,0,-1,0,0,-1,0,0,-1,0,0,-1,0,0,-1,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,-1,0,0,-1,0,0,-1,0,0,-1,0,0,-1,0,0,-1,0]);d.coords=new Float32Array([0,1,1,
0,1,1,0,1,0,0,1,0,1,1,0,1,0,0,1,1,0,0,1,0,0,1,1,0,1,1,0,1,0,0,1,0,1,1,0,1,0,0,1,1,0,0,1,0,0,1,1,0,1,1,0,1,0,0,1,0,1,1,0,1,0,0,1,1,0,0,1,0]);g.wireframe&&(d.wireframe=new Uint16Array([0,2,2,5,5,4,4,0,6,7,7,10,10,11,11,6,0,6,2,7,5,10,4,11]));g.bounding=BBox.fromCenterHalfsize([0,0,0],[a,b,c]);return h.Mesh.load(d,g,e)};Mesh.circle=function(g,e){g=g||{};var a=g.size||g.radius||1,b=Math.ceil(g.slices||24),c=g.xz||!1,d=g.empty||!1;3>b&&(b=3);var f=2*Math.PI/b,k=vec3.create(),s=vec3.create(),l=vec3.fromValues(0,
0,1),m=vec2.fromValues(0.5,0.5),p=vec2.create();c&&l.set([0,1,0]);var q=c?2:1,r=new Float32Array(3*(b+1)),w=new Float32Array(3*(b+1)),n=new Float32Array(2*(b+1)),t=null;r.set(k,0);w.set(l,0);n.set(m,0);for(k=m=t=0;k<b;++k)t=Math.sin(f*k),m=Math.cos(f*k),s[0]=t*a,s[q]=m*a,p[0]=0.5*t+0.5,p[1]=0.5*m+0.5,r.set(s,3*k+3),w.set(l,3*k+3),n.set(p,2*k+2);if(d)r=r.subarray(3),w=r.subarray(3),n=r.subarray(2),t=null;else{t=new Uint16Array(3*b);d=2;f=1;c&&(d=1,f=2);for(k=0;k<b-1;++k)t[3*k]=0,t[3*k+1]=k+d,t[3*k+
2]=k+f;t[3*k]=0;c?(t[3*k+1]=k+1,t[3*k+2]=1):(t[3*k+1]=1,t[3*k+2]=k+1)}g.bounding=BBox.fromCenterHalfsize([0,0,0],c?[a,0,a]:[a,a,0]);a={vertices:r,normals:w,coords:n,triangles:t};if(g.wireframe){c=new Uint16Array(2*b);for(k=0;k<b;k++)c[2*k]=k,c[2*k+1]=k+1;c[0]=b;a.wireframe=c}return h.Mesh.load(a,g,e)};Mesh.cylinder=function(g,e){g=g||{};for(var a=g.radius||g.size||1,b=g.height||g.size||2,c=g.subdivisions||64,d=new Float32Array(36*c),f=new Float32Array(36*c),k=new Float32Array(24*c),h=2*Math.PI/c,
l=null,m=0;m<c;++m){var p=m*h,l=[Math.sin(p),0,Math.cos(p)];d.set([l[0]*a,0.5*b,l[2]*a],18*m);f.set(l,18*m);k.set([m/c,1],12*m);l=[Math.sin(p),0,Math.cos(p)];d.set([l[0]*a,-0.5*b,l[2]*a],18*m+3);f.set(l,18*m+3);k.set([m/c,0],12*m+2);l=[Math.sin(p+h),0,Math.cos(p+h)];d.set([l[0]*a,-0.5*b,l[2]*a],18*m+6);f.set(l,18*m+6);k.set([(m+1)/c,0],12*m+4);l=[Math.sin(p+h),0,Math.cos(p+h)];d.set([l[0]*a,0.5*b,l[2]*a],18*m+9);f.set(l,18*m+9);k.set([(m+1)/c,1],12*m+6);l=[Math.sin(p),0,Math.cos(p)];d.set([l[0]*a,
0.5*b,l[2]*a],18*m+12);f.set(l,18*m+12);k.set([m/c,1],12*m+8);l=[Math.sin(p+h),0,Math.cos(p+h)];d.set([l[0]*a,-0.5*b,l[2]*a],18*m+15);f.set(l,18*m+15);k.set([(m+1)/c,0],12*m+10)}var l=18*m,q=12*m;if(!1===g.caps)d=d.subarray(0,l),f=f.subarray(0,l),k=k.subarray(0,q);else for(var r=vec3.fromValues(0,0.5*b,0),w=vec3.fromValues(0,-0.5*b,0),n=vec3.fromValues(0,1,0),t=vec3.fromValues(0,-1,0),m=0;m<c;++m){var p=m*h,x=vec3.fromValues(Math.sin(p),0,Math.cos(p)),p=vec3.fromValues(Math.sin(p+h),0,Math.cos(p+
h));d.set([x[0]*a,0.5*b,x[2]*a],l+18*m);f.set(n,l+18*m);k.set([0.5*-x[0]+0.5,0.5*x[2]+0.5],q+12*m);d.set([p[0]*a,0.5*b,p[2]*a],l+18*m+3);f.set(n,l+18*m+3);k.set([0.5*-p[0]+0.5,0.5*p[2]+0.5],q+12*m+2);d.set(r,l+18*m+6);f.set(n,l+18*m+6);k.set([0.5,0.5],q+12*m+4);d.set([p[0]*a,-0.5*b,p[2]*a],l+18*m+9);f.set(t,l+18*m+9);k.set([0.5*p[0]+0.5,0.5*p[2]+0.5],q+12*m+6);d.set([x[0]*a,-0.5*b,x[2]*a],l+18*m+12);f.set(t,l+18*m+12);k.set([0.5*x[0]+0.5,0.5*x[2]+0.5],q+12*m+8);d.set(w,l+18*m+15);f.set(t,l+18*m+15);
k.set([0.5,0.5],q+12*m+10)}c={vertices:d,normals:f,coords:k};g.bounding=BBox.fromCenterHalfsize([0,0,0],[a,0.5*b,a]);g.info={groups:[]};!1!==g.caps&&(g.info.groups.push({name:"side",start:0,length:l/3}),g.info.groups.push({name:"caps",start:l/3,length:(d.length-l)/3}));return Mesh.load(c,g,e)};Mesh.cone=function(g,e){g=g||{};for(var a=g.radius||g.size||1,b=g.height||g.size||2,c=g.subdivisions||64,d=new Float32Array(18*c),f=new Float32Array(18*c),k=new Float32Array(12*c),h=2*Math.PI/c,l=null,m=a/b,
p=0;p<c;++p){var q=p*h,l=[Math.sin(q+0.5*h),m,Math.cos(q+0.5*h)];vec3.normalize(l,l);d.set([0,b,0],18*p);f.set(l,18*p);k.set([p/c,1],12*p);l=[Math.sin(q),m,Math.cos(q)];d.set([l[0]*a,0,l[2]*a],18*p+3);vec3.normalize(l,l);f.set(l,18*p+3);k.set([p/c,0],12*p+2);l=[Math.sin(q+h),m,Math.cos(q+h)];d.set([l[0]*a,0,l[2]*a],18*p+6);vec3.normalize(l,l);f.set(l,18*p+6);k.set([(p+1)/c,0],12*p+4)}l=vec3.fromValues(0,0,0);m=vec3.fromValues(0,-1,0);for(p=0;p<c;++p){var q=p*h,r=vec3.fromValues(Math.sin(q),0,Math.cos(q)),
q=vec3.fromValues(Math.sin(q+h),0,Math.cos(q+h));d.set([q[0]*a,0,q[2]*a],18*p+9);f.set(m,18*p+9);k.set([0.5*q[0]+0.5,0.5*q[2]+0.5],12*p+6);d.set([r[0]*a,0,r[2]*a],18*p+12);f.set(m,18*p+12);k.set([0.5*r[0]+0.5,0.5*r[2]+0.5],12*p+8);d.set(l,18*p+15);f.set(m,18*p+15);k.set([0.5,0.5],12*p+10)}c={vertices:d,normals:f,coords:k};g.bounding=BBox.fromCenterHalfsize([0,0.5*b,0],[a,0.5*b,a]);return Mesh.load(c,g,e)};Mesh.sphere=function(g,e){g=g||{};for(var a=g.radius||g.size||1,b=g.lat||g.subdivisions||16,
c=g["long"]||g.subdivisions||16,d=new Float32Array((b+1)*(c+1)*3),f=new Float32Array((b+1)*(c+1)*3),k=new Float32Array((b+1)*(c+1)*2),s=new Uint16Array(b*c*6),l=g.hemi?0.5*Math.PI:Math.PI,m=0,p=0,q=0;q<=b;q++)for(var r=q*l/b,n=Math.sin(r),u=Math.cos(r),r=0;r<=c;r++){var t=2*r*Math.PI/c,x=Math.sin(t),t=Math.cos(t)*n,F=u,x=x*n,L=1-r/c,z=1-q/b;d.set([a*t,a*F,a*x],m);f.set([t,F,x],m);k.set([L,z],p);m+=3;p+=2}for(q=m=0;q<b;q++)for(r=0;r<c;r++)l=q*(c+1)+r,p=l+c+1,s.set([p,l,l+1],m),s.set([p+1,p,l+1],m+
3),m+=6;d={vertices:d,normals:f,coords:k,triangles:s};if(g.wireframe){f=new Uint16Array(c*b*4);for(m=k=0;m<b;m++){for(s=0;s<c;s++)f[k]=m*(c+1)+s,f[k+1]=m*(c+1)+s+1,k+=2;f[k-2*c]=m*(c+1)+s}for(m=0;m<c;m++)for(s=0;s<b;s++)f[k]=s*(c+1)+m,f[k+1]=(s+1)*(c+1)+m,k+=2;d.wireframe=f}g.bounding=g.hemi?BBox.fromCenterHalfsize([0,0.5*a,0],[a,0.5*a,a],a):BBox.fromCenterHalfsize([0,0,0],[a,a,a],a);return h.Mesh.load(d,g,e)};Mesh.grid=function(g,e){g=g||{};var a=g.lines||11;0>a&&(a=1);for(var b=g.size||10,c=new Float32Array(12*
a),d=0.5*b,f=0,k=-d,b=b/(a-1),s=0;s<a;s++)c[f]=k,c[f+2]=-d,c[f+3]=k,c[f+5]=d,c[f+6]=d,c[f+8]=k,c[f+9]=-d,c[f+11]=k,k+=b,f+=12;return new h.Mesh({vertices:c},g,e)};Mesh.icosahedron=function(g,e){function a(a,c){var d=l[a]<l[c]?l[a]+":"+l[c]:l[c]+":"+l[a],e=u[d];if(e)return e;e=f.length/3;f.push(0.5*(f[3*l[a]]+f[3*l[c]]),0.5*(f[3*l[a]+1]+f[3*l[c]+1]),0.5*(f[3*l[a]+2]+f[3*l[c]+2]));var g=Math.sqrt(f[3*e]*f[3*e]+f[3*e+1]*f[3*e+1]+f[3*e+2]*f[3*e+2]),h=f[3*e]/g,m=f[3*e+1]/g,t=f[3*e+2]/g;k.push(h,m,t);s.push(Math.atan2(h,
t)/Math.PI*0.5,Math.acos(m)/Math.PI);f[3*e]*=b/g;f[3*e+1]*=b/g;f[3*e+2]*=b/g;return u[d]=e}g=g||{};var b=g.radius||g.size||1,c=void 0===g.subdivisions?0:g.subdivisions;6<c&&(c=6);for(var d=(1+Math.sqrt(5))/2,f=[-1,d,0,1,d,0,-1,-d,0,1,-d,0,0,-1,d,0,1,d,0,-1,-d,0,1,-d,d,0,-1,d,0,1,-d,0,-1,-d,0,1],k=[],s=[],l=[0,11,5,0,5,1,0,1,7,0,7,10,0,10,11,1,5,9,5,11,4,11,10,2,10,7,6,7,1,8,3,9,4,3,4,2,3,2,6,3,6,8,3,8,9,4,9,5,2,4,11,6,2,10,8,6,7,9,8,1],d=f.length,m=0;m<d;m+=3){var p=Math.sqrt(f[m]*f[m]+f[m+1]*f[m+
1]+f[m+2]*f[m+2]),q=f[m]/p,r=f[m+1]/p,n=f[m+2]/p;k.push(q,r,n);s.push(Math.atan2(q,n),Math.acos(r));f[m]*=b/p;f[m+1]*=b/p;f[m+2]*=b/p}for(var u={},p=0;p<c;++p){q=[];d=l.length;for(m=0;m<d;m+=3){var r=a(m,m+1),n=a(m+1,m+2),t=a(m+2,m);q.push(l[m],r,t);q.push(l[m+1],n,r);q.push(l[m+2],t,n);q.push(r,n,t)}l=q}g.bounding=BBox.fromCenterHalfsize([0,0,0],[b,b,b],b);return new h.Mesh.load({vertices:f,coords:s,normals:k,triangles:l},g,e)};n.Texture=h.Texture=function e(a,b,c,d){function f(a){return a.constructor!==
Array?a:k==h.FLOAT?new Float32Array(a):k==h.HALF_FLOAT_OES?new Uint16Array(a):new Uint8Array(a)}c=c||{};this.gl=d=d||n.gl;this._context_id=d.context_id;a=parseInt(a);b=parseInt(b);h.debug&&console.log("GL.Texture created: ",a,b);this.handler=d.createTexture();this.width=a;this.height=b;c.depth&&(this.depth=c.depth);this.texture_type=c.texture_type||d.TEXTURE_2D;this.format=c.format||e.DEFAULT_FORMAT;this.internalFormat=c.internalFormat;this.type=c.type||e.DEFAULT_TYPE;this.magFilter=c.magFilter||
c.filter||e.DEFAULT_MAG_FILTER;this.minFilter=c.minFilter||c.filter||e.DEFAULT_MIN_FILTER;this.wrapS=c.wrap||c.wrapS||e.DEFAULT_WRAP_S;this.wrapT=c.wrap||c.wrapT||e.DEFAULT_WRAP_T;this.data=null;e.MAX_TEXTURE_IMAGE_UNITS||(e.MAX_TEXTURE_IMAGE_UNITS=d.getParameter(d.MAX_TEXTURE_IMAGE_UNITS));this.has_mipmaps=!1;if(this.format==d.DEPTH_COMPONENT&&1==d.webgl_version&&!d.extensions.WEBGL_depth_texture)throw"Depth Texture not supported";if(this.type==d.FLOAT&&!d.extensions.OES_texture_float&&1==d.webgl_version)throw"Float Texture not supported";
if(this.type==d.HALF_FLOAT_OES){if(!d.extensions.OES_texture_half_float&&1==d.webgl_version)throw"Half Float Texture extension not supported.";1<d.webgl_version&&(console.warn("using HALF_FLOAT_OES in WebGL2 is deprecated, suing HALF_FLOAT instead"),this.type=this.format==d.RGB?d.RGB16F:d.RGBA16F)}if(!(isPowerOfTwo(this.width)&&isPowerOfTwo(this.height)||(this.minFilter==d.NEAREST||this.minFilter==d.LINEAR)&&this.wrapS==d.CLAMP_TO_EDGE&&this.wrapT==d.CLAMP_TO_EDGE))if(c.ignore_pot)this.minFilter=
this.magFilter=d.LINEAR,this.wrapS=this.wrapT=d.CLAMP_TO_EDGE;else throw"Cannot use texture-wrap or mipmaps in Non-Power-of-Two textures";if(a&&b){this.internalFormat||this.computeInternalFormat();d.activeTexture(d.TEXTURE0+e.MAX_TEXTURE_IMAGE_UNITS-1);d.bindTexture(this.texture_type,this.handler);d.texParameteri(this.texture_type,d.TEXTURE_MAG_FILTER,this.magFilter);d.texParameteri(this.texture_type,d.TEXTURE_MIN_FILTER,this.minFilter);d.texParameteri(this.texture_type,d.TEXTURE_WRAP_S,this.wrapS);
d.texParameteri(this.texture_type,d.TEXTURE_WRAP_T,this.wrapT);c.anisotropic&&d.extensions.EXT_texture_filter_anisotropic&&d.texParameterf(h.TEXTURE_2D,d.extensions.EXT_texture_filter_anisotropic.TEXTURE_MAX_ANISOTROPY_EXT,c.anisotropic);var k=this.type,s=c.pixel_data;if(s&&!s.buffer){if(this.texture_type==h.TEXTURE_CUBE_MAP)if(s[0].constructor===Number)s=f(s),s=[s,s,s,s,s,s];else for(var l=0;l<s.length;++l)s[l]=f(s[l]);else s=f(s);this.data=s}if(this.texture_type==h.TEXTURE_2D)d.texImage2D(h.TEXTURE_2D,
0,this.internalFormat,a,b,0,this.format,this.type,s||null),h.isPowerOfTwo(a)&&h.isPowerOfTwo(b)&&c.minFilter&&c.minFilter!=d.NEAREST&&c.minFilter!=d.LINEAR&&(d.generateMipmap(this.texture_type),this.has_mipmaps=!0);else if(this.texture_type==h.TEXTURE_CUBE_MAP)for(l=0;6>l;++l)d.texImage2D(d.TEXTURE_CUBE_MAP_POSITIVE_X+l,0,this.internalFormat,this.width,this.height,0,this.format,this.type,s?s[l]:null);else if(this.texture_type==h.TEXTURE_3D){if(1==this.gl.webgl_version)throw"TEXTURE_3D not supported in WebGL 1. Enable WebGL 2 in the context by passing webgl2:true to the context";
if(!c.depth)throw"3d texture depth must be set in the options.depth";d.pixelStorei(d.UNPACK_PREMULTIPLY_ALPHA_WEBGL,!1);d.pixelStorei(d.UNPACK_FLIP_Y_WEBGL,!1);d.texImage3D(h.TEXTURE_3D,0,this.internalFormat,a,b,c.depth,0,this.format,this.type,s||null)}d.bindTexture(this.texture_type,null);d.activeTexture(d.TEXTURE0)}};Texture.DEFAULT_TYPE=h.UNSIGNED_BYTE;Texture.DEFAULT_FORMAT=h.RGBA;Texture.DEFAULT_MAG_FILTER=h.LINEAR;Texture.DEFAULT_MIN_FILTER=h.LINEAR;Texture.DEFAULT_WRAP_S=h.CLAMP_TO_EDGE;Texture.DEFAULT_WRAP_T=
h.CLAMP_TO_EDGE;Texture.EXTENSION="png";Texture.framebuffer=null;Texture.renderbuffer=null;Texture.loading_color=new Uint8Array([0,0,0,0]);Texture.use_renderbuffer_pool=!0;Texture.prototype.computeInternalFormat=function(){this.internalFormat=this.format;if(this.format==h.DEPTH_COMPONENT)if(this.minFilter=this.magFilter=h.NEAREST,2==gl.webgl_version)if(this.type==h.UNSIGNED_SHORT)this.internalFormat=h.DEPTH_COMPONENT16;else if(this.type==h.UNSIGNED_INT)this.internalFormat=h.DEPTH_COMPONENT24;else if(this.type==
h.FLOAT)this.internalFormat=h.DEPTH_COMPONENT32F;else throw"unsupported type for a depth texture";else{if(1==gl.webgl_version){if(this.type==h.FLOAT)throw"WebGL 1.0 does not support float depth textures";this.internalFormat=h.DEPTH_COMPONENT}}else this.format==gl.RGBA&&(2==gl.webgl_version?this.type==h.FLOAT?this.internalFormat=h.RGBA32F:this.type==h.HALF_FLOAT?this.internalFormat=h.RGBA16F:this.type==h.HALF_FLOAT_OES&&(console.warn("webgl 2 does not use HALF_FLOAT_OES, converting to HALF_FLOAT"),
this.type=h.HALF_FLOAT,this.internalFormat=h.RGBA16F):1==gl.webgl_version&&this.type==h.HALF_FLOAT&&(console.warn("webgl 1 does not use HALF_FLOAT, converting to HALF_FLOAT_OES"),this.type=h.HALF_FLOAT_OES))};Texture.prototype.delete=function(){gl.deleteTexture(this.handler);this.handler=null};Texture.prototype.getProperties=function(){return{width:this.width,height:this.height,type:this.type,format:this.format,texture_type:this.texture_type,magFilter:this.magFilter,minFilter:this.minFilter,wrapS:this.wrapS,
wrapT:this.wrapT}};Texture.prototype.hasSameProperties=function(e){return e?e.width==this.width&&e.height==this.height&&e.type==this.type&&e.format==this.format&&e.texture_type==this.texture_type:!1};Texture.prototype.hasSameSize=function(e){return e?e.width==this.width&&e.height==this.height:!1};Texture.prototype.toJSON=function(){return""};Texture.isDepthSupported=function(){return null!=gl.extensions.WEBGL_depth_texture};Texture.prototype.bind=function(e){void 0==e&&(e=0);var a=this.gl;a.activeTexture(a.TEXTURE0+
e);a.bindTexture(this.texture_type,this.handler);return e};Texture.prototype.unbind=function(e){void 0===e&&(e=0);var a=this.gl;a.activeTexture(a.TEXTURE0+e);a.bindTexture(this.texture_type,null)};Texture.prototype.setParameter=function(e,a){this.bind(0);this.gl.texParameteri(this.texture_type,e,a);switch(e){case this.gl.TEXTURE_MAG_FILTER:this.magFilter=a;break;case this.gl.TEXTURE_MIN_FILTER:this.minFilter=a;break;case this.gl.TEXTURE_WRAP_S:this.wrapS=a;break;case this.gl.TEXTURE_WRAP_T:this.wrapT=
a}};Texture.setUploadOptions=function(e,a){a=a||n.gl;e?(a.pixelStorei(a.UNPACK_PREMULTIPLY_ALPHA_WEBGL,!!e.premultiply_alpha),a.pixelStorei(a.UNPACK_FLIP_Y_WEBGL,!e.no_flip)):(a.pixelStorei(a.UNPACK_PREMULTIPLY_ALPHA_WEBGL,!1),a.pixelStorei(a.UNPACK_FLIP_Y_WEBGL,!0));a.pixelStorei(a.UNPACK_ALIGNMENT,1)};Texture.prototype.uploadImage=function(e,a){this.bind();var b=this.gl;if(!e)throw"uploadImage parameter must be Image";Texture.setUploadOptions(a,b);try{b.texImage2D(b.TEXTURE_2D,0,this.format,this.format,
this.type,e),this.width=e.videoWidth||e.width,this.height=e.videoHeight||e.height,this.data=e}catch(c){if("file:"==location.protocol)throw'image not loaded for security reasons (serve this page over "http://" instead)';throw"image not loaded for security reasons (image must originate from the same domain as this page or use Cross-Origin Resource Sharing)";}this.minFilter&&this.minFilter!=b.NEAREST&&this.minFilter!=b.LINEAR&&(b.generateMipmap(this.texture_type),this.has_mipmaps=!0);b.bindTexture(this.texture_type,
null)};Texture.prototype.uploadData=function(e,a,b){a=a||{};if(!e)throw"no data passed";var c=this.gl;this.bind();Texture.setUploadOptions(a,c);var d=a.mipmap_level||0,f=this.width,k=this.height,f=f>>d,k=k>>d,s=this.internalFormat||this.format;this.type==h.HALF_FLOAT_OES&&e.constructor===Float32Array&&console.warn("cannot uploadData to a HALF_FLOAT texture from a Float32Array, must be Uint16Array. To upload it we recomment to create a FLOAT texture, upload data there and copy to your HALF_FLOAT.");
if(this.texture_type==h.TEXTURE_2D)1==c.webgl_version?e.buffer&&e.buffer.constructor==ArrayBuffer?c.texImage2D(this.texture_type,d,s,f,k,0,this.format,this.type,e):c.texImage2D(this.texture_type,d,s,this.format,this.type,e):2==c.webgl_version&&c.texImage2D(this.texture_type,d,s,f,k,0,this.format,this.type,e);else if(this.texture_type==h.TEXTURE_3D)c.pixelStorei(c.UNPACK_PREMULTIPLY_ALPHA_WEBGL,!1),c.pixelStorei(c.UNPACK_FLIP_Y_WEBGL,!1),c.texImage3D(this.texture_type,d,s,f,k,this.depth>>d,0,this.format,
this.type,e);else if(this.texture_type==h.TEXTURE_CUBE_MAP)c.texImage2D(c.TEXTURE_CUBE_MAP_POSITIVE_X+(a.cubemap_face||0),d,s,f,k,0,this.format,this.type,e);else throw"cannot uploadData for this texture type";this.data=e;!b&&this.minFilter&&this.minFilter!=c.NEAREST&&this.minFilter!=c.LINEAR&&(c.generateMipmap(this.texture_type),this.has_mipmaps=!0);c.bindTexture(this.texture_type,null)};Texture.cubemap_camera_parameters=[{type:"posX",dir:vec3.fromValues(1,0,0),up:vec3.fromValues(0,1,0),right:vec3.fromValues(0,
0,-1)},{type:"negX",dir:vec3.fromValues(-1,0,0),up:vec3.fromValues(0,1,0),right:vec3.fromValues(0,0,1)},{type:"posY",dir:vec3.fromValues(0,1,0),up:vec3.fromValues(0,0,-1),right:vec3.fromValues(1,0,0)},{type:"negY",dir:vec3.fromValues(0,-1,0),up:vec3.fromValues(0,0,1),right:vec3.fromValues(1,0,0)},{type:"posZ",dir:vec3.fromValues(0,0,1),up:vec3.fromValues(0,1,0),right:vec3.fromValues(1,0,0)},{type:"negZ",dir:vec3.fromValues(0,0,-1),up:vec3.fromValues(0,1,0),right:vec3.fromValues(-1,0,0)}];Texture.prototype.drawTo=
function(e,a){function b(){6E4<=h.getTime()-this.time?(c.deleteRenderbuffer(c._renderbuffers_pool[m]),delete c._renderbuffers_pool[m]):setTimeout(b.bind(this),6E4)}var c=this.gl,d=c.getViewport(),f=h.getTime(),k=c.getParameter(c.FRAMEBUFFER_BINDING),s=c._framebuffer=c._framebuffer||c.createFramebuffer();c.bindFramebuffer(c.FRAMEBUFFER,s);var l=null;if(Texture.use_renderbuffer_pool){c._renderbuffers_pool||(c._renderbuffers_pool={});var m=this.width+":"+this.height;c._renderbuffers_pool[m]?(l=c._renderbuffers_pool[m],
l.time=f,c.bindRenderbuffer(c.RENDERBUFFER,l)):(c._renderbuffers_pool[m]=l=c.createRenderbuffer(),l.time=f,l.width=this.width,l.height=this.height,c.bindRenderbuffer(c.RENDERBUFFER,l),setTimeout(b.bind(l),6E4))}else l=c._renderbuffer=c._renderbuffer||c.createRenderbuffer(),l.width=this.width,l.height=this.height,c.bindRenderbuffer(c.RENDERBUFFER,l);this.format===c.DEPTH_COMPONENT?c.renderbufferStorage(c.RENDERBUFFER,c.RGBA4,this.width,this.height):c.renderbufferStorage(c.RENDERBUFFER,c.DEPTH_COMPONENT16,
this.width,this.height);c.viewport(0,0,this.width,this.height);c._current_texture_drawto=this;c._current_fbo_color=s;c._current_fbo_depth=l;if(this.texture_type==c.TEXTURE_2D)this.format!==c.DEPTH_COMPONENT?(c.framebufferTexture2D(c.FRAMEBUFFER,c.COLOR_ATTACHMENT0,c.TEXTURE_2D,this.handler,0),c.framebufferRenderbuffer(c.FRAMEBUFFER,c.DEPTH_ATTACHMENT,c.RENDERBUFFER,l)):(c.framebufferRenderbuffer(c.FRAMEBUFFER,c.COLOR_ATTACHMENT0,c.RENDERBUFFER,l),c.framebufferTexture2D(c.FRAMEBUFFER,c.DEPTH_ATTACHMENT,
c.TEXTURE_2D,this.handler,0)),e(this,a);else if(this.texture_type==c.TEXTURE_CUBE_MAP)for(this.format!==c.DEPTH_COMPONENT?c.framebufferRenderbuffer(c.FRAMEBUFFER,c.DEPTH_ATTACHMENT,c.RENDERBUFFER,l):c.framebufferRenderbuffer(c.FRAMEBUFFER,c.COLOR_ATTACHMENT0,c.RENDERBUFFER,l),f=0;6>f;f++)this.format!==c.DEPTH_COMPONENT?c.framebufferTexture2D(c.FRAMEBUFFER,c.COLOR_ATTACHMENT0,c.TEXTURE_CUBE_MAP_POSITIVE_X+f,this.handler,0):c.framebufferTexture2D(c.FRAMEBUFFER,c.DEPTH_ATTACHMENT,c.TEXTURE_CUBE_MAP_POSITIVE_X+
f,this.handler,0),e(this,f,a);this.data=null;c._current_texture_drawto=null;c._current_fbo_color=null;c._current_fbo_depth=null;c.bindFramebuffer(c.FRAMEBUFFER,k);c.bindRenderbuffer(c.RENDERBUFFER,null);c.viewport(d[0],d[1],d[2],d[3]);return this};Texture.drawTo=function(e,a,b){var c=-1,d=-1,f=null;if(!e&&!b)throw"Textures missing in drawTo";if(e&&e.length)for(var k=0;k<e.length;k++){var h=e[k];if(-1==c)c=h.width;else if(c!=h.width)throw"Cannot use Texture.drawTo if textures have different dimensions";
if(-1==d)d=h.height;else if(d!=h.height)throw"Cannot use Texture.drawTo if textures have different dimensions";if(null==f)f=h.type;else if(f!=h.type)throw"Cannot use Texture.drawTo if textures have different data type, all must have the same type";}else c=b.width,d=b.height;var l=gl.extensions.WEBGL_draw_buffers;if(!l&&e&&1<e.length)throw"Rendering to several textures not supported";f=gl.getViewport();gl._framebuffer=gl._framebuffer||gl.createFramebuffer();gl.bindFramebuffer(gl.FRAMEBUFFER,gl._framebuffer);
gl.viewport(0,0,c,d);k=null;if(b&&b.format!==gl.DEPTH_COMPONENT||b.type!=gl.UNSIGNED_INT)throw"Depth texture must be of format: gl.DEPTH_COMPONENT and type: gl.UNSIGNED_INT";b?gl.framebufferTexture2D(gl.FRAMEBUFFER,gl.DEPTH_ATTACHMENT,gl.TEXTURE_2D,b.handler,0):(k=gl._renderbuffer=gl._renderbuffer||gl.createRenderbuffer(),k.width=c,k.height=d,gl.bindRenderbuffer(gl.RENDERBUFFER,k),gl.renderbufferStorage(gl.RENDERBUFFER,gl.DEPTH_COMPONENT16,c,d),gl.framebufferRenderbuffer(gl.FRAMEBUFFER,gl.DEPTH_ATTACHMENT,
gl.RENDERBUFFER,k));if(e){c=[];for(k=0;k<e.length;k++)h=e[k],gl.framebufferTexture2D(gl.FRAMEBUFFER,gl.COLOR_ATTACHMENT0+k,gl.TEXTURE_2D,h.handler,0),c.push(gl.COLOR_ATTACHMENT0+k);1<e.length&&l.drawBuffersWEBGL(c)}else b=this._color_renderbuffer=this._color_renderbuffer||gl.createRenderbuffer(),b.width=c,b.height=d,gl.bindRenderbuffer(gl.RENDERBUFFER,b),gl.renderbufferStorage(gl.RENDERBUFFER,gl.RGBA4,c,d),gl.framebufferRenderbuffer(gl.FRAMEBUFFER,gl.COLOR_ATTACHMENT0,gl.RENDERBUFFER,b);c=gl.checkFramebufferStatus(gl.FRAMEBUFFER);
if(c!==gl.FRAMEBUFFER_COMPLETE)throw"FBO not complete: "+c;a();if(e.length)for(k=0;k<e.length;++k)e[k].data=null;gl.bindFramebuffer(gl.FRAMEBUFFER,null);gl.viewport(f[0],f[1],f[2],f[3])};Texture.drawToColorAndDepth=function(e,a,b){var c=e.gl;if(a.width!=e.width||a.height!=e.height)throw"Different size between color texture and depth texture";var d=c.getViewport();c._framebuffer=c._framebuffer||c.createFramebuffer();c.bindFramebuffer(c.FRAMEBUFFER,c._framebuffer);c.viewport(0,0,e.width,e.height);c.framebufferTexture2D(c.FRAMEBUFFER,
c.COLOR_ATTACHMENT0,c.TEXTURE_2D,e.handler,0);c.framebufferTexture2D(c.FRAMEBUFFER,c.DEPTH_ATTACHMENT,c.TEXTURE_2D,a.handler,0);b();e.data=null;a.data=null;c.bindFramebuffer(c.FRAMEBUFFER,null);c.viewport(d[0],d[1],d[2],d[3])};Texture.prototype.copyTo=function(e,a,b){var c=this.gl,d=c.getParameter(c.FRAMEBUFFER_BINDING),f=c.getViewport();a||(a=this.texture_type==c.TEXTURE_2D?h.Shader.getScreenShader():h.Shader.getCubemapCopyShader());c.disable(c.BLEND);c.disable(c.DEPTH_TEST);a&&b&&a.uniforms(b);
b=c.__copy_fbo;b||(b=c.__copy_fbo=c.createFramebuffer());c.bindFramebuffer(c.FRAMEBUFFER,b);c.viewport(0,0,e.width,e.height);if(this.texture_type==c.TEXTURE_2D)if(this.format!==c.DEPTH_COMPONENT&&this.format!==c.DEPTH_STENCIL)c.framebufferTexture2D(c.FRAMEBUFFER,c.COLOR_ATTACHMENT0,c.TEXTURE_2D,e.handler,0),this.toViewport(a);else{a=c._color_renderbuffer=c._color_renderbuffer||c.createRenderbuffer();b=a.width=e.width;var k=a.height=e.height;c.bindRenderbuffer(c.RENDERBUFFER,a);c.renderbufferStorage(c.RENDERBUFFER,
c.RGBA4,b,k);c.framebufferRenderbuffer(c.FRAMEBUFFER,c.COLOR_ATTACHMENT0,c.RENDERBUFFER,a);b=e.format==c.DEPTH_STENCIL?c.DEPTH_STENCIL_ATTACHMENT:c.DEPTH_ATTACHMENT;c.framebufferTexture2D(c.FRAMEBUFFER,b,c.TEXTURE_2D,e.handler,0);a=c.checkFramebufferStatus(c.FRAMEBUFFER);if(a!==c.FRAMEBUFFER_COMPLETE)throw"FBO not complete: "+a;c.enable(c.DEPTH_TEST);c.depthFunc(c.ALWAYS);c.colorMask(!1,!1,!1,!1);a=h.Shader.getCopyDepthShader();this.toViewport(a);c.colorMask(!0,!0,!0,!0);c.disable(c.DEPTH_TEST);c.depthFunc(c.LEQUAL);
c.framebufferRenderbuffer(c.FRAMEBUFFER,c.COLOR_ATTACHMENT0,c.RENDERBUFFER,null);c.framebufferTexture2D(c.FRAMEBUFFER,b,c.TEXTURE_2D,null,0)}else if(this.texture_type==c.TEXTURE_CUBE_MAP)for(a.uniforms({u_texture:0}),b=h.temp_mat3,k=0;6>k;k++){c.framebufferTexture2D(c.FRAMEBUFFER,c.COLOR_ATTACHMENT0,c.TEXTURE_CUBE_MAP_POSITIVE_X+k,e.handler,0);var s=h.Texture.cubemap_camera_parameters[k];mat3.identity(b);b.set(s.right,0);b.set(s.up,3);b.set(s.dir,6);this.toViewport(a,{u_rotation:b})}c.setViewport(f);
c.bindFramebuffer(c.FRAMEBUFFER,d);e.minFilter&&e.minFilter!=c.NEAREST&&e.minFilter!=c.LINEAR&&(e.bind(),c.generateMipmap(e.texture_type),e.has_mipmaps=!0);e.data=null;c.bindTexture(e.texture_type,null);return this};Texture.prototype.blit=function(){var e=new Float32Array(4);return function(a,b,c){var d=this.gl;if(this.texture_type!=d.TEXTURE_2D||this.format===d.DEPTH_COMPONENT||this.format===d.DEPTH_STENCIL)throw"blit only support TEXTURE_2D of RGB or RGBA. use copyTo instead";var f=d.getParameter(d.FRAMEBUFFER_BINDING);
e.set(d.viewport_data);(b=b||h.Shader.getScreenShader())&&c&&b.uniforms(c);c=d.__copy_fbo;c||(c=d.__copy_fbo=d.createFramebuffer());d.bindFramebuffer(d.FRAMEBUFFER,c);d.viewport(0,0,a.width,a.height);d.framebufferTexture2D(d.FRAMEBUFFER,d.COLOR_ATTACHMENT0,d.TEXTURE_2D,a.handler,0);this.bind(0);b.draw(h.Mesh.getScreenQuad(),d.TRIANGLES);d.setViewport(e);d.bindFramebuffer(d.FRAMEBUFFER,f);a.data=null;d.bindTexture(a.texture_type,null);return this}}();Texture.prototype.toViewport=function(e,a){e=e||
Shader.getScreenShader();var b=Mesh.getScreenQuad();this.bind(0);a&&e.uniforms(a);e.draw(b,gl.TRIANGLES)};Texture.prototype.fill=function(e,a){var b=gl.getParameter(gl.COLOR_CLEAR_VALUE);gl.clearColor(e[0],e[1],e[2],e[3]);this.drawTo(function(){gl.clear(gl.COLOR_BUFFER_BIT)});gl.clearColor(b[0],b[1],b[2],b[3]);!a&&this.minFilter&&this.minFilter!=gl.NEAREST&&this.minFilter!=gl.LINEAR&&(this.bind(),gl.generateMipmap(this.texture_type),this.has_mipmaps=!0)};Texture.prototype.renderQuad=function(){var e=
mat3.create(),a=vec2.create(),b=vec2.create(),c=vec4.fromValues(1,1,1,1);return function(d,f,k,h,l,m){a[0]=d;a[1]=f;b[0]=k;b[1]=h;l=l||Shader.getQuadShader(this.gl);d=Mesh.getScreenQuad(this.gl);this.bind(0);l.uniforms({u_texture:0,u_position:a,u_color:c,u_size:b,u_viewport:gl.viewport_data.subarray(2,4),u_transform:e});m&&l.uniforms(m);l.draw(d,gl.TRIANGLES)}}();Texture.prototype.applyBlur=function(e,a,b,c,d){var f=this.gl;void 0===e&&(e=1);void 0===a&&(a=1);f.disable(f.DEPTH_TEST);f.disable(f.BLEND);
c=c||this;var k=!d;if(d===c)throw"cannot use applyBlur in a texture using as temporary itself";if(c&&this.texture_type!==c.texture_type)throw"cannot use applyBlur with textures of different texture_type";var s=f.getParameter(f.FRAMEBUFFER_BINDING),l=f.getViewport(),m=f.__copy_fbo;m||(m=f.__copy_fbo=f.createFramebuffer());f.bindFramebuffer(f.FRAMEBUFFER,m);f.viewport(0,0,this.width,this.height);if(this.texture_type===f.TEXTURE_2D)m=h.Shader.getBlurShader(),d||(d=h.Texture.getTemporary(this.width,this.height,
this)),f.framebufferTexture2D(f.FRAMEBUFFER,f.COLOR_ATTACHMENT0,f.TEXTURE_2D,d.handler,0),this.toViewport(m,{u_texture:0,u_intensity:b,u_offset:[0,a/this.height]}),f.framebufferTexture2D(f.FRAMEBUFFER,f.COLOR_ATTACHMENT0,f.TEXTURE_2D,c.handler,0),f.viewport(0,0,c.width,c.height),d.toViewport(m,{u_intensity:b,u_offset:[e/d.width,0]}),k&&h.Texture.releaseTemporary(d);else if(this.texture_type===f.TEXTURE_CUBE_MAP){m=h.Shader.getCubemapBlurShader();m.uniforms({u_texture:0,u_intensity:b,u_offset:[e/this.width,
a/this.height]});this.bind(0);e=Mesh.getScreenQuad();e.bindBuffers(m);m.bind();a=null;a=d||c!=this?c:d=h.Texture.getTemporary(c.width,c.height,c);b=h.temp_mat3;for(var p=0;6>p;++p){f.framebufferTexture2D(f.FRAMEBUFFER,f.COLOR_ATTACHMENT0,f.TEXTURE_CUBE_MAP_POSITIVE_X+p,a.handler,0);var q=h.Texture.cubemap_camera_parameters[p];mat3.identity(b);b.set(q.right,0);b.set(q.up,3);b.set(q.dir,6);m._setUniform("u_rotation",b);f.drawArrays(f.TRIANGLES,0,6)}e.unbindBuffers(m);d&&d.copyTo(c);d&&k&&h.Texture.releaseTemporary(d)}f.setViewport(l);
f.bindFramebuffer(f.FRAMEBUFFER,s);c.data=null;c.minFilter&&c.minFilter!=f.NEAREST&&c.minFilter!=f.LINEAR&&(c.bind(),f.generateMipmap(c.texture_type),c.has_mipmaps=!0);f.bindTexture(c.texture_type,null)};Texture.fromURL=function(e,a,b,c){c=c||n.gl;a=a||{};a=Object.create(a);var d=a.texture||new h.Texture(1,1,a,c);64>e.length&&(d.url=e);d.bind();var f=a.temp_color||Texture.loading_color;c.pixelStorei(c.UNPACK_ALIGNMENT,4);f=a.type==c.FLOAT?new Float32Array(f):new Uint8Array(f);c.texImage2D(c.TEXTURE_2D,
0,d.format,d.width,d.height,0,d.format,d.type,f);c.bindTexture(d.texture_type,null);d.ready=!1;f=null;a.extension&&(f=a.extension);if(!f&&512>e.length){var k=e,s=e.indexOf("?");-1!=s&&(k=e.substr(0,s));s=k.lastIndexOf(".");-1!=s&&(f=k.substr(s+1).toLowerCase())}"dds"==f?(f=c.getExtension("WEBKIT_WEBGL_compressed_texture_s3tc")||c.getExtension("WEBGL_compressed_texture_s3tc"),k=new h.Texture(0,0,a,c),P.loadDDSTextureEx(c,f,e,k.handler,!0,function(a){d.texture_type=a.texture_type;d.handler=a;delete d.ready;
b&&b(d,e)})):"tga"==f?HttpRequest(e,null,function(f){if(f=h.Texture.parseTGA(f))a.texture=d,"RGB"==f.format&&(d.format=c.RGB),d=h.Texture.fromMemory(f.width,f.height,f.pixels,a),delete d.ready,b&&b(d,e)},null,{binary:!0}):(f=new Image,f.src=e,f.onload=function(){a.texture=d;h.Texture.fromImage(this,a);delete d.ready;b&&b(d,e)},f.onerror=function(){b&&b(null)});return d};Texture.parseTGA=function(e){if(!e||e.constructor!==ArrayBuffer)throw"TGA: data must be ArrayBuffer";e=new Uint8Array(e);for(var a=
new Uint8Array([0,0,2,0,0,0,0,0,0,0,0,0]),b=e.subarray(0,12),c=0;c<b.length;c++)if(a[c]!=b[c])return console.error("TGA header is not valid"),null;a=e.subarray(12,18);b={};b.width=256*a[1]+a[0];b.height=256*a[3]+a[2];b.bpp=a[4];b.bytesPerPixel=b.bpp/8;b.imageSize=b.width*b.height*b.bytesPerPixel;b.pixels=e.subarray(18,18+b.imageSize);b.pixels=new Uint8Array(b.pixels);if(0==(a[5]&16)){for(c=0;c<b.imageSize;c+=b.bytesPerPixel)e=b.pixels[c],b.pixels[c]=b.pixels[c+2],b.pixels[c+2]=e;a[5]|=16}b.format=
32==b.bpp?"RGBA":"RGB";b.flipY=!0;return b};Texture.fromImage=function(e,a){a=a||{};var b=a.texture||new h.Texture(e.width,e.height,a);b.uploadImage(e,a);b.bind();gl.texParameteri(b.texture_type,gl.TEXTURE_MAG_FILTER,b.magFilter);gl.texParameteri(b.texture_type,gl.TEXTURE_MIN_FILTER,b.minFilter);gl.texParameteri(b.texture_type,gl.TEXTURE_WRAP_S,b.wrapS);gl.texParameteri(b.texture_type,gl.TEXTURE_WRAP_T,b.wrapT);h.isPowerOfTwo(b.width)&&h.isPowerOfTwo(b.height)?a.minFilter&&a.minFilter!=gl.NEAREST&&
a.minFilter!=gl.LINEAR&&(b.bind(),gl.generateMipmap(b.texture_type),b.has_mipmaps=!0):(gl.texParameteri(b.texture_type,gl.TEXTURE_MIN_FILTER,h.LINEAR),gl.texParameteri(b.texture_type,gl.TEXTURE_WRAP_S,h.CLAMP_TO_EDGE),gl.texParameteri(b.texture_type,gl.TEXTURE_WRAP_T,h.CLAMP_TO_EDGE),b.has_mipmaps=!1);gl.bindTexture(b.texture_type,null);b.data=e;a.keep_image&&(b.img=e);return b};Texture.fromVideo=function(e,a){a=a||{};var b=a.texture||new h.Texture(e.videoWidth,e.videoHeight,a);b.bind();b.uploadImage(e,
a);a.minFilter&&a.minFilter!=gl.NEAREST&&a.minFilter!=gl.LINEAR&&(b.bind(),gl.generateMipmap(b.texture_type),b.has_mipmaps=!0,b.data=e);gl.bindTexture(b.texture_type,null);return b};Texture.fromTexture=function(e,a){a=a||{};var b=new h.Texture(e.width,e.height,a);e.copyTo(b);return b};Texture.prototype.clone=function(e){var a=this.getProperties();if(e)for(var b in e)a[b]=e[b];return Texture.fromTexture(this,a)};Texture.fromMemory=function(e,a,b,c){c=c||{};var d=c.texture||new h.Texture(e,a,c);Texture.setUploadOptions(c);
d.bind();b.constructor===Array&&(b=c.type==gl.FLOAT?new Float32Array(b):c.type==h.HALF_FLOAT||c.type==h.HALF_FLOAT_OES?new Uint16Array(b):new Uint8Array(b));gl.texImage2D(gl.TEXTURE_2D,0,d.format,e,a,0,d.format,d.type,b);d.width=e;d.height=a;d.data=b;c.minFilter&&c.minFilter!=gl.NEAREST&&c.minFilter!=gl.LINEAR&&(gl.generateMipmap(gl.TEXTURE_2D),d.has_mipmaps=!0);gl.bindTexture(d.texture_type,null);return d};Texture.fromDDSInMemory=function(e,a){a=a||{};var b=a.texture||new h.Texture(0,0,a);h.Texture.setUploadOptions(a);
b.bind();var c=gl.getExtension("WEBKIT_WEBGL_compressed_texture_s3tc")||gl.getExtension("WEBGL_compressed_texture_s3tc");P.loadDDSTextureFromMemoryEx(gl,c,e,b,!0);gl.bindTexture(b.texture_type,null);return b};Texture.fromShader=function(e,a,b,c){c=c||{};e=new h.Texture(e,a,c);e.drawTo(function(){gl.disable(gl.BLEND);gl.disable(gl.DEPTH_TEST);gl.disable(gl.CULL_FACE);var a=Mesh.getScreenQuad();b.draw(a)});return e};Texture.cubemapFromImages=function(e,a){a=a||{};if(6!=e.length)throw"missing images to create cubemap";
var b=e[0].width,c=e[0].height;a.texture_type=gl.TEXTURE_CUBE_MAP;var d=null;a.texture?(d=a.texture,d.width=b,d.height=c):d=new h.Texture(b,c,a);Texture.setUploadOptions(a);d.bind();try{for(b=0;6>b;b++)gl.texImage2D(gl.TEXTURE_CUBE_MAP_POSITIVE_X+b,0,d.format,d.format,d.type,e[b]);d.data=e}catch(f){if("file:"==location.protocol)throw'image not loaded for security reasons (serve this page over "http://" instead)';throw"image not loaded for security reasons (image must originate from the same domain as this page or use Cross-Origin Resource Sharing)";
}a.minFilter&&a.minFilter!=gl.NEAREST&&a.minFilter!=gl.LINEAR&&(gl.generateMipmap(gl.TEXTURE_CUBE_MAP),d.has_mipmaps=!0);d.unbind();return d};Texture.cubemapFromImage=function(e,a){a=a||{};if(e.width!=e.height/6&&0!=e.height%6&&!a.faces&&!a.is_polar)return console.error("Cubemap image not valid, only 1x6 (vertical) or 6x3 (cross) formats. Check size:",e.width,e.height),null;var b=e.width,c=e.height;if(a.is_polar){var d=a.size||h.nearestPowerOfTwo(e.height),f=h.Texture.fromImage(e,{ignore_pot:!0,wrap:gl.REPEAT,
filter:gl.LINEAR}),b=new h.Texture(d,d,{texture_type:gl.TEXTURE_CUBE_MAP,format:gl.RGBA});if(a.texture){var c=a.texture,k;for(k in b)c[k]=b[k];b=c}var s=mat3.create(),l={u_texture:0,u_rotation:s};gl.disable(gl.DEPTH_TEST);gl.disable(gl.BLEND);var m=h.Shader.getPolarToCubemapShader();b.drawTo(function(a,b){var c=h.Texture.cubemap_camera_parameters[b];mat3.identity(s);s.set(c.right,0);s.set(c.up,3);s.set(c.dir,6);f.toViewport(m,l)});a.keep_image&&(b.img=e);return b}void 0!==a.is_cross?(a.faces=Texture.generateCubemapCrossFacesInfo(e.width,
a.is_cross),b=c=e.width/4):a.faces?(b=a.width||a.faces[0].width,c=a.height||a.faces[0].height):c/=6;if(b!=c)return console.log("Texture not valid, width and height for every face must be square"),null;d=b;a.no_flip=!0;var p=[];for(k=0;6>k;k++){var q=createCanvas(d,d),r=q.getContext("2d");a.faces?r.drawImage(e,a.faces[k].x,a.faces[k].y,a.faces[k].width||d,a.faces[k].height||d,0,0,d,d):r.drawImage(e,0,c*k,b,c,0,0,d,d);p.push(q)}k=Texture.cubemapFromImages(p,a);a.keep_image&&(k.img=e);return k};Texture.generateCubemapCrossFacesInfo=
function(e,a){void 0===a&&(a=1);var b=e/4;return[{x:2*b,y:b,width:b,height:b},{x:0,y:b,width:b,height:b},{x:a*b,y:0,width:b,height:b},{x:a*b,y:2*b,width:b,height:b},{x:b,y:b,width:b,height:b},{x:3*b,y:b,width:b,height:b}]};Texture.cubemapFromURL=function(e,a,b){a=a||{};a=Object.create(a);a.texture_type=gl.TEXTURE_CUBE_MAP;var c=a.texture||new h.Texture(1,1,a);c.bind();Texture.setUploadOptions(a);for(var d=a.temp_color||[0,0,0,255],d=a.type==gl.FLOAT?new Float32Array(d):new Uint8Array(d),f=0;6>f;f++)gl.texImage2D(gl.TEXTURE_CUBE_MAP_POSITIVE_X+
f,0,c.format,1,1,0,c.format,c.type,d);gl.bindTexture(c.texture_type,null);c.ready=!1;d=new Image;d.src=e;d.onload=function(){a.texture=c;(c=h.Texture.cubemapFromImage(this,a))&&delete c.ready;b&&b(c)};return c};Texture.prototype.getPixels=function(e,a){a=a||0;var b=this.gl,c=b.getViewport(),d=b.getParameter(b.FRAMEBUFFER_BINDING);if(this.format==b.DEPTH_COMPONENT)throw"cannot use getPixels in depth textures";b.disable(b.DEPTH_TEST);var f=b.__copy_fbo;f||(f=b.__copy_fbo=b.createFramebuffer());b.bindFramebuffer(b.FRAMEBUFFER,
f);var f=null,k=this.width>>a,s=this.height>>a;b.viewport(0,0,k,s);this.texture_type==b.TEXTURE_2D?b.framebufferTexture2D(b.FRAMEBUFFER,b.COLOR_ATTACHMENT0,b.TEXTURE_2D,this.handler,a):this.texture_type==b.TEXTURE_CUBE_MAP&&b.framebufferTexture2D(b.FRAMEBUFFER,b.COLOR_ATTACHMENT0,b.TEXTURE_CUBE_MAP_POSITIVE_X+(e||0),this.handler,a);var l=this.format==b.RGB?3:4,l=4,m=this.type,f=m==b.UNSIGNED_BYTE?new Uint8Array(k*s*l):m==h.HALF_FLOAT||m==h.HALF_FLOAT_OES?new Uint16Array(k*s*l):new Float32Array(k*
s*l);b.readPixels(0,0,k,s,3==l?b.RGB:b.RGBA,m,f);b.bindFramebuffer(b.FRAMEBUFFER,d);b.viewport(c[0],c[1],c[2],c[3]);return f};Texture.prototype.setPixels=function(e,a,b,c){a={no_flip:a};c&&(a.cubemap_face=c);this.uploadData(e,a,b)};Texture.prototype.getCubemapPixels=function(){if(this.texture_type!==gl.TEXTURE_CUBE_MAP)throw"this texture is not a cubemap";return[this.getPixels(0),this.getPixels(1),this.getPixels(2),this.getPixels(3),this.getPixels(4),this.getPixels(5)]};Texture.prototype.setCubemapPixels=
function(e,a){if(this.texture_type!==gl.TEXTURE_CUBE_MAP)throw"this texture is not a cubemap, it should be created with { texture_type: gl.TEXTURE_CUBE_MAP }";for(var b=0;6>b;++b)this.setPixels(e[b],a,5!=b,b)};Texture.prototype.toCanvas=function(e,a,b){b=b||8192;var c=this.gl,d=Math.min(this.width,b),f=Math.min(this.height,b);this.texture_type==c.TEXTURE_CUBE_MAP&&(d*=4,f*=3);e=e||createCanvas(d,f);e.width!=d&&(e.width=d);e.height!=f&&(e.height=f);var k=null;if(this.texture_type==c.TEXTURE_2D)this.width!=
d||this.height!=f||this.type!=c.UNSIGNED_BYTE?(k=new h.Texture(d,f,{format:c.RGBA,filter:c.NEAREST}),this.copyTo(k),k=k.getPixels()):k=this.getPixels(),b=e.getContext("2d"),c=b.getImageData(0,0,d,f),c.data.set(k),b.putImageData(c,0,0),a&&(k=createCanvas(d,f),a=k.getContext("2d"),a.translate(0,k.height),a.scale(1,-1),a.drawImage(e,0,0,k.width,k.height),b.clearRect(0,0,b.canvas.width,b.canvas.height),b.drawImage(k,0,0));else if(this.texture_type==c.TEXTURE_CUBE_MAP){d=createCanvas(this.width,this.height);
a=d.getContext("2d");f=h.Texture.generateCubemapCrossFacesInfo(e.width,1);b=e.getContext("2d");b.fillStyle="black";b.fillRect(0,0,e.width,e.height);var s=this;this.type!=c.UNSIGNED_BYTE&&(s=new h.Texture(this.width,this.height,{format:c.RGBA,texture_type:c.TEXTURE_CUBE_MAP,filter:c.NEAREST,type:c.UNSIGNED_BYTE}),this.copyTo(s));for(var l=0;6>l;l++)c=a.getImageData(0,0,d.width,d.height),k=s.getPixels(l),c.data.set(k),a.putImageData(c,0,0),b.drawImage(d,f[l].x,f[l].y,d.width,d.height)}return e};Texture.binary_extension=
"png";Texture.prototype.toBinary=function(e,a){for(var b=this.toCanvas(null,e).toDataURL(a),c=b.indexOf(","),b=b.substr(c+1),b=atob(b),c=b.length,d=new Uint8Array(c),f=0;f<c;++f)d[f]=b.charCodeAt(f);return d};Texture.prototype.toBlob=function(e,a){var b=this.toBinary(e);return new Blob([b],{type:a||"image/png"})};Texture.prototype.toBlobAsync=function(e,a,b){var c=this.toCanvas(null,e);c.toBlob?c.toBlob(b,a):(e=this.toBlob(e,a),b&&b(e))};Texture.prototype.toBase64=function(e){var a=this.width,b=this.height,
c=this.getPixels(),d=createCanvas(a,b),f=d.getContext("2d"),k=f.getImageData(0,0,a,b);k.data.set(c);f.putImageData(k,0,0);e&&(e=createCanvas(a,b),a=e.getContext("2d"),a.translate(0,b),a.scale(1,-1),a.drawImage(d,0,0),d=e);return d.toDataURL("image/png")};Texture.prototype.generateMetadata=function(){var e={};e.width=this.width;e.height=this.height;this.metadata=e};Texture.compareFormats=function(e,a){return e&&a?e==a?!0:e.width!=a.width||e.height!=a.height||e.type!=a.type||e.format!=a.format||e.texture_type!=
a.texture_type?!1:!0:!1};Texture.blend=function(e,a,b,c){if(!e||!a)return!1;if(e==a)return c?e.copyTo(c):e.toViewport(),!0;gl.disable(gl.BLEND);gl.disable(gl.DEPTH_TEST);gl.disable(gl.CULL_FACE);var d=h.Shader.getBlendShader(),f=h.Mesh.getScreenQuad();a.bind(1);d.uniforms({u_texture:0,u_texture2:1,u_factor:b});if(c)return c.drawTo(function(){if(e==c||a==c)throw"Blend output cannot be the same as the input";e.bind(0);d.draw(f,gl.TRIANGLES)}),!0;e.bind(0);d.draw(f,gl.TRIANGLES);return!0};Texture.cubemapToTexture2D=
function(e,a,b,c,d){if(!e||e.texture_type!=gl.TEXTURE_CUBE_MAP)throw"No cubemap in convert";a=a||e.width;c=c?e.type:gl.UNSIGNED_BYTE;d=d||0;b||(b=new h.Texture(2*a,a,{minFilter:gl.NEAREST,type:c}));var f=gl.shaders.cubemap_to_texture2D;f||(f=gl.shaders.cubemap_to_texture2D=new h.Shader(h.Shader.SCREEN_VERTEX_SHADER,"\t\tprecision mediump float;\n\t\t#define PI 3.14159265358979323846264\n\t\tuniform samplerCube texture;\t\tvarying vec2 v_coord;\t\tuniform float u_yaw;\n\t\tvoid main() {\t\t\tfloat alpha = ((1.0 - v_coord.x) * 2.0) * PI + u_yaw;\t\t\tfloat beta = (v_coord.y * 2.0 - 1.0) * PI * 0.5;\t\t\tvec3 N = vec3( -cos(alpha) * cos(beta), sin(beta), sin(alpha) * cos(beta) );\t\t\tgl_FragColor = textureCube(texture,N);\t\t}"));
f.setUniform("u_yaw",d);b.drawTo(function(){gl.disable(gl.DEPTH_TEST);gl.disable(gl.CULL_FACE);gl.disable(gl.BLEND);e.toViewport(f)});return b};Texture.getWhiteTexture=function(e){e=e||n.gl;var a=e.textures[":white"];if(a)return a;a=new Uint8Array([255,255,255,255]);return e.textures[":white"]=new h.Texture(1,1,{pixel_data:a})};Texture.getBlackTexture=function(e){e=e||n.gl;var a=e.textures[":black"];if(a)return a;a=new Uint8Array([0,0,0,255]);return e.textures[":black"]=new h.Texture(1,1,{pixel_data:a})};
Texture.getTemporary=function(e,a,b,c){c=c||n.gl;c._texture_pool||(c._texture_pool=[]);var d=h.TEXTURE_2D,f=Texture.DEFAULT_TYPE,k=Texture.DEFAULT_FORMAT;b&&(b.texture_type&&(d=b.texture_type),b.type&&(f=b.type),b.format&&(k=b.format));b=d+":"+f+":"+e+"x"+a+":"+k;c=c._texture_pool;for(var s=0;s<c.length;++s){var l=c[s];if(l._key==b)return c.splice(s,1),l._pool=0,l}l=new h.Texture(e,a,{type:f,texture_type:d,format:k});l._key=b;l._pool=0;return l};Texture.releaseTemporary=function(e,a){a=a||n.gl;a._texture_pool||
(a._texture_pool=[]);0<e._pool&&console.warn("this texture is already in the textures pool");var b=a._texture_pool;b||(b=a._texture_pool=[]);e._pool=getTime();b.push(e);20<b.length&&(b.sort(function(a,b){return b._pool-a._pool}),e=b.pop(),e._pool=0,e.delete())};Texture.nextPOT=function(e){return Math.pow(2,Math.ceil(Math.log(e)/Math.log(2)))};h.FBO=y;y.prototype.setTextures=function(e,a,b){if(a&&a.constructor===h.Texture){if(a.format!==h.DEPTH_COMPONENT&&a.format!==h.DEPTH_STENCIL&&a.format!==h.DEPTH_COMPONENT16&&
a.format!==h.DEPTH_COMPONENT24&&a.format!==h.DEPTH_COMPONENT32F)throw"FBO Depth texture must be of format: gl.DEPTH_COMPONENT, gl.DEPTH_STENCIL or gl.DEPTH_COMPONENT16/24/32F (only in webgl2)";if(a.type!=h.UNSIGNED_SHORT&&a.type!=h.UNSIGNED_INT&&a.type!=h.UNSIGNED_INT_24_8_WEBGL&&a.type!=h.FLOAT)throw"FBO Depth texture must be of type: gl.UNSIGNED_SHORT, gl.UNSIGNED_INT, gl.UNSIGNED_INT_24_8_WEBGL";}var c=this.depth_texture==a;if(c&&e){if(e.constructor!==Array)throw"FBO: color_textures parameter must be an array containing all the textures to be binded in the color";
if(e.length==this.color_textures.length)for(var d=0;d<e.length;++d){if(e[d]!=this.color_textures[d]){c=!1;break}}else c=!1}this._stencil_enabled!==this.stencil&&(c=!1);if(!c){this.color_textures.length=e?e.length:0;if(e)for(d=0;d<e.length;++d)this.color_textures[d]=e[d];this.depth_texture=a;this.update(b)}};y.prototype.update=function(e){this._old_fbo_handler=gl.getParameter(gl.FRAMEBUFFER_BINDING);this.handler||(this.handler=gl.createFramebuffer());var a=-1,b=-1,c=null,d=this.color_textures,f=this.depth_texture;
if(d&&d.length)for(var k=0;k<d.length;k++){var s=d[k];if(s.constructor!==h.Texture)throw"FBO can only bind instances of GL.Texture";if(-1==a)a=s.width;else if(a!=s.width)throw"Cannot bind textures with different dimensions";if(-1==b)b=s.height;else if(b!=s.height)throw"Cannot bind textures with different dimensions";if(null==c)c=s.type;else if(c!=s.type)throw"Cannot bind textures to a FBO with different pixel formats";if(s.texture_type!=gl.TEXTURE_2D)throw"Cannot bind a Cubemap to a FBO";}else a=
f.width,b=f.height;this.width=a;this.height=b;gl.bindFramebuffer(gl.FRAMEBUFFER,this.handler);var l=gl.extensions.WEBGL_draw_buffers;if(1==gl.webgl_version&&!l&&d&&1<d.length)throw"Rendering to several textures not supported by your browser";c=1==gl.webgl_version?gl.FRAMEBUFFER:gl.DRAW_FRAMEBUFFER;gl.framebufferRenderbuffer(c,gl.DEPTH_ATTACHMENT,gl.RENDERBUFFER,null);gl.framebufferRenderbuffer(c,gl.DEPTH_STENCIL_ATTACHMENT,gl.RENDERBUFFER,null);if(f&&f.constructor===h.Texture){if(1==gl.webgl_version&&
!gl.extensions.WEBGL_depth_texture)throw"Rendering to depth texture not supported by your browser";this.stencil&&f.format!==gl.DEPTH_STENCIL&&console.warn("Stencil cannot be enabled if there is a depth texture with a DEPTH_STENCIL format");f.format==gl.DEPTH_STENCIL?gl.framebufferTexture2D(c,gl.DEPTH_STENCIL_ATTACHMENT,gl.TEXTURE_2D,f.handler,0):gl.framebufferTexture2D(c,gl.DEPTH_ATTACHMENT,gl.TEXTURE_2D,f.handler,0)}else k=null,f&&f.constructor===WebGLRenderbuffer&&f.width==a&&f.height==b?k=this._depth_renderbuffer=
f:(k=this._depth_renderbuffer=this._depth_renderbuffer||gl.createRenderbuffer(),k.width=a,k.height=b),gl.bindRenderbuffer(gl.RENDERBUFFER,k),this.stencil?(gl.renderbufferStorage(gl.RENDERBUFFER,gl.DEPTH_STENCIL,a,b),gl.framebufferRenderbuffer(c,gl.DEPTH_STENCIL_ATTACHMENT,gl.RENDERBUFFER,k)):(gl.renderbufferStorage(gl.RENDERBUFFER,gl.DEPTH_COMPONENT16,a,b),gl.framebufferRenderbuffer(c,gl.DEPTH_ATTACHMENT,gl.RENDERBUFFER,k));if(d&&d.length)for(this.order=[],k=0;k<d.length;k++)s=d[k],gl.framebufferTexture2D(c,
gl.COLOR_ATTACHMENT0+k,gl.TEXTURE_2D,s.handler,0),this.order.push(gl.COLOR_ATTACHMENT0+k);else f=this._color_renderbuffer=this._color_renderbuffer||gl.createRenderbuffer(),f.width=a,f.height=b,gl.bindRenderbuffer(gl.RENDERBUFFER,f),gl.renderbufferStorage(gl.RENDERBUFFER,gl.RGBA4,a,b),gl.framebufferRenderbuffer(c,gl.COLOR_ATTACHMENT0,gl.RENDERBUFFER,f);for(k=a=d?d.length:0;k<this._num_binded_textures;++k)gl.framebufferTexture2D(c,gl.COLOR_ATTACHMENT0+k,gl.TEXTURE_2D,null,0);this._num_binded_textures=
a;this._stencil_enabled=this.stencil;d&&1<d.length&&(l?l.drawBuffersWEBGL(this.order):gl.drawBuffers(this.order));d=gl.checkFramebufferStatus(c);if(d!==gl.FRAMEBUFFER_COMPLETE)throw"FBO not complete: "+d;gl.bindTexture(gl.TEXTURE_2D,null);gl.bindRenderbuffer(gl.RENDERBUFFER,null);e||gl.bindFramebuffer(c,this._old_fbo_handler)};y.prototype.bind=function(e){if(!this.color_textures.length&&!this.depth_texture)throw"FBO: no textures attached to FBO";this._old_viewport.set(gl.viewport_data);this._old_fbo_handler=
e?gl.getParameter(gl.FRAMEBUFFER_BINDING):null;this._old_fbo_handler!=this.handler&&gl.bindFramebuffer(gl.FRAMEBUFFER,this.handler);for(e=0;e<this.color_textures.length;++e)this.color_textures[e]._in_current_fbo=!0;this.depth_texture&&(this.depth_texture._in_current_fbo=!0);gl.viewport(0,0,this.width,this.height)};y.prototype.unbind=function(){gl.bindFramebuffer(gl.FRAMEBUFFER,this._old_fbo_handler);this._old_fbo_handler=null;gl.setViewport(this._old_viewport);for(var e=0;e<this.color_textures.length;++e)this.color_textures[e]._in_current_fbo=
!1;this.depth_texture&&(this.depth_texture._in_current_fbo=!1)};y.prototype.switchTo=function(e){e._old_fbo_handler=this._old_fbo_handler;e._old_viewport.set(this._old_viewport);gl.bindFramebuffer(gl.FRAMEBUFFER,e.handler);this._old_fbo_handler=null;gl.viewport(0,0,this.width,this.height);for(var a=0;a<this.color_textures.length;++a)this.color_textures[a]._in_current_fbo=!1;this.depth_texture&&(this.depth_texture._in_current_fbo=!1);for(a=0;a<e.color_textures.length;++a)e.color_textures[a]._in_current_fbo=
!0;e.depth_texture&&(e.depth_texture._in_current_fbo=!0)};y.prototype.delete=function(){gl.deleteFramebuffer(this.handler);this.handler=null};y.supported={};y.testSupport=function(e,a){var b=e+":"+a;if(null!=y.supported[b])return y.supported[b];var c=new h.Texture(1,1,{format:a,type:e});try{new h.FBO([c])}catch(d){return console.warn("This browser WEBGL implementation doesn't support this FBO format: "+h.reverse[e]+" "+h.reverse[a]),y.supported[b]=!1}return y.supported[b]=!0};n.Shader=h.Shader=function a(b,
c,d){h.debug&&console.log("GL.Shader created");if(!b||!c)throw"GL.Shader source code parameter missing";this._context_id=n.gl.context_id;var f=this.gl=n.gl,k=a.expandMacros(d);d=b.constructor===String?a.injectCode(k,b,f):b;k=c.constructor===String?a.injectCode(k,c,f):c;this.program=f.createProgram();b=b.constructor===String?h.Shader.compileSource(f.VERTEX_SHADER,d):b;c=c.constructor===String?h.Shader.compileSource(f.FRAGMENT_SHADER,k):c;f.attachShader(this.program,b,f);f.attachShader(this.program,
c,f);f.linkProgram(this.program);if(!f.getProgramParameter(this.program,f.LINK_STATUS))throw"link error: "+f.getProgramInfoLog(this.program);this.vs_shader=b;this.fs_shader=c;this.attributes={};this.uniformInfo={};this.samplers={};this.extractShaderInfo()};Shader.expandMacros=function(a){var b="";if(a)for(var c in a)b+="#define "+c+" "+(a[c]?a[c]:"")+"\n";return b};Shader.injectCode=function(a,b,c){var d=b.indexOf("\n");c=c?"#define WEBGL"+c.webgl_version+"\n":"";var f=b.substr(0,d).trim();return-1==
f.indexOf("#version")?c+a+b:f+"\n"+c+a+b.substr(d)};Shader.compileSource=function(a,b,c,d){c=c||n.gl;d=d||c.createShader(a);c.shaderSource(d,b);c.compileShader(d);if(!c.getShaderParameter(d,c.COMPILE_STATUS))throw(a==c.VERTEX_SHADER?"Vertex":"Fragment")+" shader compile error: "+c.getShaderInfoLog(d);return d};Shader.parseError=function(a,b,c){if(!a)return null;var d=a.split(" "),f=d[5].split(":");return{type:d[0],line_number:parseInt(f[1]),line_pos:parseInt(f[0]),line_code:("Fragment"==d[0]?c:b).split("\n")[parseInt(f[1])],
err:a}};Shader.prototype.updateShader=function(a,b,c){var d=this.gl||n.gl,f=Shader.expandMacros(c);this.program?(d.detachShader(this.program,this.vs_shader),d.detachShader(this.program,this.fs_shader)):this.program=d.createProgram();f=Shader.expandMacros(c);c=a.constructor===String?Shader.injectCode(f,a,d):a;f=b.constructor===String?Shader.injectCode(f,b,d):b;a=a.constructor===String?h.Shader.compileSource(d.VERTEX_SHADER,c):a;b=b.constructor===String?h.Shader.compileSource(d.FRAGMENT_SHADER,f):b;
d.attachShader(this.program,a,d);d.attachShader(this.program,b,d);d.linkProgram(this.program);if(!d.getProgramParameter(this.program,d.LINK_STATUS))throw"link error: "+d.getProgramInfoLog(this.program);this.vs_shader=a;this.fs_shader=b;this.attributes={};this.uniformInfo={};this.samplers={};this.extractShaderInfo()};Shader.prototype.extractShaderInfo=function(){for(var a=this.gl,b=a.getProgramParameter(this.program,a.ACTIVE_UNIFORMS),c=0;c<b;++c){var d=a.getActiveUniform(this.program,c);if(!d)break;
var f=d.name,k=f.indexOf("[");-1!=k&&-1==f.indexOf("].")&&(f=f.substr(0,k));if(d.type==a.SAMPLER_2D||d.type==a.SAMPLER_CUBE||d.type==h.SAMPLER_3D)this.samplers[f]=d.type;var k=Shader.getUniformFunc(d),s=!1;if(d.type==a.FLOAT_MAT2||d.type==a.FLOAT_MAT3||d.type==a.FLOAT_MAT4)s=!0;var l=h.TYPE_LENGTH[d.type]||1;this.uniformInfo[f]={type:d.type,func:k,size:d.size,type_length:l,is_matrix:s,loc:a.getUniformLocation(this.program,f),data:new Float32Array(l*d.size)}}c=0;for(b=a.getProgramParameter(this.program,
a.ACTIVE_ATTRIBUTES);c<b;++c){d=a.getActiveAttrib(this.program,c);if(!d)break;k=Shader.getUniformFunc(d);l=h.TYPE_LENGTH[d.type]||1;this.uniformInfo[d.name]={type:d.type,func:k,type_length:l,size:d.size,loc:null};this.attributes[d.name]=a.getAttribLocation(this.program,d.name)}};Shader.prototype.hasUniform=function(a){return this.uniformInfo[a]};Shader.prototype.hasAttribute=function(a){return this.attributes[a]};Shader.getUniformFunc=function(a){var b=null;switch(a.type){case h.FLOAT:b=1==a.size?
gl.uniform1f:gl.uniform1fv;break;case h.FLOAT_MAT2:b=gl.uniformMatrix2fv;break;case h.FLOAT_MAT3:b=gl.uniformMatrix3fv;break;case h.FLOAT_MAT4:b=gl.uniformMatrix4fv;break;case h.FLOAT_VEC2:b=gl.uniform2fv;break;case h.FLOAT_VEC3:b=gl.uniform3fv;break;case h.FLOAT_VEC4:b=gl.uniform4fv;break;case h.UNSIGNED_INT:case h.INT:b=1==a.size?gl.uniform1i:gl.uniform1iv;break;case h.INT_VEC2:b=gl.uniform2iv;break;case h.INT_VEC3:b=gl.uniform3iv;break;case h.INT_VEC4:b=gl.uniform4iv;break;case h.SAMPLER_2D:case h.SAMPLER_3D:case h.SAMPLER_CUBE:b=
gl.uniform1i;break;default:b=gl.uniform1f}return b};Shader.fromURL=function(a,b,c){function d(){var a=new h.Shader(k,s),b;for(b in a)f[b]=a[b];f.ready=!0}var f=new h.Shader("\n\t\t\tprecision highp float;\n\t\t\tattribute vec3 a_vertex;\n\t\t\tattribute mat4 u_mvp;\n\t\t\tvoid main() { \n\t\t\t\tgl_Position = u_mvp * vec4(a_vertex,1.0); \n\t\t\t}\n\t\t","\n\t\t\tprecision highp float;\n\t\t\tvoid main() {\n\t\t\t\tgl_FragColor = vec4(0.0, 0.0, 0.0, 1.0);\n\t\t\t}\n\t\t\t");f.ready=!1;var k=null,s=
null;HttpRequest(a,null,function(a){k=a;s&&d()});HttpRequest(b,null,function(a){s=a;k&&d()});return f};Shader.prototype.bind=function(){var a=this.gl;a.useProgram(this.program);a._current_shader=this};Shader.prototype.getLocation=function(a){return this.uniformInfo[a]?this.uniformInfo[a].loc:null};Shader._temp_uniform=new Float32Array(16);Shader.prototype.uniforms=function(a){var b=this.gl;b.useProgram(this.program);b._current_shader=this;for(var c in a)this.uniformInfo[c]&&this._setUniform(c,a[c]);
return this};Shader.prototype.uniformsArray=function(a){var b=this.gl;b.useProgram(this.program);b._current_shader=this;for(var b=0,c=a.length;b<c;++b){var d=a[b],f;for(f in d)this._setUniform(f,d[f])}return this};Shader.prototype.setUniform=function(){return function(a,b){this.gl._current_shader!=this&&this.bind();var c=this.uniformInfo[a];c&&null!==c.loc&&null!=b&&(b.constructor===Array&&(c.data.set(b),b=c.data),c.is_matrix?c.func.call(this.gl,c.loc,!1,b):c.func.call(this.gl,c.loc,b))}}();Shader.prototype._setUniform=
function(){return function(a,b){var c=this.uniformInfo[a];c&&null!==c.loc&&null!=b&&(b.constructor===Array&&(c.data.set(b),b=c.data),c.is_matrix?c.func.call(this.gl,c.loc,!1,b):c.func.call(this.gl,c.loc,b))}}();Shader.prototype.draw=function(a,b,c){c=void 0===c?b==gl.LINES?"lines":"triangles":c;this.drawBuffers(a.vertexBuffers,c?a.indexBuffers[c]:null,2>arguments.length?gl.TRIANGLES:b)};Shader.prototype.drawRange=function(a,b,c,d,f){f=void 0===f?b==gl.LINES?"lines":"triangles":f;this.drawBuffers(a.vertexBuffers,
f?a.indexBuffers[f]:null,b,c,d)};var M=new Uint8Array(16),S=new Uint8Array(16);Shader.prototype.drawBuffers=function(a,b,c,d,f){if(0!=f){var k=this.gl;k.useProgram(this.program);var h=0;M.set(S);for(var l in a){var m=a[l],p=m.attribute||l,q=this.attributes[p];null!=q&&m.buffer&&(M[q]=1,k.bindBuffer(k.ARRAY_BUFFER,m.buffer),k.enableVertexAttribArray(q),k.vertexAttribPointer(q,m.buffer.spacing,m.buffer.gl_type,!1,0,0),h=m.buffer.length/m.buffer.spacing)}a=0;0<d&&(a=d);b&&(h=b.buffer.length-a);0<f&&
f<h&&(h=f);a*=b&&b.data?b.data.constructor.BYTES_PER_ELEMENT:1;for(p in this.attributes)q=this.attributes[p],M[q]||k.disableVertexAttribArray(this.attributes[p]);!h||b&&!b.buffer||(b?(k.bindBuffer(k.ELEMENT_ARRAY_BUFFER,b.buffer),k.drawElements(c,h,b.buffer.gl_type,a),k.bindBuffer(k.ELEMENT_ARRAY_BUFFER,null)):k.drawArrays(c,a,h));return this}};Shader._instancing_arrays=[];Shader.prototype.drawInstanced=function(a,b,c,d,f,k,h){if(0!==k){var l=this.gl;if(1==l.webgl_version&&!l.extensions.ANGLE_instanced_arrays)throw"instancing not supported";
l.useProgram(this.program);var m=0;M.set(S);var p=a.vertexBuffers,q;for(q in p){var r=p[q],n=r.attribute||q,u=this.attributes[n];null!=u&&r.buffer&&(M[u]=1,l.bindBuffer(l.ARRAY_BUFFER,r.buffer),l.enableVertexAttribArray(u),l.vertexAttribPointer(u,r.buffer.spacing,r.buffer.gl_type,!1,0,0),m=r.buffer.length/r.buffer.spacing)}a=c?a.getIndexBuffer(c):null;c=0;0<f&&(c=f);a&&(m=a.buffer.length-c);0<k&&k<m&&(m=k);c*=a&&a.data?a.data.constructor.BYTES_PER_ELEMENT:1;for(n in this.attributes)u=this.attributes[n],
M[u]||l.disableVertexAttribArray(this.attributes[n]);f=l.extensions.ANGLE_instanced_arrays;k=u=0;for(var t in d){q=d[t];u=q.length;n=this.attributes[t];if(null==n)return;var x=p=0;q.constructor===Array?(p=q[0].constructor===Number?1:q[0].length,x=p*q.length):(p=this.uniformInfo[t].type_length,x=q.length,u=x/p);r=Shader._instancing_arrays[k];if(!r||r.data.length<x)r=Shader._instancing_arrays[k]={data:new Float32Array(x),buffer:l.createBuffer()};r.uniform=t;r.element_size=p;if(q.constructor===Array)for(x=
0;x<q.length;++x)r.data.set(q[x],x*p);else r.data.set(q);l.bindBuffer(l.ARRAY_BUFFER,r.buffer);l.bufferData(l.ARRAY_BUFFER,r.data,l.STREAM_DRAW);if(16==p)for(p=0;4>p;++p)l.enableVertexAttribArray(n+p),l.vertexAttribPointer(n+p,4,l.FLOAT,!1,64,16*p),f?f.vertexAttribDivisorANGLE(n+p,1):l.vertexAttribDivisor(n+p,1);else l.enableVertexAttribArray(n),l.vertexAttribPointer(n,p,l.FLOAT,!1,4*p,0),f?f.vertexAttribDivisorANGLE(n,1):l.vertexAttribDivisor(n,1);k+=1}h&&(u=h);f?a?(l.bindBuffer(l.ELEMENT_ARRAY_BUFFER,
a.buffer),f.drawElementsInstancedANGLE(b,m,a.buffer.gl_type,c,u),l.bindBuffer(l.ELEMENT_ARRAY_BUFFER,null)):f.drawArraysInstancedANGLE(b,c,m,u):a?(l.bindBuffer(l.ELEMENT_ARRAY_BUFFER,a.buffer),l.drawElementsInstanced(b,m,a.buffer.gl_type,c,u),l.bindBuffer(l.ELEMENT_ARRAY_BUFFER,null)):l.drawArraysInstanced(b,c,m,u);for(b=0;b<k;++b)if(d=Shader._instancing_arrays[b],n=this.attributes[d.uniform],p=d.element_size,16==p)for(p=0;4>p;++p)l.disableVertexAttribArray(n+p),f?f.vertexAttribDivisorANGLE(n+p,0):
l.vertexAttribDivisor(n+p,0);else l.enableVertexAttribArray(n),f?f.vertexAttribDivisorANGLE(n,0):l.vertexAttribDivisor(n,0);return this}};Shader.expandImports=function(a,b){b=b||Shader.files;var c={};if(!b)throw"Shader.files not initialized, assign files there";return a.replace(/#import\s+\"([a-zA-Z0-9_\.]+)\"\s*\n/g,function(a){a=a.split('"')[1];if(c[a])return"//already imported: "+a+"\n";var f=b[a];c[a]=!0;return f?f+"\n":"//import code not found: "+a+"\n"})};Shader.dumpErrorToConsole=function(a,
b,c){console.error(a);var d=null,d=-1!=a.indexOf("Fragment")?c:b;a=d.split("\n");for(var f in a)a[f]=f+"| "+a[f];console.groupCollapsed("Shader code");console.log(a.join("\n"));console.groupEnd()};Shader.convertTo100=function(a,b){};Shader.convertTo300=function(a,b){};Shader.validateValue=function(a,b){if(null===a||void 0===a)return!1;switch(b.type){case h.INT:case h.FLOAT:case h.SAMPLER_2D:case h.SAMPLER_CUBE:return isNumber(a);case h.INT_VEC2:case h.FLOAT_VEC2:return 2===a.length;case h.INT_VEC3:case h.FLOAT_VEC3:return 3===
a.length;case h.INT_VEC4:case h.FLOAT_VEC4:case h.FLOAT_MAT2:return 4===a.length;case h.FLOAT_MAT3:return 8===a.length;case h.FLOAT_MAT4:return 16===a.length}return!0};Shader.DEFAULT_VERTEX_SHADER="\n\t\t\tprecision highp float;\n\t\t\tattribute vec3 a_vertex;\n\t\t\tattribute vec3 a_normal;\n\t\t\tattribute vec2 a_coord;\n\t\t\tvarying vec3 v_position;\n\t\t\tvarying vec3 v_normal;\n\t\t\tvarying vec2 v_coord;\n\t\t\tuniform mat4 u_model;\n\t\t\tuniform mat4 u_mvp;\n\t\t\tvoid main() {\n\t\t\t\tv_position = (u_model * vec4(a_vertex,1.0)).xyz;\n\t\t\t\tv_normal = (u_model * vec4(a_normal,0.0)).xyz;\n\t\t\t\tv_coord = a_coord;\n\t\t\t\tgl_Position = u_mvp * vec4(a_vertex,1.0);\n\t\t\t}\n\t\t\t";
Shader.SCREEN_VERTEX_SHADER="\n\t\t\tprecision highp float;\n\t\t\tattribute vec3 a_vertex;\n\t\t\tattribute vec2 a_coord;\n\t\t\tvarying vec2 v_coord;\n\t\t\tvoid main() { \n\t\t\t\tv_coord = a_coord; \n\t\t\t\tgl_Position = vec4(a_coord * 2.0 - 1.0, 0.0, 1.0); \n\t\t\t}\n\t\t\t";Shader.SCREEN_FRAGMENT_SHADER="\n\t\t\tprecision highp float;\n\t\t\tuniform sampler2D u_texture;\n\t\t\tvarying vec2 v_coord;\n\t\t\tvoid main() {\n\t\t\t\tgl_FragColor = texture2D(u_texture, v_coord);\n\t\t\t}\n\t\t\t";
Shader.SCREEN_FRAGMENT_FX="\n\t\t\tprecision highp float;\n\t\t\tuniform sampler2D u_texture;\n\t\t\tvarying vec2 v_coord;\n\t\t\t#ifdef FX_UNIFORMS\n\t\t\t\tFX_UNIFORMS\n\t\t\t#endif\n\t\t\tvoid main() {\n\t\t\t\tvec2 uv = v_coord;\n\t\t\t\tvec4 color = texture2D(u_texture, uv);\n\t\t\t\t#ifdef FX_CODE\n\t\t\t\t\tFX_CODE ;\n\t\t\t\t#endif\n\t\t\t\tgl_FragColor = color;\n\t\t\t}\n\t\t\t";Shader.SCREEN_COLORED_FRAGMENT_SHADER="\n\t\t\tprecision highp float;\n\t\t\tuniform sampler2D u_texture;\n\t\t\tuniform vec4 u_color;\n\t\t\tvarying vec2 v_coord;\n\t\t\tvoid main() {\n\t\t\t\tgl_FragColor = u_color * texture2D(u_texture, v_coord);\n\t\t\t}\n\t\t\t";
Shader.BLEND_FRAGMENT_SHADER="\n\t\t\tprecision highp float;\n\t\t\tuniform sampler2D u_texture;\n\t\t\tuniform sampler2D u_texture2;\n\t\t\tuniform float u_factor;\n\t\t\tvarying vec2 v_coord;\n\t\t\tvoid main() {\n\t\t\t\tgl_FragColor = mix( texture2D(u_texture, v_coord), texture2D(u_texture2, v_coord), u_factor);\n\t\t\t}\n\t\t\t";Shader.QUAD_VERTEX_SHADER="\n\t\t\tprecision highp float;\n\t\t\tattribute vec3 a_vertex;\n\t\t\tattribute vec2 a_coord;\n\t\t\tvarying vec2 v_coord;\n\t\t\tuniform vec2 u_position;\n\t\t\tuniform vec2 u_size;\n\t\t\tuniform vec2 u_viewport;\n\t\t\tuniform mat3 u_transform;\n\t\t\tvoid main() { \n\t\t\t\tvec3 pos = vec3(u_position + vec2(a_coord.x,1.0 - a_coord.y)  * u_size, 1.0);\n\t\t\t\tv_coord = a_coord; \n\t\t\t\tpos = u_transform * pos;\n\t\t\t\tpos.z = 0.0;\n\t\t\t\t//normalize\n\t\t\t\tpos.x = (2.0 * pos.x / u_viewport.x) - 1.0;\n\t\t\t\tpos.y = -((2.0 * pos.y / u_viewport.y) - 1.0);\n\t\t\t\tgl_Position = vec4(pos, 1.0); \n\t\t\t}\n\t\t\t";
Shader.QUAD_FRAGMENT_SHADER="\n\t\t\tprecision highp float;\n\t\t\tuniform sampler2D u_texture;\n\t\t\tuniform vec4 u_color;\n\t\t\tvarying vec2 v_coord;\n\t\t\tvoid main() {\n\t\t\t\tgl_FragColor = u_color * texture2D(u_texture, v_coord);\n\t\t\t}\n\t\t\t";Shader.QUAD2_FRAGMENT_SHADER="\n\t\t\tprecision highp float;\n\t\t\tuniform sampler2D u_texture;\n\t\t\tuniform vec4 u_color;\n\t\t\tuniform vec4 u_texture_area;\n\t\t\tvarying vec2 v_coord;\n\t\t\tvoid main() {\n\t\t\t    vec2 uv = vec2( mix(u_texture_area.x, u_texture_area.z, v_coord.x), 1.0 - mix(u_texture_area.w, u_texture_area.y, v_coord.y) );\n\t\t\t\tgl_FragColor = u_color * texture2D(u_texture, uv);\n\t\t\t}\n\t\t\t";
Shader.PRIMITIVE2D_VERTEX_SHADER="\n\t\t\tprecision highp float;\n\t\t\tattribute vec3 a_vertex;\n\t\t\tuniform vec2 u_viewport;\n\t\t\tuniform mat3 u_transform;\n\t\t\tvoid main() { \n\t\t\t\tvec3 pos = a_vertex;\n\t\t\t\tpos = u_transform * pos;\n\t\t\t\tpos.z = 0.0;\n\t\t\t\t//normalize\n\t\t\t\tpos.x = (2.0 * pos.x / u_viewport.x) - 1.0;\n\t\t\t\tpos.y = -((2.0 * pos.y / u_viewport.y) - 1.0);\n\t\t\t\tgl_Position = vec4(pos, 1.0); \n\t\t\t}\n\t\t\t";Shader.FLAT_VERTEX_SHADER="\n\t\t\tprecision highp float;\n\t\t\tattribute vec3 a_vertex;\n\t\t\tuniform mat4 u_mvp;\n\t\t\tvoid main() { \n\t\t\t\tgl_Position = u_mvp * vec4(a_vertex,1.0); \n\t\t\t}\n\t\t\t";
Shader.FLAT_FRAGMENT_SHADER="\n\t\t\tprecision highp float;\n\t\t\tuniform vec4 u_color;\n\t\t\tvoid main() {\n\t\t\t\tgl_FragColor = u_color;\n\t\t\t}\n\t\t\t";Shader.SCREEN_FLAT_FRAGMENT_SHADER=Shader.FLAT_FRAGMENT_SHADER;Shader.createFX=function(a,b,c){a=h.Shader.removeComments(a,!0);a={FX_CODE:a,FX_UNIFORMS:b||""};if(!c)return new h.Shader(h.Shader.SCREEN_VERTEX_SHADER,h.Shader.SCREEN_FRAGMENT_FX,a);c.updateShader(h.Shader.SCREEN_VERTEX_SHADER,h.Shader.SCREEN_FRAGMENT_FX,a);return c};Shader.removeComments=
function(a,b){if(!a)return"";a=a.replace(/(\/\*([^*]|[\r\n]|(\*+([^*\/]|[\r\n])))*\*+\/)|(\/\/.*)/g,"");for(var c=a.split("\n"),d=[],f=0;f<c.length;++f){var k=c[f],h=k.indexOf("//");-1!=h&&(k=c[f].substr(0,h));k=k.trim();k.length&&d.push(k)}return d.join(b?"":"\n")};Shader.prototype.toViewport=function(a){var b=h.Mesh.getScreenQuad();a&&this.uniforms(a);this.draw(b)};Shader.getScreenShader=function(a){a=a||n.gl;var b=a.shaders[":screen"];if(b)return b;b=a.shaders[":screen"]=new h.Shader(Shader.SCREEN_VERTEX_SHADER,
Shader.SCREEN_FRAGMENT_SHADER);return b.uniforms({u_texture:0})};Shader.getFlatScreenShader=function(a){a=a||n.gl;var b=a.shaders[":flat_screen"];if(b)return b;b=a.shaders[":flat_screen"]=new h.Shader(Shader.SCREEN_VERTEX_SHADER,Shader.FLAT_FRAGMENT_SHADER);return b.uniforms({u_color:[1,1,1,1]})};Shader.getColoredScreenShader=function(a){a=a||n.gl;var b=a.shaders[":colored_screen"];if(b)return b;b=a.shaders[":colored_screen"]=new h.Shader(Shader.SCREEN_VERTEX_SHADER,Shader.SCREEN_COLORED_FRAGMENT_SHADER);
return b.uniforms({u_texture:0,u_color:vec4.fromValues(1,1,1,1)})};Shader.getQuadShader=function(a){a=a||n.gl;var b=a.shaders[":quad"];return b?b:a.shaders[":quad"]=new h.Shader(Shader.QUAD_VERTEX_SHADER,Shader.QUAD_FRAGMENT_SHADER)};Shader.getPartialQuadShader=function(a){a=a||n.gl;var b=a.shaders[":quad2"];return b?b:a.shaders[":quad2"]=new h.Shader(Shader.QUAD_VERTEX_SHADER,Shader.QUAD2_FRAGMENT_SHADER)};Shader.getBlendShader=function(a){a=a||n.gl;var b=a.shaders[":blend"];return b?b:a.shaders[":blend"]=
new h.Shader(Shader.SCREEN_VERTEX_SHADER,Shader.BLEND_FRAGMENT_SHADER)};Shader.getBlurShader=function(a){a=a||n.gl;var b=a.shaders[":blur"];if(b)return b;b=new h.Shader(Shader.SCREEN_VERTEX_SHADER,"\n\t\t\tprecision highp float;\n\t\t\tvarying vec2 v_coord;\n\t\t\tuniform sampler2D u_texture;\n\t\t\tuniform vec2 u_offset;\n\t\t\tuniform float u_intensity;\n\t\t\tvoid main() {\n\t\t\t   vec4 sum = vec4(0.0);\n\t\t\t   sum += texture2D(u_texture, v_coord + u_offset * -4.0) * 0.05/0.98;\n\t\t\t   sum += texture2D(u_texture, v_coord + u_offset * -3.0) * 0.09/0.98;\n\t\t\t   sum += texture2D(u_texture, v_coord + u_offset * -2.0) * 0.12/0.98;\n\t\t\t   sum += texture2D(u_texture, v_coord + u_offset * -1.0) * 0.15/0.98;\n\t\t\t   sum += texture2D(u_texture, v_coord) * 0.16/0.98;\n\t\t\t   sum += texture2D(u_texture, v_coord + u_offset * 4.0) * 0.05/0.98;\n\t\t\t   sum += texture2D(u_texture, v_coord + u_offset * 3.0) * 0.09/0.98;\n\t\t\t   sum += texture2D(u_texture, v_coord + u_offset * 2.0) * 0.12/0.98;\n\t\t\t   sum += texture2D(u_texture, v_coord + u_offset * 1.0) * 0.15/0.98;\n\t\t\t   gl_FragColor = u_intensity * sum;\n\t\t\t}\n\t\t\t");
return a.shaders[":blur"]=b};Shader.getCopyDepthShader=function(a){a=a||n.gl;var b=a.shaders[":copy_depth"];if(b)return b;b=new h.Shader(Shader.SCREEN_VERTEX_SHADER,"\n\t\t\t#extension GL_EXT_frag_depth : enable\n\t\t\tprecision highp float;\n\t\t\tvarying vec2 v_coord;\n\t\t\tuniform sampler2D u_texture;\n\t\t\tvoid main() {\n\t\t\t   gl_FragDepthEXT = texture2D( u_texture, v_coord ).x;\n\t\t\t   gl_FragColor = vec4(1.0);\n\t\t\t}\n\t\t\t");return a.shaders[":copy_depth"]=b};Shader.getCubemapShowShader=
function(a){a=a||n.gl;var b=a.shaders[":show_cubemap"];if(b)return b;b=new h.Shader(Shader.DEFAULT_VERTEX_SHADER,"\n\t\t\tprecision highp float;\n\t\t\tvarying vec3 v_normal;\n\t\t\tuniform samplerCube u_texture;\n\t\t\tvoid main() {\n\t\t\t   gl_FragColor = textureCube( u_texture, v_normal );\n\t\t\t}\n\t\t\t");b.uniforms({u_texture:0});return a.shaders[":show_cubemap"]=b};Shader.getPolarToCubemapShader=function(a){a=a||n.gl;var b=a.shaders[":polar_to_cubemap"];if(b)return b;b=new h.Shader(Shader.SCREEN_VERTEX_SHADER,
"\n\t\t\tprecision highp float;\n\t\t\tvarying vec2 v_coord;\n\t\t\tuniform sampler2D u_texture;\n\t\t\tuniform mat3 u_rotation;\n\t\t\tvoid main() {\n\t\t\t\tvec2 uv = vec2( v_coord.x, 1.0 - v_coord.y );\n\t\t\t\tvec3 dir = normalize( vec3( uv - vec2(0.5), 0.5 ));\n\t\t\t\tdir = u_rotation * dir;\n\t\t\t\tfloat u = atan(dir.x,dir.z) / 6.28318531;\n\t\t\t\tfloat v = (asin(dir.y) / 1.57079633) * 0.5 + 0.5;\n\t\t\t\tu = mod(u,1.0);\n\t\t\t\tv = mod(v,1.0);\n\t\t\t   gl_FragColor = texture2D( u_texture, vec2(u,v) );\n\t\t\t}\n\t\t\t");
return a.shaders[":polar_to_cubemap"]=b};Shader.getCubemapCopyShader=function(a){a=a||n.gl;var b=a.shaders[":copy_cubemap"];if(b)return b;b=new h.Shader(Shader.SCREEN_VERTEX_SHADER,"\n\t\t\tprecision highp float;\n\t\t\tvarying vec2 v_coord;\n\t\t\tuniform samplerCube u_texture;\n\t\t\tuniform mat3 u_rotation;\n\t\t\tvoid main() {\n\t\t\t\tvec2 uv = vec2( v_coord.x, 1.0 - v_coord.y );\n\t\t\t\tvec3 dir = vec3( uv - vec2(0.5), 0.5 );\n\t\t\t\tdir = u_rotation * dir;\n\t\t\t   gl_FragColor = textureCube( u_texture, dir );\n\t\t\t}\n\t\t\t");
return a.shaders[":copy_cubemap"]=b};Shader.getCubemapBlurShader=function(a){a=a||n.gl;var b=a.shaders[":blur_cubemap"];if(b)return b;b=new h.Shader(Shader.SCREEN_VERTEX_SHADER,"\n\t\t\t#ifndef NUM_SAMPLES\n\t\t\t\t#define NUM_SAMPLES 4\n\t\t\t#endif\n\t\t\t\n\t\t\tprecision highp float;\n\t\t\tvarying vec2 v_coord;\n\t\t\tuniform samplerCube u_texture;\n\t\t\tuniform mat3 u_rotation;\n\t\t\tuniform vec2 u_offset;\n\t\t\tuniform float u_intensity;\n\t\t\tvoid main() {\n\t\t\t\tvec4 sum = vec4(0.0);\n\t\t\t\tvec2 uv = vec2( v_coord.x, 1.0 - v_coord.y ) - vec2(0.5);\n\t\t\t\tvec3 dir = vec3(0.0);\n\t\t\t\tvec4 color = vec4(0.0);\n\t\t\t\tfor( int x = -2; x <= 2; x++ )\n\t\t\t\t{\n\t\t\t\t\tfor( int y = -2; y <= 2; y++ )\n\t\t\t\t\t{\n\t\t\t\t\t\tdir.xy = uv + vec2( u_offset.x * float(x), u_offset.y * float(y)) * 0.5;\n\t\t\t\t\t\tdir.z = 0.5;\n\t\t\t\t\t\tdir = u_rotation * dir;\n\t\t\t\t\t\tcolor = textureCube( u_texture, dir );\n\t\t\t\t\t\tcolor.xyz = color.xyz * color.xyz;/*linearize*/\n\t\t\t\t\t\tsum += color;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tsum /= 25.0;\n\t\t\t   gl_FragColor = vec4( sqrt( sum.xyz ), sum.w ) ;\n\t\t\t}\n\t\t\t");
return a.shaders[":blur_cubemap"]=b};Shader.FXAA_FUNC="\n\tuniform vec2 u_viewportSize;\n\tuniform vec2 u_iViewportSize;\n\t#define FXAA_REDUCE_MIN   (1.0/ 128.0)\n\t#define FXAA_REDUCE_MUL   (1.0 / 8.0)\n\t#define FXAA_SPAN_MAX     8.0\n\t\n\t/* from mitsuhiko/webgl-meincraft based on the code on geeks3d.com */\n\tvec4 applyFXAA(sampler2D tex, vec2 fragCoord)\n\t{\n\t\tvec4 color = vec4(0.0);\n\t\t/*vec2 u_iViewportSize = vec2(1.0 / u_viewportSize.x, 1.0 / u_viewportSize.y);*/\n\t\tvec3 rgbNW = texture2D(tex, (fragCoord + vec2(-1.0, -1.0)) * u_iViewportSize).xyz;\n\t\tvec3 rgbNE = texture2D(tex, (fragCoord + vec2(1.0, -1.0)) * u_iViewportSize).xyz;\n\t\tvec3 rgbSW = texture2D(tex, (fragCoord + vec2(-1.0, 1.0)) * u_iViewportSize).xyz;\n\t\tvec3 rgbSE = texture2D(tex, (fragCoord + vec2(1.0, 1.0)) * u_iViewportSize).xyz;\n\t\tvec3 rgbM  = texture2D(tex, fragCoord  * u_iViewportSize).xyz;\n\t\tvec3 luma = vec3(0.299, 0.587, 0.114);\n\t\tfloat lumaNW = dot(rgbNW, luma);\n\t\tfloat lumaNE = dot(rgbNE, luma);\n\t\tfloat lumaSW = dot(rgbSW, luma);\n\t\tfloat lumaSE = dot(rgbSE, luma);\n\t\tfloat lumaM  = dot(rgbM,  luma);\n\t\tfloat lumaMin = min(lumaM, min(min(lumaNW, lumaNE), min(lumaSW, lumaSE)));\n\t\tfloat lumaMax = max(lumaM, max(max(lumaNW, lumaNE), max(lumaSW, lumaSE)));\n\t\t\n\t\tvec2 dir;\n\t\tdir.x = -((lumaNW + lumaNE) - (lumaSW + lumaSE));\n\t\tdir.y =  ((lumaNW + lumaSW) - (lumaNE + lumaSE));\n\t\t\n\t\tfloat dirReduce = max((lumaNW + lumaNE + lumaSW + lumaSE) * (0.25 * FXAA_REDUCE_MUL), FXAA_REDUCE_MIN);\n\t\t\n\t\tfloat rcpDirMin = 1.0 / (min(abs(dir.x), abs(dir.y)) + dirReduce);\n\t\tdir = min(vec2(FXAA_SPAN_MAX, FXAA_SPAN_MAX), max(vec2(-FXAA_SPAN_MAX, -FXAA_SPAN_MAX), dir * rcpDirMin)) * u_iViewportSize;\n\t\t\n\t\tvec3 rgbA = 0.5 * (texture2D(tex, fragCoord * u_iViewportSize + dir * (1.0 / 3.0 - 0.5)).xyz + \n\t\t\ttexture2D(tex, fragCoord * u_iViewportSize + dir * (2.0 / 3.0 - 0.5)).xyz);\n\t\tvec3 rgbB = rgbA * 0.5 + 0.25 * (texture2D(tex, fragCoord * u_iViewportSize + dir * -0.5).xyz + \n\t\t\ttexture2D(tex, fragCoord * u_iViewportSize + dir * 0.5).xyz);\n\t\t\n\t\treturn vec4(rgbA,1.0);\n\t\tfloat lumaB = dot(rgbB, luma);\n\t\tif ((lumaB < lumaMin) || (lumaB > lumaMax))\n\t\t\tcolor = vec4(rgbA, 1.0);\n\t\telse\n\t\t\tcolor = vec4(rgbB, 1.0);\n\t\treturn color;\n\t}\n";
Shader.getFXAAShader=function(a){a=a||n.gl;var b=a.shaders[":fxaa"];if(b)return b;var b=new h.Shader(Shader.SCREEN_VERTEX_SHADER,"\n\t\t\tprecision highp float;\n\t\t\tvarying vec2 v_coord;\n\t\t\tuniform sampler2D u_texture;\n\t\t\t"+Shader.FXAA_FUNC+"\n\t\t\t\n\t\t\tvoid main() {\n\t\t\t   gl_FragColor = applyFXAA( u_texture, v_coord * u_viewportSize) ;\n\t\t\t}\n\t\t\t"),c=vec2.fromValues(a.viewport_data[2],a.viewport_data[3]),d=vec2.fromValues(1/a.viewport_data[2],1/a.viewport_data[3]);b.setup=
function(){c[0]=a.viewport_data[2];c[1]=a.viewport_data[3];d[0]=1/a.viewport_data[2];d[1]=1/a.viewport_data[3];this.uniforms({u_viewportSize:c,u_iViewportSize:d})};return a.shaders[":fxaa"]=b};Shader.getFlatShader=function(a){a=a||n.gl;var b=a.shaders[":flat"];if(b)return b;b=new h.Shader(Shader.FLAT_VERTEX_SHADER,Shader.FLAT_FRAGMENT_SHADER);b.uniforms({u_color:[1,1,1,1]});return a.shaders[":flat"]=b};h.create=function(a){function b(a){if(!l.ignore_events){var c=l.mouse.buttons;h.augmentEvent(a,
f);a.eventType=a.eventType||a.type;var d=getTime();q.dragging=a.dragging;q.position[0]=a.canvasx;q.position[1]=a.canvasy;q.x=a.canvasx;q.y=a.canvasy;q.mousex=a.mousex;q.mousey=a.mousey;q.canvasx=a.canvasx;q.canvasy=a.canvasy;q.clientx=a.mousex;q.clienty=a.mousey;q.buttons=a.buttons;q.left_button=!!(q.buttons&h.LEFT_MOUSE_BUTTON_MASK);q.middle_button=!!(q.buttons&h.MIDDLE_MOUSE_BUTTON_MASK);q.right_button=!!(q.buttons&h.RIGHT_MOUSE_BUTTON_MASK);if("mousedown"==a.eventType){0==c&&(f.removeEventListener("mousemove",
b),c=f.ownerDocument,c.addEventListener("mousemove",b),c.addEventListener("mouseup",b));p=d;if(l.onmousedown)l.onmousedown(a);v.trigger(l,"mousedown")}else if("mousemove"==a.eventType){if(l.onmousemove)l.onmousemove(a);v.trigger(l,"mousemove",a)}else if("mouseup"==a.eventType){0==l.mouse.buttons&&(f.addEventListener("mousemove",b),c=f.ownerDocument,c.removeEventListener("mousemove",b),c.removeEventListener("mouseup",b));a.click_time=d-p;if(l.onmouseup)l.onmouseup(a);v.trigger(l,"mouseup",a)}else if("mousewheel"==
a.eventType||"wheel"==a.eventType||"DOMMouseScroll"==a.eventType){a.eventType="mousewheel";a.wheel="wheel"==a.type?-a.deltaY:null!=a.wheelDeltaY?a.wheelDeltaY:-60*a.detail;a.delta=void 0!==a.wheelDelta?a.wheelDelta/40:a.deltaY?-a.deltaY/3:0;if(l.onmousewheel)l.onmousewheel(a);v.trigger(l,"mousewheel",a)}else if("dragstart"==a.eventType){if(l.ondragstart)l.ondragstart(a);v.trigger(l,"dragstart",a)}if(l.onmouse)l.onmouse(a);if(!a.skip_preventDefault)return"mousemove"!=a.eventType&&a.stopPropagation(),
a.preventDefault(),!1}}function c(a){var b=a.changedTouches,c=b[0],d="";if(!(l.ontouch&&!0===l.ontouch(a)||!0===v.trigger(l,a.type,a)||!r||a.touches.length&&a.changedTouches[0].identifier!==a.touches[0].identifier||1<b)){switch(a.type){case "touchstart":d="mousedown";break;case "touchmove":d="mousemove";break;case "touchend":d="mouseup";break;default:return}b=document.createEvent("MouseEvent");b.initMouseEvent(d,!0,!0,window,1,c.screenX,c.screenY,c.clientX,c.clientY,!1,!1,!1,!1,0,null);b.originalEvent=
b;b.is_touch=!0;c.target.dispatchEvent(b);a.preventDefault()}}function d(a){a.eventType=a.type;l.ongesture&&!1===l.ongesture(a)||!1!==v.trigger(l,a.type,a)&&a.preventDefault()}a=a||{};var f=null;if(a.canvas)if("string"==typeof a.canvas){if(f=document.getElementById(a.canvas),!f)throw"Canvas element not found: "+a.canvas;}else f=a.canvas;else{var k=null;a.container&&(k=a.container.constructor===String?document.querySelector(a.container):a.container);if(k&&!a.width){var s=k.getBoundingClientRect();
a.width=s.width;a.height=s.height}f=createCanvas(a.width||800,a.height||600);k&&k.appendChild(f)}"alpha"in a||(a.alpha=!1);var l=null,k=null;2==a.version?k=["webgl2","experimental-webgl2"]:1==a.version||void 0===a.version?k=["webgl","experimental-webgl"]:0===a.version&&(k=["webgl2","experimental-webgl2","webgl","experimental-webgl"]);if(!k)throw"Incorrect WebGL version, must be 1 or 2";a={alpha:void 0===a.alpha?!0:a.alpha,depth:void 0===a.depth?!0:a.depth,stencil:void 0===a.stencil?!0:a.stencil,antialias:void 0===
a.antialias?!0:a.antialias,premultipliedAlpha:void 0===a.premultipliedAlpha?!0:a.premultipliedAlpha,preserveDrawingBuffer:void 0===a.preserveDrawingBuffer?!0:a.preserveDrawingBuffer};for(s=0;s<k.length;++s){try{l=f.getContext(k[s],a)}catch(m){}if(l)break}if(!l){if(f.getContext("webgl"))throw"WebGL supported but not with those parameters";throw"WebGL not supported";}l.webgl_version="WebGL2RenderingContext"===l.constructor.name?2:1;n.gl=l;f.is_webgl=!0;f.gl=l;l.context_id=this.last_context_id++;l.extensions=
{};l.extensions.OES_standard_derivatives=l.derivatives_supported=l.getExtension("OES_standard_derivatives")||!1;l.extensions.WEBGL_depth_texture=l.getExtension("WEBGL_depth_texture")||l.getExtension("WEBKIT_WEBGL_depth_texture")||l.getExtension("MOZ_WEBGL_depth_texture");l.extensions.OES_element_index_uint=l.getExtension("OES_element_index_uint");l.extensions.WEBGL_draw_buffers=l.getExtension("WEBGL_draw_buffers");l.extensions.EXT_shader_texture_lod=l.getExtension("EXT_shader_texture_lod");l.extensions.EXT_sRGB=
l.getExtension("EXT_sRGB");l.extensions.EXT_texture_filter_anisotropic=l.getExtension("EXT_texture_filter_anisotropic")||l.getExtension("WEBKIT_EXT_texture_filter_anisotropic")||l.getExtension("MOZ_EXT_texture_filter_anisotropic");l.extensions.EXT_frag_depth=l.getExtension("EXT_frag_depth")||l.getExtension("WEBKIT_EXT_frag_depth")||l.getExtension("MOZ_EXT_frag_depth");l.extensions.WEBGL_lose_context=l.getExtension("WEBGL_lose_context")||l.getExtension("WEBKIT_WEBGL_lose_context")||l.getExtension("MOZ_WEBGL_lose_context");
l.extensions.ANGLE_instanced_arrays=l.getExtension("ANGLE_instanced_arrays");l.extensions.disjoint_timer_query=l.getExtension("EXT_disjoint_timer_query");l.extensions.OES_texture_float_linear=l.getExtension("OES_texture_float_linear");l.extensions.OES_texture_float_linear&&(l.extensions.OES_texture_float=l.getExtension("OES_texture_float"));l.extensions.EXT_color_buffer_float=l.getExtension("EXT_color_buffer_float");l.extensions.OES_texture_half_float_linear=l.getExtension("OES_texture_half_float_linear");
l.extensions.OES_texture_half_float_linear&&(l.extensions.OES_texture_half_float=l.getExtension("OES_texture_half_float"));l.HIGH_PRECISION_FORMAT=1==l.webgl_version?l.extensions.OES_texture_half_float?h.HALF_FLOAT_OES:l.extensions.OES_texture_float?h.FLOAT:h.UNSIGNED_BYTE:h.HALF_FLOAT_OES;l.max_texture_units=l.getParameter(l.MAX_TEXTURE_IMAGE_UNITS);l._viewport_func?console.warn("Creating LiteGL context over the same canvas twice"):(l._viewport_func=l.viewport,l.viewport_data=new Float32Array([0,
0,l.canvas.width,l.canvas.height]),l.viewport=function(a,b,c,d){var f=this.viewport_data;f[0]=a|0;f[1]=b|0;f[2]=c|0;f[3]=d|0;this._viewport_func(a,b,c,d)},l.getViewport=function(a){return a?(a[0]=l.viewport_data[0],a[1]=l.viewport_data[1],a[2]=l.viewport_data[2],a[3]=l.viewport_data[3],a):new Float32Array(l.viewport_data)},l.setViewport=function(a,b){l.viewport_data.set(a);b&&(l.viewport_data[1]=this.drawingBufferHeight-a[1]-a[3]);this._viewport_func(a[0],l.viewport_data[1],a[2],a[3])});if(!h.reverse)for(s in h.reverse=
{},l)l[s]&&l[s].constructor===Number&&(h.reverse[l[s]]=s);if("undefined"==typeof glMatrix)throw"glMatrix not found, LiteGL requires glMatrix to be included";var p=0;l.shaders={};l.textures={};l.meshes={};l.makeCurrent=function(){n.gl=this};l.execute=function(a){var b=n.gl;n.gl=this;a();n.gl=b};l.animate=function(a){function b(){if(!l.destroyed){f._requestFrame_id=c(b);var a=getTime(),k=0.001*(a-d);f.mouse&&(f.mouse.last_buttons=f.mouse.buttons);if(f.onupdate)f.onupdate(k);v.trigger(f,"update",k);
f.ondraw&&(k=n.gl,n.gl=f,f.ondraw(),v.trigger(f,"draw"),n.gl=k);d=a}}if(!1===a)n.cancelAnimationFrame(this._requestFrame_id),this._requestFrame_id=null;else{var c=n.requestAnimationFrame,d=getTime(),f=this;this._requestFrame_id=c(b)}};l.destroy=function(){w&&(document.removeEventListener("keydown",w),document.removeEventListener("keyup",w));this.canvas.parentNode&&this.canvas.parentNode.removeChild(this.canvas);this.destroyed=!0;n.gl==this&&(n.gl=null)};var q=l.mouse={buttons:0,last_buttons:0,left_button:!1,
middle_button:!1,right_button:!1,position:new Float32Array(2),x:0,y:0,deltax:0,deltay:0,clientx:0,clienty:0,isInsideRect:function(a,b,c,d,f){var k=this.y;f&&(k=l.canvas.height-k);return this.x>a&&this.x<a+c&&k>b&&k<b+d?!0:!1},isButtonPressed:function(a){if(a==h.LEFT_MOUSE_BUTTON)return this.buttons&h.LEFT_MOUSE_BUTTON_MASK;if(a==h.MIDDLE_MOUSE_BUTTON)return this.buttons&h.MIDDLE_MOUSE_BUTTON_MASK;if(a==h.RIGHT_MOUSE_BUTTON)return this.buttons&h.RIGHT_MOUSE_BUTTON_MASK},wasButtonPressed:function(a){var b=
0;a==h.LEFT_MOUSE_BUTTON?b=h.LEFT_MOUSE_BUTTON_MASK:a==h.MIDDLE_MOUSE_BUTTON?b=h.MIDDLE_MOUSE_BUTTON_MASK:a==h.RIGHT_MOUSE_BUTTON&&(b=h.RIGHT_MOUSE_BUTTON_MASK);return this.buttons&b&&!(this.last_buttons&b)}};l.captureMouse=function(a,c){f.addEventListener("mousedown",b);f.addEventListener("mousemove",b);f.addEventListener("dragstart",b);a&&(f.addEventListener("mousewheel",b,!1),f.addEventListener("wheel",b,!1));f.addEventListener("contextmenu",function(a){a.preventDefault();return!1});c&&this.captureTouch(!0)};
var r=!1;l.captureTouch=function(a){r=a;f.addEventListener("touchstart",c,!0);f.addEventListener("touchmove",c,!0);f.addEventListener("touchend",c,!0);f.addEventListener("touchcancel",c,!0);f.addEventListener("gesturestart",d);f.addEventListener("gesturechange",d);f.addEventListener("gestureend",d)};l.keys={};var w=null;l.captureKeys=function(a,b){function c(b){var d=a;b.eventType=b.type;var f=b.target.nodeName.toLowerCase();if("input"!==f&&"textarea"!==f&&"select"!==f){b.character=String.fromCharCode(b.keyCode).toLowerCase();
var f=!1,k=h.mapKeyCode(b.keyCode);k||(k=b.character);b.altKey||b.ctrlKey||b.metaKey||(k&&(l.keys[k]="keydown"==b.type),f=l.keys[b.keyCode],l.keys[b.keyCode]="keydown"==b.type);if(f!=l.keys[b.keyCode]){if("keydown"==b.type&&l.onkeydown)l.onkeydown(b);else if("keyup"==b.type&&l.onkeyup)l.onkeyup(b);v.trigger(l,b.type,b)}if(l.onkey)l.onkey(b);d&&(b.isChar||h.blockable_keys[b.keyIdentifier||b.key])&&b.preventDefault()}}w||(l.keys={},document.addEventListener("keydown",c),document.addEventListener("keyup",
c),w=c)};l.gamepads=null;l.captureGamepads=function(){var a=navigator.getGamepads||navigator.webkitGetGamepads||navigator.mozGetGamepads;a&&(this.gamepads=a.call(navigator))};l.getGamepads=function(a){var b=navigator.getGamepads||navigator.webkitGetGamepads||navigator.mozGetGamepads;if(b){b=b.call(navigator);this.gamepads||(this.gamepads=[]);for(var c=0;4>c;c++){var d=b[c];if(d&&!a){var f=d,k=f.xbox||{axes:[],buttons:{},hat:""};k.axes.lx=f.axes[0];k.axes.ly=f.axes[1];k.axes.rx=f.axes[2];k.axes.ry=
f.axes[3];k.axes.triggers=f.axes[4];for(var h=0;h<f.buttons.length;h++)switch(h){case 0:k.buttons.a=f.buttons[h].pressed;break;case 1:k.buttons.b=f.buttons[h].pressed;break;case 2:k.buttons.x=f.buttons[h].pressed;break;case 3:k.buttons.y=f.buttons[h].pressed;break;case 4:k.buttons.lb=f.buttons[h].pressed;break;case 5:k.buttons.rb=f.buttons[h].pressed;break;case 6:k.buttons.lt=f.buttons[h].pressed;break;case 7:k.buttons.rt=f.buttons[h].pressed;break;case 8:k.buttons.back=f.buttons[h].pressed;break;
case 9:k.buttons.start=f.buttons[h].pressed;break;case 10:k.buttons.ls=f.buttons[h].pressed;break;case 11:k.buttons.rs=f.buttons[h].pressed;break;case 12:f.buttons[h].pressed&&(k.hat+="up");break;case 13:f.buttons[h].pressed&&(k.hat+="down");break;case 14:f.buttons[h].pressed&&(k.hat+="left");break;case 15:f.buttons[h].pressed&&(k.hat+="right");break;case 16:k.buttons.home=f.buttons[h].pressed}f.xbox=k}if(d&&!d.prev_buttons){d.prev_buttons=new Uint8Array(32);f=new CustomEvent("gamepadconnected");
f.eventType=f.type;f.gamepad=d;if(this.ongamepadconnected)this.ongamepadconnected(f);v.trigger(l,"gamepadconnected",f)}if(d)for(k=0;k<d.buttons.length;++k){h=d.buttons[k];h.was_pressed=!1;if(h.pressed&&!d.prev_buttons[k]){h.was_pressed=!0;f=new CustomEvent("gamepadButtonDown");f.eventType=f.type;f.button=h;f.which=k;f.gamepad=d;if(l.onbuttondown)l.onbuttondown(f);v.trigger(l,"buttondown",f)}else if(!h.pressed&&d.prev_buttons[k]){f=new CustomEvent("gamepadButtonUp");f.eventType=f.type;f.button=h;f.which=
k;f.gamepad=d;if(l.onbuttondown)l.onbuttondown(f);v.trigger(l,"buttonup",f)}d.prev_buttons[k]=h.pressed?1:0}}return this.gamepads=b}};l.fullscreen=function(){var a=this.canvas;a.requestFullScreen?a.requestFullScreen():a.webkitRequestFullScreen?a.webkitRequestFullScreen():a.mozRequestFullScreen?a.mozRequestFullScreen():console.error("Fullscreen not supported")};l.snapshot=function(a,b,c,d,f){var k=createCanvas(c,d),h=k.getContext("2d"),s=h.getImageData(0,0,k.width,k.height),m=new Uint8Array(c*d*4);
l.readPixels(a,b,k.width,k.height,l.RGBA,l.UNSIGNED_BYTE,m);s.data.set(m);h.putImageData(s,0,0);if(f)return k;a=createCanvas(c,d);h=a.getContext("2d");h.translate(0,d);h.scale(1,-1);h.drawImage(k,0,0);return a};var u={};l.loadTexture=function(a,b,c){if(this.textures[a])return this.textures[a];if(u[a])return null;var d=new Image;d.url=a;d.onload=function(){var a=h.Texture.fromImage(this,b);a.img=this;l.textures[this.url]=a;delete u[this.url];c&&c(a)};d.src=a;u[a]=!0;return null};l.drawTexture=function(){var a=
mat3.create(),b=vec2.create(),c=vec2.create(),d=vec4.create(),f=vec4.fromValues(1,1,1,1),k=vec2.create(),h={u_texture:0,u_position:b,u_color:f,u_size:c,u_texture_area:d,u_viewport:k,u_transform:a};return function(a,f,s,m,p,q,r,n,t,w,u){b[0]=f;b[1]=s;void 0===m&&(m=a.width);void 0===p&&(p=a.height);c[0]=m;c[1]=p;void 0===q&&(q=0);void 0===r&&(r=0);void 0===n&&(n=a.width);void 0===t&&(t=a.height);d[0]=q/a.width;d[1]=r/a.height;d[2]=(q+n)/a.width;d[3]=(r+t)/a.height;k[0]=this.viewport_data[2];k[1]=this.viewport_data[3];
w=w||Shader.getPartialQuadShader(this);f=Mesh.getScreenQuad(this);a.bind(0);w.uniforms(h);u&&w.uniforms(u);w.draw(f,l.TRIANGLES)}}();l.canvas.addEventListener("webglcontextlost",function(a){a.preventDefault();l.context_lost=!0;if(l.onlosecontext)l.onlosecontext(a)},!1);l.reset=function(){l.viewport(0,0,this.canvas.width,this.canvas.height);l.disable(l.BLEND);l.disable(l.CULL_FACE);l.disable(l.DEPTH_TEST);l.frontFace(l.CCW);l._current_texture_drawto=null;l._current_fbo_color=null;l._current_fbo_depth=
null};l.dump=function(){console.log("userAgent: ",navigator.userAgent);console.log("Supported extensions:");var a=l.getSupportedExtensions();console.log(a.join(","));a="VENDOR VERSION MAX_VERTEX_ATTRIBS MAX_VARYING_VECTORS MAX_VERTEX_UNIFORM_VECTORS MAX_VERTEX_TEXTURE_IMAGE_UNITS MAX_FRAGMENT_UNIFORM_VECTORS MAX_TEXTURE_SIZE MAX_TEXTURE_IMAGE_UNITS".split(" ");console.log("WebGL info:");for(var b in a)console.log(" * "+a[b]+": "+l.getParameter(l[a[b]]));console.log("*************************************************")};
l.reset();return l};h.mapKeyCode=function(a){return{8:"BACKSPACE",9:"TAB",13:"ENTER",16:"SHIFT",17:"CTRL",27:"ESCAPE",32:"SPACE",37:"LEFT",38:"UP",39:"RIGHT",40:"DOWN"}[a]||(65<=a&&90>=a?String.fromCharCode(a):null)};h.dragging=!1;h.last_pos=[0,0];h.augmentEvent=function(a,b){var c=null;b=b||a.target||gl.canvas;c=b.getBoundingClientRect();a.mousex=a.clientX-c.left;a.mousey=a.clientY-c.top;a.canvasx=a.mousex;a.canvasy=c.height-a.mousey;a.deltax=0;a.deltay=0;"mousedown"==a.type?this.dragging=!0:"mousemove"!=
a.type&&"mouseup"==a.type&&0==a.buttons&&(this.dragging=!1);void 0===a.movementX||h.isMobile()?(a.deltax=a.mousex-this.last_pos[0],a.deltay=a.mousey-this.last_pos[1]):(a.deltax=a.movementX,a.deltay=a.movementY);this.last_pos[0]=a.mousex;this.last_pos[1]=a.mousey;a.dragging=this.dragging;a.leftButton=!!(gl.mouse.buttons&h.LEFT_MOUSE_BUTTON_MASK);a.middleButton=!!(gl.mouse.buttons&h.MIDDLE_MOUSE_BUTTON_MASK);a.rightButton=!!(gl.mouse.buttons&h.RIGHT_MOUSE_BUTTON_MASK);a.buttons_mask=0;a.leftButton&&
(a.buttons_mask=1);a.middleButton&&(a.buttons_mask|=2);a.rightButton&&(a.buttons_mask|=4);a.isButtonPressed=function(a){return this.buttons_mask&1<<a}};h.isMobile=function(){return void 0!==this.mobile?this.mobile:n.navigator?navigator.userAgent.match(/iPhone/i)||navigator.userAgent.match(/iPod/i)||navigator.userAgent.match(/iPad/i)||navigator.userAgent.match(/Android/i)?this.mobile=!0:this.mobile=!1:this.mobile=!1};var v=n.LEvent=h.LEvent={bind:function(a,b,c,d){if(!a)throw"cannot bind event to null";
if(!c)throw"cannot bind to null callback";if(a.constructor===String)throw"cannot bind event to a string";var f=a.__levents;f||(Object.defineProperty(a,"__levents",{value:{},enumerable:!1}),f=a.__levents);f.hasOwnProperty(b)?f[b].push([c,d]):f[b]=[[c,d]];if(a.onLEventBinded)a.onLEventBinded(b,c,d)},unbind:function(a,b,c,d){if(!a)throw"cannot unbind event to null";if(!c)throw"cannot unbind from null callback";if(a.constructor===String)throw"cannot bind event to a string";var f=a.__levents;if(f&&f.hasOwnProperty(b)){for(var k=
0,h=f[b].length;k<h;++k){var l=f[b][k];if(l[0]===c&&l[1]===d){f[b].splice(k,1);break}}0==f[b].length&&delete f[b];if(a.onLEventUnbinded)a.onLEventUnbinded(b,c,d)}},unbindAll:function(a,b,c){if(!a)throw"cannot unbind events in null";var d=a.__levents;if(d){if(a.onLEventUnbindAll)a.onLEventUnbindAll(b,c);if(b)for(var f in d){a=d[f];for(var k=a.length-1;0<=k;--k)a[k][1]!=b||c&&c!==a[k][0]||a.splice(k,1)}else delete a.__levents}},unbindAllEvent:function(a,b){if(!a)throw"cannot unbind events in null";
var c=a.__levents;if(c&&(delete c[b],a.onLEventUnbindAll))a.onLEventUnbindAll(b,target_instance,callback)},isBind:function(a,b,c,d){if(!a)throw"LEvent cannot have null as instance";if(a=a.__levents){if(!a.hasOwnProperty(b))return!1;for(var f=0,k=a[b].length;f<k;++f){var h=a[b][f];if(h[0]===c&&h[1]===d)return!0}return!1}},hasBind:function(a,b){if(!a)throw"LEvent cannot have null as instance";var c=a.__levents;return c&&c.hasOwnProperty(b)&&c[b].length?!0:!1},hasBindTo:function(a,b){if(!a)throw"LEvent cannot have null as instance";
var c=a.__levents;if(!c)return!1;for(var d in c)for(var f=c[d],k=0;k<f.length;++k)if(f[k][1]===b)return!0;return!1},trigger:function(a,b,c,d,f){if(!a)throw"cannot trigger event from null";if(a.constructor===String)throw"cannot bind event to a string";a=a.__levents;if(!a||!a.hasOwnProperty(b))return!1;a=a[b];if(d)for(d=a.length-1;0<=d;--d){var k=a[d];if(f){if(k&&!0===k[0].apply(k[1],c))return!0}else if(k&&!0===k[0].call(k[1],b,c))return!0}else{d=0;for(var h=a.length;d<h;++d)if(k=a[d],f){if(k&&!0===
k[0].apply(k[1],c))return!0}else if(k&&!0===k[0].call(k[1],b,c))return!0}return!1},triggerArray:function(a,b,c,d,f){for(var k=!1,h=0,l=a.length;h<l;++h){var m=a[h];if(!m)throw"cannot trigger event from null";if(m.constructor===String)throw"cannot bind event to a string";if((m=m.__levents)&&m.hasOwnProperty(b))if(d)for(var p=m[b].length-1;0<=p;--p){var q=m[b][p];if(f){if(!0===q[0].apply(q[1],c)){k=!0;break}}else if(!0===q[0].call(q[1],b,c)){k=!0;break}}else for(var p=0,r=m[b].length;p<r;++p)if(q=m[b][p],
f){if(!0===q[0].apply(q[1],c)){k=!0;break}}else if(!0===q[0].call(q[1],b,c)){k=!0;break}}return k},extendObject:function(a){a.bind=function(a,c,d){return v.bind(this,a,c,d)};a.trigger=function(a,c){return v.trigger(this,a,c)};a.unbind=function(a,c,d){return v.unbind(this,a,c,instance)};a.unbindAll=function(a,c){return v.unbindAll(this,a,c)}},extendClass:function(a){this.extendObject(a.prototype)}};n.CLIP_INSIDE=h.CLIP_INSIDE=0;n.CLIP_OUTSIDE=h.CLIP_OUTSIDE=1;n.CLIP_OVERLAP=h.CLIP_OVERLAP=2;n.geo=
{createPlane:function(a,b){return new Float32Array([b[0],b[1],b[2],-vec3.dot(a,b)])},distancePointToPlane:function(a,b){return(vec3.dot(a,b)+b[3])/Math.sqrt(b[0]*b[0]+b[1]*b[1]+b[2]*b[2])},distance2PointToPlane:function(a,b){return(vec3.dot(a,b)+b[3])/(b[0]*b[0]+b[1]*b[1]+b[2]*b[2])},projectPointOnLine:function(a,b,c,d){d=d||vec3.create();a=vec3.fromValues(a[0]-b[0],a[1]-b[1],a[2]-b[2]);c=vec3.fromValues(c[0]-b[0],c[1]-b[1],c[2]-b[2]);a=vec3.dot(a,c)/vec3.dot(c,c);d[0]=b[0]+a[0]*c[0];d[1]=b[1]+a[1]*
c[1];d[2]=b[2]+a[2]*c[2];return d},project2DPointOnLine:function(a,b,c,d){d=d||vec2.create();a=vec2.fromValues(a[0]-b[0],a[1]-b[1]);c=vec2.fromValues(c[0]-b[0],c[1]-b[1]);a=vec2.dot(a,c)/vec2.dot(c,c);d[0]=b[0]+a[0]*c[0];d[1]=b[1]+a[1]*c[1];return d},projectPointOnPlane:function(a,b,c,d){d=d||vec3.create();b=vec3.subtract(vec3.create(),a,b);b=vec3.dot(b,c);return vec3.subtract(d,a,vec3.scale(vec3.create(),c,b))},reflectPointInPlane:function(a,b,c){b=-(-1*(b[0]*c[0]+b[1]*c[1]+b[2]*c[2])+c[0]*a[0]+
c[1]*a[1]+c[2]*a[2])/(c[0]*c[0]+c[1]*c[1]+c[2]*c[2]);return vec3.fromValues(a[0]+b*c[0]*2,a[1]+b*c[1]*2,a[2]+b*c[2]*2)},testRayPlane:function(a,b,c,d,f){c=vec3.dot(c,d)-vec3.dot(d,a);d=vec3.dot(d,b);if(Math.abs(d)<EPSILON)return!1;d=c/d;if(0>d)return!1;f&&vec3.add(f,a,vec3.scale(f,b,d));return!0},testSegmentPlane:function(){var a=vec3.create();return function(b,c,d,f,k){d=vec3.dot(d,f)-vec3.dot(f,b);c=vec3.sub(a,c,b);f=vec3.dot(f,c);if(Math.abs(f)<EPSILON)return!1;f=d/f;if(0>f||1<f)return!1;k&&vec3.add(k,
b,vec3.scale(k,c,f));return!0}}(),testRaySphere:function(){var a=vec3.create();return function(b,c,d,f,k,h){var l=vec3.subtract(a,b,d),m=c[0]*c[0]+c[1]*c[1]+c[2]*c[2];d=2*l[0]*c[0]+2*l[1]*c[1]+2*l[2]*c[2];f=d*d-4*m*(l[0]*l[0]+l[1]*l[1]+l[2]*l[2]-f*f);if(0>f)return!1;if(k){f=Math.sqrt(f);l=1/(2*m);m=(-d+f)*l;d=(-d-f)*l;d=m<d?m:d;if(void 0!==h&&d>h)return!1;vec3.add(k,b,vec3.scale(k,c,d))}return!0}}(),testRayCylinder:function(a,b,c,d,f,k){var h=vec3.clone(a);a=vec3.add(vec3.create(),a,vec3.scale(vec3.create(),
b,1E5));var l=0;b=vec3.subtract(vec3.create(),d,c);var m=vec3.subtract(vec3.create(),h,c);c=vec3.subtract(vec3.create(),a,h);d=vec3.dot(m,b);a=vec3.dot(c,b);b=vec3.dot(b,b);if(0>d&&0>d+a||d>b&&d+a>b)return!1;var p=vec3.dot(c,c),q=vec3.dot(m,c),l=b*p-a*a;f=vec3.dot(m,m)-f*f;var r=b*f-d*d;if(Math.abs(l)<EPSILON){if(0<r)return!1;k&&vec3.add(k,h,vec3.scale(k,c,0>d?-q/p:d>b?(a-q)/p:0));return!0}m=b*q-a*d;r=m*m-l*r;if(0>r)return!1;l=(-m-Math.sqrt(r))/l;if(0>l||1<l)return!1;if(0>d+l*a){if(0>=a)return!1;
l=-d/a;k&&vec3.add(k,h,vec3.scale(k,c,l));return 0>=f+2*l*(q+l*p)}if(d+l*a>b){if(0<=a)return!1;l=(b-d)/a;k&&vec3.add(k,h,vec3.scale(k,c,l));return 0>=f+b-2*d+l*(2*(q-a)+l*p)}k&&vec3.add(k,h,vec3.scale(k,c,l));return!0},testRayBox:function(){var a=new Float32Array(3),b=new Float32Array(3),c=new Float32Array(3);return function(d,f,k,h,l,m){m=m||Number.MAX_VALUE;var p=!0,q=0;a.fill(0);c.fill(0);b.fill(0);for(q=0;3>q;++q)d[q]<k[q]?(a[q]=1,b[q]=k[q],p=!1):d[q]>h[q]?(a[q]=0,b[q]=h[q],p=!1):a[q]=2;if(p)return l&&
vec3.copy(l,d),!0;for(q=0;3>q;++q)c[q]=2!=a[q]&&0!=f[q]?(b[q]-d[q])/f[q]:-1;p=0;for(q=1;3>q;q++)c[p]<c[q]&&(p=q);if(0>c[p]||c[p]>m)return!1;for(q=0;3>q;++q)if(p!=q){m=d[q]+c[p]*f[q];if(m<k[q]||m>h[q])return!1;l&&(l[q]=m)}else l&&(l[q]=b[q]);return!0}}(),testRayBBox:function(){var a=mat4.create(),b=vec3.create(),c=vec3.create();return function(d,f,k,h,l,m,p){if(!d||!f||!k)throw"parameters missing";h&&(mat4.invert(a,h),vec3.add(b,d,f),d=vec3.transformMat4(c,d,a),vec3.transformMat4(b,b,a),vec3.sub(b,
b,d),f=vec3.normalize(b,b));d=this.testRayBox(d,f,k.subarray(6,9),k.subarray(9,12),l,m);!p&&h&&l&&vec3.transformMat4(l,l,h);return d}}(),testPointBBox:function(a,b){return a[0]<b[6]||a[0]>b[9]||a[1]<b[7]||a[0]>b[10]||a[2]<b[8]||a[0]>b[11]?!1:!0},testBBoxBBox:function(a,b){if(Math.abs(b[0]-a[0])>a[3]+b[3]||Math.abs(b[1]-a[1])>a[4]+b[4]||Math.abs(b[2]-a[2])>a[5]+b[5])return!1;var c=BBox.getMin(b);geo.testPointBBox(c,a)&&(c=BBox.getMax(b),geo.testPointBBox(c,a));return!0},testSphereBBox:function(a,b,
c){for(var d=0,f=BBox.getMin(c),k=BBox.getMax(c),h=0;3>h;++h)a[h]<f[h]?(c=a[h]-f[h],d+=c*c):a[h]>k[h]&&(c=a[h]-k[h],d+=c*c);return d<=b*b?!0:!1},closestPointBetweenLines:function(a,b,c,d,f,k){b=vec3.subtract(vec3.create(),b,a);d=vec3.subtract(vec3.create(),d,c);var h=vec3.subtract(vec3.create(),a,c),l=vec3.dot(b,b),m=vec3.dot(b,d),p=vec3.dot(d,d),q=vec3.dot(b,h),r=vec3.dot(d,h),n=l*p-m*m,u;n<EPSILON?(u=0,l=m>p?q/m:r/p):(u=(m*r-p*q)/n,l=(l*r-m*q)/n);f&&vec3.add(f,a,vec3.scale(vec3.create(),b,u));k&&
vec3.add(k,c,vec3.scale(vec3.create(),d,l));a=vec3.add(vec3.create(),h,vec3.subtract(vec3.create(),vec3.scale(vec3.create(),b,u),vec3.scale(vec3.create(),d,l)));return vec3.length(a)},extractPlanes:function(a,b){function c(a){var c=b.subarray(a,a+3),c=vec3.length(c);0!==c&&(c=1/c,b[a]*=c,b[a+1]*=c,b[a+2]*=c,b[a+3]*=c)}b=b||new Float32Array(24);b.set([a[3]-a[0],a[7]-a[4],a[11]-a[8],a[15]-a[12]],0);c(0);b.set([a[3]+a[0],a[7]+a[4],a[11]+a[8],a[15]+a[12]],4);c(4);b.set([a[3]+a[1],a[7]+a[5],a[11]+a[9],
a[15]+a[13]],8);c(8);b.set([a[3]-a[1],a[7]-a[5],a[11]-a[9],a[15]-a[13]],12);c(12);b.set([a[3]-a[2],a[7]-a[6],a[11]-a[10],a[15]-a[14]],16);c(16);b.set([a[3]+a[2],a[7]+a[6],a[11]+a[10],a[15]+a[14]],20);c(20);return b},frustumTestBox:function(a,b){var c=0,d=0,c=planeBoxOverlap(a.subarray(0,4),b);if(c==CLIP_OUTSIDE)return CLIP_OUTSIDE;d+=c;c=planeBoxOverlap(a.subarray(4,8),b);if(c==CLIP_OUTSIDE)return CLIP_OUTSIDE;d+=c;c=planeBoxOverlap(a.subarray(8,12),b);if(c==CLIP_OUTSIDE)return CLIP_OUTSIDE;d+=c;
c=planeBoxOverlap(a.subarray(12,16),b);if(c==CLIP_OUTSIDE)return CLIP_OUTSIDE;d+=c;c=planeBoxOverlap(a.subarray(16,20),b);if(c==CLIP_OUTSIDE)return CLIP_OUTSIDE;d+=c;c=planeBoxOverlap(a.subarray(20,24),b);return c==CLIP_OUTSIDE?CLIP_OUTSIDE:0==d+c?CLIP_INSIDE:CLIP_OVERLAP},frustumTestSphere:function(a,b,c){var d,f=!1;d=distanceToPlane(a.subarray(0,4),b);if(d<-c)return CLIP_OUTSIDE;d>=-c&&d<=c&&(f=!0);d=distanceToPlane(a.subarray(4,8),b);if(d<-c)return CLIP_OUTSIDE;d>=-c&&d<=c&&(f=!0);d=distanceToPlane(a.subarray(8,
12),b);if(d<-c)return CLIP_OUTSIDE;d>=-c&&d<=c&&(f=!0);d=distanceToPlane(a.subarray(12,16),b);if(d<-c)return CLIP_OUTSIDE;d>=-c&&d<=c&&(f=!0);d=distanceToPlane(a.subarray(16,20),b);if(d<-c)return CLIP_OUTSIDE;d>=-c&&d<=c&&(f=!0);d=distanceToPlane(a.subarray(20,24),b);if(d<-c)return CLIP_OUTSIDE;d>=-c&&d<=c&&(f=!0);return f?CLIP_OVERLAP:CLIP_INSIDE},testPoint2DInPolygon:function(a,b){for(var c=!1,d=-1,f=a.length,k=f-1;++d<f;k=d)(a[d][1]<=b[1]&&b[1]<a[k][1]||a[k][1]<=b[1]&&b[1]<a[d][1])&&b[0]<(a[k][0]-
a[d][0])*(b[1]-a[d][1])/(a[k][1]-a[d][1])+a[d][0]&&(c=!c);return c}};n.BBox=h.BBox={center:0,halfsize:3,min:6,max:9,radius:12,data_length:13,corners:[vec3.fromValues(1,1,1),vec3.fromValues(1,1,-1),vec3.fromValues(1,-1,1),vec3.fromValues(1,-1,-1),vec3.fromValues(-1,1,1),vec3.fromValues(-1,1,-1),vec3.fromValues(-1,-1,1),vec3.fromValues(-1,-1,-1)],create:function(){return new Float32Array(13)},clone:function(a){return new Float32Array(a)},copy:function(a,b){a.set(b);return a},fromPoint:function(a){var b=
this.create();b.set(a,0);b.set(a,6);b.set(a,9);return b},fromMinMax:function(a,b){var c=this.create();this.setMinMax(c,a,b);return c},fromCenterHalfsize:function(a,b){var c=this.create();this.setCenterHalfsize(c,a,b);return c},fromPoints:function(a){var b=this.create();this.setFromPoints(b,a);return b},setFromPoints:function(a,b){var c=a.subarray(6,9),d=a.subarray(9,12);c[0]=b[0];c[1]=b[1];c[2]=b[2];d.set(c);for(var f=3,k=b.length;f<k;f+=3){var h=b[f],l=b[f+1],m=b[f+2];h<c[0]?c[0]=h:h>d[0]&&(d[0]=
h);l<c[1]?c[1]=l:l>d[1]&&(d[1]=l);m<c[2]?c[2]=m:m>d[2]&&(d[2]=m)}a[0]=0.5*(c[0]+d[0]);a[1]=0.5*(c[1]+d[1]);a[2]=0.5*(c[2]+d[2]);a[3]=d[0]-a[0];a[4]=d[1]-a[1];a[5]=d[2]-a[2];a[12]=Math.sqrt(a[3]*a[3]+a[4]*a[4]+a[5]*a[5]);return a},setMinMax:function(a,b,c){a[6]=b[0];a[7]=b[1];a[8]=b[2];a[9]=c[0];a[10]=c[1];a[11]=c[2];var d=a.subarray(3,6);vec3.sub(d,c,b);vec3.scale(d,d,0.5);a[0]=c[0]-d[0];a[1]=c[1]-d[1];a[2]=c[2]-d[2];a[12]=vec3.length(a.subarray(3,6));return a},setCenterHalfsize:function(a,b,c,d){a[0]=
b[0];a[1]=b[1];a[2]=b[2];a[3]=c[0];a[4]=c[1];a[5]=c[2];a[6]=a[0]-a[3];a[7]=a[1]-a[4];a[8]=a[2]-a[5];a[9]=a[0]+a[3];a[10]=a[1]+a[4];a[11]=a[2]+a[5];a[12]=d?d:vec3.length(c);return a},transformMat4:function(){for(var a=0,b=0,c=0,d=new Float32Array(24),f=[],k=0;24>k;k+=3)f.push(d.subarray(k,k+3));return function(k,h,m){var p=h[0],q=h[1],r=h[2];a=h[3];b=h[4];c=h[5];h=this.corners;for(var n=0;8>n;++n){var u=h[n],t=f[n];t[0]=a*u[0]+p;t[1]=b*u[1]+q;t[2]=c*u[2]+r;mat4.multiplyVec3(t,m,t)}return this.setFromPoints(k,
d)}}(),getCorners:function(a,b){var c=a.subarray(3,6),d=null;b?(b.set(this.corners),d=b):d=new Float32Array(this.corners);for(var f=0;8>f;++f){var k=d.subarray(3*f,3*f+3);vec3.multiply(k,c,k);vec3.add(k,k,a)}return d},merge:function(a,b,c){var d=a.subarray(6,9),f=a.subarray(9,12);vec3.min(d,b.subarray(6,9),c.subarray(6,9));vec3.max(f,b.subarray(9,12),c.subarray(9,12));return BBox.setMinMax(a,d,f)},extendToPoint:function(a,b){b[0]<a[6]?a[6]=b[0]:b[0]>a[9]&&(a[9]=b[0]);b[1]<a[7]?a[7]=b[1]:b[1]>a[10]&&
(a[10]=b[1]);b[2]<a[8]?a[8]=b[2]:b[2]>a[11]&&(a[11]=b[2]);var c=a.subarray(6,9),d=a.subarray(9,12),c=vec3.add(a.subarray(0,3),c,d);vec3.scale(c,c,0.5);vec3.subtract(a.subarray(3,6),d,c);a[12]=vec3.length(a.subarray(3,6));return a},clampPoint:function(a,b,c){a[0]=Math.clamp(c[0],b[0]-b[3],b[0]+b[3]);a[1]=Math.clamp(c[1],b[1]-b[4],b[1]+b[4]);a[2]=Math.clamp(c[2],b[2]-b[5],b[2]+b[5])},isPointInside:function(a,b){return a[0]-a[3]>b[0]||a[1]-a[4]>b[1]||a[2]-a[5]>b[2]||a[0]+a[3]<b[0]||a[1]+a[4]<b[1]||a[2]+
a[5]<b[2]?!1:!0},getCenter:function(a){return a.subarray(0,3)},getHalfsize:function(a){return a.subarray(3,6)},getMin:function(a){return a.subarray(6,9)},getMax:function(a){return a.subarray(9,12)},getRadius:function(a){return a[12]}};n.distanceToPlane=h.distanceToPlane=function(a,b){return vec3.dot(a,b)+a[3]};n.planeBoxOverlap=h.planeBoxOverlap=function(a,b){var c=a[3],d=Math.abs(b[3]*a[0])+Math.abs(b[4]*a[1])+Math.abs(b[5]*a[2]),c=vec3.dot(a,b)+c;return c<=-d?CLIP_OUTSIDE:c<=d?CLIP_OVERLAP:CLIP_INSIDE};
n.Octree=h.Octree=function(a){this.root=null;this.total_nodes=this.total_depth=0;a&&(this.buildFromMesh(a),this.total_nodes=this.trim())};Octree.MAX_NODE_TRIANGLES_RATIO=0.1;Octree.MAX_OCTREE_DEPTH=8;Octree.OCTREE_MARGIN_RATIO=0.01;Octree.OCTREE_MIN_MARGIN=0.1;Octree.prototype.buildFromMesh=function(a){this.total_nodes=this.total_depth=0;var b=a.getBuffer("vertices").data;if(a=a.getIndexBuffer("triangles"))a=a.data;var c=this.computeAABB(b);this.root=c;this.total_nodes=1;this.total_triangles=a?a.length/
3:b.length/9;this.max_node_triangles=this.total_triangles*Octree.MAX_NODE_TRIANGLES_RATIO;var d=vec3.create();vec3.scale(d,c.size,Octree.OCTREE_MARGIN_RATIO);d[0]<Octree.OCTREE_MIN_MARGIN&&(d[0]=Octree.OCTREE_MIN_MARGIN);d[1]<Octree.OCTREE_MIN_MARGIN&&(d[1]=Octree.OCTREE_MIN_MARGIN);d[2]<Octree.OCTREE_MIN_MARGIN&&(d[2]=Octree.OCTREE_MIN_MARGIN);vec3.sub(c.min,c.min,d);vec3.add(c.max,c.max,d);c.faces=[];c.inside=0;if(a)for(d=0;d<a.length;d+=3){var f=new Float32Array([b[3*a[d]],b[3*a[d]+1],b[3*a[d]+
2],b[3*a[d+1]],b[3*a[d+1]+1],b[3*a[d+1]+2],b[3*a[d+2]],b[3*a[d+2]+1],b[3*a[d+2]+2],d/3]);this.addToNode(f,c,0)}else for(d=0;d<b.length;d+=9)f=new Float32Array(10),f.set(b.subarray(d,d+9)),f[9]=d/9,this.addToNode(f,c,0);return c};Octree.prototype.addToNode=function(a,b,c){b.inside+=1;if(b.c){var d=this.computeAABB(a),f=!1,k;for(k in b.c){var h=b.c[k];if(Octree.isInsideAABB(d,h)){this.addToNode(a,h,c+1);f=!0;break}}f||(null==b.faces&&(b.faces=[]),b.faces.push(a))}else if(null==b.faces&&(b.faces=[]),
b.faces.push(a),b.faces.length>this.max_node_triangles&&c<Octree.MAX_OCTREE_DEPTH){this.splitNode(b);this.total_depth<c+1&&(this.total_depth=c+1);var l=b.faces.concat();b.faces=null;for(k in l){a=l[k];var d=this.computeAABB(a),f=!1,m;for(m in b.c)if(h=b.c[m],Octree.isInsideAABB(d,h)){this.addToNode(a,h,c+1);f=!0;break}f||(null==b.faces&&(b.faces=[]),b.faces.push(a))}}};Octree.prototype.octree_pos_ref=[[0,0,0],[0,0,1],[0,1,0],[0,1,1],[1,0,0],[1,0,1],[1,1,0],[1,1,1]];Octree.prototype.splitNode=function(a){a.c=
[];var b=[0.5*(a.max[0]-a.min[0]),0.5*(a.max[1]-a.min[1]),0.5*(a.max[2]-a.min[2])],c;for(c in this.octree_pos_ref){var d=this.octree_pos_ref[c],f={};this.total_nodes+=1;f.min=[a.min[0]+b[0]*d[0],a.min[1]+b[1]*d[1],a.min[2]+b[2]*d[2]];f.max=[f.min[0]+b[0],f.min[1]+b[1],f.min[2]+b[2]];f.faces=null;f.inside=0;a.c.push(f)}};Octree.prototype.computeAABB=function(a){for(var b=new Float32Array([a[0],a[1],a[2]]),c=new Float32Array([a[0],a[1],a[2]]),d=0;d<a.length;d+=3)for(var f=0;3>f;f++)b[f]>a[d+f]&&(b[f]=
a[d+f]),c[f]<a[d+f]&&(c[f]=a[d+f]);return{min:b,max:c,size:vec3.sub(vec3.create(),c,b)}};Octree.prototype.trim=function(a){a=a||this.root;if(!a.c)return 1;for(var b=1,c=[],d=a.c,f=0;f<d.length;++f)d[f].inside&&(c.push(d[f]),b+=this.trim(d[f]));a.c=c;return b};Octree.prototype.testRay=function(){var a=vec3.create(),b=vec3.create(),c=vec3.create(),d=vec3.create();return function(f,k,h,l,m){if(!this.root)throw"Error: octree not build";a.set(f);b.set(k);c.set(this.root.min);d.set(this.root.max);h=Octree.hitTestBox(a,
b,c,d);if(!h)return null;h=Octree.testRayInNode(this.root,a,b,m);return null!=h?(k=vec3.scale(vec3.create(),k,h.t),vec3.add(k,k,f),h.pos=k,h):null}}();Octree.prototype.testSphere=function(a,b){a=vec3.clone(a);if(!this.root)throw"Error: octree not build";var c=b*b;return Octree.testSphereBox(a,c,vec3.clone(this.root.min),vec3.clone(this.root.max))?Octree.testSphereInNode(this.root,a,c):!1};Octree.testRayInNode=function(a,b,c,d){var f=null,k=null;if(a.faces)for(var h=0,l=a.faces.length;h<l;++h){var m=
a.faces[h],f=Octree.hitTestTriangle(b,c,m.subarray(0,3),m.subarray(3,6),m.subarray(6,9),d);null!=f&&(f.face=m,k?k.mergeWith(f):k=f)}var l=vec3.create(),m=vec3.create(),p;if(a.c)for(h=0;h<a.c.length;++h)p=a.c[h],l.set(p.min),m.set(p.max),f=Octree.hitTestBox(b,c,l,m),null==f||k&&f.t>k.t||(f=Octree.testRayInNode(p,b,c,d),null!=f&&(k?k.mergeWith(f):k=f));return k};Octree.testSphereInNode=function(a,b,c){if(a.faces)for(var d=0,f=a.faces.length;d<f;++d){var k=a.faces[d];if(Octree.testSphereTriangle(b,c,
k.subarray(0,3),k.subarray(3,6),k.subarray(6,9)))return!0}var f=vec3.create(),k=vec3.create(),h;if(a.c)for(d=0;d<a.c.length;++d)if(h=a.c[d],f.set(h.min),k.set(h.max),Octree.testSphereBox(b,c,f,k)&&Octree.testSphereInNode(h,b,c))return!0;return!1};Octree.isInsideAABB=function(a,b){return a.min[0]<b.min[0]||a.min[1]<b.min[1]||a.min[2]<b.min[2]||a.max[0]>b.max[0]||a.max[1]>b.max[1]||a.max[2]>b.max[2]?!1:!0};Octree.hitTestBox=function(){var a=vec3.create(),b=vec3.create(),c=vec3.create(),d=vec3.create(),
f=vec3.create(),k=vec3.create(),h=vec3.fromValues(1E-6,1E-6,1E-6);return function(l,m,p,q){vec3.subtract(a,p,l);vec3.subtract(b,q,l);if(0>vec3.maxValue(a)&&0<vec3.minValue(b))return new HitTest(0,l,m);c[0]=1/m[0];c[1]=1/m[1];c[2]=1/m[2];vec3.multiply(a,a,c);vec3.multiply(b,b,c);vec3.min(d,a,b);vec3.max(f,a,b);var r=vec3.maxValue(d),n=vec3.minValue(f);return 0<r&&r<n?(l=vec3.add(vec3.create(),vec3.scale(k,m,r),l),vec3.add(p,p,h),vec3.subtract(p,p,h),new HitTest(r,l,vec3.fromValues((l[0]>q[0])-(l[0]<
p[0]),(l[1]>q[1])-(l[1]<p[1]),(l[2]>q[2])-(l[2]<p[2])))):null}}();Octree.hitTestTriangle=function(){var a=vec3.create(),b=vec3.create(),c=vec3.create(),d=vec3.create();return function(f,k,h,l,m,p){vec3.subtract(a,l,h);vec3.subtract(b,m,h);l=vec3.cross(vec3.create(),a,b);vec3.normalize(l,l);if(!p&&0<vec3.dot(l,k))return null;p=vec3.dot(l,vec3.subtract(d,h,f))/vec3.dot(l,k);if(0<p){k=vec3.scale(vec3.create(),k,p);vec3.add(k,k,f);vec3.subtract(c,k,h);f=vec3.dot(b,b);h=vec3.dot(b,a);m=vec3.dot(b,c);var q=
vec3.dot(a,a),r=vec3.dot(a,c),n=f*q-h*h,q=(q*m-h*r)/n;f=(f*r-h*m)/n;if(0<=q&&0<=f&&1>=q+f)return new HitTest(p,k,l)}return null}}();Octree.testSphereTriangle=function(){var a=vec3.create(),b=vec3.create(),c=vec3.create(),d=vec3.create(),f=vec3.create(),k=vec3.create(),h=vec3.create(),l=vec3.create();return function(m,p,q,r,n){vec3.sub(a,q,m);vec3.sub(b,r,m);vec3.sub(c,n,m);vec3.sub(d,b,a);vec3.sub(f,c,a);vec3.cross(l,d,f);m=vec3.dot(a,l);q=vec3.dot(l,l);m=m*m>p*q;var u=vec3.dot(a,a),t=vec3.dot(a,
b),x=vec3.dot(a,c),F=vec3.dot(b,b),v=vec3.dot(b,c),z=vec3.dot(c,c);q=u>p&t>u&x>u;r=F>p&t>F&v>F;n=z>p&x>z&v>z;var u=t-u,t=v-F,y=x-z;vec3.sub(k,c,b);vec3.sub(h,a,c);F=vec3.dot(d,d);z=vec3.dot(k,k);x=vec3.dot(h,h);v=vec3.scale(vec3.create(),a,F);vec3.sub(v,v,vec3.scale(vec3.create(),d,u));u=vec3.scale(vec3.create(),b,z);vec3.sub(u,u,vec3.scale(vec3.create(),k,t));t=vec3.scale(vec3.create(),c,x);vec3.sub(t,t,vec3.scale(vec3.create(),h,y));var G=vec3.scale(vec3.create(),c,F),G=vec3.sub(G,G,v),H=vec3.scale(vec3.create(),
a,z),H=vec3.sub(H,H,u),y=vec3.scale(vec3.create(),b,x),y=vec3.sub(y,y,t),F=vec3.dot(v,v)>p*F*F&0<vec3.dot(v,G),z=vec3.dot(u,u)>p*z*z&0<vec3.dot(u,H);p=vec3.dot(t,t)>p*x*x&0<vec3.dot(t,y);return!(m|q|r|n|F|z|p)}}();Octree.testSphereBox=function(a,b,c,d){for(var f,k=0,h=0;3>h;++h)a[h]<c[h]?(f=a[h]-c[h],k+=f*f):a[h]>d[h]&&(f=a[h]-d[h],k+=f*f);return k<=b?!0:!1};n.HitTest=h.HitTest=function(a,b,c){this.t=arguments.length?a:Number.MAX_VALUE;this.hit=b;this.normal=c;this.face=null};HitTest.prototype={mergeWith:function(a){0<
a.t&&a.t<this.t&&(this.t=a.t,this.hit=a.hit,this.normal=a.normal,this.face=a.face)}};n.Ray=h.Ray=function(a,b){this.origin=vec3.create();this.direction=vec3.create();this.collision_point=vec3.create();a&&this.origin.set(a);b&&this.direction.set(b)};Ray.prototype.testPlane=function(a,b){return geo.testRayPlane(this.origin,this.direction,a,b,this.collision_point)};Ray.prototype.testSphere=function(a,b,c){return geo.testRaySphere(this.origin,this.direction,a,b,this.collision_point,c)};n.Raytracer=h.Raytracer=
function(a,b){this.viewport=vec4.create();this.ray00=vec3.create();this.ray10=vec3.create();this.ray01=vec3.create();this.ray11=vec3.create();this.eye=vec3.create();this.setup(a,b)};Raytracer.prototype.setup=function(a,b){b=b||gl.viewport_data;this.viewport.set(b);var c=b[0],d=c+b[2],f=b[1],k=f+b[3];vec3.set(this.ray00,c,f,1);vec3.set(this.ray10,d,f,1);vec3.set(this.ray01,c,k,1);vec3.set(this.ray11,d,k,1);vec3.unproject(this.ray00,this.ray00,a,b);vec3.unproject(this.ray10,this.ray10,a,b);vec3.unproject(this.ray01,
this.ray01,a,b);vec3.unproject(this.ray11,this.ray11,a,b);c=this.eye;vec3.unproject(c,c,a,b);vec3.subtract(this.ray00,this.ray00,c);vec3.subtract(this.ray10,this.ray10,c);vec3.subtract(this.ray01,this.ray01,c);vec3.subtract(this.ray11,this.ray11,c)};Raytracer.prototype.getRayForPixel=function(){var a=vec3.create(),b=vec3.create();return function(c,d,f){f=f||vec3.create();c=(c-this.viewport[0])/this.viewport[2];d=1-(d-this.viewport[1])/this.viewport[3];vec3.lerp(a,this.ray00,this.ray10,c);vec3.lerp(b,
this.ray01,this.ray11,c);vec3.lerp(f,a,b,d);return vec3.normalize(f,f)}}();var U=mat4.create();Raytracer.hitTestBox=function(a,b,c,d,f){var k=new Float32Array(30);f&&(f=mat4.invert(U,f),a=mat4.multiplyVec3(k.subarray(3,6),f,a),b=mat4.rotateVec3(k.subarray(6,9),f,b));var h=vec3.subtract(k.subarray(9,12),c,a);vec3.divide(h,h,b);var l=vec3.subtract(k.subarray(12,15),d,a);vec3.divide(l,l,b);f=vec3.min(k.subarray(15,18),h,l);h=vec3.max(k.subarray(18,21),h,l);f=vec3.maxValue(f);h=vec3.minValue(h);return 0<
f&&f<=h?(b=vec3.scale(k.subarray(21,24),b,f),vec3.add(b,a,b),vec3.addValue(k.subarray(24,27),c,1E-6),vec3.subValue(k.subarray(27,30),d,1E-6),new HitTest(f,b,vec3.fromValues((b[0]>d[0])-(b[0]<c[0]),(b[1]>d[1])-(b[1]<c[1]),(b[2]>d[2])-(b[2]<c[2])))):null};Raytracer.hitTestSphere=function(a,b,c,d){var f=vec3.subtract(vec3.create(),a,c),k=vec3.dot(b,b),h=2*vec3.dot(b,f),f=vec3.dot(f,f)-d*d,f=h*h-4*k*f;return 0<f?(k=(-h-Math.sqrt(f))/(2*k),a=vec3.add(vec3.create(),a,vec3.scale(vec3.create(),b,k)),new HitTest(k,
a,vec3.scale(vec3.create(),vec3.subtract(vec3.create(),a,c),1/d))):null};Raytracer.hitTestTriangle=function(a,b,c,d,f){var k=vec3.subtract(vec3.create(),d,c),h=vec3.subtract(vec3.create(),f,c);f=vec3.cross(vec3.create(),k,h);vec3.normalize(f,f);d=vec3.dot(f,vec3.subtract(vec3.create(),c,a))/vec3.dot(f,b);if(0<d){a=vec3.add(vec3.create(),a,vec3.scale(vec3.create(),b,d));var l=vec3.subtract(vec3.create(),a,c);c=vec3.dot(h,h);b=vec3.dot(h,k);var h=vec3.dot(h,l),m=vec3.dot(k,k),k=vec3.dot(k,l),l=c*m-
b*b,m=(m*h-b*k)/l,k=(c*k-b*h)/l;if(0<=m&&0<=k&&1>=m+k)return new HitTest(d,a,f)}return null};Mesh.parseOBJ=function(a,b){b=b||{};for(var c=[],d=[],f=[],k=[],h=[],l=[],m=[],p={},q=0,r=null,n=null,u=0,t=0,x=0,v=n=0,y=0,z=null,G=!1,H=!1,M=!1,N=!1,P=0,Q=b.noindex?b.noindex:1E7<a.length?!0:!1,K=b.flipAxis,I=K||b.flipNormals,A=null,C=[],E={v:1,vt:2,vn:3,f:4,g:5,o:6},D=a.split("\n"),R=D.length,J=0;J<R;++J)if(r=D[J].replace(/[ \t]+/g," ").replace(/\s\s*$/,""),"#"!=r[0]&&""!=r){var z=r.split(" "),B=E[z[0]];
N&&1==B&&(N=!1);3>=B&&(n=parseFloat(z[1]),v=parseFloat(z[2]),2!=B&&("\\"==z[3]?(++J,r=D[J].replace(/[ \t]+/g," ").replace(/\s\s*$/,""),y=parseFloat(r)):y=parseFloat(z[3])));if(1==B)K?h.push(-1*n,y,v):h.push(n,v,y);else if(2==B)l.push(n,v);else if(3==B)I?m.push(-v,-y,n):m.push(n,v,y);else if(4==B){if(N=!0,!(4>z.length)){B=[];for(r=1;r<z.length;++r){if(!(z[r]in p)||Q){n=z[r].split("/");if(1==n.length)x=t=u=parseInt(n[0])-1;else if(2==n.length)u=parseInt(n[0])-1,t=parseInt(n[1])-1,x=-1;else if(3==n.length)u=
parseInt(n[0])-1,t=parseInt(n[1])-1,x=parseInt(n[2])-1;else return console.err("Problem parsing: unknown number of values per face"),!1;3<r&&Q&&(n=c.length,c.push(c[n-9*(r-3)],c[n-9*(r-3)+1],c[n-9*(r-3)+2]),c.push(c[n-3],c[n-2],c[n-1]),n=d.length,d.push(d[n-6*(r-3)],d[n-6*(r-3)+1]),d.push(d[n-2],d[n-1]),n=f.length,f.push(f[n-9*(r-3)],f[n-9*(r-3)+1],f[n-9*(r-3)+2]),f.push(f[n-3],f[n-2],f[n-1]));y=v=n=0;3*u+2<h.length&&(G=!0,n=h[3*u+0],v=h[3*u+1],y=h[3*u+2]);c.push(n,v,y);v=n=0;2*t+1<l.length&&(H=!0,
n=l[2*t+0],v=l[2*t+1]);d.push(n,v);v=n=0;y=1;-1!=x&&(3*x+2<m.length&&(M=!0,n=m[3*x+0],v=m[3*x+1],y=m[3*x+2]),f.push(n,v,y));Q||(p[z[r]]=q++)}Q||(u=p[z[r]],B.push(u),P<u&&(P=u))}if(!Q)for(r=2;r<B.length;r++)k.push(B[0],B[r-1],B[r])}}else 5==B?1<z.length&&(null!=A&&(A.length=k.length-A.start,0<A.length&&C.push(A)),A={name:z[1],start:k.length,length:-1,material:""}):"usemtl"==z[0]&&A&&(A.material=z[1])}if(!h.length)return console.error("OBJ doesnt have vertices, maybe the file is not a OBJ"),null;A&&
1<k.length-A.start&&(A.length=k.length-A.start,C.push(A));if((65536<P||Q)&&0<k.length){console.log("Deindexing mesh...");h=new Float32Array(3*k.length);l=f&&f.length?new Float32Array(3*k.length):null;m=d&&d.length?new Float32Array(2*k.length):null;for(r=0;r<k.length;r+=1)h.set(c.slice(3*k[r],3*k[r]+3),3*r),l&&l.set(f.slice(3*k[r],3*k[r]+3),3*r),m&&m.set(d.slice(2*k[r],2*k[r]+2),2*r);c=h;l&&(f=l);m&&(d=m);k=null}h={};G&&(h.vertices=new Float32Array(c));M&&0<f.length&&(h.normals=new Float32Array(f));
H&&0<d.length&&(h.coords=new Float32Array(d));k&&0<k.length&&(h.triangles=new Uint16Array(k));c={};1<C.length&&(c.groups=C);h.info=c;if(b.only_data)return h;C=null;C=Mesh.load(h,null,b.mesh);C.updateBoundingBox();return C};Mesh.parsers.obj=Mesh.parseOBJ;Mesh.encoders.obj=function(a,b){var c=a.getBuffer("vertices");if(!c)return null;var d=[];d.push("# Generated with liteGL.js by Javi Agenjo\n");for(var f=c.data,k=0;k<f.length;k+=3)d.push("v "+f[k].toFixed(4)+" "+f[k+1].toFixed(4)+" "+f[k+2].toFixed(4));
if(c=a.getBuffer("normals"))for(d.push(""),c=c.data,k=0;k<c.length;k+=3)d.push("vn "+c[k].toFixed(4)+" "+c[k+1].toFixed(4)+" "+c[k+2].toFixed(4));if(c=a.getBuffer("coords"))for(d.push(""),c=c.data,k=0;k<c.length;k+=2)d.push("vt "+c[k].toFixed(4)+" "+c[k+1].toFixed(4)+"  0.0000");c=a.info.groups;if(k=a.getIndexBuffer("triangles")){f=k.data;c&&c.length||(c=[{start:0,length:f.length,name:"mesh"}]);for(var h=0;h<c.length;++h){var l=c[h];d.push("g "+l.name);d.push("usemtl "+(l.material||"mat_"+h));k=l.start;
for(l=k+l.length;k<l;k+=3)d.push("f "+(f[k]+1)+"/"+(f[k]+1)+"/"+(f[k]+1)+" "+(f[k+1]+1)+"/"+(f[k+1]+1)+"/"+(f[k+1]+1)+" "+(f[k+2]+1)+"/"+(f[k+2]+1)+"/"+(f[k+2]+1))}}else for(c&&c.length||(c=[{start:0,length:f.length/3,name:"mesh"}]),h=0;h<c.length;++h)for(l=c[h],d.push("g "+l.name),d.push("usemtl "+(l.material||"mat_"+h)),k=l.start,l=k+l.length;k<l;k+=3)d.push("f "+(k+1)+"/"+(k+1)+"/"+(k+1)+" "+(k+2)+"/"+(k+2)+"/"+(k+2)+" "+(k+3)+"/"+(k+3)+"/"+(k+3));return d.join("\n")};Mesh.parsers.mesh=function(a,
b){for(var c={},d=a.split("\n"),f=0;f<d.length;++f){var h=d[f],n=h[0],h=h.substr(1).split(","),l=h[0];if("-"==n){for(var m=new Float32Array(Number(h[1])),n=0;n<m.length;++n)m[n]=Number(h[n+2]);c[l]=m}else if("*"==n){m=65536<Number(h[1])?new Uint32Array(Number(h[1])):new Uint16Array(Number(h[1]));for(n=0;n<m.length;++n)m[n]=Number(h[n+2]);c[l]=m}else if("@"==n)if("bones"==l){l=[];m=Number(h[1]);for(n=0;n<m;++n){var p=h.slice(3+17*n,3+17*(n+1)-1).map(Number);l.push([h[2+17*n],p])}c.bones=l}else if("bind_matrix"==
l)c.bind_matrix=h.slice(1,17).map(Number);else{if("groups"==l)for(c.info={groups:[]},l=Number(h[1]),n=0;n<l;++n)c.info.groups.push({name:h[2+4*n],material:h[4*n+3],start:Number(h[4*n+4]),length:Number(h[4*n+5])})}else console.warn("type unknown: "+h[0])}if(b.only_data)return c;d=null;d=Mesh.load(c,null,b.mesh);d.updateBoundingBox();return d};Mesh.encoders.mesh=function(a,b){var c=[],d;for(d in a.vertexBuffers){var f=a.vertexBuffers[d],f=["-"+d,f.data.length,f.data,typedArrayToArray(f.data)];c.push(f.join(","))}for(d in a.indexBuffers)f=
a.indexBuffers[d],f=["*"+d,f.data.length,f.data,typedArrayToArray(f.data)],c.push(f.join(","));a.bounding&&c.push(["@bounding",typedArrayToArray(a.bounding.subarray(0,6))].join());if(a.info&&a.info.groups){d=[];for(f=0;f<a.info.groups.length;++f){var h=a.info.groups[f];d.push(h.name,h.material,h.start,h.length)}c.push(["@groups",a.info.groups.length].concat(d).join(","))}a.bones&&c.push(["@bones",a.bones.length,a.bones.flat()].join());a.bind_matrix&&c.push(["@bind_matrix",typedArrayToArray(a.bind_matrix)].join());
return c.join("\n")};n.WBin&&(n.WBin.classes.Mesh=Mesh);Mesh.binary_file_formats.wbin=!0;Mesh.parsers.wbin=Mesh.fromBinary=function(a,b){if(!n.WBin)throw"To use binary meshes you need to install WBin.js from https://github.com/jagenjo/litescene.js/blob/master/src/utils/wbin.js ";b=b||{};var c=null,c=a.constructor==ArrayBuffer?WBin.load(a,!0):a;c.info||console.warn("This WBin doesn't seem to contain a mesh. Classname: ",c["@classname"]);c.format&&h.Mesh.decompress(c);var d={};if(c.vertex_buffers)for(var f in c.vertex_buffers)d[c.vertex_buffers[f]]=
c[c.vertex_buffers[f]];else c.vertices&&(d.vertices=c.vertices),c.normals&&(d.normals=c.normals),c.coords&&(d.coords=c.coords),c.weights&&(d.weights=c.weights),c.bone_indices&&(d.bone_indices=c.bone_indices);var k={};if(c.index_buffers)for(f in c.index_buffers)k[c.index_buffers[f]]=c[c.index_buffers[f]];else c.triangles&&(k.triangles=c.triangles),c.wireframe&&(k.wireframe=c.wireframe);d={vertex_buffers:d,index_buffers:k,bounding:c.bounding,info:c.info};if(c.bones){d.bones=c.bones;for(f=0;f<d.bones.length;++f)d.bones[f][1]=
mat4.clone(d.bones[f][1]);c.bind_matrix&&(d.bind_matrix=mat4.clone(c.bind_matrix))}c.morph_targets&&(d.morph_targets=c.morph_targets);if(b.only_data)return d;c=b.mesh||new h.Mesh;c.configure(d);return c};Mesh.encoders.wbin=function(a,b){return a.toBinary(b)};Mesh.prototype.toBinary=function(a){if(!n.WBin)throw"to use Mesh.toBinary you need to have WBin included. Check the repository for wbin.js";this.info||(this.info={});a={object_class:"Mesh",info:this.info,groups:this.groups};if(this.bones){for(var b=
[],c=0;c<this.bones.length;++c)b.push([this.bones[c][0],mat4.toArray(this.bones[c][1])]);a.bones=b;this.bind_matrix&&(a.bind_matrix=this.bind_matrix)}this.bounding||this.updateBoundingBox();a.bounding=this.bounding;var b=[],d=[];for(c in this.vertexBuffers){var f=this.vertexBuffers[c];a[f.name]=f.data;b.push(f.name);"vertices"==f.name&&(a.info.num_vertices=f.data.length/3)}for(c in this.indexBuffers)f=this.indexBuffers[c],a[c]=f.data,d.push(c);a.vertex_buffers=b;a.index_buffers=d;h.Mesh.enable_wbin_compression&&
h.Mesh.compress(a);return WBin.create(a,"Mesh")};Mesh.compress=function(a,b){b=b||"bounding_compressed";a.format={type:b};var c=Mesh.compressors[b];if(!c)throw"compression format not supported:"+b;return c(a)};Mesh.decompress=function(a){if(a.format){var b=Mesh.decompressors[a.format.type];if(!b)throw"decompression format not supported:"+a.format.type;return b(a)}};Mesh.compressors.bounding_compressed=function(a){if(!a.vertex_buffers)throw"buffers not found";for(var b=BBox.getMin(a.bounding),c=BBox.getMax(a.bounding),
d=vec3.sub(vec3.create(),c,b),f=a.vertices,h=new Uint16Array(f.length),c=0;c<f.length;c+=3)h[c]=(f[c]-b[0])/d[0]*65535,h[c+1]=(f[c+1]-b[1])/d[1]*65535,h[c+2]=(f[c+2]-b[2])/d[2]*65535;a.vertices=h;if(a.normals){d=a.normals;b=new Uint8Array(d.length);f=b.constructor==Uint8Array?255:65535;for(c=0;c<d.length;c+=3)b[c]=(0.5*d[c]+0.5)*f,b[c+1]=(0.5*d[c+1]+0.5)*f,b[c+2]=(0.5*d[c+2]+0.5)*f;a.normals=b}if(a.coords){b=a.coords;f=[1E4,1E4,-1E4,-1E4];for(c=0;c<b.length;c+=2)d=b[c],f[0]>d?f[0]=d:f[2]<d&&(f[2]=
d),d=b[c+1],f[1]>d?f[1]=d:f[3]<d&&(f[3]=d);a.format.uvs_bounding=f;h=new Uint16Array(b.length);d=[f[2]-f[0],f[3]-f[1]];for(c=0;c<b.length;c+=2)h[c]=(b[c]-f[0])/d[0]*65535,h[c+1]=(b[c+1]-f[1])/d[1]*65535;a.coords=h}if(a.weights){d=a.weights;b=new Uint16Array(d.length);f=b.constructor==Uint8Array?255:65535;for(c=0;c<d.length;c+=4)b[c]=d[c]*f,b[c+1]=d[c+1]*f,b[c+2]=d[c+2]*f,b[c+3]=d[c+3]*f;a.weights=b}};Mesh.decompressors.bounding_compressed=function(a){var b=a.bounding;if(!b)throw"error in mesh decompressing data: bounding not found, cannot use the bounding decompression.";
for(var c=BBox.getMin(b),b=BBox.getMax(b),d=vec3.sub(vec3.create(),b,c),f=a.format,h=1/255,n=1/65535,l=a.vertices,m=new Float32Array(l.length),b=0,p=l.length;b<p;b+=3)m[b]=l[b]*n*d[0]+c[0],m[b+1]=l[b+1]*n*d[1]+c[1],m[b+2]=l[b+2]*n*d[2]+c[2];a.vertices=m;if(a.normals&&a.normals.constructor!=Float32Array){d=a.normals;c=new Float32Array(d.length);l=d.constructor==Uint8Array?h:n;b=0;for(p=d.length;b<p;b+=3)c[b]=d[b]*l*2-1,c[b+1]=d[b+1]*l*2-1,c[b+2]=d[b+2]*l*2-1,m=c.subarray(b,b+3),vec3.normalize(m,m);
a.normals=c}if(a.coords&&f.uvs_bounding&&a.coords.constructor!=Float32Array){c=a.coords;f=f.uvs_bounding;d=[f[2]-f[0],f[3]-f[1]];l=new Float32Array(c.length);b=0;for(p=c.length;b<p;b+=2)l[b]=c[b]*n*d[0]+f[0],l[b+1]=c[b+1]*n*d[1]+f[1];a.coords=l}if(a.weights&&a.weights.constructor!=Float32Array){f=a.weights;d=new Float32Array(f.length);h=f.constructor==Uint8Array?h:n;b=0;for(p=f.length;b<p;b+=4)d[b]=f[b]*h,d[b+1]=f[b+1]*h,d[b+2]=f[b+2]*h,d[b+3]=f[b+3]*h;a.weights=d}}})("undefined"!=typeof window?window:
"undefined"!=typeof self?self:global);
