Guest User

3D UI

a guest
Feb 18th, 2021
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #version 430
  2.  
  3. const float TAU = 6.283185307179586476925286766559;
  4.  
  5. const vec3 blue = vec3(46, 155, 230) / 255;
  6. const vec3 lightBlue = vec3(20, 99, 153) / 255;
  7. const vec3 orange = vec3(230, 133, 54) / 255;
  8.  
  9. in vec2 vTexCoords;
  10. uniform float uAspectRatio;
  11. uniform vec3 uCamX;
  12. uniform vec3 uCamY;
  13. uniform vec3 uCamZ;
  14. uniform vec3 uCamPos;
  15. uniform float uFocalLength;
  16.  
  17. uniform float uTime;
  18.  
  19. #define rot2(a) mat2(cos(a), -sin(a), sin(a), cos(a))
  20.  
  21. vec3 rgb2hsv(vec3 c) {
  22.     vec4 K = vec4(0, -1 / 3, 2 / 3, -1);
  23.     vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g));
  24.     vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r));
  25.  
  26.     float d = q.x - min(q.w, q.y);
  27.     float e = 0.000001;
  28.     return vec3(abs(q.z + (q.w - q.y) / (6 * d + e)), d / (q.x + e), q.x);
  29. }
  30.  
  31. // All components are in the range [0, 1], including hue.
  32. vec3 hsv2rgb(vec3 c) {
  33.     vec4 K = vec4(1, 2 / 3, 1 / 3, 3);
  34.     vec3 p = abs(fract(c.xxx + K.xyz) * 6 - K.www);
  35.     return c.z * mix(K.xxx, clamp(p - K.xxx, 0, 1), c.y);
  36. }
  37.  
  38. struct Ray {
  39.   vec3 origin;
  40.   vec3 direction;
  41. };
  42.  
  43. struct Plane {
  44.   vec3 origin;
  45.   vec3 normal;
  46. };
  47.  
  48. vec2 computePlaneUv(vec3 p, Plane plane) {
  49.   vec3 xAxis = (vec3( - plane.normal.y, plane.normal.x, 0));
  50.   vec3 yAxis = cross(xAxis, plane.normal);
  51.  
  52.   return vec2(
  53.     dot(xAxis, p - plane.origin),
  54.     dot(yAxis, p - plane.origin)
  55.   );
  56. }
  57.  
  58. float gridOfDisks(vec2 uv, float radius, float size, float margin) {
  59.     vec2 gridUv = fract(uv / 5.);
  60.     vec2 gridId = floor(uv / 5.);
  61.     if (abs(gridId.x) < size && abs(gridId.y) < size) {
  62.       float d = distance(vec2(0.5), gridUv);
  63.       return smoothstep(radius+margin, radius- margin, d);
  64.     }
  65.     return 0.;
  66. }
  67.  
  68. float disk(vec2 uv, float radius) {
  69.     return smoothstep(radius+0.01, radius-0.01, length(uv));
  70. }
  71.  
  72. float circleStripe(vec2 uv, float radius, float angle) {
  73.     float d = length(uv) - radius;
  74.     return smoothstep(0.01, -0.01, abs(d) - 1.);
  75. }
  76.  
  77. float sector(vec2 uv, float radius, float fraction, float width, float offset) {
  78.     float d = length(uv) - radius;
  79.     float a = atan(uv.y, uv.x) / TAU + 0.5;
  80.  
  81.     if (fract(a + offset) - fraction < 0.)
  82.       return smoothstep(0.01, -0.01, abs(d) - width);
  83.     else
  84.       return 0.;
  85. }
  86.  
  87. float dashedSector(vec2 uv, float radius, float fraction, float width, float offset, float enguerrand) {
  88.   float d = length(uv) - radius;
  89.     float a = atan(uv.y, uv.x) / TAU + 0.5;
  90.     a = a + offset;
  91.     a *= enguerrand;
  92.     if (fract(a) - fraction < 0.)
  93.       return smoothstep(0.01, -0.01, abs(d) - width);
  94.     else
  95.       return 0.;
  96. }
  97.  
  98. float horloge(vec2 uv, float radius, float extension, float offset) {
  99.     float d = length(uv) - radius;
  100.     float a = atan(uv.y, uv.x) / TAU + 0.5 + offset;
  101.  
  102.     vec2 duv = vec2(d, fract(a * 5.));
  103.     float dDisk = distance(duv, vec2(0., 0.5));
  104.  
  105.     return smoothstep(0.5, 0.0, abs(dDisk-extension));
  106. }
  107.  
  108. vec3 planeUVs(Ray ray, Plane plane) {
  109.   float t = dot(ray.origin - plane.origin, plane.normal) / dot(ray.direction, plane.normal);
  110.   vec3 P = ray.origin + t * ray.direction;
  111.   return vec3(
  112.     computePlaneUv(P, plane),
  113.     t
  114.   );
  115. }
  116.  
  117. vec3 scene(vec2 texCoords) {
  118.     // Setup camera
  119.    
  120.     Ray ray = {
  121.       uCamPos,
  122.       normalize(
  123.         uCamX * (texCoords.x - 0.5) * uAspectRatio
  124.       + uCamY * (texCoords.y - 0.5)
  125.       - uCamZ * uFocalLength
  126.     )};
  127.  
  128.  
  129.     vec3 col = vec3(0.);
  130.     // vec3 blue = vec3(50, 70, 255) / 255;
  131.    
  132.     /*
  133.     { // plane 0
  134.       Plane plane = { vec3(0.,2, 0), vec3(0., 1., 0.) };
  135.       vec3 res = planeUVs(ray, plane);
  136.       if (res.z > 0) {
  137.         col +=  0.5*sector(res.xy, 10, 0.2, 1, uTime*0.4) * blue;
  138.       }
  139.     }*/
  140.  
  141.     { // Grid of points
  142.       Plane plane = { vec3(0., 2.5, 0.), vec3(0., 1., 0.) };
  143.       vec3 res = planeUVs(ray, plane);
  144.       if (res.z > 0) {
  145.         col += gridOfDisks(res.xy, 0.15, 100, 0.015) * 0.2 * blue * smoothstep(50, 20, length(res.xy));
  146.       }
  147.     }
  148.    
  149.     { // Grid of points bis
  150.       Plane plane = { vec3(0., 3, 0.), vec3(0., 1., 0.) };
  151.       vec3 res = planeUVs(ray, plane);
  152.       if (res.z > 0) {
  153.         col += gridOfDisks(res.xy * 0.9, 0.03, 100, 0.2) * blue * smoothstep(50, 20, length(res.xy));
  154.       }
  155.     }
  156.  
  157.     { // Big Sector up
  158.       for(float i = 0; i < 2; i++) {
  159.         Plane plane = { vec3(0., 3, 0.), vec3(0., 1. + i * 0.015, 0.) };
  160.         vec3 res = planeUVs(ray, plane);
  161.         if (res.z > 0) {
  162.           col +=  sector(res.xy, 10, 0.2, 1, uTime*0.3) * blue;
  163.         }
  164.       }
  165.     }
  166.    
  167.     { // Big Sector down
  168.       for(float i = 0; i < 2; i++) {
  169.         Plane plane = { vec3(0., 4.5, 0.), vec3(0., 1. + i * 0.015, 0.) };
  170.         vec3 res = planeUVs(ray, plane);
  171.         if (res.z > 0) {
  172.           col +=  sector(res.xy, 10, 0.2, 1, uTime*0.31) * blue;
  173.         }
  174.       }
  175.     }
  176.  
  177.     { // variable sector 1
  178.       Plane plane = { vec3(0., -2, 0.), vec3(0., 1., 0.) };
  179.       vec3 res = planeUVs(ray, plane);
  180.       if (res.z > 0) {
  181.         col +=  sector(res.xy, 8, mix(0.2, 0.5, sin(uTime*0.5) * 0.5+ 0.5), 0.1, -uTime*0.3) * blue;
  182.       }
  183.     }
  184.     { // variable sector 2
  185.       Plane plane = { vec3(0., -3.5, 0.), vec3(0., 1., 0.) };
  186.       vec3 res = planeUVs(ray, plane);
  187.       if (res.z > 0) {
  188.         col +=  sector(res.xy, 10, mix(0.2, 0.8, cos(uTime*0.8) * 0.5+ 0.5), 0.1, -uTime*0.3) * blue;
  189.       }
  190.     }
  191.  
  192.     { // variable sector 3
  193.       Plane plane = { vec3(0., -5.5, 0.), vec3(0., 1., 0.) };
  194.       vec3 res = planeUVs(ray, plane);
  195.       if (res.z > 0) {
  196.         col +=  sector(res.xy, 5, mix(0.8, 0.9, cos(-uTime*0.5) * 0.5+ 0.5), 0.1, uTime*0.2) * blue;
  197.       }
  198.     }
  199.  
  200.     { // cadran down
  201.      for(float i = 0; i < 2; i++) {
  202.         Plane plane = { vec3(0., 2+i*0.5, 0.), vec3(0., 1., 0.) };
  203.         vec3 res = planeUVs(ray, plane);
  204.         if (res.z > 0) {
  205.           col += dashedSector(res.xy, 15 - 2*i, 0.15 + 0.05*i, 0.5, uTime*(0.05 - 0.06 * i), 100) * blue;
  206.         }
  207.      }
  208.     }
  209.  
  210.     { // cadran top
  211.       Plane plane = { vec3(0., 0, 0.), vec3(0., 1., 0.) };
  212.       vec3 res = planeUVs(ray, plane);
  213.       if (res.z > 0) {
  214.         col += dashedSector(res.xy, 12, 0.1, 0.1, uTime*0.09, 8) * blue;
  215.       }
  216.     }
  217.  
  218.     { // cadran top
  219.       Plane plane = { vec3(0., 0, 0.), vec3(0., 1., 0.) };
  220.       vec3 res = planeUVs(ray, plane);
  221.       if (res.z > 0) {
  222.         col += horloge(res.xy, 5, 0.75 + 0.1 * cos(uTime*5), uTime*0.05) * vec3(255, 215, 94)/255.;
  223.       }
  224.     }
  225.    
  226.     return col;
  227. }
  228.  
  229. void main()
  230. {
  231.     vec3 col = vec3(0.);
  232.     const int N = 1 + 2 * int( 10 * length(vTexCoords - 0.5));
  233.     for (int x = -N/2; x <= N/2; ++x) {
  234.       for (int y = -N/2; y <= N/2; ++y) {
  235.         //col += scene(vTexCoords + vec2(x, y)*(length(vTexCoords - 0.5))) / N / N;
  236.         col += scene(vTexCoords + vec2(x, y)*0.001) / N / N;
  237.       }
  238.     }
  239.    
  240.     col += 0.01 * lightBlue;
  241.     col *= clamp(1 - length(vTexCoords - 0.5) / 2, 0, 1);
  242.     col = pow(col, vec3(0.4545)); // gamma corection
  243.  
  244.     // saturate
  245.     col *= vec3(1, 1, 1.1);
  246.  
  247.     gl_FragColor = vec4(col, 1.);
  248. }
Add Comment
Please, Sign In to add comment