Browse Source

make encodings.glsl more reusable.

Ben Houston 9 years ago
parent
commit
3ca76ecd4c

+ 1 - 1
src/renderers/shaders/ShaderChunk/emissivemap_fragment.glsl

@@ -2,7 +2,7 @@
 
 
 	vec4 emissiveColor = texture2D( emissiveMap, vUv );
 	vec4 emissiveColor = texture2D( emissiveMap, vUv );
 
 
-	emissiveColor.rgb = texelDecode( emissiveColor, emissiveMapEncoding ).rgb;
+	emissiveColor.rgb = EncodingToLinear( emissiveColor, emissiveMapEncoding ).rgb;
 
 
 	totalEmissiveLight *= emissiveColor.rgb;
 	totalEmissiveLight *= emissiveColor.rgb;
 
 

+ 69 - 52
src/renderers/shaders/ShaderChunk/encodings.glsl

@@ -9,79 +9,96 @@
 #define ENCODING_RGBM16 3005
 #define ENCODING_RGBM16 3005
 //#define ENCODING_RGBM16 3007
 //#define ENCODING_RGBM16 3007
 
 
-vec4 texelDecode( in vec4 encodedTexel, in int encoding ) {
+vec4 LinearToLinear( in vec4 value ) {
+  return value;
+}
 
 
-  // Q: should we use a switch statement here instead of a set of ifs?
+vec4 sRGBToLinear( in vec4 value ) {
+  return vec4( pow( value.xyz, vec3( float( GAMMA_FACTOR ) ) ), value.w );
+}
 
 
-  if( encoding == ENCODING_Linear ) {
-    return encodedTexel;
-  }
+vec4 RGBEToLinear( in vec4 value ) {
+  return vec4( value.xyz * exp2( value.w*256.0 - 128.0 ), 1.0 );
+}
 
 
-  if( encoding == ENCODING_sRGB ) {
-    return vec4( pow( encodedTexel.xyz, vec3( float( GAMMA_FACTOR ) ) ), encodedTexel.w );
-  }
+vec4 RGBM7ToLinear( in vec4 value ) {
+  return vec4( value.xyz * value.w * 7.0, 1.0 );
+}
 
 
-  if( encoding == ENCODING_RGBE ) {
-    return vec4( encodedTexel.xyz * exp2( encodedTexel.w*256.0 - 128.0 ), 1.0 );
-  }
+vec4 RGBM16ToLinear( in vec4 value ) {
+  return vec4( value.xyz * value.w * 16.0, 1.0 );
+}
 
 
-  // TODO, see here http://graphicrants.blogspot.ca/2009/04/rgbm-color-encoding.html
-  //if( encoding == ENCODING_LogLuv ) {
-  //}
+vec4 LinearTosRGB( in vec4 value ) {
+  return vec4( pow( value.xyz, vec3( 1.0 / float( GAMMA_FACTOR ) ) ), value.w );
+}
 
 
-  if( encoding == ENCODING_RGBM7 ) {
-    return vec4( encodedTexel.xyz * encodedTexel.w * 7.0, 1.0 );
-  }
+vec4 LinearToRGBE( in vec4 value ) {
+  float maxComponent = max(max(value.r, value.g), value.b );
+  float fExp = ceil( log2(maxComponent) );
+  return vec4( value.rgb / exp2(fExp), (fExp + 128.0) / 255.0 );
+}
 
 
-  if( encoding == ENCODING_RGBM16 ) {
-    return vec4( encodedTexel.xyz * encodedTexel.w * 16.0, 1.0 );
-  }
+vec4 EncodingToLinear( in vec4 value, in int fromEncoding ) {
+
+  switch( fromEncoding ) {
 
 
-  // TODO
-  //if( encoding == ENCODING_RGBD ) {
-  //}
+    case ENCODING_Linear:
+      return value;
 
 
-  // return red when encoding not supported
-  return vec4( 1.0, 0.0, 0.0, 1.0 );
+    case ENCODING_sRGB:
+      return LinearTosRGB( value );
+
+    //case ENCODING_LogLuv:
+    //  return LinearToLogLuv( value );
+
+    case ENCODING_RGBE:
+      return LinearToRGBE( value );
+
+    case ENCODING_RGBM7:
+      return LinearToRGBM7( value );
+
+    case ENCODING_RGBM16:
+      return LinearToRGBM16( value );
+
+    //case ENCODING_RGBD:
+    //  return LinearToRGBD( value );
+
+    default:
+      return vec4( 1.0, 0.0, 0.0, 1.0 );
+
+  }
 
 
 }
 }
 
 
-vec4 texelEncode( in vec4 linearRgba, in int encoding )
+vec4 LinearToEncoding( in vec4 value, in int toEncoding )
 {
 {
+  switch( toEncoding ) {
 
 
-  // Q: should we use a switch statement here instead of a set of ifs?
+    case ENCODING_Linear:
+      return value;
 
 
-  if( encoding == ENCODING_Linear ) {
-    return linearRgba;
-  }
+    case ENCODING_sRGB:
+      return sRGBToLinear( value );
 
 
-  if( encoding == ENCODING_sRGB ) {
-    return vec4( pow( linearRgba.xyz, vec3( 1.0 / float( GAMMA_FACTOR ) ) ), linearRgba.w );
-  }
+    //case ENCODING_LogLuv:
+    //  return LogLuvToLinear( value );
 
 
-  if( encoding == ENCODING_RGBE ) {
-    float maxComponent = max(max(linearRgba.r, linearRgba.g), linearRgba.b );
-    float fExp = ceil( log2(maxComponent) );
-    return vec4( linearRgba.rgb / exp2(fExp), (fExp + 128.0) / 255.0 );
-  }
+    case ENCODING_RGBE:
+      return RGBEToLinear( value );
 
 
-  // TODO, see here http://graphicrants.blogspot.ca/2009/04/rgbm-color-encoding.html
-  //if( encoding == ENCODING_LogLuv ) {
-  //}
+    //case ENCODING_RGBM7:
+    //  return RGBM7ToLinear( value );
 
 
-  // TODO
-  //if( encoding == ENCODING_RGBM7 ) {
-  //}
+    //case ENCODING_RGBM16:
+    //  return RGBM16ToLinear( value );
 
 
-  // TODO
-  //if( encoding == ENCODING_RGBM16 ) {
-  //}
+    //case ENCODING_RGBD:
+    //  return RGBDToLinear( value );
 
 
-  // TODO
-  //if( encoding == ENCODING_RGBD ) {
-  //}
+    default:
+      return vec4( 1.0, 0.0, 0.0, 1.0 );
 
 
-  // return red when encoding not supported
-  return vec4( 1.0, 0.0, 0.0, 1.0 );
+  }
 
 
 }
 }

+ 1 - 1
src/renderers/shaders/ShaderChunk/envmap_fragment.glsl

@@ -43,7 +43,7 @@
 		vec4 envColor = texture2D( envMap, reflectView.xy * 0.5 + 0.5 );
 		vec4 envColor = texture2D( envMap, reflectView.xy * 0.5 + 0.5 );
 	#endif
 	#endif
 
 
-	envColor = texelDecode( envColor, envMapEncoding );
+	envColor = EncodingToLinear( envColor, envMapEncoding );
 
 
 	#ifdef ENVMAP_BLENDING_MULTIPLY
 	#ifdef ENVMAP_BLENDING_MULTIPLY
 
 

+ 2 - 2
src/renderers/shaders/ShaderChunk/lights_pars.glsl

@@ -186,7 +186,7 @@
 
 
 		#endif
 		#endif
 
 
-		envMapColor.rgb = texelDecode( envMapColor, envMapEncoding ).rgb;
+		envMapColor.rgb = EncodingToLinear( envMapColor, envMapEncoding ).rgb;
 
 
 		return PI * envMapColor.rgb * envMapIntensity;
 		return PI * envMapColor.rgb * envMapIntensity;
 
 
@@ -278,7 +278,7 @@
 
 
 		#endif
 		#endif
 
 
-		envMapColor.rgb = texelDecode( envMapColor, envMapEncoding ).rgb;
+		envMapColor.rgb = EncodingToLinear( envMapColor, envMapEncoding ).rgb;
 
 
 		return envMapColor.rgb * envMapIntensity;
 		return envMapColor.rgb * envMapIntensity;
 
 

+ 1 - 1
src/renderers/shaders/ShaderChunk/map_fragment.glsl

@@ -2,7 +2,7 @@
 
 
 	vec4 texelColor = texture2D( map, vUv );
 	vec4 texelColor = texture2D( map, vUv );
 
 
-	texelColor = texelDecode( texelColor, mapEncoding );
+	texelColor = EncodingToLinear( texelColor, mapEncoding );
 	diffuseColor *= texelColor;
 	diffuseColor *= texelColor;
 
 
 #endif
 #endif