encodings.glsl 2.3 KB
Newer Older
B
Ben Houston 已提交
1 2
// For a discussion of what this is, please read this: http://lousodrome.net/blog/light/2013/05/26/gamma-correct-and-hdr-rendering-in-a-32-bits-buffer/

3
// These encodings should have the same integer values as THREE.LinearEncoding, THREE.sRGBEncoding, etc...
4
#define ENCODING_Linear 3000
B
Ben Houston 已提交
5 6
#define ENCODING_sRGB   3001
#define ENCODING_RGBE   3002
7 8 9 10 11 12 13 14 15 16 17 18 19 20
//#define ENCODING_LogLuv 3003
#define ENCODING_RGBM7  3004
#define ENCODING_RGBM16 3005
//#define ENCODING_RGBM16 3007

vec4 texelDecode( in vec4 encodedTexel, in int encoding ) {

  // Q: should we use a switch statement here instead of a set of ifs?

  if( encoding == ENCODING_Linear ) {
    return encodedTexel;
  }

  if( encoding == ENCODING_sRGB ) {
B
Ben Houston 已提交
21
    return vec4( pow( encodedTexel.xyz, vec3( GAMMA_FACTOR ) ), encodedTexel.w );
22 23 24 25 26 27
  }

  if( encoding == ENCODING_RGBE ) {
    return vec4( encodedTexel.xyz * exp2( encodedTexel.w*256.0 - 128.0 ), 1.0 );
  }

B
Ben Houston 已提交
28
  // TODO, see here http://graphicrants.blogspot.ca/2009/04/rgbm-color-encoding.html
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
  //if( encoding == ENCODING_LogLuv ) {
  //}

  if( encoding == ENCODING_RGBM7 ) {
    return vec4( encodedTexel.xyz * encodedTexel.w * 7.0, 1.0 );
  }

  if( encoding == ENCODING_RGBM16 ) {
    return vec4( encodedTexel.xyz * encodedTexel.w * 16.0, 1.0 );
  }

  // TODO
  //if( encoding == ENCODING_RGBD ) {
  //}

  // return red when encoding not supported
  return vec4( 1.0, 0.0, 0.0, 1.0 );

}

vec4 texelEncode( in vec4 linearRgba, in int encoding )
{

  // Q: should we use a switch statement here instead of a set of ifs?

  if( encoding == ENCODING_Linear ) {
    return linearRgba;
  }

  if( encoding == ENCODING_sRGB ) {
B
Ben Houston 已提交
59
    return vec4( pow( linearRgba.xyz, vec3( 1.0 / GAMMA_FACTOR ) ), linearRgba.w );
60 61 62 63 64 65 66 67
  }

  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 );
  }

B
Ben Houston 已提交
68
  // TODO, see here http://graphicrants.blogspot.ca/2009/04/rgbm-color-encoding.html
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
  //if( encoding == ENCODING_LogLuv ) {
  //}

  // TODO
  //if( encoding == ENCODING_RGBM7 ) {
  //}

  // TODO
  //if( encoding == ENCODING_RGBM16 ) {
  //}

  // TODO
  //if( encoding == ENCODING_RGBD ) {
  //}

  // return red when encoding not supported
  return vec4( 1.0, 0.0, 0.0, 1.0 );

}