-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.min.html
41 lines (32 loc) · 35.8 KB
/
index.min.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="author" content="Antonino Perricone">
<title>Steroid test</title>
<meta property="og:title" content="Spherical light + Ammo test" />
<meta property="og:image" content="thumb.png" />
<meta property="og:description" content="Spherical light By Antonino Perricone" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
<style>
canvas { border: 1px solid black; }
body { overflow:hidden; }
input { position:absolute; }
span#me
{
position:absolute;
bottom: 8px;
right: 8px;
/*background-color: rgba(192,192,64,0.5);*/
}
</style>
<script type="text/javascript">
function Engine(){function t(){var t=innerWidth-16,r=innerHeight-16;n.c.width=t,n.c.height=r,e.bindTexture(e.TEXTURE_2D,s),e.texImage2D(e.TEXTURE_2D,0,e.RGBA,t,r,0,e.RGBA,e.UNSIGNED_BYTE,null);for(var i=0;2>i;i++)e.activeTexture(e.TEXTURE1),e.bindTexture(e.TEXTURE_2D,a[i]),e.texImage2D(e.TEXTURE_2D,0,e.DEPTH_COMPONENT,t,r,0,e.DEPTH_COMPONENT,e.UNSIGNED_SHORT,null),e.activeTexture(e.TEXTURE0),e.bindFramebuffer(e.FRAMEBUFFER,n.frameBuffer[i]),e.framebufferTexture2D(e.FRAMEBUFFER,e.COLOR_ATTACHMENT0,e.TEXTURE_2D,s,0),e.framebufferTexture2D(e.FRAMEBUFFER,e.DEPTH_ATTACHMENT,e.TEXTURE_2D,a[i],0);e.bindFramebuffer(e.FRAMEBUFFER,null),n.paint()}this.c=document.createElement("canvas"),document.body.appendChild(this.c);var e=this.c.getContext("webgl");if(null==e&&(e=this.c.getContext("experimental-webgl")),null==e)throw document.body.innerHTML="Unable to create WebGL context";var r=e.getExtension("WEBGL_depth_texture");if(null==r)throw document.body.innerHTML="no depth texture";var r=e.getExtension("EXT_frag_depth");if(null==r)throw document.body.innerHTML="no frag depth";this.__defineGetter__("gl",function(){return e}),e.enable(e.DEPTH_TEST),e.depthFunc(e.LEQUAL),e.enable(e.CULL_FACE),e.cullFace(e.BACK),this.camera=void 0,this.light=void 0,this.meshes=[];var i=e.createBuffer();e.bindBuffer(e.ARRAY_BUFFER,i),e.bufferData(e.ARRAY_BUFFER,new Float32Array([-1,-1,1,-1,-1,1,1,1]),e.STATIC_DRAW),this.postVBuffer=i,e.activeTexture(e.TEXTURE1);for(var a=[e.createTexture(),e.createTexture()],o=0;2>o;o++)e.bindTexture(e.TEXTURE_2D,a[o]),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_MIN_FILTER,e.NEAREST),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_MAG_FILTER,e.NEAREST),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_WRAP_S,e.CLAMP_TO_EDGE),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_WRAP_T,e.CLAMP_TO_EDGE);this.depthTexture=a,e.activeTexture(e.TEXTURE0);var s=e.createTexture();e.bindTexture(e.TEXTURE_2D,s),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_MIN_FILTER,e.NEAREST),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_MAG_FILTER,e.NEAREST),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_WRAP_S,e.CLAMP_TO_EDGE),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_WRAP_T,e.CLAMP_TO_EDGE),this.colorBuffer=s,this.frameBuffer=[e.createFramebuffer(),e.createFramebuffer()];var n=this;this.postProgram=null,LoadProgram(e,"post.min.vs","post.min.fs",function(t){n.postProgram=t,e.useProgram(t),e.uniform1i(t.uniforms.colorBuffer,0),e.uniform1i(t.uniforms.depthBuffer,1),e.useProgram(null),n.paint()}),this.copyZ=null,LoadProgram(e,"post.min.vs","copyZ.min.fs",function(t){n.copyZ=t,e.useProgram(t),e.uniform1i(t.uniforms.depthBuffer,1),e.useProgram(null),n.paint()}),this.particles=new ParticleSystem(e),window.addEventListener("resize",t),t()}function Camera(){this._ortho=!0,this._pos=[0,0,0],this._look=[0,0,-1],this._up=[0,1,0],this._nearPlane=-1,this._farPlane=1,this._heightOrFov=2,this._aspectRatio=1,this._needCalcProj=this._needCalcView=!1;var t=[1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1];this._projection=new Float32Array(t),this._view=new Float32Array(t),this._viewProj=new Float32Array(t),this._invViewProj=new Float32Array(t)}function Transform(t,e){for(var r=[0,0,0,0],i=0;4>i;i++)for(var a=0;4>a;a++)r[a]+=t[4*i+a]*e[i];return r}function floatDiff(t,e){var r=e-t;return r>=.001||-.001>=r}function Mesh(t){void 0!=t&&(this.use16bit=!0,this.nIndices=0,this.nVertices=0,this.nBytePerVertex=0,this.normalDelta=0,this.texCoordsDelta=0,this.vBuffer=null,this.iBuffer=null,this.materialColor=[1,1,1,1],Mesh.LoadProgram(t),"gl"in t&&(t=t.gl),this.__defineGetter__("gl",function(){return t}),this.modelMat=new Float32Array([1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1]))}function MeshCreator(){this.Begin(),this.creating=!1}function cmpDecimalArray(t,e){for(var r=0;r<t.length;r++)if(Math.abs(t[r]-e[r])>.001)return!1;return!0}function RotatingCamera(){Camera.call(this),this._btnDown=[!1,!1,!1,!1,!1,!1],this._alpha=0,this._beta=0,this._dist=1,this._needCalcRotate=!1}function Light(t,e,r){"gl"in t&&(t=t.gl),e=e||[0,0,0],r=r||1,Mesh.call(this,t),this.pos=e,this.needRecalcMatrices=!0,this.__defineGetter__("radius",function(){return r}),this.__defineSetter__("radius",function(t){r=t,this.needRecalcMatrices=!0});var i=22;this.__defineGetter__("farPlane",function(){return i}),this.__defineSetter__("farPlane",function(t){i=t,this.needRecalcMatrices=!0}),this.__defineGetter__("posRadius",function(){return[this.modelMat[12],this.modelMat[13],this.modelMat[14],r]}),this.seeColor=[1,1,1],this.__defineGetter__("colorFar",function(){return[this.seeColor[0],this.seeColor[1],this.seeColor[2],i]});var a=new MeshCreator;a.Begin(),a.AddSphere([0,0,0],r),a.End(t,this),this.materialColor=[1,1,1,1],this.cubeTexture=t.createTexture(),t.bindTexture(t.TEXTURE_CUBE_MAP,this.cubeTexture),t.texParameteri(t.TEXTURE_CUBE_MAP,t.TEXTURE_MIN_FILTER,t.NEAREST),t.texParameteri(t.TEXTURE_CUBE_MAP,t.TEXTURE_MAG_FILTER,t.NEAREST),t.texParameteri(t.TEXTURE_CUBE_MAP,t.TEXTURE_WRAP_S,t.CLAMP_TO_EDGE),t.texParameteri(t.TEXTURE_CUBE_MAP,t.TEXTURE_WRAP_T,t.CLAMP_TO_EDGE);for(var o=0;6>o;o++)t.texImage2D(t.TEXTURE_CUBE_MAP_POSITIVE_X+o,0,t.RGBA,512,512,0,t.RGBA,t.UNSIGNED_BYTE,null);var s=t.createRenderbuffer();t.bindRenderbuffer(t.RENDERBUFFER,s),t.renderbufferStorage(t.RENDERBUFFER,t.DEPTH_COMPONENT16,512,512),t.bindTexture(t.TEXTURE_CUBE_MAP,null),t.bindTexture(t.TEXTURE_2D,null),this.frameBuffers=[],this.frameBuffers.length=6;for(var o=0;6>o;o++){this.frameBuffers[o]=t.createFramebuffer(),t.bindFramebuffer(t.FRAMEBUFFER,this.frameBuffers[o]),t.framebufferRenderbuffer(t.FRAMEBUFFER,t.DEPTH_ATTACHMENT,t.RENDERBUFFER,s),t.framebufferTexture2D(t.FRAMEBUFFER,t.COLOR_ATTACHMENT0,t.TEXTURE_CUBE_MAP_POSITIVE_X+o,this.cubeTexture,0);var n=t.checkFramebufferStatus(t.FRAMEBUFFER);n!=t.FRAMEBUFFER_COMPLETE&&console.log("FAIL("+n+")")}t.bindFramebuffer(t.FRAMEBUFFER,null),this.matrices=[],this.matrices.length=6;for(var o=0;6>o;o++)this.matrices[o]=new Float32Array(16);this.currentRenderTarget=0,this.__defineGetter__("currentPVMatrix",function(){return this.matrices[this.currentRenderTarget]})}function Audio(){if(this.context=new AudioContext,void 0!=this.context){this.gainNode=this.context.createGain(),this.gainNode.connect(this.context.destination),this.gainNode.gain.value=2;var t=.1,e=this.context.sampleRate,r=t*e,i=[];i.length=r;for(var a=0;r>a;a++)i[a]=2*Math.random()-1;this["final"]=LowPass(100,e,i,r),this.bufferSize=r}}function LowPass(t,e,r,a){var o=1/(2*t*Math.PI),s=1/e,n=s/(o+s),h=[];for(h.length=a,h[0]=r[0],i=1;i<a;i++)h[i]=h[i-1]+n*(r[i]-h[i-1]);return h[0]=h[a-1]+n*(r[0]-h[a-1]),h}function HighPass(t,e,r,a){var o=1/(2*t*Math.PI),s=1/e,n=s/(o+s),h=[];for(h.length=a,h[0]=r[0],i=1;i<a;i++)h[i]=n*(h[i-1]+r[i]-r[i-1]);return h[0]=n*(h[a-1]+r[0]-r[a-1]),h}function ParticleSystem(t){this.particles=[],this.lastTime=Date.now();var e=t;"gl"in t&&(e=t.gl),this.__defineGetter__("gl",function(){return e}),LoadProgram(t,"party.min.vs","party.min.fs",function(t){ParticleSystem.program=t,e.useProgram(t),e.uniform1i(t.uniforms.depthBuffer,0)}),this.vBuffer=t.createBuffer(),t.bindBuffer(t.ARRAY_BUFFER,this.vBuffer),ParticleSystem.N_MAX_DRAW=2e3,this.array=new Float32Array(4*ParticleSystem.N_MAX_DRAW),t.bufferData(t.ARRAY_BUFFER,this.array,t.DYNAMIC_DRAW),this.center_posvel=[0,0,0,0,0,0],this.mean=[0,0,0]}const checks=!0;Engine.prototype.paint=function(){var t=this.gl,e=this.c.width,r=this.c.height;if(this.camera&&(this.camera.aspectRatio(e,r),this.camera.UpdateMatrices(),Mesh.SetCamera(t,this.camera,e,r),ParticleSystem.SetCamera(t,this.camera,e,r)),this.light){Mesh.SetLight(t,this.light,this.particles),t.depthFunc(t.LEQUAL),t.polygonOffset(4,4),t.disable(t.CULL_FACE);for(var i=0;6>i;i++){this.light.Begin(t,i),Mesh.SetCubeUniforms(t,this.light,this.particles);for(var a=0;a<this.meshes.length;a++){var o=this.meshes[a];o.constructor==Mesh&&o.Draw(Mesh.PROGRAMID_CUBE)}this.light.End(t)}t.disable(t.POLYGON_OFFSET_FILL),t.enable(t.CULL_FACE)}t.bindFramebuffer(t.FRAMEBUFFER,this.frameBuffer[0]),t.viewport(0,0,this.c.width,this.c.height),t.clearColor(.7,.7,1,1),t.clear(t.COLOR_BUFFER_BIT|t.DEPTH_BUFFER_BIT),t.colorMask(!1,!1,!1,!1),t.depthFunc(t.LEQUAL);for(var a=0;a<this.meshes.length;a++){var o=this.meshes[a];o.Draw(Mesh.PROGRAMID_UNLIT)}if(null!=this.copyZ){t.bindFramebuffer(t.FRAMEBUFFER,this.frameBuffer[1]),t.colorMask(!0,!1,!1,!1),t.useProgram(this.copyZ);var s=this.copyZ.attributes;t.bindBuffer(t.ARRAY_BUFFER,this.postVBuffer),t.enableVertexAttribArray(s.aVertexPosition),t.vertexAttribPointer(s.aVertexPosition,2,t.FLOAT,!1,0,0),t.disable(t.CULL_FACE),t.activeTexture(t.TEXTURE1),t.bindTexture(t.TEXTURE_2D,this.depthTexture[0]),t.depthFunc(t.ALWAYS),t.drawArrays(t.TRIANGLE_STRIP,0,4),t.enable(t.CULL_FACE),t.bindFramebuffer(t.FRAMEBUFFER,this.frameBuffer[0]),t.bindTexture(t.TEXTURE_2D,this.depthTexture[1]),t.activeTexture(t.TEXTURE0)}t.colorMask(!0,!0,!0,!0),t.depthFunc(t.EQUAL);for(var a=0;a<this.meshes.length;a++){var o=this.meshes[a];o.Draw(Mesh.PROGRAMID_MESH)}if(t.depthFunc(t.LEQUAL),t.depthMask(!1),this.particles.draw(),t.depthMask(!0),null!=this.postProgram){t.bindFramebuffer(t.FRAMEBUFFER,null);var s=this.postProgram.attributes,n=this.postProgram.uniforms;t.bindBuffer(t.ARRAY_BUFFER,this.postVBuffer),t.useProgram(this.postProgram),t.enableVertexAttribArray(s.aVertexPosition),t.vertexAttribPointer(s.aVertexPosition,2,t.FLOAT,!1,0,0),t.uniform2f(n.zNearFar,this.camera.nearPlane(),this.camera.farPlane()),t.disable(t.DEPTH_TEST),t.disable(t.CULL_FACE),t.activeTexture(t.TEXTURE1),t.bindTexture(t.TEXTURE_2D,this.depthTexture[0]),t.activeTexture(t.TEXTURE0),t.bindTexture(t.TEXTURE_2D,this.colorBuffer),t.drawArrays(t.TRIANGLE_STRIP,0,4),t.enable(t.CULL_FACE),t.enable(t.DEPTH_TEST)}},LoadProgram=function(t,e,r,i){function a(){void 0!=o&&void 0!=s&&i(CreateProgram(t,o,s))}var o=void 0,s=void 0,n=new XMLHttpRequest;n.open("GET",e,!0),n.onload=function(t){200==n.status?(o=n.response,a()):alert("unable to download "+e)},n.send();var h=new XMLHttpRequest;h.open("GET",r,!0),h.onload=function(t){200==h.status?(s=h.response,a()):alert("unable to download "+r)},h.send()},CreateProgram=function(t,e,r){"gl"in t&&(t=t.gl);var i=t.createShader(t.VERTEX_SHADER);t.shaderSource(i,e),t.compileShader(i),console.log("compile vertex shader..."),console.log(t.getShaderInfoLog(i));var a=t.createShader(t.FRAGMENT_SHADER);t.shaderSource(a,r),t.compileShader(a),console.log("compile fragment shader..."),console.log(t.getShaderInfoLog(a));var o=t.createProgram();t.attachShader(o,i),t.attachShader(o,a),t.linkProgram(o),console.log("linking..."),console.log(t.getProgramInfoLog(o)),o.attributes={};for(var s=t.getProgramParameter(o,t.ACTIVE_ATTRIBUTES),n=0;s>n;n++){var h=t.getActiveAttrib(o,n);o.attributes[h.name]=t.getAttribLocation(o,h.name)}o.uniforms={};for(var l=t.getProgramParameter(o,t.ACTIVE_UNIFORMS),n=0;l>n;n++){var f=t.getActiveUniform(o,n);o.uniforms[f.name]=t.getUniformLocation(o,f.name)}return o},Camera.prototype.UpdateMatrices=function(){function t(t,e){return[t[1]*e[2]-t[2]*e[1],t[2]*e[0]-t[0]*e[2],t[0]*e[1]-t[1]*e[0]]}function e(t,e){return t[0]*e[0]+t[1]*e[1]+t[2]*e[2]}function r(t){var r=Math.sqrt(e(t,t));return[t[0]/r,t[1]/r,t[2]/r]}if(this._needCalcView){var i=r([this._pos[0]-this._look[0],this._pos[1]-this._look[1],this._pos[2]-this._look[2]]);this._at=i;var a=r(t(this._up,i)),o=r(t(i,a));this._upC=o,this._rg=a,this._view[0]=a[0],this._view[1]=o[0],this._view[2]=i[0],this._view[3]=0,this._view[4]=a[1],this._view[5]=o[1],this._view[6]=i[1],this._view[7]=0,this._view[8]=a[2],this._view[9]=o[2],this._view[10]=i[2],this._view[11]=0,this._view[12]=-e(a,this._pos),this._view[13]=-e(o,this._pos),this._view[14]=-e(i,this._pos),this._view[15]=1}if(this._needCalcProj){var s,n,h=2/this._heightOrFov,l=h/this._aspectRatio,f=this._farPlane-this._nearPlane;this._ortho?(s=2/f,n=(this._farPlane+this._nearPlane)/f,this._projection[11]=0,this._projection[15]=1):(s=(this._farPlane+this._nearPlane)/f,n=2*this._farPlane*this._nearPlane/f,this._projection[11]=-1,this._projection[15]=0),this._projection[0]=l,this._projection[5]=h,this._projection[10]=-s,this._projection[14]=-n}if(this._needCalcProj||this._needCalcView){for(var u=0,c=0;4>c;c++)for(var d=0;4>d;d++){this._viewProj[u]=0;for(var m=0;4>m;m++)this._viewProj[u]+=this._view[m+4*c]*this._projection[4*m+d];u++}var _=this._viewProj;this._invViewProj[0]=_[5]*_[10]*_[15]-_[5]*_[11]*_[14]-_[9]*_[6]*_[15]+_[9]*_[7]*_[14]+_[13]*_[6]*_[11]-_[13]*_[7]*_[10],this._invViewProj[1]=-_[1]*_[10]*_[15]+_[1]*_[11]*_[14]+_[9]*_[2]*_[15]-_[9]*_[3]*_[14]-_[13]*_[2]*_[11]+_[13]*_[3]*_[10],this._invViewProj[2]=_[1]*_[6]*_[15]-_[1]*_[7]*_[14]-_[5]*_[2]*_[15]+_[5]*_[3]*_[14]+_[13]*_[2]*_[7]-_[13]*_[3]*_[6],this._invViewProj[3]=-_[1]*_[6]*_[11]+_[1]*_[7]*_[10]+_[5]*_[2]*_[11]-_[5]*_[3]*_[10]-_[9]*_[2]*_[7]+_[9]*_[3]*_[6],this._invViewProj[4]=-_[4]*_[10]*_[15]+_[4]*_[11]*_[14]+_[8]*_[6]*_[15]-_[8]*_[7]*_[14]-_[12]*_[6]*_[11]+_[12]*_[7]*_[10],this._invViewProj[5]=_[0]*_[10]*_[15]-_[0]*_[11]*_[14]-_[8]*_[2]*_[15]+_[8]*_[3]*_[14]+_[12]*_[2]*_[11]-_[12]*_[3]*_[10],this._invViewProj[6]=-_[0]*_[6]*_[15]+_[0]*_[7]*_[14]+_[4]*_[2]*_[15]-_[4]*_[3]*_[14]-_[12]*_[2]*_[7]+_[12]*_[3]*_[6],this._invViewProj[7]=_[0]*_[6]*_[11]-_[0]*_[7]*_[10]-_[4]*_[2]*_[11]+_[4]*_[3]*_[10]+_[8]*_[2]*_[7]-_[8]*_[3]*_[6],this._invViewProj[8]=_[4]*_[9]*_[15]-_[4]*_[11]*_[13]-_[8]*_[5]*_[15]+_[8]*_[7]*_[13]+_[12]*_[5]*_[11]-_[12]*_[7]*_[9],this._invViewProj[9]=-_[0]*_[9]*_[15]+_[0]*_[11]*_[13]+_[8]*_[1]*_[15]-_[8]*_[3]*_[13]-_[12]*_[1]*_[11]+_[12]*_[3]*_[9],this._invViewProj[10]=_[0]*_[5]*_[15]-_[0]*_[7]*_[13]-_[4]*_[1]*_[15]+_[4]*_[3]*_[13]+_[12]*_[1]*_[7]-_[12]*_[3]*_[5],this._invViewProj[11]=-_[0]*_[5]*_[11]+_[0]*_[7]*_[9]+_[4]*_[1]*_[11]-_[4]*_[3]*_[9]-_[8]*_[1]*_[7]+_[8]*_[3]*_[5],this._invViewProj[12]=-_[4]*_[9]*_[14]+_[4]*_[10]*_[13]+_[8]*_[5]*_[14]-_[8]*_[6]*_[13]-_[12]*_[5]*_[10]+_[12]*_[6]*_[9],this._invViewProj[13]=_[0]*_[9]*_[14]-_[0]*_[10]*_[13]-_[8]*_[1]*_[14]+_[8]*_[2]*_[13]+_[12]*_[1]*_[10]-_[12]*_[2]*_[9],this._invViewProj[14]=-_[0]*_[5]*_[14]+_[0]*_[6]*_[13]+_[4]*_[1]*_[14]-_[4]*_[2]*_[13]-_[12]*_[1]*_[6]+_[12]*_[2]*_[5],this._invViewProj[15]=_[0]*_[5]*_[10]-_[0]*_[6]*_[9]-_[4]*_[1]*_[10]+_[4]*_[2]*_[9]+_[8]*_[1]*_[6]-_[8]*_[2]*_[5];var p=_[0]*this._invViewProj[0]+_[1]*this._invViewProj[4]+_[2]*this._invViewProj[8]+_[3]*this._invViewProj[12];if(0!=p)for(var u=0;16>u;u++)this._invViewProj[u]/=p}this._needCalcProj=!1,this._needCalcView=!1},Camera.prototype.get3D=function(t,e,r){for(var i=Transform(this._invViewProj,[t,e,void 0==r?0:r,1]),a=0;3>a;a++)i[a]/=i[3];for(var o=[],a=0;3>a;a++)o[a]=t*this._rg[a]*this._aspectRatio/this._heightOrFov+e*this._upC[a]/this._heightOrFov+-20*this._at[a]+this._pos[a];return i},Camera.prototype.projection=function(){return this.UpdateMatrices(),this._projection},Camera.prototype.view=function(){return this.UpdateMatrices(),this._view},Camera.prototype.viewProj=function(){return this.UpdateMatrices(),this._viewProj},Camera.prototype.ortho=function(t){return void 0!=t&&(this._needCalcProj|=this._ortho!=t,this._ortho=t),this._ortho},Camera.prototype.persp=function(t){return void 0!=t&&this.ortho(!t),!this._ortho},Camera.prototype.height=function(t){return void 0!=t&&(this._needCalcProj|=floatDiff(this._heightOrFov,t),this._heightOrFov=t),this._heightOrFov},Camera.prototype.fovRad=function(t){void 0!=t&&this.height(Math.tan(t/2))},Camera.prototype.fovDeg=function(t){void 0!=t&&this.height(Math.tan(Math.PI*t/360))},Camera.prototype.aspectRatio=function(t,e){if(void 0!=t){var r=t;void 0!=e&&(r=t/e),this._needCalcProj|=floatDiff(this._aspectRatio,r),this._aspectRatio=r}return this._aspectRatio},Camera.prototype.pos=function(t,e,r){if(void 0!=t){var i=t;if(void 0!=e&&(i=[t,e,r]),3!=i.length)throw"invalid parameter";this._needCalcView|=floatDiff(this._pos[0],i[0])||floatDiff(this._pos[1],i[1])||floatDiff(this._pos[2],i[2]),this._pos=i}return this._pos},Camera.prototype.look=function(t,e,r){if(void 0!=t){var i=t;if(void 0!=e&&(i=[t,e,r]),3!=i.length)throw"invalid parameter";this._needCalcView|=floatDiff(this._look[0],i[0])||floatDiff(this._look[1],i[1])||floatDiff(this._look[2],i[2]),this._look=i}return this._look},Camera.prototype.up=function(t,e,r){if(void 0!=t){var i=t;if(void 0!=e&&(i=[t,e,r]),3!=i.length)throw"invalid parameter";this._needCalcView|=floatDiff(this._up[0],i[0])||floatDiff(this._up[1],i[1])||floatDiff(this._up[2],i[2]),this._up=i}return this._up},Camera.prototype.nearPlane=function(t){return void 0!=t&&(this._needCalcProj|=floatDiff(this._nearPlane,t),this._nearPlane=t),this._nearPlane},Camera.prototype.farPlane=function(t){return void 0!=t&&(this._needCalcProj|=floatDiff(this._nearPlane,t),this._farPlane=t),this._farPlane},Mesh.prototype.__defineGetter__("pos",function(){return[this.modelMat[12],this.modelMat[13],this.modelMat[14]]}),Mesh.prototype.__defineSetter__("pos",function(t){this.modelMat[12]=t[0],this.modelMat[13]=t[1],this.modelMat[14]=t[2]}),Mesh.prototype.__defineSetter__("quat",function(t){var e=t[3]*t[3],r=t[3]*t[0];r+=r;var i=t[3]*t[1];i+=i;var a=t[3]*t[2];a+=a;var o=t[0]*t[0],s=t[0]*t[1];s+=s;var n=t[0]*t[2];n+=n;var h=t[1]*t[1],l=t[1]*t[2];l+=l;var f=t[2]*t[2];this.modelMat[0]=e+o-h-f,this.modelMat[1]=s+a,this.modelMat[2]=n-i,this.modelMat[4]=s-a,this.modelMat[5]=e-o+h-f,this.modelMat[6]=l+r,this.modelMat[8]=n+i,this.modelMat[9]=l-r,this.modelMat[10]=e-o-h+f}),Mesh.PROGRAMID_MESH=0,Mesh.PROGRAMID_UNLIT=1,Mesh.PROGRAMID_CUBE=2,Mesh.programs=[],Mesh.loadRequested=!1,Mesh.LoadProgram=function(t){Mesh.loadRequested||(Mesh.loadRequested=!0,LoadProgram(t,"mesh.min.vs","mesh.min.fs",function(e){Mesh.programs[Mesh.PROGRAMID_MESH]=e;var r=t;"gl"in t&&(r=t.gl),r.useProgram(e),r.uniform1i(e.uniforms.shadowMap,0),r.uniform1i(e.uniforms.depthBuffer,1),"paint"in t&&t.paint()}),LoadProgram(t,"mesh.min.vs","unlit.min.fs",function(e){Mesh.programs[Mesh.PROGRAMID_UNLIT]=e,"paint"in t&&t.paint()}),LoadProgram(t,"cube.min.vs","zbuff.min.fs",function(e){Mesh.programs[Mesh.PROGRAMID_CUBE]=e,"paint"in t&&t.paint()}))},Mesh.SetCamera=function(t,e,r,i){"gl"in t&&(t=t.gl);for(var a=0;2>a;a++)if(void 0!=Mesh.programs[a]){var o=Mesh.programs[a].uniforms;t.useProgram(Mesh.programs[a]),t.uniformMatrix4fv(o.uPVMatrix,!1,e.viewProj()),t.uniform3fv(o.uCamPosition,e.pos()),t.uniform4f(o.uScreenSizeNearFar,r,i,e.nearPlane(),e.farPlane())}t.useProgram(null)},Mesh.SetCubeUniforms=function(t,e,r){if("gl"in t&&(t=t.gl),void 0!=Mesh.programs[Mesh.PROGRAMID_CUBE]){var i=Mesh.programs[Mesh.PROGRAMID_CUBE].uniforms;t.useProgram(Mesh.programs[Mesh.PROGRAMID_CUBE]),t.uniformMatrix4fv(i.uPVMatrix,!1,e.currentPVMatrix);var a=e.pos;if(void 0!=r)for(var o=0;3>o;o++)a[o]+=r.mean[o];t.uniform3fv(i.uCenter,a),t.useProgram(null)}},Mesh.SetLight=function(t,e,r){if(void 0!=Mesh.programs[0]){"gl"in t&&(t=t.gl);var i=Mesh.programs[0].uniforms;t.useProgram(Mesh.programs[0]),t.uniform4fv(i.uLightColorFar,e.colorFar);var a=e.posRadius;if(void 0!=r)for(var o=0;3>o;o++)a[o]+=r.mean[o];t.uniform4fv(i.uLightPosRadius,a),t.useProgram(null),t.bindTexture(t.TEXTURE_CUBE_MAP,e.cubeTexture)}},Mesh.prototype.PreDraw=function(t){if(t=t||Mesh.PROGRAMID_MESH,void 0!=Mesh.programs[t]){var e=this.gl,r=Mesh.programs[t].attributes;e.useProgram(Mesh.programs[t]),e.bindBuffer(e.ARRAY_BUFFER,this.vBuffer),e.bindBuffer(e.ELEMENT_ARRAY_BUFFER,this.iBuffer),e.enableVertexAttribArray(r.aVertexPosition),e.vertexAttribPointer(r.aVertexPosition,3,e.FLOAT,!1,this.nBytePerVertex,0),"aNormal"in r&&(0!=this.normalDelta?(e.enableVertexAttribArray(r.aNormal),e.vertexAttribPointer(r.aNormal,3,e.FLOAT,!1,this.nBytePerVertex,this.normalDelta)):(e.disableVertexAttribArray(r.aNormal),e.vertexAttrib3f(r.aNormal,0,0,0))),"aTexCoord"in r&&(0!=this.texCoordsDelta?(e.enableVertexAttribArray(r.aTexCoord),e.vertexAttribPointer(r.aTexCoord,2,e.FLOAT,!1,this.nBytePerVertex,this.texCoordsDelta)):(e.disableVertexAttribArray(r.aTexCoord),e.vertexAttrib2f(r.aTexCoord,0,0)))}},Mesh.prototype.ReDraw=function(t){if(t=t||Mesh.PROGRAMID_MESH,void 0!=Mesh.programs[t]){var e=this.gl,r=Mesh.programs[t].uniforms;e.useProgram(Mesh.programs[t]),e.uniformMatrix4fv(r.uModelMatrix,!1,this.modelMat),e.uniform4fv(r.uMaterialColor,this.materialColor),e.drawElements(e.TRIANGLES,this.nIndices,this.use16bit?e.UNSIGNED_SHORT:e.UNSIGNED_INT,0)}},Mesh.prototype.EndDraw=function(t){if(t=t||Mesh.PROGRAMID_MESH,void 0!=Mesh.programs[t]){var e=this.gl,r=Mesh.programs[t].attributes;e.disableVertexAttribArray(r.aVertexPosition),e.disableVertexAttribArray(r.aNormal),e.disableVertexAttribArray(r.aTexCoord),e.bindBuffer(e.ARRAY_BUFFER,null)}},Mesh.prototype.Draw=function(t){t=t||0,void 0!=Mesh.programs[t]&&(this.PreDraw(t),this.ReDraw(t),this.EndDraw(t))},MeshCreator.prototype.Begin=function(t,e){this.creating=!0,this.normals=t||!1,this.texCoords=e||!1,this.currentNormal=[0,0,0],this.currentTexture=[0,0],this.currentPoint=[0,0,0],this.points=[],this.mappedPoints=[],this.triangles=[]},MeshCreator.prototype.Normal=function(t,e,r){if(!this.creating)throw"Invalid call: outside Begin/End";if(this.normals&&void 0!=t){var i=t;if(void 0!=e&&(i=[t,e,r]),3!=i.length)throw"Invalid parameter";this.currentNormal=i}return this.currentNormal},MeshCreator.prototype.Texture=function(t,e){if(!this.creating)throw"Invalid call: outside Begin/End";if(this.texCoords&&void 0!=t){var r=t;if(void 0!=e&&(r=[t,e]),2!=r.length)throw"Invalid parameter";this.currentTexture=r}return this.currentTexture},MeshCreator.prototype.Point=function(t,e,r){if(!this.creating)throw"Invalid call: outside Begin/End";if(void 0!=t){var i=t;if(void 0!=e&&(i=[t,e,r]),3!=i.length)throw"Invalid parameter";this.currentPoint=i,this.AddPoint()}return this.currentPoint},MeshCreator.prototype.AddPoint=function(){for(var t=Math.round(1e3*(this.currentPoint[0]*this.currentPoint[0]+this.currentPoint[1]*this.currentPoint[1]+this.currentPoint[2]*this.currentPoint[2])),e=t-1;t+1>=e;e++)for(var r=this.mappedPoints[e]||[],i=0;i<r.length;i++){var a=this.points[r[i]];if(cmpDecimalArray(a.pos,this.currentPoint)&&cmpDecimalArray(a.nor,this.currentNormal)&&cmpDecimalArray(a.tex,this.currentTexture))return void this.triangles.push(r[i])}void 0==this.mappedPoints[t]&&(this.mappedPoints[t]=[]);var o=this.points.length;this.mappedPoints[t].push(o),this.points.push({pos:this.currentPoint,nor:this.currentNormal,tex:this.currentTexture}),this.triangles.push(o)},MeshCreator.prototype.AddBox=function(t,e,r){function i(t,e,i,o){a.Texture(0,0),a.Point(t),r?(a.Texture(0,1),a.Point(i),a.Texture(1,0),a.Point(e),a.Texture(1,0),a.Point(e),a.Texture(0,1),a.Point(i)):(a.Texture(1,0),a.Point(e),a.Texture(0,1),a.Point(i),a.Texture(0,1),a.Point(i),a.Texture(1,0),a.Point(e)),a.Texture(1,1),a.Point(o)}if(t=t||[-1,-1,-1],e=e||[1,1,1],r=r||!1,3!=t.length)throw"Invalid parameter";if(3!=e.length)throw"Invalid parameter";var a=this,o=r?-1:1;this.Normal([0,o,0]),i([e[0],e[1],t[2]],[t[0],e[1],t[2]],[e[0],e[1],e[2]],[t[0],e[1],e[2]]),this.Normal([0,-o,0]),i([t[0],t[1],t[2]],[e[0],t[1],t[2]],[t[0],t[1],e[2]],[e[0],t[1],e[2]]),this.Normal([o,0,0]),i([e[0],t[1],t[2]],[e[0],e[1],t[2]],[e[0],t[1],e[2]],[e[0],e[1],e[2]]),this.Normal([-o,0,0]),i([t[0],e[1],t[2]],[t[0],t[1],t[2]],[t[0],e[1],e[2]],[t[0],t[1],e[2]]),this.Normal([0,0,o]),i([t[0],e[1],e[2]],[t[0],t[1],e[2]],[e[0],e[1],e[2]],[e[0],t[1],e[2]]),this.Normal([0,0,-o]),i([t[0],t[1],t[2]],[t[0],e[1],t[2]],[e[0],t[1],t[2]],[e[0],e[1],t[2]])},MeshCreator.prototype.AddSphere=function(t,e,r,i){t=t||[0,0,0],e=e||1,r=r||8,i=i||16;for(var a=Math.PI/r,o=2*Math.PI/i,s=0,n=[1,1],h=[0,0],l=[1,1],f=[0,0],u=0;r>u;u++){n[0]=n[1],h[0]=h[1],s+=a,n[1]=Math.cos(s),h[1]=Math.sin(s);var c=0;l[1]=1,f[1]=0;for(var d=0;i>d;d++){l[0]=l[1],f[0]=f[1],c+=o,l[1]=Math.cos(c),f[1]=Math.sin(c);for(var m=[[0,0,0],[0,0,0],[0,0,0],[0,0,0]],_=[[0,0,0],[0,0,0],[0,0,0],[0,0,0]],p=0;4>p;p++){var v=Math.floor(p/2),E=p%2;m[p][0]=h[v]*l[E],m[p][1]=n[v],m[p][2]=h[v]*f[E],_[p][0]=t[0]+m[p][0]*e,_[p][1]=t[1]+m[p][1]*e,_[p][2]=t[2]+m[p][2]*e}0!=u&&(this.Normal(m[0]),this.Point(_[0]),this.Normal(m[1]),this.Point(_[1]),this.Normal(m[2]),this.Point(_[2])),u!=r-1&&(this.Normal(m[2]),this.Point(_[2]),this.Normal(m[1]),this.Point(_[1]),this.Normal(m[3]),this.Point(_[3]))}}},MeshCreator.prototype.End=function(t,e){if("gl"in t&&(t=t.gl),!this.creating)throw"Invalid call: End without begin";this.creating=!1;var r=this.triangles.length%3;0!=r&&(console.log("warning Mesh.End: "+r+" vertices ignored"),this.triangles.length-=r),e=e||new Mesh(t),e.use16bit=this.points.length<65520,e.nIndices=this.triangles.length,e.nVertices=this.points.length,e.nBytePerVertex=3;var i=3;this.normals?(e.nBytePerVertex+=3,e.normalDelta=i,i+=3):e.normalDelta=0,this.texCoords?(e.nBytePerVertex+=2,e.texCoordsDelta=i,i+=2):e.texCoordsDelta=0;for(var a=new Float32Array(e.nVertices*e.nBytePerVertex),o=0;o<this.points.length;o++){for(var s=this.points[o],n=o*e.nBytePerVertex,h=0;3>h;h++)a[n+h]=s.pos[h];if(this.normals)for(var h=0;3>h;h++)a[n+h+e.normalDelta]=s.nor[h];if(this.texCoords)for(var h=0;2>h;h++)a[n+h+e.texCoordsDelta]=s.tex[h]}e.nBytePerVertex*=4,e.normalDelta*=4,e.texCoordsDelta*=4,console.log("Created mesh with "+e.nVertices+" vertices and "+e.nIndices/3+" triangles");var l=t.createBuffer();t.bindBuffer(t.ARRAY_BUFFER,l),t.bufferData(t.ARRAY_BUFFER,a,t.STATIC_DRAW);var f=t.createBuffer();return t.bindBuffer(t.ELEMENT_ARRAY_BUFFER,f),e.use16bit?t.bufferData(t.ELEMENT_ARRAY_BUFFER,new Int16Array(this.triangles),t.STATIC_DRAW):t.bufferData(t.ELEMENT_ARRAY_BUFFER,new Int32Array(this.triangles),t.STATIC_DRAW),t.bindBuffer(t.ARRAY_BUFFER,null),t.bindBuffer(t.ELEMENT_ARRAY_BUFFER,null),e.vBuffer=l,e.iBuffer=f,this.points=[],this.mappedPoints=[],this.triangles=[],e},RotatingCamera.prototype=new Camera,RotatingCamera.prototype.UpdateMatrices=function(){if(this._needCalcRotate){var t=Math.sin(this._alpha),e=Math.cos(this._alpha),r=Math.sin(this._beta),i=Math.cos(this._beta),a=[0,0,0],o=this.look();a[0]=o[0]+e*r*this._dist,a[1]=o[1]+t*this._dist,a[2]=o[2]+e*i*this._dist,this.pos(a),this.nearPlane(.1*this._dist),this.farPlane(10*this._dist)}Camera.prototype.UpdateMatrices.call(this),this._needCalcRotate=!1},RotatingCamera.prototype.alpha=function(t){return void 0!=t&&(this._needCalcRotate=floatDiff(this._alpha,t),this._alpha=t),this._alpha},RotatingCamera.prototype.beta=function(t){return void 0!=t&&(this._needCalcRotate=floatDiff(this._beta,t),this._beta=t),this._beta},RotatingCamera.prototype.dist=function(t){return void 0!=t&&(this._needCalcRotate=floatDiff(this._dist,t),this._dist=t),this._dist},RotatingCamera.prototype.alphaDeg=function(t){void 0!=t&&this.alpha(Math.PI*t/180)},RotatingCamera.prototype.betaDeg=function(t){void 0!=t&&this.beta(Math.PI*t/180)},RotatingCamera.prototype.ApplyDeltas=function(t,e,r){this._alpha+=e/100,this._beta-=t/100,0>r&&(this._dist*=1.1),r>0&&(this._dist/=1.1),this._needCalcRotate=!0,this.UpdateMatrices()},RotatingCamera.prototype.AddListeners=function(t,e){t=t||document.body,e=e||function(){};var r,i,a=!1,o=this;t.addEventListener("mousedown",function(t){a=!0,r=t.screenX,i=t.screenY}),document.body.addEventListener("mouseup",function(){a=!1}),document.body.addEventListener("mousemove",function(t){if(a){var s=t.screenX-r,n=t.screenY-i;r=t.screenX,i=t.screenY,o.ApplyDeltas(s,n,0),e()}}),t.addEventListener("wheel",function(t){t.deltaY=t.deltaY?t.deltaY:-t.wheelDelta,o.ApplyDeltas(0,0,t.deltaY),o._needCalcRotate=!0,o.UpdateMatrices(),e()})},Light.prototype=new Mesh,Light.prototype.constructor=Light,Light.prototype.PreDraw=function(){Mesh.prototype.PreDraw.call(this,Mesh.PROGRAMID_UNLIT)},Light.prototype.ReDraw=function(){Mesh.prototype.ReDraw.call(this,Mesh.PROGRAMID_UNLIT)},Light.prototype.EndDraw=function(){Mesh.prototype.EndDraw.call(this,Mesh.PROGRAMID_UNLIT)},Light.prototype.Draw=function(){Mesh.prototype.Draw.call(this,Mesh.PROGRAMID_UNLIT)},Light.prototype.Begin=function(t,e){if("gl"in t&&(t=t.gl),this.currentRenderTarget=e,this.needRecalcMatrices){this.needRecalcMatrices=!1;var r=.9*this.radius,i=this.farPlane,a=i-r,o=-(i+r)/a,s=-(2*i*r)/a;this.matrices[0].set([0,0,-o,1,0,1,0,0,1,0,0,0,0,0,s,0]),this.matrices[1].set([0,0,o,-1,0,1,0,0,-1,0,0,0,0,0,s,0]),this.matrices[2].set([1,0,0,0,0,0,o,-1,0,-1,0,0,0,0,s,0]),this.matrices[3].set([1,0,0,0,0,0,-o,1,0,1,0,0,0,0,s,0]),this.matrices[4].set([1,0,0,0,0,1,0,0,0,0,o,-1,0,0,s,0]),this.matrices[5].set([-1,0,0,0,0,1,0,0,0,0,-o,1,0,0,s,0])}t.bindFramebuffer(t.FRAMEBUFFER,this.frameBuffers[e]),t.viewport(0,0,512,512),t.clearColor(0==(1&e)?0:1,0==(2&e)?0:1,0==(4&e)?0:1,1),t.clear(t.COLOR_BUFFER_BIT|t.DEPTH_BUFFER_BIT)},Light.prototype.End=function(t){"gl"in t&&(t=t.gl),t.bindFramebuffer(t.FRAMEBUFFER,null)},Audio.prototype.setCamera=function(t,e,r){void 0!=this.context&&(this.context.listener.setPosition(t[0],t[1],t[2]),this.context.listener.setOrientation(e[0],e[1],e[2],r[0],r[1],r[2]))},Audio.prototype.setFirePos=function(t){if(void 0!=this.context)if(void 0==this.fire){for(var e=this.context.sampleRate,r=e,i=this.context.createBuffer(1,r,e),a=i.getChannelData(0),o=[],s=0;r>s;s++)o[s]=2*Math.random()-1;var n=o;n=LowPass(200,e,n,r),n=HighPass(100,e,n,r);for(var s=0;r>s;s++)a[s]=10*n[s];var h=this.context.createBufferSource();h.buffer=i,h.loop=!0,this.fire=this.context.createPanner(),h.connect(this.fire),this.fire.connect(this.gainNode),this.fire.setPosition(t[0],t[1],t[2]),this.fire.refDistance=10,h.start()}else this.fire.setPosition(t[0],t[1],t[2])},Audio.prototype.hit=function(t,e){if(void 0!=this.context){for(var r=this.context.sampleRate,i=this.context.createBuffer(1,this.bufferSize,r),a=i.getChannelData(0),o=0;o<this.bufferSize;o++){var s=1-o/this.bufferSize;a[o]=this["final"][o]*s*t}var n=this.context.createBufferSource();n.buffer=i,n.loop=!1;var h=this.context.createPanner();n.connect(h),h.connect(this.gainNode),h.setPosition(e[0],e[1],e[2]),h.refDistance=10,n.start()}},ParticleSystem.prototype.update=function(){var t=Date.now(),e=t-this.lastTime;if(this.lastTime=t,e>1&&(e=1/60),e>0){for(var r=[],i=0;i<this.particles.length;i++){var a=this.particles[i];if(a[4]+=5*e,a[0]+=e*a[3],a[1]+=e*a[4],a[2]+=e*a[5],a[6]-=e,a[6]<=0)r.push(i);else{a[0]<-10&&(a[0]=-10),a[0]>10&&(a[0]=10),a[1]<-1&&(a[1]=-1),a[1]>10&&(a[1]=10),a[2]<-10&&(a[2]=-10),a[2]>10&&(a[2]=10);var o=[a[0]-this.center_posvel[0],a[1]-this.center_posvel[1],a[2]-this.center_posvel[2]],s=o[0]*o[0]+o[1]*o[1]+o[2]*o[2];1>s&&(s=1/Math.sqrt(s)-1,a[0]+=o[0]*s,a[1]+=o[1]*s,a[2]+=o[2]*s)}}for(var i=r.length-1;i>=0;i--)this.particles.splice(r[i],1)}for(var n=20+10*Math.random(),i=0;n>i;i++){var a=[],h=Math.random(),l=(Math.random()-.5)*Math.PI,f=2*(Math.random()-.5)*Math.PI,u=Math.sin(l),c=Math.cos(l),d=Math.sin(f),m=Math.cos(f);a[0]=c*d*h,a[1]=u*h,a[2]=c*m*h,a[3]=-a[0]*(.2*Math.random()+.9)+.2*this.center_posvel[3],a[4]=Math.random()-.5+.2*this.center_posvel[4],a[5]=-a[2]*(.2*Math.random()+.9)+.2*this.center_posvel[5],a[0]+=this.center_posvel[0],a[1]+=this.center_posvel[1],a[2]+=this.center_posvel[2],a[6]=.5*Math.random()+.5,this.particles.push(a)}for(var _=Math.min(this.particles.length,ParticleSystem.N_MAX_DRAW),i=0,p=0;_>i;i++,p+=4){var a=this.particles[i];this.array[p+0]=a[0],this.array[p+1]=a[1],this.array[p+2]=a[2],this.array[p+3]=a[6]}this.mean=[0,0,0,0];for(var _=1e3,i=0;i<this.particles.length;i++){var a=this.particles[i];this.mean[0]+=a[0]-this.center_posvel[0],this.mean[1]+=a[1]-this.center_posvel[1],this.mean[2]+=a[2]-this.center_posvel[2],this.mean[3]+=a[6],_+=a[6]<1?a[6]:1}this.mean[0]/=_,this.mean[1]/=_,this.mean[2]/=_,this.mean[3]/=this.particles.length,this.radius=0;for(var i=0;i<this.particles.length;i++){var a=this.particles[i];if(a[6]<=1){var s=[this.mean[0]-a[0]+this.center_posvel[0],this.mean[1]-a[1]+this.center_posvel[1],this.mean[2]-a[2]+this.center_posvel[2]],h=s[0]*s[0]+s[1]*s[1]+s[2]*s[2];h>this.radius&&(this.radius=h)}}this.radius=Math.sqrt(this.radius),this.radius>2&&(this.radius=2);var v=this.gl;v.bindBuffer(v.ARRAY_BUFFER,this.vBuffer),v.bufferSubData(v.ARRAY_BUFFER,0,this.array),v.bindBuffer(v.ARRAY_BUFFER,null)},ParticleSystem.SetCamera=function(t,e,r,i){
if(void 0!=ParticleSystem.program&&null!=ParticleSystem.program){"gl"in t&&(t=t.gl);var a=ParticleSystem.program.uniforms;t.useProgram(ParticleSystem.program),t.uniformMatrix4fv(a.uPVMatrix,!1,e.viewProj()),t.useProgram(null)}},ParticleSystem.prototype.draw=function(){if(void 0!=ParticleSystem.program&&null!=ParticleSystem.program){var t=this.gl,e=ParticleSystem.program.attributes;t.blendFunc(t.SRC_ALPHA,t.ONE),t.enable(t.BLEND),t.useProgram(ParticleSystem.program),t.bindBuffer(t.ARRAY_BUFFER,this.vBuffer),t.enableVertexAttribArray(e.aVertexPosition),t.vertexAttribPointer(e.aVertexPosition,4,t.FLOAT,!1,0,0),this.selected?t.uniform3f(ParticleSystem.program.uniforms.uPColor,0,.1,1):t.uniform3f(ParticleSystem.program.uniforms.uPColor,1,.1,0),t.drawArrays(t.POINTS,0,Math.min(this.particles.length,ParticleSystem.N_MAX_DRAW)),t.disable(t.BLEND)}},document.addEventListener("DOMContentLoaded",function(t){function e(t){if("phyUpdate"==t.data[0]){_.pos=t.data[1],s.particles.selected=t.data[1][6],h.setFirePos(t.data[1]);for(var e=0;10>e;e++)f[e].pos=t.data[2+e+e],f[e].quat=t.data[3+e+e],t.data[2+e+e][3]?f[e].materialColor=[1,1,1,1]:f[e].materialColor=u[e];h.setCamera(s.camera.pos(),s.camera._at,s.camera._up);for(var e=0;e<t.data[22].length;e++){var r=t.data[22][e];h.hit(r[3]/5,r)}s.particles.center_posvel=t.data[1],s.particles.update(),_.radius=.5+s.particles.radius/4,s.particles.selected?_.seeColor=[.4,.9,.9]:_.seeColor=[.9,.9,.4],s.paint()}else"camUpdate"==t.data[0]&&v.ApplyDeltas(t.data[1],t.data[2],t.data[3])}function r(){p=new Worker("phyPart.min.js"),p.onmessage=e}function i(t){var e=s.c.getBoundingClientRect(),r=t.clientX-e.left,i=t.clientY-e.top;return[r/s.c.width*2-1,1-i/s.c.height*2]}function a(t){var e=i(t),r=v.pos(),a=v.get3D(e[0],e[1],1),o=[t.type,t.screenX,t.screenY,t.deltaY?t.deltaY:-t.wheelDelta,r,a];p.postMessage(o)}function o(t){if(0==t.touches.length&&"touchend"==t.type&&a({type:"mouseup",clientX:0,clientY:0,screenX:0,screenY:0,deltaY:0}),1==t.touches.length){var e=t.touches[0],r={clientX:e.clientX,clientY:e.clientY,screenX:e.screenX,screenY:e.screenY,deltaY:0};switch(t.type){case"touchstart":r.type="mousedown";break;case"touchend":r.type="mouseup";break;case"touchcancel":r.type="mouseup";break;case"touchmove":r.type="mousemove"}a(r)}if(2==t.touches.length){var o=i(t.touches[0]),s=i(t.touches[1]),n=[s[0]-o[0],s[1]-o[1]];if("touchmove"==t.type){var e=t.touches[0];a({type:"wheel",clientX:e.clientX,clientY:e.clientY,screenX:e.screenX,screenY:e.screenY,deltaY:oldD-n})}E=n}t.preventDefault()}var s=new Engine,n=s.gl,h=new Audio;Mesh.LoadProgram(s);for(var l,f=[],u=[[.8,.3,.8,1],[.3,.8,.3,1],[.3,.3,.8,1],[.8,.8,.3,1],[.8,.3,.8,1],[.3,.8,.8,1],[.8,.4,.1,1],[.8,.1,.4,1],[.4,.8,.1,1],[.1,.4,.8,1]],c=0;10>c;c++){l=new MeshCreator,l.Begin(!0),l.AddBox();var d=l.End(n);d.materialColor=u[c],s.meshes.push(d),f.push(d)}l=new MeshCreator,l.Begin(!0),l.AddBox([-10,-1,-10],[10,10,10],!0);var m=l.End(n);m.materialColor=[.65,.65,.65,1],s.meshes.push(m);var _=new Light(n,[3,4,5]);s.meshes.push(_),s.light=_;var p;document.getElementById("restart").onclick=function(){p.terminate(),s.particles.particles=[],r()},r();var v=new RotatingCamera;v.persp(!0),v.fovDeg(35),v.look(0,4,0),v.alpha(.37),v.beta(-.3),v.dist(57),s.camera=v,s.paint();var E=0;document.body.addEventListener("mousedown",a),document.body.addEventListener("mouseup",a),document.body.addEventListener("mousemove",a),document.body.addEventListener("touchstart",o),document.body.addEventListener("touchend",o),document.body.addEventListener("touchcancel",o),document.body.addEventListener("touchmove",o),document.body.addEventListener("wheel",a)});
</script>
</head>
<body>
<input type="button" value="restart" id="restart">
<span id="me">By <a href="http://aperricone.altervista.org">Antonino Perricone</a> © 2016</span>
</body>
</html>