TrackballEditor.shader 3.7 KB

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