Skip to content

Commit 1463eea

Browse files
committed
Premultiply background color to fix blending issues
1 parent 7bea69d commit 1463eea

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

src/RenderWebGL.js

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -422,14 +422,16 @@ class RenderWebGL extends EventEmitter {
422422
setBackgroundColor (red, green, blue, alpha=1) {
423423
this.dirty = true;
424424

425-
this._backgroundColor4f[0] = red;
426-
this._backgroundColor4f[1] = green;
427-
this._backgroundColor4f[2] = blue;
425+
// WebGL will want the color to be pre-multiplied.
426+
427+
this._backgroundColor4f[0] = red * alpha;
428+
this._backgroundColor4f[1] = green * alpha;
429+
this._backgroundColor4f[2] = blue * alpha;
428430
this._backgroundColor4f[3] = alpha;
429431

430-
this._backgroundColor3b[0] = red * 255;
431-
this._backgroundColor3b[1] = green * 255;
432-
this._backgroundColor3b[2] = blue * 255;
432+
this._backgroundColor3b[0] = red * alpha * 255;
433+
this._backgroundColor3b[1] = green * alpha * 255;
434+
this._backgroundColor3b[2] = blue * alpha * 255;
433435

434436
}
435437

@@ -915,7 +917,13 @@ class RenderWebGL extends EventEmitter {
915917

916918
twgl.bindFramebufferInfo(gl, null);
917919
gl.viewport(0, 0, gl.canvas.width, gl.canvas.height);
918-
gl.clearColor(...this._backgroundColor4f);
920+
// Context expects pre-multiplied colors.
921+
gl.clearColor(
922+
this._backgroundColor4f[0],
923+
this._backgroundColor4f[1],
924+
this._backgroundColor4f[2],
925+
this._backgroundColor4f[3]
926+
);
919927
gl.clear(gl.COLOR_BUFFER_BIT);
920928

921929
const snapshotRequested = this._snapshotCallbacks.length > 0;
@@ -1557,7 +1565,13 @@ class RenderWebGL extends EventEmitter {
15571565
gl.viewport(0, 0, bounds.width, bounds.height);
15581566
const projection = twgl.m4.ortho(bounds.left, bounds.right, bounds.top, bounds.bottom, -1, 1);
15591567

1560-
gl.clearColor(...this._backgroundColor4f);
1568+
// Context expects pre-multiplied colors.
1569+
gl.clearColor(
1570+
this._backgroundColor4f[0],
1571+
this._backgroundColor4f[1],
1572+
this._backgroundColor4f[2],
1573+
this._backgroundColor4f[3]
1574+
);
15611575
gl.clear(gl.COLOR_BUFFER_BIT);
15621576
this._drawThese(this._drawList, ShaderManager.DRAW_MODE.default, projection);
15631577

0 commit comments

Comments
 (0)