|
|
function inherit(a,b){for(var c=Object.create(a.prototype),d=Object.keys(b),e=0;e<d.length;e++)c[d[e]]=b[d[e]];return c}function text(a){return a.join("\n")}function makePerspective(a,b,c,d){var e=c*Math.tan(a*Math.PI/360),f=-e,g=f*b,h=e*b;return makeFrustum(g,h,f,e,c,d)}function makeFrustum(a,b,c,d,e,f){var g=2*e/(b-a),h=2*e/(d-c),i=(b+a)/(b-a),j=(d+c)/(d-c),k=-(f+e)/(f-e),l=-2*f*e/(f-e);return $M([[g,0,i,0],[0,h,j,0],[0,0,k,l],[0,0,-1,0]])}var ImageTexture=function(){function a(a,b,c){Texture.call(this,a,b,c)}return a.prototype=inherit(Texture,{fill:function(a,b){var c=this.gl;c.bindTexture(c.TEXTURE_2D,this.texture),b?c.texSubImage2D(c.TEXTURE_2D,0,0,0,this.size.w,this.size.h,this.format,c.UNSIGNED_BYTE,a):c.texImage2D(c.TEXTURE_2D,0,this.format,this.format,c.UNSIGNED_BYTE,a)}}),a}(),WebGLCanvas=function(){function a(a,d,e){this.canvas=a,this.size=d;var f=!0;for(var g in k)d.w===k[g]&&(f=!1);this.canvas.width=f?d.w:192===d.w?d.w-12:d.w-8,this.canvas.height=d.h,this.onInitWebGL(),this.onInitShaders(),c.call(this),e&&b.call(this),this.onInitTextures(),h.call(this)}function b(){var a=this.gl;this.framebuffer=a.createFramebuffer(),a.bindFramebuffer(a.FRAMEBUFFER,this.framebuffer),this.framebufferTexture=new Texture(this.gl,this.size,a.RGBA);var b=a.createRenderbuffer();a.bindRenderbuffer(a.RENDERBUFFER,b),a.renderbufferStorage(a.RENDERBUFFER,a.DEPTH_COMPONENT16,this.size.w,this.size.h),a.framebufferTexture2D(a.FRAMEBUFFER,a.COLOR_ATTACHMENT0,a.TEXTURE_2D,this.framebufferTexture.texture,0),a.framebufferRenderbuffer(a.FRAMEBUFFER,a.DEPTH_ATTACHMENT,a.RENDERBUFFER,b)}function c(){var a=[1,1,0,-1,1,0,1,-1,0,-1,-1,0],b=this.gl;this.quadVPBuffer=b.createBuffer(),b.bindBuffer(b.ARRAY_BUFFER,this.quadVPBuffer),b.bufferData(b.ARRAY_BUFFER,new Float32Array(a),b.STATIC_DRAW),this.quadVPBuffer.itemSize=3,this.quadVPBuffer.numItems=4;var c=1,d=1;this.quadVTCBuffer=b.createBuffer(),b.bindBuffer(b.ARRAY_BUFFER,this.quadVTCBuffer),a=[c,0,0,0,c,d,0,d],b.bufferData(b.ARRAY_BUFFER,new Float32Array(a),b.STATIC_DRAW)}function d(){this.mvMatrix=Matrix.I(4)}function e(a){this.mvMatrix=this.mvMatrix.x(a)}function f(a){e.call(this,Matrix.Translation($V([a[0],a[1],a[2]])).ensure4x4())}function g(){this.program.setMatrixUniform("uPMatrix",new Float32Array(this.perspectiveMatrix.flatten())),this.program.setMatrixUniform("uMVMatrix",new Float32Array(this.mvMatrix.flatten()))}function h(){var a=this.gl;this.perspectiveMatrix=makePerspective(45,1,.1,100),d.call(this),f.call(this,[0,0,-2.415]),a.bindBuffer(a.ARRAY_BUFFER,this.quadVPBuffer),a.vertexAttribPointer(this.vertexPositionAttribute,3,a.FLOAT,!1,0,0),a.bindBuffer(a.ARRAY_BUFFER,this.quadVTCBuffer),a.vertexAttribPointer(this.textureCoordAttribute,2,a.FLOAT,!1,0,0),this.onInitSceneTextures(),g.call(this),this.framebuffer&&a.bindFramebuffer(a.FRAMEBUFFER,this.framebuffer)}var i=Script.createFromSource("x-shader/x-vertex",text(["attribute vec3 aVertexPosition;","attribute vec2 aTextureCoord;","uniform mat4 uMVMatrix;","uniform mat4 uPMatrix;","varying highp vec2 vTextureCoord;","void main(void) {"," gl_Position = uPMatrix * uMVMatrix * vec4(aVertexPosition, 1.0);"," vTextureCoord = aTextureCoord;","}"])),j=Script.createFromSource("x-shader/x-fragment",text(["precision highp float;","varying highp vec2 vTextureCoord;","uniform sampler2D texture;","void main(void) {"," gl_FragColor = texture2D(texture, vTextureCoord);","}"])),k=[192,368,608,1088,1472,1952,3008];return a.prototype={toString:function(){return"WebGLCanvas Size: "+this.size},checkLastError:function(a){var b=this.gl.getError();if(b!==this.gl.NO_ERROR){var c=this.glNames[b];c="undefined"!=typeof c?c+"("+b+")":"Unknown WebGL ENUM (0x"+value.toString(16)+")",a?debug.log("WebGL Error: %s, %s",a,c):debug.log("WebGL Error: %s",c),debug.trace()}},onInitWebGL:function(){try{this.gl=this.canvas.getContext("experimental-webgl")}catch(a){debug.log("inInitWebGL error = "+a)}if(this.gl||debug.error("Unable to initialize WebGL. Your browser may not support it."),!this.glNames){this.glNames={};for(var b in this.gl)"number"==typeof this.gl[b]&&(this.gl
|