ShadowsMidtonesHighlightsCurves.shader 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. Shader "Hidden/Universal Render Pipeline/Editor/Shadows Midtones Highlights Curve"
  2. {
  3. CGINCLUDE
  4. #include "UnityCG.cginc"
  5. #pragma editor_sync_compilation
  6. #pragma target 3.5
  7. float4 _ShaHiLimits; // xy: shadows min/max, zw: highlight min/max
  8. float4 _Variants; // x: disabled state, y: x-scale, wz: unused
  9. float3 BlendScreen(float3 base, float3 blend)
  10. {
  11. return 1.0 - (1.0 - blend) * (1.0 - base);
  12. }
  13. float4 DrawCurve(v2f_img i, float3 background, float3 shadowsCurveColor, float3 midtonesCurveColor, float3 highlightsCurveColor)
  14. {
  15. float x = i.uv.x * _Variants.y;
  16. float y = i.uv.y;
  17. float aa = fwidth(i.uv.y) * 2.0;
  18. float shadowsY = 1.0 - smoothstep(_ShaHiLimits.x, _ShaHiLimits.y, x);
  19. float shadowsCurve = smoothstep(shadowsY + aa, shadowsY, y);
  20. float shadowsLine = smoothstep(shadowsY - aa, shadowsY, y) - smoothstep(shadowsY, shadowsY + aa, y);
  21. float highlightsY = smoothstep(_ShaHiLimits.z, _ShaHiLimits.w, x);
  22. float highlightsCurve = smoothstep(highlightsY + aa, highlightsY, y);
  23. float highlightsLine = smoothstep(highlightsY - aa, highlightsY, y) - smoothstep(highlightsY, highlightsY + aa, y);
  24. float midtonesY = 1.0 - shadowsY - highlightsY;
  25. float midtonesCurve = smoothstep(midtonesY + aa, midtonesY, y);
  26. float midtonesLine = smoothstep(midtonesY - aa, midtonesY, y) - smoothstep(midtonesY, midtonesY + aa, y);
  27. float grad = lerp(0.7, 1.0, y);
  28. float3 shadowsColor = shadowsCurveColor * shadowsCurve * grad;
  29. float3 midtonesColor = midtonesCurveColor * midtonesCurve * grad;
  30. float3 highlightsColor = highlightsCurveColor * highlightsCurve * grad;
  31. float3 color = BlendScreen(shadowsColor, midtonesColor);
  32. color = BlendScreen(color, highlightsColor);
  33. color = BlendScreen(background, color * _Variants.xxx);
  34. const float kAlpha = 0.3 * _Variants.xxx;
  35. color += shadowsLine * shadowsColor * kAlpha;
  36. color += midtonesLine * midtonesColor * kAlpha;
  37. color += highlightsLine * highlightsColor * kAlpha;
  38. return float4(color, 1.0);
  39. }
  40. float4 FragCurveDark(v2f_img i) : SV_Target
  41. {
  42. return DrawCurve(i, (pow(0.196, 2.2)).xxx, pow(float3(0.161, 0.851, 0.761), 2.2), pow(float3(0.741, 0.949, 0.443), 2.2), pow(float3(0.9, 0.9, 0.651), 2.2));
  43. }
  44. float4 FragCurveLight(v2f_img i) : SV_Target
  45. {
  46. return DrawCurve(i, (pow(0.635, 2.2)).xxx, pow(float3(0.161, 0.851, 0.761), 2.2), pow(float3(0.741, 0.949, 0.443), 2.2), pow(float3(1.0, 1.0, 0.651), 2.2));
  47. }
  48. ENDCG
  49. SubShader
  50. {
  51. Cull Off ZWrite Off ZTest Always
  52. // (0) Dark skin
  53. Pass
  54. {
  55. CGPROGRAM
  56. #pragma vertex vert_img
  57. #pragma fragment FragCurveDark
  58. ENDCG
  59. }
  60. // (1) Light skin
  61. Pass
  62. {
  63. CGPROGRAM
  64. #pragma vertex vert_img
  65. #pragma fragment FragCurveLight
  66. ENDCG
  67. }
  68. }
  69. }