encodings.glsl 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // 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/
  2. // These encodings should have the same integer values as THREE.LinearEncoding, THREE.sRGBEncoding, etc...
  3. #define ENCODING_Linear 3000
  4. #define ENCODING_sRGB 3001
  5. #define ENCODING_RGBE 3002
  6. #define ENCODING_LogLuv 3003
  7. #define ENCODING_RGBM7 3004
  8. #define ENCODING_RGBM16 3005
  9. #define ENCODING_RGBD 3006
  10. #define ENCODING_Gamma 3007
  11. vec4 LinearToLinear( in vec4 value ) {
  12. return value;
  13. }
  14. vec4 GammaToLinear( in vec4 value, in float gammaFactor ) {
  15. return vec4( pow( value.xyz, vec3( gammaFactor ) ), value.w );
  16. }
  17. vec4 LinearTosGamma( in vec4 value, in float gammaFactor ) {
  18. return vec4( pow( value.xyz, vec3( 1.0 / gammaFactor ) ), value.w );
  19. }
  20. vec4 sRGBToLinear( in vec4 value ) {
  21. return vec4( mix( pow( value.rgb * 0.9478672986 + vec3( 0.0521327014 ), vec3( 2.4 ) ), value.rgb * 0.0773993808, vec3( lessThanEqual( value.rgb, vec3( 0.04045 ) ) ) ), value.w );
  22. }
  23. vec4 LinearTosRGB( in vec4 value ) {
  24. return vec4( mix( pow( value.rgb, vec3( 0.41666 ) ) - vec3( 0.055 ), value.rgb * 12.92, vec3( lessThanEqual( value.rgb, vec3( 0.0031308 ) ) ) ), value.w );
  25. }
  26. vec4 RGBEToLinear( in vec4 value ) {
  27. return vec4( value.xyz * exp2( value.w*256.0 - 128.0 ), 1.0 );
  28. }
  29. vec4 LinearToRGBE( in vec4 value ) {
  30. float maxComponent = max(max(value.r, value.g), value.b );
  31. float fExp = ceil( log2(maxComponent) );
  32. return vec4( value.rgb / exp2(fExp), (fExp + 128.0) / 255.0 );
  33. }
  34. // reference: http://iwasbeingirony.blogspot.ca/2010/06/difference-between-rgbm-and-rgbd.html
  35. vec4 RGBMToLinear( in vec4 value, in float maxRange ) {
  36. return vec4( value.xyz * value.w * maxRange, 1.0 );
  37. }
  38. vec4 LinearToRGBM( in vec4 value, in float maxRange ) {
  39. float maxRGB = max( value.x, max( value.g, value.b ) );
  40. float M = maxRGB / maxRange;
  41. M = ceil( M * 255.0 ) / 255.0;
  42. return vec4( value.rgb / ( M * maxRange ), M );
  43. }
  44. // reference: http://iwasbeingirony.blogspot.ca/2010/06/difference-between-rgbm-and-rgbd.html
  45. vec4 RGBDToLinear( in vec4 value, in float maxRange ) {
  46. return vec4( value.rgb * ( ( maxRange / 255.0 ) / value.a ), 1.0 );
  47. }
  48. vec4 LinearToRGBD( in vec4 value, in float maxRange ) {
  49. float maxRGB = max( value.x, max( value.g, value.b ) );
  50. float D = max( maxRange / maxRGB, 1.0 );
  51. D = saturate( floor( D ) / 255.0 );
  52. return vec4( value.rgb * ( D * ( 255.0 / maxRange ) ), D );
  53. }
  54. // LogLuv reference: http://graphicrants.blogspot.ca/2009/04/rgbm-color-encoding.html
  55. // M matrix, for encoding
  56. const mat3 cLogLuvM = mat3(
  57. 0.2209, 0.3390, 0.4184,
  58. 0.1138, 0.6780, 0.7319,
  59. 0.0102, 0.1130, 0.2969);
  60. vec4 LinearToLogLuv( in vec4 value ) {
  61. vec3 Xp_Y_XYZp = value.rgb * cLogLuvM;
  62. Xp_Y_XYZp = max(Xp_Y_XYZp, vec3(1e-6, 1e-6, 1e-6));
  63. vec4 vResult;
  64. vResult.xy = Xp_Y_XYZp.xy / Xp_Y_XYZp.z;
  65. float Le = 2.0 * log2(Xp_Y_XYZp.y) + 127.0;
  66. vResult.w = fract(Le);
  67. vResult.z = (Le - (floor(vResult.w*255.0))/255.0)/255.0;
  68. return vResult;
  69. }
  70. // Inverse M matrix, for decoding
  71. const mat3 cLogLuvInverseM = mat3(
  72. 6.0014, -2.7008, -1.7996,
  73. -1.3320, 3.1029, -5.7721,
  74. 0.3008, -1.0882, 5.6268);
  75. vec4 LogLuvToLinear( in vec4 value ) {
  76. float Le = value.z * 255.0 + value.w;
  77. vec3 Xp_Y_XYZp;
  78. Xp_Y_XYZp.y = exp2((Le - 127.0) / 2.0);
  79. Xp_Y_XYZp.z = Xp_Y_XYZp.y / value.y;
  80. Xp_Y_XYZp.x = value.x * Xp_Y_XYZp.z;
  81. vec3 vRGB = Xp_Y_XYZp.rgb * cLogLuvInverseM;
  82. return vec4( max(vRGB, 0.0), 1.0 );
  83. }