Advertisement
Guest User

Wheel Menu Shader

a guest
Jun 15th, 2019
345
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.00 KB | None | 0 0
  1. shader_type canvas_item;
  2.  
  3. uniform vec2 centre = vec2(0.5);
  4.  
  5. uniform float smoothing = 0.01; // The smaller this value is, the rougher looking the edges of the wheel are
  6.  
  7. uniform float initial_outer_r = 0.45; // The default outer radius value.
  8. uniform float inner_r = 0.2; // The inner radius value
  9.  
  10. uniform int no_of_colours = 4; // The number of colours. This is set automatically by the script
  11. uniform float gap = 0.05; // The gap between each wheel segment
  12.  
  13. /* All of these colour values are placeholders. These correspond to the colours of each segment starting from the bottom right and going clockwise
  14. It was done like this because there are no arrays for shaders.
  15. The list can be expanded to your liking */
  16. uniform vec3 colour0 = vec3(1.0, 1.0, 1.0);
  17. uniform vec3 colour1 = vec3(1.0, 1.0, 1.0);
  18. uniform vec3 colour2 = vec3(1.0, 1.0, 1.0);
  19. uniform vec3 colour3 = vec3(1.0, 1.0, 1.0);
  20. uniform vec3 colour4 = vec3(1.0, 1.0, 1.0);
  21. uniform vec3 colour5 = vec3(1.0, 1.0, 1.0);
  22. uniform vec3 colour6 = vec3(1.0, 1.0, 1.0);
  23. uniform vec3 colour7 = vec3(1.0, 1.0, 1.0);
  24.  
  25.  
  26. uniform int selected_colour = -1; // If a colour is selected then this value corresponds to that colour label. If the value is -1, then no colour is selected.
  27. uniform float elapsed_time = 0; /* This is changed within the script continuously as the mouse hovers over a segment
  28. How much time has elapsed since the mouse hovered over the button. This is for animations.
  29. This was used instead of TIME so that the animation could start from the segment's initial size */
  30.  
  31. void fragment(){
  32. float PI = 3.14159;
  33.  
  34. vec2 pos = UV-centre; // Position of the pixel relative to the centre
  35.  
  36. float r = length(pos); // Distance of a point from the centre
  37. float a = atan(pos.y, pos.x); // Angle of a point starting from the right going clockwise
  38.  
  39. float norm_a = fract(a/(2.0*PI)); // The angle but it is normalized so it goes from 0 to 1 around the circle clockwise starting from the right
  40.  
  41. vec3 colour_rgb = vec3(1.0); // This represents the colour of the pixel without the alpha value
  42.  
  43. float outer_r = initial_outer_r; // This value can change due to the animation of the segment whilst being hovered over
  44. float anim_r = 0.04*(0.5*(1.0+sin(2.0*PI*fract(elapsed_time)))); // Used for the animation for the outer radius increasing and decreasing in size
  45.  
  46. float gap_alpha = 1.0; // The alpha value within the gaps. This variable is used to help smoothen the borders of the gaps
  47.  
  48. for (float i=0.0;i<=float(no_of_colours);i++){
  49.  
  50. if ((norm_a < (i/float(no_of_colours)) + (gap/(2.0*PI)/2.0))&&(norm_a > (i/float(no_of_colours)))){
  51. gap_alpha = smoothstep((i/float(no_of_colours)), (i/float(no_of_colours)) + (gap/(2.0*PI)/2.0), norm_a);
  52. }
  53. else if ((norm_a > (i/float(no_of_colours)) - (gap/(2.0*PI)/2.0))&&(norm_a < (i/float(no_of_colours)))){
  54. gap_alpha = 1.0-smoothstep((i/float(no_of_colours)) - (gap/(2.0*PI)/2.0), (i/float(no_of_colours)), norm_a);
  55. }
  56.  
  57. // Each of the following sections of code correspond to one uniform colour variable listed prior.. The pixel is set to that colour
  58. else if ((norm_a > (0./float(no_of_colours))) && (norm_a < (1./float(no_of_colours)))){
  59. if (selected_colour == 0){
  60. outer_r = initial_outer_r + anim_r;
  61. }
  62. colour_rgb = colour0;
  63. }
  64. else if ((norm_a > (1./float(no_of_colours))) && (norm_a < (2./float(no_of_colours)))){
  65. if (selected_colour == 1){
  66. outer_r = initial_outer_r + anim_r;
  67. }
  68. colour_rgb = colour1;
  69. }
  70. else if ((norm_a > (2./float(no_of_colours))) && (norm_a < (3./float(no_of_colours)))){
  71. if (selected_colour == 2){
  72. outer_r = initial_outer_r + anim_r;
  73. }
  74. colour_rgb = colour2;
  75. }
  76. else if ((norm_a > (3./float(no_of_colours))) && (norm_a < (4./float(no_of_colours)))){
  77. if (selected_colour == 3){
  78. outer_r = initial_outer_r + anim_r;
  79. }
  80. colour_rgb = colour3;
  81. }
  82. else if ((norm_a > (4./float(no_of_colours))) && (norm_a < (5./float(no_of_colours)))){
  83. if (selected_colour == 4){
  84. outer_r = initial_outer_r + anim_r;
  85. }
  86. colour_rgb = colour4;
  87. }
  88. else if ((norm_a > (5./float(no_of_colours))) && (norm_a < (6./float(no_of_colours)))){
  89. if (selected_colour == 5){
  90. outer_r = initial_outer_r + anim_r;
  91. }
  92. colour_rgb = colour5;
  93. }
  94. else if ((norm_a > (6./float(no_of_colours))) && (norm_a < (7./float(no_of_colours)))){
  95. if (selected_colour == 6){
  96. outer_r = initial_outer_r + anim_r;
  97. }
  98. colour_rgb = colour6;
  99. }
  100. else if ((norm_a > (7./float(no_of_colours))) && (norm_a < (8./float(no_of_colours)))){
  101. if (selected_colour == 7){
  102. outer_r = initial_outer_r + anim_r;
  103. }
  104. colour_rgb = colour7;
  105. }
  106. }
  107.  
  108. if (r >= initial_outer_r){
  109. COLOR = vec4( colour_rgb, (1.-smoothstep(outer_r,outer_r+smoothing,r))*gap_alpha ); // Makes the outside of the circle transparent with a smoothing factor
  110. }
  111. else{
  112. COLOR = vec4( colour_rgb, smoothstep(inner_r,inner_r+smoothing,r)*gap_alpha ); // Makes the inside of the circle transparent with a smoothing factor
  113. }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement