-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
1 lines (1 loc) · 24.3 KB
/
test.js
File metadata and controls
1 lines (1 loc) · 24.3 KB
1
function Engine(){function t(){var t=innerWidth-16,r=innerHeight-16;o.c.width=t,o.c.height=r,e.bindRenderbuffer(e.RENDERBUFFER,i),e.renderbufferStorage(e.RENDERBUFFER,e.DEPTH_COMPONENT16,t,r),e.bindTexture(e.TEXTURE_2D,a),e.texImage2D(e.TEXTURE_2D,0,e.RGBA,t,r,0,e.RGBA,e.UNSIGNED_BYTE,null),o.frameBuffer=e.createFramebuffer(),e.bindFramebuffer(e.FRAMEBUFFER,o.frameBuffer),e.framebufferRenderbuffer(e.FRAMEBUFFER,e.DEPTH_ATTACHMENT,e.RENDERBUFFER,i),e.framebufferTexture2D(e.FRAMEBUFFER,e.COLOR_ATTACHMENT0,e.TEXTURE_2D,a,0),e.bindFramebuffer(e.FRAMEBUFFER,null),o.paint()}this.c=document.createElement("canvas"),document.body.appendChild(this.c);var e=this.c.getContext("webgl");if(null==e)throw document.body.innerHTML="Unable to create WebGL context";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 r=e.createBuffer();e.bindBuffer(e.ARRAY_BUFFER,r),e.bufferData(e.ARRAY_BUFFER,new Float32Array([-1,-1,1,-1,-1,1,1,1]),e.STATIC_DRAW),this.postVBuffer=r;var i=e.createRenderbuffer();e.bindRenderbuffer(e.RENDERBUFFER,i),e.renderbufferStorage(e.RENDERBUFFER,e.DEPTH_COMPONENT16,512,512);var a=e.createTexture();e.bindTexture(e.TEXTURE_2D,a),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=a,this.frameBuffer=e.createFramebuffer(),e.bindFramebuffer(e.FRAMEBUFFER,this.frameBuffer),e.framebufferRenderbuffer(e.FRAMEBUFFER,e.DEPTH_ATTACHMENT,e.RENDERBUFFER,i),e.framebufferTexture2D(e.FRAMEBUFFER,e.COLOR_ATTACHMENT0,e.TEXTURE_2D,a,0),e.bindFramebuffer(e.FRAMEBUFFER,null);var o=this;this.postProgram=null,LoadProgram(e,"post.vs","post.fs",function(t){o.postProgram=t,e.useProgram(t),e.uniform1i(t.uniforms.fBuffer,0),o.paint()}),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)}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],"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]),Mesh.LoadProgram(t))}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.__defineGetter__("colorFar",function(){return[this.materialColor[0],this.materialColor[1],this.materialColor[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]});var h=new Camera;h.persp(!0),h.height(1),h.aspectRatio(1),h.pos(0,0,0),h.look(0,0,-1),h.up(0,1,0),h.nearPlane(.9),h.farPlane(22),h.UpdateMatrices()}function Audio(){this.context=new AudioContext,void 0!=this.context&&(this.gainNode=this.context.createGain(),this.gainNode.connect(this.context.destination))}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}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}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)),this.light){Mesh.SetLight(t,this.light),t.depthFunc(t.LEQUAL),t.polygonOffset(4,4);for(var i=0;6>i;i++){this.light.Begin(t,i),Mesh.SetCubeUniforms(t,this.light);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)}null!=this.postProgram&&t.bindFramebuffer(t.FRAMEBUFFER,this.frameBuffer),t.viewport(0,0,this.c.width,this.c.height),t.clearColor(.2,.2,.8,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)}Mesh.ForceProgram=void 0,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(null!=this.postProgram){t.bindFramebuffer(t.FRAMEBUFFER,null);var s=this.postProgram.attributes;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.disable(t.DEPTH_TEST),t.disable(t.CULL_FACE),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 f=t.getProgramParameter(o,t.ACTIVE_UNIFORMS),n=0;f>n;n++){var l=t.getActiveUniform(o,n);o.uniforms[l.name]=t.getUniformLocation(o,l.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]]),a=r(t(this._up,i)),o=r(t(i,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,f=h/this._aspectRatio,l=this._farPlane-this._nearPlane;this._ortho?(s=2/l,n=(this._farPlane+this._nearPlane)/l,this._projection[11]=0,this._projection[15]=1):(s=(this._farPlane+this._nearPlane)/l,n=2*this._farPlane*this._nearPlane/l,this._projection[11]=-1,this._projection[15]=0),this._projection[0]=f,this._projection[5]=h,this._projection[10]=-s,this._projection[14]=-n}if(this._needCalcProj||this._needCalcView)for(var u=0,d=0;4>d;d++)for(var c=0;4>c;c++){this._viewProj[u]=0;for(var _=0;4>_;_++)this._viewProj[u]+=this._view[_+4*d]*this._projection[4*_+c];u++}this._needCalcProj=!1,this._needCalcView=!1},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],f=t[1]*t[2];f+=f;var l=t[2]*t[2];this.modelMat[0]=e+o-h-l,this.modelMat[4]=s-a,this.modelMat[8]=n+i,this.modelMat[1]=s+a,this.modelMat[5]=e-o+h-l,this.modelMat[9]=f-r,this.modelMat[2]=n-i,this.modelMat[6]=f+r,this.modelMat[10]=e-o-h+l}),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.vs","mesh.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.vs","unlit.fs",function(e){Mesh.programs[Mesh.PROGRAMID_UNLIT]=e,"paint"in t&&t.paint()}),LoadProgram(t,"cube.vs","zbuff.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.uniform2f(o.uScreenSize,r,i)}t.useProgram(null)},Mesh.SetCubeUniforms=function(t,e){if("gl"in t&&(t=t.gl),void 0!=Mesh.programs[Mesh.PROGRAMID_CUBE]){var r=Mesh.programs[Mesh.PROGRAMID_CUBE].uniforms;t.useProgram(Mesh.programs[Mesh.PROGRAMID_CUBE]),t.uniformMatrix4fv(r.uPVMatrix,!1,e.currentPVMatrix),t.uniform3fv(r.uCenter,e.pos),t.useProgram(null)}},Mesh.SetLight=function(t,e){if(void 0!=Mesh.programs[0]){"gl"in t&&(t=t.gl);var r=Mesh.programs[0].uniforms;t.useProgram(Mesh.programs[0]),t.uniform4fv(r.uLightColorFar,e.colorFar),t.uniform4fv(r.uLightPosRadius,e.posRadius),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],f=[1,1],l=[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 d=0;f[1]=1,l[1]=0;for(var c=0;i>c;c++){f[0]=f[1],l[0]=l[1],d+=o,f[1]=Math.cos(d),l[1]=Math.sin(d);for(var _=[[0,0,0],[0,0,0],[0,0,0],[0,0,0]],p=[[0,0,0],[0,0,0],[0,0,0],[0,0,0]],m=0;4>m;m++){var E=Math.floor(m/2),R=m%2;_[m][0]=h[E]*f[R],_[m][1]=n[E],_[m][2]=h[E]*l[R],p[m][0]=t[0]+_[m][0]*e,p[m][1]=t[1]+_[m][1]*e,p[m][2]=t[2]+_[m][2]*e}0!=u&&(this.Normal(_[0]),this.Point(p[0]),this.Normal(_[1]),this.Point(p[1]),this.Normal(_[2]),this.Point(p[2])),u!=r-1&&(this.Normal(_[2]),this.Point(p[2]),this.Normal(_[1]),this.Point(p[1]),this.Normal(_[3]),this.Point(p[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 f=t.createBuffer();t.bindBuffer(t.ARRAY_BUFFER,f),t.bufferData(t.ARRAY_BUFFER,a,t.STATIC_DRAW);var l=t.createBuffer();return t.bindBuffer(t.ELEMENT_ARRAY_BUFFER,l),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=f,e.iBuffer=l,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.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._alpha+=n/100,o._beta-=s/100,o._needCalcRotate=!0,o.UpdateMatrices(),e()}}),t.addEventListener("mousewheel",function(t){t.wheelDelta=t.detail?-t.detail:t.wheelDelta,t.wheelDelta<0?o._dist*=1.1:o._dist/=1.1,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.hit=function(){if(void 0!=this.context){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;for(var o=LowPass(100,e,i,r),s=this.context.createBuffer(1,r,e),n=s.getChannelData(0),a=0;r>a;a++){var h=1-a/r;n[a]=o[a]*h}var f=this.context.createBufferSource();f.buffer=s,f.loop=!1,f.connect(this.context.destination),f.start()}},document.addEventListener("DOMContentLoaded",function(t){function e(t){u.pos=t.data[0];for(var e=0;10>e;e++)s[e].pos=t.data[1+e+e],s[e].quat=t.data[2+e+e];for(var e=0;e<t.data[3].length;e++);i.paint()}function r(){d=new Worker("phyPart.js"),d.onmessage=e}var i=new Engine,a=i.gl;new Audio;Mesh.LoadProgram(i);for(var o,s=[],n=[[.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]],h=0;10>h;h++){o=new MeshCreator,o.Begin(!0),o.AddBox();var f=o.End(a);f.materialColor=n[h],i.meshes.push(f),s.push(f)}o=new MeshCreator,o.Begin(!0),o.AddBox([-10,-1,-10],[10,10,10],!0);var l=o.End(a);l.materialColor=[.65,.65,.65,1],i.meshes.push(l);var u=new Light(a,[3,4,5]);i.meshes.push(u),i.light=u;var d;document.getElementById("restart").onclick=function(){d.terminate(),r()},r();var c=new RotatingCamera;c.persp(!0),c.fovDeg(35),c.look(0,4,0),c.alpha(.37),c.beta(-.3),c.dist(57),i.camera=c,i.paint(),c.AddListeners(void 0)});