GeezerVison is showing github.com — Premultiplied alpha, take 2 · walney/scratch-render@10a6d87
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: src/BitmapSkin.js +5-13Lines changed: 5 additions & 13 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -18,9 +18,6 @@ class BitmapSkin extends Skin { | 18 | 18 | /** @type {!RenderWebGL} */ | 19 | 19 | this._renderer = renderer; | 20 | 20 |
| 21 | | - /** @type {WebGLTexture} */ | 22 | | - this._texture = null; | 23 | | - | 24 | 21 | /** @type {Array<int>} */ | 25 | 22 | this._textureSize = [0, 0]; | 26 | 23 | } | @@ -95,22 +92,17 @@ class BitmapSkin extends Skin { | 95 | 92 | textureData = context.getImageData(0, 0, bitmapData.width, bitmapData.height); | 96 | 93 | } | 97 | 94 |
| 98 | | - if (this._texture) { | 99 | | - gl.bindTexture(gl.TEXTURE_2D, this._texture); | 100 | | - gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, textureData); | 101 | | - this._silhouette.update(textureData); | 102 | | - } else { | 103 | | - // TODO: mipmaps? | | 95 | + if (this._texture === null) { | 104 | 96 | const textureOptions = { | 105 | | - auto: true, | 106 | | - wrap: gl.CLAMP_TO_EDGE, | 107 | | - src: textureData | | 97 | + auto: false, | | 98 | + wrap: gl.CLAMP_TO_EDGE | 108 | 99 | }; | 109 | 100 |
| 110 | 101 | this._texture = twgl.createTexture(gl, textureOptions); | 111 | | - this._silhouette.update(textureData); | 112 | 102 | } | 113 | 103 |
| | 104 | + this._setTexture(textureData); | | 105 | + | 114 | 106 | // Do these last in case any of the above throws an exception | 115 | 107 | this._costumeResolution = costumeResolution || 2; | 116 | 108 | this._textureSize = BitmapSkin._getBitmapSize(bitmapData); | |
Collapse file Copy file name to clipboardExpand all lines: src/Drawable.js +3Lines changed: 3 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -685,6 +685,9 @@ class Drawable { | 685 | 685 | const localPosition = getLocalPosition(drawable, vec); | 686 | 686 | if (localPosition[0] < 0 || localPosition[1] < 0 || | 687 | 687 | localPosition[0] > 1 || localPosition[1] > 1) { | | 688 | + dst[0] = 0; | | 689 | + dst[1] = 0; | | 690 | + dst[2] = 0; | 688 | 691 | dst[3] = 0; | 689 | 692 | return dst; | 690 | 693 | } | |
Collapse file Copy file name to clipboardExpand all lines: src/EffectTransform.js +25-5Lines changed: 25 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -130,15 +130,21 @@ class EffectTransform { | 130 | 130 | const effects = drawable.enabledEffects; | 131 | 131 | const uniforms = drawable.getUniforms(); | 132 | 132 |
| 133 | | - if ((effects & ShaderManager.EFFECT_INFO.ghost.mask) !== 0) { | 134 | | - // gl_FragColor.a *= u_ghost | 135 | | - inOutColor[3] *= uniforms.u_ghost; | 136 | | - } | 137 | | - | 138 | 133 | const enableColor = (effects & ShaderManager.EFFECT_INFO.color.mask) !== 0; | 139 | 134 | const enableBrightness = (effects & ShaderManager.EFFECT_INFO.brightness.mask) !== 0; | 140 | 135 |
| 141 | 136 | if (enableColor || enableBrightness) { | | 137 | + // gl_FragColor.rgb /= gl_FragColor.a + epsilon; | | 138 | + // Here, we're dividing by the (previously pre-multiplied) alpha to ensure HSV is properly calculated | | 139 | + // for partially transparent pixels. | | 140 | + // epsilon is present in the shader because dividing by 0 (fully transparent pixels) messes up calculations. | | 141 | + // We're doing this with a Uint8ClampedArray here, so dividing by 0 just gives 255. We're later multiplying | | 142 | + // by 0 again, so it won't affect results. | | 143 | + const alpha = inOutColor[3] / 255; | | 144 | + inOutColor[0] /= alpha; | | 145 | + inOutColor[1] /= alpha; | | 146 | + inOutColor[2] /= alpha; | | 147 | + | 142 | 148 | // vec3 hsl = convertRGB2HSL(gl_FragColor.xyz); | 143 | 149 | const hsl = rgbToHsl(inOutColor); | 144 | 150 |
| @@ -171,6 +177,20 @@ class EffectTransform { | 171 | 177 | } | 172 | 178 | // gl_FragColor.rgb = convertHSL2RGB(hsl); | 173 | 179 | inOutColor.set(hslToRgb(hsl)); | | 180 | + | | 181 | + // gl_FragColor.rgb *= gl_FragColor.a + epsilon; | | 182 | + // Now we're doing the reverse, premultiplying by the alpha once again. | | 183 | + inOutColor[0] *= alpha; | | 184 | + inOutColor[1] *= alpha; | | 185 | + inOutColor[2] *= alpha; | | 186 | + } | | 187 | + | | 188 | + if ((effects & ShaderManager.EFFECT_INFO.ghost.mask) !== 0) { | | 189 | + // gl_FragColor *= u_ghost | | 190 | + inOutColor[0] *= uniforms.u_ghost; | | 191 | + inOutColor[1] *= uniforms.u_ghost; | | 192 | + inOutColor[2] *= uniforms.u_ghost; | | 193 | + inOutColor[3] *= uniforms.u_ghost; | 174 | 194 | } | 175 | 195 |
| 176 | 196 | return inOutColor; | |
Collapse file Copy file name to clipboardExpand all lines: src/PenSkin.js +4-17Lines changed: 4 additions & 17 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -79,6 +79,9 @@ class PenSkin extends Skin { | 79 | 79 | constructor (id, renderer) { | 80 | 80 | super(id); | 81 | 81 |
| | 82 | + // This silhouette will be updated with data from `gl.readPixels`, which is premultiplied. | | 83 | + this._silhouette.premultiplied = true; | | 84 | + | 82 | 85 | /** | 83 | 86 | * @private | 84 | 87 | * @type {RenderWebGL} | @@ -88,9 +91,6 @@ class PenSkin extends Skin { | 88 | 91 | /** @type {HTMLCanvasElement} */ | 89 | 92 | this._canvas = document.createElement('canvas'); | 90 | 93 |
| 91 | | - /** @type {WebGLTexture} */ | 92 | | - this._texture = null; | 93 | | - | 94 | 94 | /** @type {WebGLTexture} */ | 95 | 95 | this._exportTexture = null; | 96 | 96 |
| @@ -123,7 +123,7 @@ class PenSkin extends Skin { | 123 | 123 |
| 124 | 124 | const NO_EFFECTS = 0; | 125 | 125 | /** @type {twgl.ProgramInfo} */ | 126 | | - this._stampShader = this._renderer._shaderManager.getShader(ShaderManager.DRAW_MODE.stamp, NO_EFFECTS); | | 126 | + this._stampShader = this._renderer._shaderManager.getShader(ShaderManager.DRAW_MODE.default, NO_EFFECTS); | 127 | 127 |
| 128 | 128 | /** @type {twgl.ProgramInfo} */ | 129 | 129 | this._lineShader = this._renderer._shaderManager.getShader(ShaderManager.DRAW_MODE.lineSample, NO_EFFECTS); | @@ -317,13 +317,6 @@ class PenSkin extends Skin { | 317 | 317 |
| 318 | 318 | twgl.bindFramebufferInfo(gl, this._framebuffer); | 319 | 319 |
| 320 | | - // Needs a blend function that blends a destination that starts with | 321 | | - // no alpha. | 322 | | - gl.blendFuncSeparate( | 323 | | - gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA, | 324 | | - gl.ONE, gl.ONE_MINUS_SRC_ALPHA | 325 | | - ); | 326 | | - | 327 | 320 | gl.viewport(0, 0, bounds.width, bounds.height); | 328 | 321 |
| 329 | 322 | gl.useProgram(currentShader.program); | @@ -344,8 +337,6 @@ class PenSkin extends Skin { | 344 | 337 | _exitDrawLineOnBuffer () { | 345 | 338 | const gl = this._renderer.gl; | 346 | 339 |
| 347 | | - gl.blendFuncSeparate(gl.ONE, gl.ONE_MINUS_SRC_ALPHA, gl.ZERO, gl.ONE); | 348 | | - | 349 | 340 | twgl.bindFramebufferInfo(gl, null); | 350 | 341 | } | 351 | 342 |
| @@ -490,8 +481,6 @@ class PenSkin extends Skin { | 490 | 481 |
| 491 | 482 | twgl.bindFramebufferInfo(gl, this._framebuffer); | 492 | 483 |
| 493 | | - gl.blendFuncSeparate(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA, gl.ONE, gl.ONE_MINUS_SRC_ALPHA); | 494 | | - | 495 | 484 | this._drawRectangleRegionEnter(this._stampShader, this._bounds); | 496 | 485 | } | 497 | 486 |
| @@ -501,8 +490,6 @@ class PenSkin extends Skin { | 501 | 490 | _exitDrawToBuffer () { | 502 | 491 | const gl = this._renderer.gl; | 503 | 492 |
| 504 | | - gl.blendFuncSeparate(gl.ONE, gl.ONE_MINUS_SRC_ALPHA, gl.ZERO, gl.ONE); | 505 | | - | 506 | 493 | twgl.bindFramebufferInfo(gl, null); | 507 | 494 | } | 508 | 495 |
| |
Collapse file Copy file name to clipboardExpand all lines: src/RenderWebGL.js +8-17Lines changed: 8 additions & 17 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -196,7 +196,7 @@ class RenderWebGL extends EventEmitter { | 196 | 196 | gl.disable(gl.DEPTH_TEST); | 197 | 197 | /** @todo disable when no partial transparency? */ | 198 | 198 | gl.enable(gl.BLEND); | 199 | | - gl.blendFuncSeparate(gl.ONE, gl.ONE_MINUS_SRC_ALPHA, gl.ZERO, gl.ONE); | | 199 | + gl.blendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA); | 200 | 200 | } | 201 | 201 |
| 202 | 202 | /** | @@ -834,7 +834,8 @@ class RenderWebGL extends EventEmitter { | 834 | 834 | projection, | 835 | 835 | { | 836 | 836 | extraUniforms, | 837 | | - ignoreVisibility: true // Touching color ignores sprite visibility | | 837 | + ignoreVisibility: true, // Touching color ignores sprite visibility, | | 838 | + effectMask: ~ShaderManager.EFFECT_INFO.ghost.mask | 838 | 839 | }); | 839 | 840 |
| 840 | 841 | gl.stencilFunc(gl.EQUAL, 1, 1); | @@ -1554,7 +1555,7 @@ class RenderWebGL extends EventEmitter { | 1554 | 1555 | const projection = twgl.m4.ortho(bounds.left, bounds.right, bounds.top, bounds.bottom, -1, 1); | 1555 | 1556 |
| 1556 | 1557 | // Draw the stamped sprite onto the PenSkin's framebuffer. | 1557 | | - this._drawThese([stampID], ShaderManager.DRAW_MODE.stamp, projection, {ignoreVisibility: true}); | | 1558 | + this._drawThese([stampID], ShaderManager.DRAW_MODE.default, projection, {ignoreVisibility: true}); | 1558 | 1559 | skin._silhouetteDirty = true; | 1559 | 1560 | } | 1560 | 1561 |
| @@ -1744,14 +1745,6 @@ class RenderWebGL extends EventEmitter { | 1744 | 1745 | } | 1745 | 1746 |
| 1746 | 1747 | twgl.setUniforms(currentShader, uniforms); | 1747 | | - | 1748 | | - /* adjust blend function for this skin */ | 1749 | | - if (drawable.skin.hasPremultipliedAlpha){ | 1750 | | - gl.blendFuncSeparate(gl.ONE, gl.ONE_MINUS_SRC_ALPHA, gl.ONE, gl.ONE_MINUS_SRC_ALPHA); | 1751 | | - } else { | 1752 | | - gl.blendFuncSeparate(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA, gl.ONE, gl.ONE_MINUS_SRC_ALPHA); | 1753 | | - } | 1754 | | - | 1755 | 1748 | twgl.drawBufferInfo(gl, this._bufferInfo, gl.TRIANGLES); | 1756 | 1749 | } | 1757 | 1750 |
| @@ -1903,12 +1896,10 @@ class RenderWebGL extends EventEmitter { | 1903 | 1896 | */ | 1904 | 1897 | Drawable.sampleColor4b(vec, drawables[index].drawable, __blendColor); | 1905 | 1898 | // if we are fully transparent, go to the next one "down" | 1906 | | - const sampleAlpha = __blendColor[3] / 255; | 1907 | | - // premultiply alpha | 1908 | | - dst[0] += __blendColor[0] * blendAlpha * sampleAlpha; | 1909 | | - dst[1] += __blendColor[1] * blendAlpha * sampleAlpha; | 1910 | | - dst[2] += __blendColor[2] * blendAlpha * sampleAlpha; | 1911 | | - blendAlpha *= (1 - sampleAlpha); | | 1899 | + dst[0] += __blendColor[0] * blendAlpha; | | 1900 | + dst[1] += __blendColor[1] * blendAlpha; | | 1901 | + dst[2] += __blendColor[2] * blendAlpha; | | 1902 | + blendAlpha *= (1 - (__blendColor[3] / 255)); | 1912 | 1903 | } | 1913 | 1904 | // Backdrop could be transparent, so we need to go to the "clear color" of the | 1914 | 1905 | // draw scene (white) as a fallback if everything was alpha | |
Collapse file Copy file name to clipboardExpand all lines: src/SVGSkin.js +2-1Lines changed: 2 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -90,7 +90,8 @@ class SVGSkin extends Skin { | 90 | 90 | const textureOptions = { | 91 | 91 | auto: false, | 92 | 92 | wrap: this._renderer.gl.CLAMP_TO_EDGE, | 93 | | - src: textureData | | 93 | + src: textureData, | | 94 | + premultiplyAlpha: true | 94 | 95 | }; | 95 | 96 |
| 96 | 97 | const mip = twgl.createTexture(this._renderer.gl, textureOptions); | |
Collapse file Copy file name to clipboardExpand all lines: src/ShaderManager.js +1-6Lines changed: 1 addition & 6 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -171,12 +171,7 @@ ShaderManager.DRAW_MODE = { | 171 | 171 | /** | 172 | 172 | * Sample a "texture" to draw a line with caps. | 173 | 173 | */ | 174 | | - lineSample: 'lineSample', | 175 | | - | 176 | | - /** | 177 | | - * Draw normally except for pre-multiplied alpha | 178 | | - */ | 179 | | - stamp: 'stamp' | | 174 | + lineSample: 'lineSample' | 180 | 175 | }; | 181 | 176 |
| 182 | 177 | module.exports = ShaderManager; |
Collapse file Copy file name to clipboardExpand all lines: src/Silhouette.js +66-5Lines changed: 66 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -39,13 +39,38 @@ const __cornerWork = [ | 39 | 39 |
| 40 | 40 | /** | 41 | 41 | * Get the color from a given silhouette at an x/y local texture position. | | 42 | + * Multiply color values by alpha for proper blending. | 42 | 43 | * @param {Silhouette} The silhouette to sample. | 43 | 44 | * @param {number} x X position of texture (0-1). | 44 | 45 | * @param {number} y Y position of texture (0-1). | 45 | 46 | * @param {Uint8ClampedArray} dst A color 4b space. | 46 | 47 | * @return {Uint8ClampedArray} The dst vector. | 47 | 48 | */ | 48 | 49 | const getColor4b = ({_width: width, _height: height, _colorData: data}, x, y, dst) => { | | 50 | + // 0 if outside bouds, otherwise read from data. | | 51 | + if (x >= width || y >= height || x < 0 || y < 0) { | | 52 | + return dst.fill(0); | | 53 | + } | | 54 | + const offset = ((y * width) + x) * 4; | | 55 | + // premultiply alpha | | 56 | + const alpha = data[offset + 3] / 255; | | 57 | + dst[0] = data[offset] * alpha; | | 58 | + dst[1] = data[offset + 1] * alpha; | | 59 | + dst[2] = data[offset + 2] * alpha; | | 60 | + dst[3] = data[offset + 3]; | | 61 | + return dst; | | 62 | +}; | | 63 | + | | 64 | +/** | | 65 | + * Get the color from a given silhouette at an x/y local texture position. | | 66 | + * Do not multiply color values by alpha, as it has already been done. | | 67 | + * @param {Silhouette} The silhouette to sample. | | 68 | + * @param {number} x X position of texture (0-1). | | 69 | + * @param {number} y Y position of texture (0-1). | | 70 | + * @param {Uint8ClampedArray} dst A color 4b space. | | 71 | + * @return {Uint8ClampedArray} The dst vector. | | 72 | + */ | | 73 | +const getPremultipliedColor4b = ({_width: width, _height: height, _colorData: data}, x, y, dst) => { | 49 | 74 | // 0 if outside bouds, otherwise read from data. | 50 | 75 | if (x >= width || y >= height || x < 0 || y < 0) { | 51 | 76 | return dst.fill(0); | @@ -78,9 +103,45 @@ class Silhouette { | 78 | 103 | */ | 79 | 104 | this._colorData = null; | 80 | 105 |
| | 106 | + /** | | 107 | + * Whether or not the color data is premultiplied with its alpha channel. | | 108 | + * If it isn't, it will be multiplied here. | | 109 | + * @type {boolean} | | 110 | + */ | | 111 | + this._isPremultiplied = false; | | 112 | + | | 113 | + // By default, silhouettes are assumed not to contain premultiplied image data, | | 114 | + // so when we get a color, we want to multiply it by its alpha channel. | | 115 | + // Point `_getColor` to the version of the function that multiplies. | | 116 | + this._getColor = getColor4b; | | 117 | + | 81 | 118 | this.colorAtNearest = this.colorAtLinear = (_, dst) => dst.fill(0); | 82 | 119 | } | 83 | 120 |
| | 121 | + /** | | 122 | + * @returns {boolean} true if the silhouette color data is premultiplied, false if not. | | 123 | + */ | | 124 | + get premultiplied () { | | 125 | + return this._isPremultiplied; | | 126 | + } | | 127 | + | | 128 | + /** | | 129 | + * Set the alpha premultiplication state of this silhouette, to ensure proper color values are returned. | | 130 | + * If set to true, the silhouette will assume it is being set with premultiplied color data, | | 131 | + * and will not multiply color values by alpha. | | 132 | + * If set to false, it will multiply color values by alpha. | | 133 | + * @param {boolean} isPremultiplied Whether this silhouette will be populated with premultiplied color data. | | 134 | + */ | | 135 | + set premultiplied (isPremultiplied) { | | 136 | + this._isPremultiplied = isPremultiplied; | | 137 | + | | 138 | + if (isPremultiplied) { | | 139 | + this._getColor = getPremultipliedColor4b; | | 140 | + } else { | | 141 | + this._getColor = getColor4b; | | 142 | + } | | 143 | + } | | 144 | + | 84 | 145 | /** | 85 | 146 | * Update this silhouette with the bitmapData for a skin. | 86 | 147 | * @param {*} bitmapData An image, canvas or other element that the skin | @@ -124,7 +185,7 @@ class Silhouette { | 124 | 185 | * @returns {Uint8ClampedArray} dst | 125 | 186 | */ | 126 | 187 | colorAtNearest (vec, dst) { | 127 | | - return getColor4b( | | 188 | + return this._getColor( | 128 | 189 | this, | 129 | 190 | Math.floor(vec[0] * (this._width - 1)), | 130 | 191 | Math.floor(vec[1] * (this._height - 1)), | @@ -151,10 +212,10 @@ class Silhouette { | 151 | 212 | const xFloor = Math.floor(x); | 152 | 213 | const yFloor = Math.floor(y); | 153 | 214 |
| 154 | | - const x0y0 = getColor4b(this, xFloor, yFloor, __cornerWork[0]); | 155 | | - const x1y0 = getColor4b(this, xFloor + 1, yFloor, __cornerWork[1]); | 156 | | - const x0y1 = getColor4b(this, xFloor, yFloor + 1, __cornerWork[2]); | 157 | | - const x1y1 = getColor4b(this, xFloor + 1, yFloor + 1, __cornerWork[3]); | | 215 | + const x0y0 = this._getColor(this, xFloor, yFloor, __cornerWork[0]); | | 216 | + const x1y0 = this._getColor(this, xFloor + 1, yFloor, __cornerWork[1]); | | 217 | + const x0y1 = this._getColor(this, xFloor, yFloor + 1, __cornerWork[2]); | | 218 | + const x1y1 = this._getColor(this, xFloor + 1, yFloor + 1, __cornerWork[3]); | 158 | 219 |
| 159 | 220 | dst[0] = (x0y0[0] * x0D * y0D) + (x0y1[0] * x0D * y1D) + (x1y0[0] * x1D * y0D) + (x1y1[0] * x1D * y1D); | 160 | 221 | dst[1] = (x0y0[1] * x0D * y0D) + (x0y1[1] * x0D * y1D) + (x1y0[1] * x1D * y0D) + (x1y1[1] * x1D * y1D); | |
Collapse file Copy file name to clipboardExpand all lines: src/Skin.js +20Lines changed: 20 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -33,6 +33,9 @@ class Skin extends EventEmitter { | 33 | 33 | /** @type {Vec3} */ | 34 | 34 | this._rotationCenter = twgl.v3.create(0, 0); | 35 | 35 |
| | 36 | + /** @type {WebGLTexture} */ | | 37 | + this._texture = null; | | 38 | + | 36 | 39 | /** | 37 | 40 | * The uniforms to be used by the vertex and pixel shaders. | 38 | 41 | * Some of these are used by other parts of the renderer as well. | @@ -171,6 +174,23 @@ class Skin extends EventEmitter { | 171 | 174 | */ | 172 | 175 | updateSilhouette () {} | 173 | 176 |
| | 177 | + /** | | 178 | + * Set this skin's texture to the given image. | | 179 | + * @param {ImageData|HTMLCanvasElement} textureData - The canvas or image data to set the texture to. | | 180 | + */ | | 181 | + _setTexture (textureData) { | | 182 | + const gl = this._renderer.gl; | | 183 | + | | 184 | + gl.bindTexture(gl.TEXTURE_2D, this._texture); | | 185 | + // Premultiplied alpha is necessary for proper blending. | | 186 | + // See http://www.realtimerendering.com/blog/gpus-prefer-premultiplication/ | | 187 | + gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, true); | | 188 | + gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, textureData); | | 189 | + gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, false); | | 190 | + | | 191 | + this._silhouette.update(textureData); | | 192 | + } | | 193 | + | 174 | 194 | /** | 175 | 195 | * Set the contents of this skin to an empty skin. | 176 | 196 | * @fires Skin.event:WasAltered | |
|