浏览代码

allowing for adjustable gamma factor in WebGLRenderer.

Ben Houston 10 年之前
父节点
当前提交
1c24e4d1ad
共有 3 个文件被更改,包括 34 次插入28 次删除
  1. 13 8
      src/math/Color.js
  2. 19 20
      src/renderers/WebGLRenderer.js
  3. 2 0
      src/renderers/webgl/WebGLProgram.js

+ 13 - 8
src/math/Color.js

@@ -173,21 +173,26 @@ THREE.Color.prototype = {
 
 
 	},
 	},
 
 
-	copyGammaToLinear: function ( color ) {
+	copyGammaToLinear: function ( color, gammaFactor ) {
 
 
-		this.r = color.r * color.r;
-		this.g = color.g * color.g;
-		this.b = color.b * color.b;
+		gammaFactor = gammaFactor || 2.0;
+
+		this.r = Math.pow( color.r, gammaFactor );
+		this.g = Math.pow( color.g, gammaFactor );
+		this.b = Math.pow( color.b, gammaFactor );
 
 
 		return this;
 		return this;
 
 
 	},
 	},
 
 
-	copyLinearToGamma: function ( color ) {
+	copyLinearToGamma: function ( color, gammaFactor ) {
+
+		gammaFactor = gammaFactor || 2.0;
 
 
-		this.r = Math.sqrt( color.r );
-		this.g = Math.sqrt( color.g );
-		this.b = Math.sqrt( color.b );
+		var safeInverse = ( gammaFactor > 0 ) ? ( 1.0 / gammaFactor ) : 1.0;
+		this.r = Math.pow( color.r, safeInverse );
+		this.g = Math.pow( color.g, safeInverse );
+		this.b = Math.pow( color.b, safeInverse );
 
 
 		return this;
 		return this;
 
 

+ 19 - 20
src/renderers/WebGLRenderer.js

@@ -58,6 +58,7 @@ THREE.WebGLRenderer = function ( parameters ) {
 
 
 	// physically based shading
 	// physically based shading
 
 
+	this.gammaFactor = 2.0;	// for backwards compatibility
 	this.gammaInput = false;
 	this.gammaInput = false;
 	this.gammaOutput = false;
 	this.gammaOutput = false;
 
 
@@ -4524,7 +4525,7 @@ THREE.WebGLRenderer = function ( parameters ) {
 
 
 		if ( _this.gammaInput ) {
 		if ( _this.gammaInput ) {
 
 
-			uniforms.diffuse.value.copyGammaToLinear( material.color );
+			uniforms.diffuse.value.copyGammaToLinear( material.color, this.gammaFactor );
 
 
 		} else {
 		} else {
 
 
@@ -4658,9 +4659,9 @@ THREE.WebGLRenderer = function ( parameters ) {
 
 
 		if ( _this.gammaInput ) {
 		if ( _this.gammaInput ) {
 
 
-			uniforms.ambient.value.copyGammaToLinear( material.ambient );
-			uniforms.emissive.value.copyGammaToLinear( material.emissive );
-			uniforms.specular.value.copyGammaToLinear( material.specular );
+			uniforms.ambient.value.copyGammaToLinear( material.ambient, this.gammaFactor );
+			uniforms.emissive.value.copyGammaToLinear( material.emissive, this.gammaFactor );
+			uniforms.specular.value.copyGammaToLinear( material.specular, this.gammaFactor );
 
 
 		} else {
 		} else {
 
 
@@ -4682,8 +4683,8 @@ THREE.WebGLRenderer = function ( parameters ) {
 
 
 		if ( _this.gammaInput ) {
 		if ( _this.gammaInput ) {
 
 
-			uniforms.ambient.value.copyGammaToLinear( material.ambient );
-			uniforms.emissive.value.copyGammaToLinear( material.emissive );
+			uniforms.ambient.value.copyGammaToLinear( material.ambient, this.gammaFactor );
+			uniforms.emissive.value.copyGammaToLinear( material.emissive, this.gammaFactor );
 
 
 		} else {
 		} else {
 
 
@@ -5157,11 +5158,11 @@ THREE.WebGLRenderer = function ( parameters ) {
 
 
 	//
 	//
 
 
-	function setColorGamma( array, offset, color, intensitySq ) {
+	function setColorGamma( array, offset, color, intensity ) {
 
 
-		array[ offset ]     = color.r * color.r * intensitySq;
-		array[ offset + 1 ] = color.g * color.g * intensitySq;
-		array[ offset + 2 ] = color.b * color.b * intensitySq;
+		array[ offset ]     = Math.pow( color.r * intensity, this.gammaFactor );
+		array[ offset + 1 ] = Math.pow( color.g * intensity, this.gammaFactor );
+		array[ offset + 2 ] = Math.pow( color.b * intensity, this.gammaFactor );
 
 
 	}
 	}
 
 
@@ -5233,9 +5234,9 @@ THREE.WebGLRenderer = function ( parameters ) {
 
 
 				if ( _this.gammaInput ) {
 				if ( _this.gammaInput ) {
 
 
-					r += color.r * color.r;
-					g += color.g * color.g;
-					b += color.b * color.b;
+					r += Math.pow( color.r, this.gammaFactor );
+					g += Math.pow( color.g, this.gammaFactor );
+					b += Math.pow( color.b, this.gammaFactor );
 
 
 				} else {
 				} else {
 
 
@@ -5264,7 +5265,7 @@ THREE.WebGLRenderer = function ( parameters ) {
 
 
 				if ( _this.gammaInput ) {
 				if ( _this.gammaInput ) {
 
 
-					setColorGamma( dirColors, dirOffset, color, intensity * intensity );
+					setColorGamma( dirColors, dirOffset, color, intensity );
 
 
 				} else {
 				} else {
 
 
@@ -5284,7 +5285,7 @@ THREE.WebGLRenderer = function ( parameters ) {
 
 
 				if ( _this.gammaInput ) {
 				if ( _this.gammaInput ) {
 
 
-					setColorGamma( pointColors, pointOffset, color, intensity * intensity );
+					setColorGamma( pointColors, pointOffset, color, intensity );
 
 
 				} else {
 				} else {
 
 
@@ -5312,7 +5313,7 @@ THREE.WebGLRenderer = function ( parameters ) {
 
 
 				if ( _this.gammaInput ) {
 				if ( _this.gammaInput ) {
 
 
-					setColorGamma( spotColors, spotOffset, color, intensity * intensity );
+					setColorGamma( spotColors, spotOffset, color, intensity );
 
 
 				} else {
 				} else {
 
 
@@ -5361,10 +5362,8 @@ THREE.WebGLRenderer = function ( parameters ) {
 
 
 				if ( _this.gammaInput ) {
 				if ( _this.gammaInput ) {
 
 
-					intensitySq = intensity * intensity;
-
-					setColorGamma( hemiSkyColors, hemiOffset, skyColor, intensitySq );
-					setColorGamma( hemiGroundColors, hemiOffset, groundColor, intensitySq );
+					setColorGamma( hemiSkyColors, hemiOffset, skyColor, intensity );
+					setColorGamma( hemiGroundColors, hemiOffset, groundColor, intensity );
 
 
 				} else {
 				} else {
 
 

+ 2 - 0
src/renderers/webgl/WebGLProgram.js

@@ -165,6 +165,7 @@ THREE.WebGLProgram = ( function () {
 
 
 				_this.gammaInput ? "#define GAMMA_INPUT" : "",
 				_this.gammaInput ? "#define GAMMA_INPUT" : "",
 				_this.gammaOutput ? "#define GAMMA_OUTPUT" : "",
 				_this.gammaOutput ? "#define GAMMA_OUTPUT" : "",
+				"#define GAMMA_FACTOR " + this.renderer.gammaFactor,
 
 
 				"#define MAX_DIR_LIGHTS " + parameters.maxDirLights,
 				"#define MAX_DIR_LIGHTS " + parameters.maxDirLights,
 				"#define MAX_POINT_LIGHTS " + parameters.maxPointLights,
 				"#define MAX_POINT_LIGHTS " + parameters.maxPointLights,
@@ -279,6 +280,7 @@ THREE.WebGLProgram = ( function () {
 
 
 				_this.gammaInput ? "#define GAMMA_INPUT" : "",
 				_this.gammaInput ? "#define GAMMA_INPUT" : "",
 				_this.gammaOutput ? "#define GAMMA_OUTPUT" : "",
 				_this.gammaOutput ? "#define GAMMA_OUTPUT" : "",
+				"#define GAMMA_FACTOR " + this.renderer.gammaFactor,
 
 
 				( parameters.useFog && parameters.fog ) ? "#define USE_FOG" : "",
 				( parameters.useFog && parameters.fog ) ? "#define USE_FOG" : "",
 				( parameters.useFog && parameters.fogExp ) ? "#define FOG_EXP2" : "",
 				( parameters.useFog && parameters.fogExp ) ? "#define FOG_EXP2" : "",