encodings.glsl 3.4 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
#define ENCODING_Gamma  3007
12

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

17 18 19 20 21 22 23
vec4 GammaToLinear( in vec4 value, in float gammaFactor ) {
  return vec4( pow( value.xyz, vec3( gammaFactor ) ), value.w );
}
vec4 LinearTosGamma( in vec4 value, in float gammaFactor ) {
  return vec4( pow( value.xyz, vec3( 1.0 / gammaFactor ) ), value.w );
}

B
Ben Houston 已提交
24
vec4 sRGBToLinear( in vec4 value ) {
B
Ben Houston 已提交
25
  return vec4( mix( pow( value.rgb * 0.9478672986 + vec3( 0.0521327014 ), vec3( 2.4 ) ), value.rgb * 0.0773993808, lessThanEqual( value.rgb, vec3( 0.04045 ) ) ), value.w );
B
Ben Houston 已提交
26
}
27
vec4 LinearTosRGB( in vec4 value ) {
B
Ben Houston 已提交
28
  return vec4( mix( pow( value.rgb, vec3( 0.41666 ) ) - vec3( 0.055 ), value.rgb * 12.92, lessThanEqual( value.rgb, vec3( 0.0031308 ) ) ), value.w );
29
}
30

B
Ben Houston 已提交
31 32 33
vec4 RGBEToLinear( in vec4 value ) {
  return vec4( value.xyz * exp2( value.w*256.0 - 128.0 ), 1.0 );
}
34 35 36 37 38
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 );
}
39

40 41 42 43 44 45 46 47 48
// 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 已提交
49
}
50

51 52 53 54 55 56 57 58 59
// 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 已提交
60
}
61

62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
// 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 已提交
78
}
79

80 81 82 83 84 85 86 87 88 89 90 91 92
// 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 已提交
93
}