Advertisement
Guest User

Hw42

a guest
Mar 2nd, 2022
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.77 KB | None | 0 0
  1.  
  2. // Title: HW4
  3. // Author: Iris
  4. // Course: ShaderArt (DIGF-3011, Winter 2022)
  5.  
  6. #ifdef GL_ES
  7. precision mediump float;
  8. #endif
  9.  
  10. uniform vec2 u_mouse;
  11. uniform vec2 u_resolution;
  12. uniform float u_time;
  13.  
  14.  
  15. // calculateHue()
  16. // select a color from a collection of 6 based on a percentage
  17. // returns a RGB color
  18.  
  19. // random()
  20. // by Patricio Gonzalez-Vivo
  21. float random (vec2 st) {
  22.     return fract(sin(dot(st.xy,
  23.                          vec2(12.9898,78.233)))*
  24.         43758.5453123);
  25. }
  26.  
  27. float map(float value, float min1, float max1, float min2, float max2) {
  28.   return min2 + (value - min1) * (max2 - min2) / (max1 - min1);
  29. }
  30.  
  31. vec3 calculateHue(vec3 colors[3], float pct){
  32.  
  33.     pct = fract(pct);
  34.  
  35.     vec3 result = vec3(0.0); // Define the result so that we don't return nothing by accident
  36.    
  37.     float numberOfSections = 3.; // Number of sections (maximum of 12)
  38.  
  39.     for (int position = 0; position <= 3; position ++){                 // Loops through the following code 12 times
  40.         float leftPosition = float(position) / numberOfSections;        // Defines the left most position of the current section you're in
  41.         float rightPosition = float(position) + 1. / numberOfSections;  // Defines the right most position of the current section you're in
  42.         if (pct > leftPosition && pct <= rightPosition){                // Checks what section of the screen you are in (divided into numberOfSections)
  43.             result = colors[position];                                  // Colors your section with the appropriate colour
  44.         }
  45.     }
  46.  
  47.     return result;
  48. }
  49.  
  50. vec3 rgb_normalize( int r, int g, int b){
  51.     return vec3(float(r)/255., float(g)/255., float(b)/255.);
  52. }
  53.  
  54. void main (void) {
  55.  
  56.     vec2 st = gl_FragCoord.xy/u_resolution.xy;
  57.  
  58.  
  59.     vec3 colors[3];
  60.     // DEFINE YOUR COLOURS FOR THE TOP ROW HERE
  61.     colors[0] = rgb_normalize(128, 0, 32);
  62.     colors[1] = rgb_normalize(125, 112, 152);
  63.     colors[2] = rgb_normalize(255, 0, 0);
  64.  
  65.     vec3 color = vec3(0);
  66.  
  67.     float row = 1.0 - st.y;
  68.     row = floor( row * 3.0); // Split the screen into 4 rows
  69.    
  70.     // calculate the brightness based on the cell index using map()
  71.     // this creates steps for brightness: 0., 0.25, 0.5, 0.75, 1.0
  72.     float minBri = 0.3;
  73.     float brightness = map(abs(row), 0., 4., 1., minBri );
  74.     brightness *= map(abs(row), 0., 4., 1., minBri );    
  75.  
  76.     if( row == 0.0){
  77.         // SHOW THE TOP ROW
  78.         color = calculateHue(colors, st.x*-1.);
  79.     } else if( row == 1.0 ){
  80.         // SHOW THE FIRST MIDDLE ROW
  81.         color = calculateHue(colors, st.x-1.*tan(u_time)/3.);
  82.  
  83.     } else if( row == 2.0 ){
  84.         // SHOW THE SECOND MIDDLE ROW
  85.         color = calculateHue(colors, random(st*u_time/8.));
  86.     }
  87.    
  88.    
  89.  
  90.     gl_FragColor = vec4(color,1.0);
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement