Lutify.cginc 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // Lutify - Unity Asset
  2. // Copyright (c) 2015 - Thomas Hourdel
  3. // http://www.thomashourdel.com
  4. #include "UnityCG.cginc"
  5. sampler2D _MainTex;
  6. sampler3D _LookupTex3D;
  7. sampler2D _LookupTex2D;
  8. float4 _Params;
  9. /*
  10. * sRGB <-> Linear from http://entropymine.com/imageworsener/srgbformula/
  11. * using a bit more precise values than the IEC 61966-2-1 standard
  12. * see http://en.wikipedia.org/wiki/SRGB for more information
  13. */
  14. float3 sRGB(float3 color)
  15. {
  16. color = (color <= float3(0.0031308, 0.0031308, 0.0031308)) ? color * 12.9232102 : 1.055 * pow(color, 0.41666) - 0.055;
  17. return color;
  18. }
  19. float4 sRGB(float4 color)
  20. {
  21. color.rgb = (color.rgb <= float3(0.0031308, 0.0031308, 0.0031308)) ? color.rgb * 12.9232102 : 1.055 * pow(color.rgb, 0.41666) - 0.055;
  22. return color;
  23. }
  24. float4 Linear(float4 color)
  25. {
  26. color.rgb = (color.rgb <= float3(0.0404482, 0.0404482, 0.0404482)) ? color.rgb / 12.9232102 : pow((color.rgb + 0.055) * 0.9478672, 2.4);
  27. return color;
  28. }
  29. float4 lookup_gamma(float4 o)
  30. {
  31. o.rgb = tex3D(_LookupTex3D, o.rgb * _Params.x + _Params.y).rgb;
  32. return o;
  33. }
  34. float4 lookup_linear(float4 o)
  35. {
  36. o.rgb = tex3D(_LookupTex3D, sRGB(o.rgb) * _Params.x + _Params.y).rgb;
  37. return Linear(o);
  38. }
  39. float4 frag(v2f_img i) : SV_Target
  40. {
  41. float4 color = saturate(tex2D(_MainTex, i.uv));
  42. float4 x;
  43. #if defined(LUTIFY_LINEAR)
  44. x = lookup_linear(color);
  45. #else
  46. x = lookup_gamma(color);
  47. #endif
  48. #if defined(LUTIFY_SPLIT_H)
  49. return i.uv.x > 0.5 ? lerp(color, x, _Params.z) : color;
  50. #elif defined(LUTIFY_SPLIT_V)
  51. return i.uv.y > 0.5 ? lerp(color, x, _Params.z) : color;
  52. #else
  53. return lerp(color, x, _Params.z);
  54. #endif
  55. }
  56. float4 internal_tex3d(sampler2D tex, float4 uv, float2 pixelsize, float tilewidth)
  57. {
  58. uv.y = 1.0 - uv.y;
  59. uv.z *= tilewidth;
  60. float shift = floor(uv.z);
  61. uv.xy = uv.xy * tilewidth * pixelsize + 0.5 * pixelsize;
  62. uv.x += shift * pixelsize.y;
  63. #if defined(LUTIFY_FILTERING_POINT)
  64. float w = step(0.5, uv.z - shift);
  65. #else
  66. float w = uv.z - shift;
  67. #endif
  68. uv.xyz = lerp(tex2D(tex, uv.xy).rgb, tex2D(tex, uv.xy + float2(pixelsize.y, 0)).rgb, w);
  69. return uv;
  70. }
  71. float4 lookup_gamma_2d(float4 o)
  72. {
  73. o = internal_tex3d(_LookupTex2D, o, _Params.xy, _Params.z);
  74. return o;
  75. }
  76. float4 lookup_linear_2d(float4 o)
  77. {
  78. o = internal_tex3d(_LookupTex2D, sRGB(o), _Params.xy, _Params.z);
  79. return Linear(o);
  80. }
  81. float4 frag_2d(v2f_img i) : SV_Target
  82. {
  83. float4 color = saturate(tex2D(_MainTex, i.uv));
  84. float4 x;
  85. #if defined(LUTIFY_LINEAR)
  86. x = lookup_linear_2d(color);
  87. #else
  88. x = lookup_gamma_2d(color);
  89. #endif
  90. #if defined(LUTIFY_SPLIT_H)
  91. return i.uv.x > 0.5 ? lerp(color, x, _Params.w) : color;
  92. #elif defined(LUTIFY_SPLIT_V)
  93. return i.uv.y > 0.5 ? lerp(color, x, _Params.w) : color;
  94. #else
  95. return lerp(color, x, _Params.w);
  96. #endif
  97. }