Advertisement
baryonSasuke

Fragment shader

Jan 7th, 2021
691
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #version 460
  2.  
  3. layout(location = 0) out vec4 outColor;
  4.  
  5. in PerVertexData {
  6.     vec2 tex;
  7. } fragIn;
  8.  
  9. layout (location = 0) uniform Rectangle{
  10.     mat4 projectionViewMatrix;
  11.     vec4 radius;
  12.     vec4 color;
  13.     vec2 dimensions;
  14.     float hasTexture;
  15. }rectangle;
  16.  
  17. uniform sampler2D texture_sampler;
  18.  
  19. void main() {
  20.  
  21. //    rectangle corner rounding method from ThinMatrix' devlop https://www.youtube.com/watch?v=d5ttbNtpgi4&t=12s
  22.     float r1 = rectangle.radius.x;
  23.     float r2 = rectangle.radius.y;
  24.     float r3 = rectangle.radius.z;
  25.     float r4 = rectangle.radius.w;
  26.  
  27.     vec2 c1 = vec2(r1, rectangle.dimensions.y - r1);
  28.     vec2 c2 = vec2(rectangle.dimensions.x - r2,rectangle.dimensions.y - r2);
  29.     vec2 c3 = vec2(r3,r3);
  30.     vec2 c4 = vec2(rectangle.dimensions.x - r4,r4);
  31.  
  32.     vec2 coords = fragIn.tex * rectangle.dimensions;
  33.  
  34.     // in corner 1 (top left)
  35.     if(coords.x < c1.x && coords.y > c1.y) {
  36.         if(length(coords - c1) > r1) {
  37.             discard;
  38.         }
  39.     }
  40.  
  41. //    Top right corner
  42.     if(coords.x > c2.x && coords.y > c2.y) {
  43.         if(length(coords - c2) > r2) {
  44.             discard;
  45.         }
  46.     }
  47.  
  48. //    Botton left corner
  49.     if(coords.x < c3.x && coords.y < c3.y) {
  50.         if(length(coords - c3) > r3) {
  51.             discard;
  52.         }
  53.     }
  54.  
  55. //    Botton right corner
  56.     if(coords.x > c4.x && coords.y < c4.y) {
  57.         if(length(coords - c4) > r4) {
  58.             discard;
  59.         }
  60.     }
  61.  
  62.     vec4 finalColor;
  63.     if (rectangle.hasTexture == 0) {
  64.         outColor = rectangle.color;
  65.     }
  66.     else {
  67.         outColor = texture(texture_sampler, fragIn.tex);
  68.     }
  69.  
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement