Merge pull request #418 from peabrainiac/develop

pen transparency fix
This commit is contained in:
Chris Willis-Ford 2019-04-29 17:04:15 -07:00 committed by GitHub
commit c9c780aa69
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 26 additions and 10 deletions

View File

@ -154,6 +154,13 @@ class PenSkin extends Skin {
return true; return true;
} }
/**
* @returns {boolean} true if alpha is premultiplied, false otherwise
*/
get hasPremultipliedAlpha () {
return true;
}
/** /**
* @return {Array<number>} the "native" size, in texels, of this skin. [width, height] * @return {Array<number>} the "native" size, in texels, of this skin. [width, height]
*/ */
@ -181,8 +188,9 @@ class PenSkin extends Skin {
clear () { clear () {
const gl = this._renderer.gl; const gl = this._renderer.gl;
twgl.bindFramebufferInfo(gl, this._framebuffer); twgl.bindFramebufferInfo(gl, this._framebuffer);
gl.clearColor(1, 1, 1, 0); /* Reset framebuffer to transparent black */
gl.clearColor(0, 0, 0, 0);
gl.clear(gl.COLOR_BUFFER_BIT); gl.clear(gl.COLOR_BUFFER_BIT);
const ctx = this._canvas.getContext('2d'); const ctx = this._canvas.getContext('2d');
@ -598,7 +606,7 @@ class PenSkin extends Skin {
this._silhouetteBuffer = twgl.createFramebufferInfo(gl, [{format: gl.RGBA}], width, height); this._silhouetteBuffer = twgl.createFramebufferInfo(gl, [{format: gl.RGBA}], width, height);
} }
gl.clearColor(1, 1, 1, 0); gl.clearColor(0, 0, 0, 0);
gl.clear(gl.COLOR_BUFFER_BIT); gl.clear(gl.COLOR_BUFFER_BIT);
this._silhouetteDirty = true; this._silhouetteDirty = true;

View File

@ -1620,7 +1620,14 @@ class RenderWebGL extends EventEmitter {
} }
twgl.setUniforms(currentShader, uniforms); twgl.setUniforms(currentShader, uniforms);
/* adjust blend function for this skin */
if (drawable.skin.hasPremultipliedAlpha){
gl.blendFuncSeparate(gl.ONE, gl.ONE_MINUS_SRC_ALPHA, gl.ONE, gl.ONE_MINUS_SRC_ALPHA);
} else {
gl.blendFuncSeparate(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA, gl.ONE, gl.ONE_MINUS_SRC_ALPHA);
}
twgl.drawBufferInfo(gl, this._bufferInfo, gl.TRIANGLES); twgl.drawBufferInfo(gl, this._bufferInfo, gl.TRIANGLES);
} }

View File

@ -76,6 +76,13 @@ class Skin extends EventEmitter {
return false; return false;
} }
/**
* @returns {boolean} true if alpha is premultiplied, false otherwise
*/
get hasPremultipliedAlpha () {
return false;
}
/** /**
* @return {int} the unique ID for this Skin. * @return {int} the unique ID for this Skin.
*/ */

View File

@ -194,12 +194,6 @@ void main()
discard; discard;
} }
#endif // DRAW_MODE_colorMask #endif // DRAW_MODE_colorMask
// WebGL defaults to premultiplied alpha
#ifndef DRAW_MODE_stamp
gl_FragColor.rgb *= gl_FragColor.a;
#endif // DRAW_MODE_stamp
#endif // DRAW_MODE_silhouette #endif // DRAW_MODE_silhouette
#else // DRAW_MODE_lineSample #else // DRAW_MODE_lineSample