Просмотр исходного кода

WebGPURenderer: Simplify clear color generation. (#30486)

Michael Herzog 11 месяцев назад
Родитель
Сommit
cdd7c6c25a

+ 0 - 10
src/renderers/common/Renderer.js

@@ -1919,16 +1919,6 @@ class Renderer {
 			// #30329
 			renderContext.clearColorValue = this.backend.getClearColor();
 
-			// premultiply alpha
-
-			if ( this.backend.isWebGLBackend === true || this.alpha === true ) {
-
-				renderContext.clearColorValue.r *= renderContext.clearColorValue.a;
-				renderContext.clearColorValue.g *= renderContext.clearColorValue.a;
-				renderContext.clearColorValue.b *= renderContext.clearColorValue.a;
-
-			}
-
 		}
 
 		this.backend.clear( color, depth, stencil, renderContext );

+ 21 - 12
src/renderers/webgl-fallback/WebGLBackend.js

@@ -706,6 +706,27 @@ class WebGLBackend extends Backend {
 
 	}
 
+	/**
+	 * Returns the clear color and alpha into a single
+	 * color object.
+	 *
+	 * @return {Color4} The clear color.
+	 */
+	getClearColor() {
+
+		const clearColor = super.getClearColor();
+
+		// Since the canvas is always created with alpha: true,
+		// WebGL must always premultiply the clear color.
+
+		clearColor.r *= clearColor.a;
+		clearColor.g *= clearColor.a;
+		clearColor.b *= clearColor.a;
+
+		return clearColor;
+
+	}
+
 	/**
 	 * Performs a clear operation.
 	 *
@@ -723,12 +744,6 @@ class WebGLBackend extends Backend {
 
 			const clearColor = this.getClearColor();
 
-			// premultiply alpha
-
-			clearColor.r *= clearColor.a;
-			clearColor.g *= clearColor.a;
-			clearColor.b *= clearColor.a;
-
 			descriptor = {
 				textures: null,
 				clearColorValue: clearColor
@@ -756,12 +771,6 @@ class WebGLBackend extends Backend {
 
 				clearColor = this.getClearColor();
 
-				// premultiply alpha
-
-				clearColor.r *= clearColor.a;
-				clearColor.g *= clearColor.a;
-				clearColor.b *= clearColor.a;
-
 			}
 
 			const clearDepth = renderer.getClearDepth();

+ 25 - 14
src/renderers/webgpu/WebGPUBackend.js

@@ -818,6 +818,30 @@ class WebGPUBackend extends Backend {
 
 	}
 
+	/**
+	 * Returns the clear color and alpha into a single
+	 * color object.
+	 *
+	 * @return {Color4} The clear color.
+	 */
+	getClearColor() {
+
+		const clearColor = super.getClearColor();
+
+		// only premultiply alpha when alphaMode is "premultiplied"
+
+		if ( this.renderer.alpha === true ) {
+
+			clearColor.r *= clearColor.a;
+			clearColor.g *= clearColor.a;
+			clearColor.b *= clearColor.a;
+
+		}
+
+		return clearColor;
+
+	}
+
 	/**
 	 * Performs a clear operation.
 	 *
@@ -842,20 +866,7 @@ class WebGPUBackend extends Backend {
 		if ( color ) {
 
 			const clearColor = this.getClearColor();
-
-			if ( this.renderer.alpha === true ) {
-
-				// premultiply alpha
-
-				const a = clearColor.a;
-
-				clearValue = { r: clearColor.r * a, g: clearColor.g * a, b: clearColor.b * a, a: a };
-
-			} else {
-
-				clearValue = { r: clearColor.r, g: clearColor.g, b: clearColor.b, a: clearColor.a };
-
-			}
+			clearValue = { r: clearColor.r, g: clearColor.g, b: clearColor.b, a: clearColor.a };
 
 		}
 

粤ICP备19079148号