Skip to content

Commit

Permalink
WebGPURenderer: Fix premultiplied alpha with clear colors. (#29538)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mugen87 authored Oct 2, 2024
1 parent 1e67d7e commit 47fb8fe
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 8 deletions.
14 changes: 12 additions & 2 deletions src/renderers/common/Background.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,25 @@ class Background extends DataMap {

if ( renderer.autoClear === true || forceClear === true ) {

_clearColor.multiplyScalar( _clearColor.a );

const clearColorValue = renderContext.clearColorValue;

clearColorValue.r = _clearColor.r;
clearColorValue.g = _clearColor.g;
clearColorValue.b = _clearColor.b;
clearColorValue.a = _clearColor.a;

// premultiply alpha

if ( renderer.backend.isWebGLBackend === true || renderer.alpha === true ) {

clearColorValue.r *= clearColorValue.a;
clearColorValue.g *= clearColorValue.a;
clearColorValue.b *= clearColorValue.a;

}

//

renderContext.depthClearValue = renderer._clearDepth;
renderContext.stencilClearValue = renderer._clearStencil;

Expand Down
30 changes: 24 additions & 6 deletions src/renderers/webgl-fallback/WebGLBackend.js
Original file line number Diff line number Diff line change
Expand Up @@ -433,9 +433,17 @@ class WebGLBackend extends Backend {

if ( descriptor === null ) {

const clearColor = this.getClearColor();

// premultiply alpha

clearColor.r *= clearColor.a;
clearColor.g *= clearColor.a;
clearColor.b *= clearColor.a;

descriptor = {
textures: null,
clearColorValue: this.getClearColor()
clearColorValue: clearColor
};

}
Expand All @@ -450,13 +458,23 @@ class WebGLBackend extends Backend {

if ( clear !== 0 ) {

const clearColor = descriptor.clearColorValue || this.getClearColor();
let clearColor;

// premultiply alpha
if ( descriptor.clearColorValue ) {

clearColor.r *= clearColor.a;
clearColor.g *= clearColor.a;
clearColor.b *= clearColor.a;
clearColor = descriptor.clearColorValue;

} else {

clearColor = this.getClearColor();

// premultiply alpha

clearColor.r *= clearColor.a;
clearColor.g *= clearColor.a;
clearColor.b *= clearColor.a;

}

if ( depth ) this.state.setDepthMask( true );

Expand Down

0 comments on commit 47fb8fe

Please sign in to comment.