You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

128 lines
3.2 KiB

  1. Shader "Hidden/Post FX/Lut Generator"
  2. {
  3. CGINCLUDE
  4. #pragma target 3.0
  5. #pragma multi_compile __ TONEMAPPING_NEUTRAL TONEMAPPING_FILMIC
  6. #include "UnityCG.cginc"
  7. #include "ACES.cginc"
  8. #include "Common.cginc"
  9. #include "ColorGrading.cginc"
  10. #include "Tonemapping.cginc"
  11. half3 _Balance;
  12. half3 _Lift;
  13. half3 _InvGamma;
  14. half3 _Gain;
  15. half3 _Offset;
  16. half3 _Power;
  17. half3 _Slope;
  18. half _HueShift;
  19. half _Saturation;
  20. half _Contrast;
  21. half3 _ChannelMixerRed;
  22. half3 _ChannelMixerGreen;
  23. half3 _ChannelMixerBlue;
  24. half4 _NeutralTonemapperParams1;
  25. half4 _NeutralTonemapperParams2;
  26. sampler2D _Curves;
  27. half4 _LutParams;
  28. half3 ColorGrade(half3 color)
  29. {
  30. half3 aces = unity_to_ACES(color);
  31. // ACEScc (log) space
  32. half3 acescc = ACES_to_ACEScc(aces);
  33. acescc = OffsetPowerSlope(acescc, _Offset, _Power, _Slope);
  34. half2 hs = RgbToHsv(acescc).xy;
  35. half satMultiplier = SecondaryHueSat(hs.x, _Curves);
  36. satMultiplier *= SecondarySatSat(hs.y, _Curves);
  37. satMultiplier *= SecondaryLumSat(AcesLuminance(acescc), _Curves);
  38. acescc = Saturation(acescc, _Saturation * satMultiplier);
  39. acescc = ContrastLog(acescc, _Contrast);
  40. aces = ACEScc_to_ACES(acescc);
  41. // ACEScg (linear) space
  42. half3 acescg = ACES_to_ACEScg(aces);
  43. acescg = WhiteBalance(acescg, _Balance);
  44. acescg = LiftGammaGain(acescg, _Lift, _InvGamma, _Gain);
  45. half3 hsv = RgbToHsv(max(acescg, 0.0));
  46. hsv.x = SecondaryHueHue(hsv.x + _HueShift, _Curves);
  47. acescg = HsvToRgb(hsv);
  48. acescg = ChannelMixer(acescg, _ChannelMixerRed, _ChannelMixerGreen, _ChannelMixerBlue);
  49. #if TONEMAPPING_FILMIC
  50. aces = ACEScg_to_ACES(acescg);
  51. color = FilmicTonemap(aces);
  52. #elif TONEMAPPING_NEUTRAL
  53. color = ACEScg_to_unity(acescg);
  54. color = NeutralTonemap(color, _NeutralTonemapperParams1, _NeutralTonemapperParams2);
  55. #else
  56. color = ACEScg_to_unity(acescg);
  57. #endif
  58. // YRGB curves (done in linear/LDR for now)
  59. color = YrgbCurve(color, _Curves);
  60. return color;
  61. }
  62. half4 FragCreateLut(VaryingsDefault i) : SV_Target
  63. {
  64. // 2D strip lut
  65. half2 uv = i.uv - _LutParams.yz;
  66. half3 color;
  67. color.r = frac(uv.x * _LutParams.x);
  68. color.b = uv.x - color.r / _LutParams.x;
  69. color.g = uv.y;
  70. // Lut is in LogC
  71. half3 colorLogC = color * _LutParams.w;
  72. // Switch back to unity linear and color grade
  73. half3 colorLinear = LogCToLinear(colorLogC);
  74. half3 graded = ColorGrade(colorLinear);
  75. return half4(graded, 1.0);
  76. }
  77. ENDCG
  78. SubShader
  79. {
  80. Cull Off ZWrite Off ZTest Always
  81. // (0)
  82. Pass
  83. {
  84. CGPROGRAM
  85. #pragma vertex VertDefault
  86. #pragma fragment FragCreateLut
  87. ENDCG
  88. }
  89. }
  90. }