瀏覽代碼

Merge pull request #20983 from gkjohnson/pmrem-background-update

PMREMGenerator: Improve handling of background and clear color
Mr.doob 4 年之前
父節點
當前提交
5fe52c1d71
共有 1 個文件被更改,包括 35 次插入10 次删除
  1. 35 10
      src/extras/PMREMGenerator.js

+ 35 - 10
src/extras/PMREMGenerator.js

@@ -54,6 +54,7 @@ const ENCODINGS = {
 const _flatCamera = /*@__PURE__*/ new OrthographicCamera();
 const { _lodPlanes, _sizeLods, _sigmas } = /*@__PURE__*/ _createPlanes();
 const _clearColor = /*@__PURE__*/ new Color();
+const _backgroundColor = /*@__PURE__*/ new Color();
 let _oldTarget = null;
 
 // Golden Ratio
@@ -86,6 +87,17 @@ const _axisDirections = [
  * interpolate diffuse lighting while limiting sampling computation.
  */
 
+function convertLinearToRGBE( color ) {
+
+	const maxComponent = Math.max( color.r, color.g, color.b );
+	const fExp = Math.min( Math.max( Math.ceil( Math.log2( maxComponent ) ), - 128.0 ), 127.0 );
+	color.multiplyScalar( Math.pow( 2.0, - fExp ) );
+
+	const alpha = ( fExp + 128.0 ) / 255.0;
+	return alpha;
+
+}
+
 class PMREMGenerator {
 
 	constructor( renderer ) {
@@ -261,24 +273,36 @@ class PMREMGenerator {
 		const toneMapping = renderer.toneMapping;
 		renderer.getClearColor( _clearColor );
 		const clearAlpha = renderer.getClearAlpha();
+		const originalBackground = scene.background;
 
 		renderer.toneMapping = NoToneMapping;
 		renderer.outputEncoding = LinearEncoding;
 
-		let background = scene.background;
-		if ( background && background.isColor ) {
+		const background = scene.background;
+		if ( background ) {
+
+			if ( background.isColor ) {
+
+				_backgroundColor.copy( background ).convertSRGBToLinear();
+				scene.background = null;
 
-			background.convertSRGBToLinear();
-			// Convert linear to RGBE
-			const maxComponent = Math.max( background.r, background.g, background.b );
-			const fExp = Math.min( Math.max( Math.ceil( Math.log2( maxComponent ) ), - 128.0 ), 127.0 );
-			background = background.multiplyScalar( Math.pow( 2.0, - fExp ) );
-			const alpha = ( fExp + 128.0 ) / 255.0;
-			renderer.setClearColor( background, alpha );
-			scene.background = null;
+				const alpha = convertLinearToRGBE( _backgroundColor );
+				renderer.setClearColor( _backgroundColor );
+				renderer.setClearAlpha( alpha );
+
+			}
+
+		} else {
+
+			_backgroundColor.copy( _clearColor ).convertSRGBToLinear();
+
+			const alpha = convertLinearToRGBE( _backgroundColor );
+			renderer.setClearColor( _backgroundColor );
+			renderer.setClearAlpha( alpha );
 
 		}
 
+
 		for ( let i = 0; i < 6; i ++ ) {
 
 			const col = i % 3;
@@ -309,6 +333,7 @@ class PMREMGenerator {
 		renderer.toneMapping = toneMapping;
 		renderer.outputEncoding = outputEncoding;
 		renderer.setClearColor( _clearColor, clearAlpha );
+		scene.background = originalBackground;
 
 	}