encodings.glsl 3.0 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
#define ENCODING_LogLuv 3003
8 9
#define ENCODING_RGBM7  3004
#define ENCODING_RGBM16 3005
10
#define ENCODING_RGBD   3006
11

B
Ben Houston 已提交
12 13 14
vec4 LinearToLinear( in vec4 value ) {
  return value;
}
15

B
Ben Houston 已提交
16 17 18
vec4 sRGBToLinear( in vec4 value ) {
  return vec4( pow( value.xyz, vec3( float( GAMMA_FACTOR ) ) ), value.w );
}
19 20 21
vec4 LinearTosRGB( in vec4 value ) {
  return vec4( pow( value.xyz, vec3( 1.0 / float( GAMMA_FACTOR ) ) ), value.w );
}
22

B
Ben Houston 已提交
23 24 25
vec4 RGBEToLinear( in vec4 value ) {
  return vec4( value.xyz * exp2( value.w*256.0 - 128.0 ), 1.0 );
}
26 27 28 29 30
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 );
}
31

32 33 34 35 36 37 38 39 40
// reference: http://iwasbeingirony.blogspot.ca/2010/06/difference-between-rgbm-and-rgbd.html
vec4 RGBMToLinear( in vec4 value, in float maxRange ) {
  return vec4( value.xyz * value.w * maxRange, 1.0 );
}
vec4 LinearToRGBM( in vec4 value, in float maxRange ) {
  float maxRGB = max( value.x, max( value.g, value.b ) );
  float M      = maxRGB / maxRange;
  M            = ceil( M * 255.0 ) / 255.0;
  return vec4( value.rgb / ( M * maxRange ), M );
B
Ben Houston 已提交
41
}
42

43 44 45 46 47 48 49 50 51
// reference: http://iwasbeingirony.blogspot.ca/2010/06/difference-between-rgbm-and-rgbd.html
vec4 RGBDToLinear( in vec4 value, in float maxRange ) {
    return vec4( value.rgb * ( ( maxRange / 255.0 ) / value.a ), 1.0 );
}
vec4 LinearToRGBD( in vec4 value, in float maxRange ) {
    float maxRGB = max( value.x, max( value.g, value.b ) );
    float D      = max( maxRange / maxRGB, 1.0 );
    D            = saturate( floor( D ) / 255.0 );
    return vec4( value.rgb * ( D * ( 255.0 / maxRange ) ), D );
B
Ben Houston 已提交
52
}
53

54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
// LogLuv reference: http://graphicrants.blogspot.ca/2009/04/rgbm-color-encoding.html

// M matrix, for encoding
const mat3 cLogLuvM = mat3(
  0.2209, 0.3390, 0.4184,
  0.1138, 0.6780, 0.7319,
  0.0102, 0.1130, 0.2969);
vec4 LinearToLogLuv( in vec4 value )  {
  vec3 Xp_Y_XYZp = value.rgb * cLogLuvM;
  Xp_Y_XYZp = max(Xp_Y_XYZp, vec3(1e-6, 1e-6, 1e-6));
  vec4 vResult;
  vResult.xy = Xp_Y_XYZp.xy / Xp_Y_XYZp.z;
  float Le = 2.0 * log2(Xp_Y_XYZp.y) + 127.0;
  vResult.w = fract(Le);
  vResult.z = (Le - (floor(vResult.w*255.0))/255.0)/255.0;
  return vResult;
B
Ben Houston 已提交
70
}
71

72 73 74 75 76 77 78 79 80 81 82 83 84
// Inverse M matrix, for decoding
const mat3 cLogLuvInverseM = mat3(
  6.0014, -2.7008, -1.7996,
  -1.3320,  3.1029, -5.7721,
  0.3008, -1.0882,  5.6268);
vec4 LogLuvToLinear( in vec4 value ) {
  float Le = value.z * 255.0 + value.w;
  vec3 Xp_Y_XYZp;
  Xp_Y_XYZp.y = exp2((Le - 127.0) / 2.0);
  Xp_Y_XYZp.z = Xp_Y_XYZp.y / value.y;
  Xp_Y_XYZp.x = value.x * Xp_Y_XYZp.z;
  vec3 vRGB = Xp_Y_XYZp.rgb * cLogLuvInverseM;
  return vec4( max(vRGB, 0.0), 1.0 );
B
Ben Houston 已提交
85
}