home *** CD-ROM | disk | FTP | other *** search
/ Xentax forum attachments archive / xentax.7z / 18759 / ROE.7z / QSSHADER_PostFxColor_1.h < prev    next >
Encoding:
C/C++ Source or Header  |  2020-09-17  |  1.8 KB  |  63 lines

  1. #ifndef QSSHADER_PostFxColor_H
  2. #define QSSHADER_PostFxColor_H
  3.  
  4. float3 RgbToHsl(float3 c)
  5. {
  6.     float4 K = float4(0.0f, -1.0f / 3.0f, 2.0f / 3.0f, -1.0f);
  7.     float4 p = lerp(float4(c.bg, K.wz), float4(c.gb, K.xy), step(c.b, c.g));
  8.     float4 q = lerp(float4(p.xyw, c.r), float4(c.r, p.yzx), step(p.x, c.r));
  9.  
  10.     float d = q.x - min(q.w, q.y);
  11.     float e = 1.0e-10;
  12.     return float3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x);
  13. }
  14.  
  15. float3 HslToRgb(float3 c)
  16. {
  17.     float4 K = float4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
  18.     float3 p = abs(frac(c.xxx + K.xyz) * 6.0 - K.www);
  19.     return c.z * lerp(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);
  20. }
  21.  
  22. float3 RgbShiftByHsl(float3 src, float3 hslShift)
  23. {
  24.     float3 hsl = RgbToHsl(src);
  25.     hsl += hslShift;
  26.     return HslToRgb(hsl);
  27. }
  28.  
  29. float3 RgbShiftByHsl(float3 src, float3 hslShift, float mask)
  30. {
  31.     float3 hsl = RgbToHsl(src);
  32.     hsl += hslShift;
  33.     float3 shiftedRgb = HslToRgb(hsl);
  34.     return lerp(src, shiftedRgb, mask);
  35. }
  36.  
  37. float3 ColorGrading(sampler2D table, float3 src)
  38. {
  39.     // requires a volume texture 16x16x16 unwrapped in a 2d texture 256x16
  40.     // can be optimized by using a volume texture
  41.     float2 offset = float2(0.5f / 256.0f, 0.5f / 16.0f);
  42.     float scale = 15.0f / 16.0f; 
  43.  
  44.     // Also consider blur value in the blur buffer written by translucency
  45.     float intB = floor(src.b * 14.9999f) / 16.0f;
  46.     float fracB = src.b * 15.0f - intB * 16.0f;
  47.  
  48.     float u = intB + src.r * scale / 16.0f;
  49.     float v = src.g * scale;
  50.  
  51.     float3 color0 = tex2D(table, offset + float2(u,v)).rgb;
  52.     float3 color1 = tex2D(table, offset + float2(u + 1.0f / 16.0f, v)).rgb;
  53.  
  54.     return lerp(color0, color1, fracB);
  55. }
  56.  
  57. float GetLuminance(float3 color)
  58. {
  59.     const float3 lumWeight = float3(0.2125f, 0.7154f, 0.0721f);
  60.     return dot(color, lumWeight);
  61. }
  62.  
  63. #endif //QSSHADER_PostFxColor_H