| 1 | class GLContext |
| 2 | { |
| 3 | constructor (canvas) |
| 4 | { |
| 5 | this.canvas = canvas; |
| 6 | |
| 7 | const attrs = { alpha: true, premultipliedAlpha: false, antialias: true }; |
| 8 | this.gl = canvas.getContext("webgl", attrs) || canvas.getContext("experimental-webgl", attrs); |
| 9 | |
| 10 | if (!this.gl) |
| 11 | { throw new Error("WebGL is not supported in this browser."); } |
| 12 | |
| 13 | const gl = this.gl; |
| 14 | gl.enable(gl.BLEND); |
| 15 | gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA); |
| 16 | gl.disable(gl.DEPTH_TEST); |
| 17 | gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, false); |
| 18 | |
| 19 | this.defaultShader = new Shader(Shader.DEFAULT_VERT, Shader.DEFAULT_FRAG); |
| 20 | this.textures = new WeakMap(); |
| 21 | |
| 22 | this.positionBuffer = gl.createBuffer(); |
| 23 | gl.bindBuffer(gl.ARRAY_BUFFER, this.positionBuffer); |
| 24 | gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([ |
| 25 | -0.5, -0.5, |
| 26 | 0.5, -0.5, |
| 27 | -0.5, 0.5, |
| 28 | 0.5, 0.5 |
| 29 | ]), gl.STATIC_DRAW); |
| 30 | |
| 31 | this.texCoordBuffer = gl.createBuffer(); |
| 32 | this._lastWidth = 0; |
| 33 | this._lastHeight = 0; |
| 34 | this.setSize(canvas.width, canvas.height); |
| 35 | } |
| 36 | |
| 37 | setSize (width, height) |
| 38 | { |
| 39 | const gl = this.gl; |
| 40 | if (width !== this._lastWidth || height !== this._lastHeight) |
| 41 | { |
| 42 | this._lastWidth = width; |
| 43 | this._lastHeight = height; |
| 44 | gl.viewport(0, 0, width, height); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | clear (r, g, b, a = 1) |
| 49 | { |
| 50 | const gl = this.gl; |
| 51 | gl.clearColor(r / 255, g / 255, b / 255, a); |
| 52 | gl.clear(gl.COLOR_BUFFER_BIT); |
| 53 | } |
| 54 | |
| 55 | clearTransparent () |
| 56 | { |
| 57 | const gl = this.gl; |
| 58 | gl.clearColor(0, 0, 0, 0); |
| 59 | gl.clear(gl.COLOR_BUFFER_BIT); |
| 60 | } |
| 61 | |
| 62 | getTexture (htmlImage) |
| 63 | { |
| 64 | if (!htmlImage) |
| 65 | { return null; } |
| 66 | |
| 67 | let texture = this.textures.get(htmlImage); |
| 68 | if (texture) |
| 69 | { return texture; } |
| 70 | |
| 71 | const gl = this.gl; |
| 72 | texture = gl.createTexture(); |
| 73 | gl.bindTexture(gl.TEXTURE_2D, texture); |
| 74 | gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE); |
| 75 | gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE); |
| 76 | gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR); |
| 77 | gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR); |
| 78 | gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, htmlImage); |
| 79 | |
| 80 | this.textures.set(htmlImage, texture); |
| 81 | return texture; |
| 82 | } |
| 83 | |
| 84 | drawSprite (options) |
| 85 | { |
| 86 | const gl = this.gl; |
| 87 | const width = this._lastWidth; |
| 88 | const height = this._lastHeight; |
| 89 | if (width <= 0 || height <= 0) |
| 90 | { return; } |
| 91 | |
| 92 | const shader = options.shader || this.defaultShader; |
| 93 | const info = shader.getProgramInfo(gl); |
| 94 | if (!info) |
| 95 | { return; } |
| 96 | |
| 97 | const texture = options.texture; |
| 98 | if (!texture) |
| 99 | { return; } |
| 100 | |
| 101 | gl.useProgram(info.program); |
| 102 | |
| 103 | gl.bindBuffer(gl.ARRAY_BUFFER, this.positionBuffer); |
| 104 | if (info.attribs.position >= 0) |
| 105 | { |
| 106 | gl.enableVertexAttribArray(info.attribs.position); |
| 107 | gl.vertexAttribPointer(info.attribs.position, 2, gl.FLOAT, false, 0, 0); |
| 108 | } |
| 109 | |
| 110 | const uv = options.uvRect || { u0: 0, v0: 0, u1: 1, v1: 1 }; |
| 111 | gl.bindBuffer(gl.ARRAY_BUFFER, this.texCoordBuffer); |
| 112 | gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([ |
| 113 | uv.u0, uv.v0, |
| 114 | uv.u1, uv.v0, |
| 115 | uv.u0, uv.v1, |
| 116 | uv.u1, uv.v1 |
| 117 | ]), gl.DYNAMIC_DRAW); |
| 118 | |
| 119 | if (info.attribs.texCoord >= 0) |
| 120 | { |
| 121 | gl.enableVertexAttribArray(info.attribs.texCoord); |
| 122 | gl.vertexAttribPointer(info.attribs.texCoord, 2, gl.FLOAT, false, 0, 0); |
| 123 | } |
| 124 | |
| 125 | const matrix = GLContext._buildSpriteMatrix( |
| 126 | options.x, options.y, |
| 127 | options.w, options.h, |
| 128 | options.rotationDeg || 0, |
| 129 | options.mirrorX, options.mirrorY, |
| 130 | width, height |
| 131 | ); |
| 132 | |
| 133 | this._setUniform(info, "u_matrix", matrix); |
| 134 | this._setUniform(info, "u_alpha", options.alpha == null ? 1 : options.alpha); |
| 135 | |
| 136 | gl.activeTexture(gl.TEXTURE0); |
| 137 | gl.bindTexture(gl.TEXTURE_2D, texture); |
| 138 | this._setUniform(info, "u_texture", 0); |
| 139 | |
| 140 | if (options.uniforms) |
| 141 | { |
| 142 | for (const name in options.uniforms) |
| 143 | { |
| 144 | if (!Object.prototype.hasOwnProperty.call(options.uniforms, name)) |
| 145 | { continue; } |
| 146 | |
| 147 | this._setUniform(info, name, options.uniforms[name]); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4); |
| 152 | } |
| 153 | |
| 154 | _setUniform (info, name, value) |
| 155 | { |
| 156 | const entry = info.uniforms[name]; |
| 157 | if (!entry || entry.location == null) |
| 158 | { return; } |
| 159 | |
| 160 | const gl = this.gl; |
| 161 | const loc = entry.location; |
| 162 | |
| 163 | if (typeof value === "number") |
| 164 | { |
| 165 | if (entry.type === gl.INT || entry.type === gl.SAMPLER_2D) |
| 166 | { gl.uniform1i(loc, value); } |
| 167 | else |
| 168 | { gl.uniform1f(loc, value); } |
| 169 | return; |
| 170 | } |
| 171 | |
| 172 | if (value instanceof Float32Array) |
| 173 | { |
| 174 | if (value.length === 16) |
| 175 | { gl.uniformMatrix4fv(loc, false, value); } |
| 176 | else if (value.length === 9) |
| 177 | { gl.uniformMatrix3fv(loc, false, value); } |
| 178 | else if (value.length === 4) |
| 179 | { gl.uniform4fv(loc, value); } |
| 180 | else if (value.length === 3) |
| 181 | { gl.uniform3fv(loc, value); } |
| 182 | else if (value.length === 2) |
| 183 | { gl.uniform2fv(loc, value); } |
| 184 | return; |
| 185 | } |
| 186 | |
| 187 | if (Array.isArray(value)) |
| 188 | { |
| 189 | if (value.length === 4) |
| 190 | { gl.uniform4fv(loc, value); } |
| 191 | else if (value.length === 3) |
| 192 | { gl.uniform3fv(loc, value); } |
| 193 | else if (value.length === 2) |
| 194 | { gl.uniform2fv(loc, value); } |
| 195 | else if (value.length === 1) |
| 196 | { gl.uniform1f(loc, value[0]); } |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | // Column-major mat4: projection (canvas Y-down) * T * R * S |
| 201 | static _buildSpriteMatrix (x, y, w, h, rotationDeg, mirrorX, mirrorY, canvasW, canvasH) |
| 202 | { |
| 203 | const sx = (mirrorX ? -1 : 1) * w; |
| 204 | const sy = (mirrorY ? -1 : 1) * h; |
| 205 | const rad = rotationDeg * Math.PI / 180; |
| 206 | const c = Math.cos(rad); |
| 207 | const s = Math.sin(rad); |
| 208 | |
| 209 | // Model maps unit quad [-0.5,0.5] into pixel space. |
| 210 | const m00 = c * sx; |
| 211 | const m01 = -s * sy; |
| 212 | const m10 = s * sx; |
| 213 | const m11 = c * sy; |
| 214 | const m30 = x; |
| 215 | const m31 = y; |
| 216 | |
| 217 | const pr0 = 2 / canvasW; |
| 218 | const pr5 = -2 / canvasH; |
| 219 | const pr12 = -1; |
| 220 | const pr13 = 1; |
| 221 | |
| 222 | // P * M (only 2D affine terms matter) |
| 223 | return new Float32Array([ |
| 224 | pr0 * m00, pr5 * m10, 0, 0, |
| 225 | pr0 * m01, pr5 * m11, 0, 0, |
| 226 | 0, 0, -1, 0, |
| 227 | pr0 * m30 + pr12, pr5 * m31 + pr13, 0, 1 |
| 228 | ]); |
| 229 | } |
| 230 | } |