Lutify-Preview.cginc 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // Upgrade NOTE: replaced '_GUIClipTextureMatrix' with 'unity_GUIClipTextureMatrix'
  2. // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
  3. // Lutify - Unity Asset
  4. // Copyright (c) 2015 - Thomas Hourdel
  5. // http://www.thomashourdel.com
  6. #include "UnityCG.cginc"
  7. sampler2D _MainTex;
  8. sampler2D _LookupTex2D;
  9. float4 _Params;
  10. struct appdata_t
  11. {
  12. float4 vertex : POSITION;
  13. fixed4 color : COLOR;
  14. float2 texcoord : TEXCOORD0;
  15. };
  16. struct v2f
  17. {
  18. float4 vertex : SV_POSITION;
  19. float4 color : COLOR;
  20. float2 texcoord : TEXCOORD0;
  21. float2 clipUV : TEXCOORD1;
  22. };
  23. float4 _MainTex_ST;
  24. float4 _Color;
  25. float4x4 unity_GUIClipTextureMatrix;
  26. sampler2D _GUIClipTexture;
  27. float _BottomFrame;
  28. float4 _FrameColor;
  29. v2f vert_preview(appdata_t v)
  30. {
  31. v2f o;
  32. o.vertex = UnityObjectToClipPos(v.vertex);
  33. float4 eyePos = mul(UNITY_MATRIX_MV, v.vertex);
  34. o.clipUV = mul(unity_GUIClipTextureMatrix, eyePos);
  35. o.color = v.color;
  36. o.texcoord = TRANSFORM_TEX(v.texcoord,_MainTex);
  37. return o;
  38. }
  39. /*
  40. * sRGB <-> Linear from http://entropymine.com/imageworsener/srgbformula/
  41. * using a bit more precise values than the IEC 61966-2-1 standard
  42. * see http://en.wikipedia.org/wiki/SRGB for more information
  43. */
  44. float4 sRGB(float4 color)
  45. {
  46. color.rgb = (color.rgb <= float3(0.0031308, 0.0031308, 0.0031308)) ? color.rgb * 12.9232102 : 1.055 * pow(color.rgb, 0.41666) - 0.055;
  47. return color;
  48. }
  49. float4 Linear(float4 color)
  50. {
  51. color.rgb = (color.rgb <= float3(0.0404482, 0.0404482, 0.0404482)) ? color.rgb / 12.9232102 : pow((color.rgb + 0.055) * 0.9478672, 2.4);
  52. return color;
  53. }
  54. float4 internal_tex3d(sampler2D tex, float4 uv, float2 pixelsize, float tilewidth)
  55. {
  56. uv.y = 1.0 - uv.y;
  57. uv.z *= tilewidth;
  58. float shift = floor(uv.z);
  59. uv.xy = uv.xy * tilewidth * pixelsize + 0.5 * pixelsize;
  60. uv.x += shift * pixelsize.y;
  61. float w = (_Params.w >= 1) ? step(0.5, uv.z - shift) : uv.z - shift;
  62. uv.xyz = lerp(tex2D(tex, uv.xy).rgb, tex2D(tex, uv.xy + float2(pixelsize.y, 0)).rgb, w);
  63. return uv;
  64. }
  65. float4 lookup_gamma_2d(float4 o)
  66. {
  67. o = internal_tex3d(_LookupTex2D, o, _Params.xy, _Params.z);
  68. return o;
  69. }
  70. float4 lookup_linear_2d(float4 o)
  71. {
  72. o = internal_tex3d(_LookupTex2D, sRGB(o), _Params.xy, _Params.z);
  73. return Linear(o);
  74. }
  75. float4 name_rect(float4 c, float v)
  76. {
  77. c.rgb = lerp(_FrameColor.rgb, c.rgb, step(_BottomFrame, v));
  78. return c;
  79. }
  80. float4 frag_gamma(v2f i) : SV_Target
  81. {
  82. float4 col;
  83. col.rgb = saturate(tex2D(_MainTex, i.texcoord).rgb * i.color.rgb);
  84. col.a = i.color.a * tex2D(_GUIClipTexture, i.clipUV).a;
  85. return name_rect(lookup_gamma_2d(col), i.texcoord.y);
  86. }
  87. float4 frag_linear(v2f i) : SV_Target
  88. {
  89. float4 col;
  90. col.rgb = saturate(tex2D(_MainTex, i.texcoord).rgb * i.color.rgb);
  91. col.a = i.color.a * tex2D(_GUIClipTexture, i.clipUV).a;
  92. return name_rect(lookup_linear_2d(col), i.texcoord.y);
  93. }