Guest User

Matrix Dot Shader

a guest
Jul 22nd, 2025
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
OpenGL Shading 1.49 KB | Source Code | 0 0
  1. // This code works in Shader Editor
  2.  
  3. #ifdef GL_FRAGMENT_PRECISION_HIGH
  4. precision highp float;
  5. #else
  6. precision mediump float;
  7. #endif
  8.  
  9. uniform vec2 resolution;
  10. uniform float time;
  11.  
  12.  
  13. // Matrix settings
  14. const float size = 0.03225;
  15. const float padding = 0.05;
  16.  
  17. // Sine properties
  18. const float scale = 0.4;
  19. const float speed = 1.0;
  20. const float freq = 2.0;
  21. const float offset = 0.5;
  22. const float lineWidth = 0.02;
  23.  
  24.  
  25. // Returns a value 1..0..-inf
  26. // that represents how far a and b are from each other
  27. // 1 means a == b and 0 means abs(a - b) == epsilon
  28. float eq(float a, float b, float epsilon)
  29. {
  30.     return (epsilon - abs(a - b)) / epsilon;
  31. }
  32.  
  33.  
  34. // This function is called when
  35. // the current pixel is within a matrix dot
  36. vec4 draw(vec2 uv)
  37. {
  38.     float sine = sin(time * speed + uv.x * freq) * scale + offset;
  39.     float aa = eq(sine, uv.y, 0.02);
  40.     if(aa > 0.0)
  41.         return mix(vec4(0.05), vec4(0.1, 0.8, 0.6, 1.0), aa);
  42.     return vec4(0.05);
  43. }
  44.  
  45. void main(void) {
  46.     float aspect = resolution.y / resolution.x;
  47.  
  48.     vec2 uv = gl_FragCoord.xy / resolution.xy;
  49.     vec2 uvac = uv * vec2(1.0, aspect);
  50.     // UV coordinates repeated in a square grid
  51.     vec2 uv_grid = mod(uvac, size) * (1.0 / size);
  52.  
  53.     // Pixelated UV coords, where a pixel is equal to a dot
  54.     vec2 uv_px = vec2(
  55.         size * floor(uv.x / size),
  56.         size / aspect * floor(uv.y / size * aspect)
  57.     );
  58.  
  59.     // Is the pixel within a dot?
  60.     if(distance(uv_grid, vec2(0.5)) < size * (1.0 / size / 2.0) - padding)
  61.         gl_FragColor = vec4(draw(uv_px));
  62. }
  63.  
Advertisement
Add Comment
Please, Sign In to add comment