ColorSpace.bslinc 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. mixin ColorSpace
  2. {
  3. code
  4. {
  5. /**
  6. * Converts a color in RGB space into YCoCg color space. Note that Co and Cg
  7. * components are in [-0.5, 0.5] range and therefore cannot be stored as-is
  8. * in a normal color texture.
  9. */
  10. float3 RGBToYCoCg(float3 input)
  11. {
  12. float Y = dot(input, float3(0.25f, 0.5f, 0.25f));
  13. float Co = input.r * 0.5f + input.b * -0.5f;
  14. float Cg = dot(input, float3(-0.25f, 0.5f, -0.25f));
  15. return float3(Y, Co, Cg);
  16. }
  17. /**
  18. * Converts a color in YCoCg color space into RGB color space.
  19. */
  20. float3 YCoCg(float3 input)
  21. {
  22. float R = input.r + input.g - input.b;
  23. float G = input.r + input.b;
  24. float B = input.r - input.g - input.b;
  25. return float3(R, G, B);
  26. }
  27. /** Calculates luminance from a color in RGB color space. */
  28. float LuminanceRGB(float3 input)
  29. {
  30. return 0.299f * input.r + 0.587f * input.g + 0.114f * input.b;
  31. }
  32. ////////////////////// HDR SCALING HELPER FUNCTIONS ///////////////////
  33. // HDR scaling methods use luminance based scaling, instead of a tonemap operator, as
  34. // described here:
  35. // http://graphicrants.blogspot.hr/2013/12/tone-mapping.html
  36. float HDRScaleWeight(float luma, float exposureScale)
  37. {
  38. return rcp(1 + luma * exposureScale);
  39. }
  40. float HDRScaleWeightInv(float luma, float exposureScale)
  41. {
  42. return rcp(1 - luma * exposureScale);
  43. }
  44. float3 HDRScaleRGB(float3 v, float exposureScale)
  45. {
  46. float luma = LuminanceRGB(v);
  47. return v * HDRScaleWeight(luma, exposureScale);
  48. }
  49. float3 HDRScaleY(float3 v, float exposureScale)
  50. {
  51. float luma = v.r;
  52. return v * HDRScaleWeight(luma, exposureScale);
  53. }
  54. float3 HDRScaleG(float3 v, float exposureScale)
  55. {
  56. float luma = v.g;
  57. return v * HDRScaleWeight(luma, exposureScale);
  58. }
  59. float3 HDRScaleRGBInv(float3 v, float exposureScale)
  60. {
  61. float luma = LuminanceRGB(v);
  62. return v * HDRScaleWeightInv(luma, exposureScale);
  63. }
  64. float3 HDRScaleYInv(float3 v, float exposureScale)
  65. {
  66. float luma = v.r;
  67. return v * HDRScaleWeightInv(luma, exposureScale);
  68. }
  69. float3 HDRScaleGInv(float3 v, float exposureScale)
  70. {
  71. float luma = v.g;
  72. return v * HDRScaleWeightInv(luma, exposureScale);
  73. }
  74. };
  75. };