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.

118 lines
3.5 KiB

  1. Shader "Hidden/Post FX/UI/Trackball"
  2. {
  3. CGINCLUDE
  4. #include "UnityCG.cginc"
  5. #define PI 3.14159265359
  6. #define PI2 6.28318530718
  7. float _Offset;
  8. float _DisabledState;
  9. float2 _Resolution; // x: size, y: size / 2
  10. float3 HsvToRgb(float3 c)
  11. {
  12. float4 K = float4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
  13. float3 p = abs(frac(c.xxx + K.xyz) * 6.0 - K.www);
  14. return c.z * lerp(K.xxx, saturate(p - K.xxx), c.y);
  15. }
  16. float4 CreateWheel(v2f_img i, float crossColor, float offsetColor)
  17. {
  18. const float kHueOuterRadius = 0.45;
  19. const float kHueInnerRadius = 0.38;
  20. const float kLumOuterRadius = 0.495;
  21. const float kLumInnerRadius = 0.48;
  22. float4 color = (0.0).xxxx;
  23. float2 uvc = i.uv - (0.5).xx;
  24. float dist = sqrt(dot(uvc, uvc));
  25. float delta = fwidth(dist);
  26. float angle = atan2(uvc.x, uvc.y);
  27. // Cross
  28. {
  29. float radius = (0.5 - kHueInnerRadius) * _Resolution.x + 1.0;
  30. float2 pixel = (_Resolution.xx - 1.0) * i.uv + 1.0;
  31. float vline = step(floor(fmod(pixel.x, _Resolution.y)), 0.0);
  32. vline *= step(radius, pixel.y) * step(pixel.y, _Resolution.x - radius);
  33. float hline = step(floor(fmod(pixel.y, _Resolution.y)), 0.0);
  34. hline *= step(radius, pixel.x) * step(pixel.x, _Resolution.x - radius);
  35. color += hline.xxxx * (1.0).xxxx;
  36. color += vline.xxxx * (1.0).xxxx;
  37. color = saturate(color);
  38. color *= half4((crossColor).xxx, 0.05);
  39. }
  40. // Hue
  41. {
  42. float alphaOut = smoothstep(kHueOuterRadius - delta, kHueOuterRadius + delta, dist);
  43. float alphaIn = smoothstep(kHueInnerRadius - delta, kHueInnerRadius + delta, dist);
  44. float hue = angle;
  45. hue = 1.0 - ((hue > 0.0) ? hue : PI2 + hue) / PI2;
  46. float4 c = float4(HsvToRgb(float3(hue, 1.0, 1.0)), 1.0);
  47. color += lerp((0.0).xxxx, c, alphaIn - alphaOut);
  48. }
  49. // Offset
  50. {
  51. float alphaOut = smoothstep(kLumOuterRadius - delta, kLumOuterRadius + delta, dist);
  52. float alphaIn = smoothstep(kLumInnerRadius - delta, kLumInnerRadius + delta / 2, dist);
  53. float4 c = float4((offsetColor).xxx, 1.0);
  54. float a = PI * _Offset;
  55. if (_Offset >= 0 && angle < a && angle > 0.0)
  56. c = float4((1.0).xxx, 0.5);
  57. else if (angle > a && angle < 0.0)
  58. c = float4((1.0).xxx, 0.5);
  59. color += lerp((0.0).xxxx, c, alphaIn - alphaOut);
  60. }
  61. return color * _DisabledState;
  62. }
  63. float4 FragTrackballDark(v2f_img i) : SV_Target
  64. {
  65. return CreateWheel(i, 1.0, 0.15);
  66. }
  67. float4 FragTrackballLight(v2f_img i) : SV_Target
  68. {
  69. return CreateWheel(i, 0.0, 0.3);
  70. }
  71. ENDCG
  72. SubShader
  73. {
  74. Cull Off ZWrite Off ZTest Always
  75. // (0) Dark skin
  76. Pass
  77. {
  78. CGPROGRAM
  79. #pragma vertex vert_img
  80. #pragma fragment FragTrackballDark
  81. ENDCG
  82. }
  83. // (1) Light skin
  84. Pass
  85. {
  86. CGPROGRAM
  87. #pragma vertex vert_img
  88. #pragma fragment FragTrackballLight
  89. ENDCG
  90. }
  91. }
  92. }