Advertisement
Guest User

Untitled

a guest
Oct 6th, 2019
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #fragment shader
  2. #version 330 core
  3. out vec4 OutColor;
  4. in vec4 FragColor;
  5. in vec4 FragPosition;
  6. in vec2 BoxCenter;
  7. in vec2 BoxSize;
  8. in vec2 ScreenResolution;
  9. in float BoxCornerRadius;
  10.  
  11. const float threshold = 0.004;
  12.  
  13. float square(float val)
  14. {
  15.     return (val * val);
  16. }
  17.  
  18. float distance_squared(vec2 p1, vec2 p2)
  19. {
  20.     vec2 vector = p2 - p1;
  21.     return (vector.x * vector.x) + (vector.y * vector.y);
  22. }
  23.  
  24. /// @brief Calculate a circular gradient with cut off edges
  25. /// @param pixel_position : opengl coordindate of the pixel
  26. /// @param max_size : do not apply gradient past this boundary
  27. /// @param center : center of the circle
  28. /// @param radius : radius of the circle
  29. /// @param threshold : how far out from the radius to allow gradient
  30. float calc_corner_alpha(vec2 pixel_position, vec2 center, float radius, float threshold)
  31. {
  32.     if (radius <= 0.0)
  33.     {
  34.         return 1.0;
  35.     }
  36.  
  37.     float lower_squared = square(radius);
  38.     float dist_squared = distance_squared(pixel_position, center);
  39.     float upper_squared = square(radius + threshold);
  40.     return smoothstep(upper_squared, lower_squared, distance_squared(pixel_position, center));
  41. }
  42.  
  43. void main()
  44. {
  45.     float half_width = (BoxSize.x / 2.0) - BoxCornerRadius;
  46.     float half_height = (BoxSize.y / 2.0) - BoxCornerRadius;
  47.     float left = BoxCenter.x - half_width;
  48.     float right = BoxCenter.x + half_width;
  49.     float top = BoxCenter.y + half_height;
  50.     float bottom = BoxCenter.y - half_height;
  51.     float a = 0.0;
  52.     float aspect_ratio = ScreenResolution.x / ScreenResolution.y;
  53.     vec2 curPosition = FragPosition.xy;
  54.     if ((curPosition.x >= left && curPosition.x <= right) || // sits within vertical section
  55.         (curPosition.y >= bottom && curPosition.y <= top)) // sits within horizontal section
  56.     {
  57.         // forms a sort of cross shape where the alpha is 1.0
  58.         a = 1.0;
  59.     }
  60.     else if (curPosition.x < left)
  61.     {
  62.         if (curPosition.y > top)
  63.         {
  64.             a = calc_corner_alpha(FragPosition.xy, vec2(left, top), BoxCornerRadius, threshold);
  65.         }
  66.         else
  67.         {
  68.             a = calc_corner_alpha(FragPosition.xy, vec2(left, bottom), BoxCornerRadius, threshold);
  69.         }
  70.     }
  71.     else if (curPosition.x > right)
  72.     {
  73.         if (curPosition.y > top)
  74.         {
  75.             a = calc_corner_alpha(FragPosition.xy, vec2(right, top), BoxCornerRadius, threshold);
  76.         }
  77.         else
  78.         {
  79.             a = calc_corner_alpha(FragPosition.xy, vec2(right, bottom), BoxCornerRadius, threshold);
  80.         }
  81.     }
  82.     OutColor = vec4(FragColor.rgb, a);
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement