Advertisement
nullpainter

glsl transition between colors.

Aug 10th, 2024 (edited)
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. glsl transition between colors.
  3.  
  4. I pictured it as cutting the interval, as having a playback position inside of that interval ... sort of like movie frames.
  5. */
  6. vec3 frame(
  7.   vec3 a, // start color
  8.   vec3 b, // end color
  9.   float t1, // start time
  10.   float t2, // end time
  11.   float t   // time
  12. ) {
  13.   vec3 x = vec3(0,0,0); // black so we have no color if we mix frames together
  14.  
  15.   if(t>=t1 && t<t2){
  16.     x=mix(a,b,(t-t1)/(t2-t1));
  17.   }
  18.  
  19.   return x;
  20. }
  21.  
  22. void mainImage( out vec4 fragColor, in vec2 fragCoord )
  23. {
  24.   // COLORS
  25.   vec3 a=vec3(1,0,0);
  26.   vec3 b=vec3(0,1,0);
  27.   vec3 c=vec3(0,0,1);
  28.   vec3 d=vec3(0,0,0);
  29.   vec3 e=vec3(1,0,1);
  30.  
  31.   float PERIOD=9.;
  32.   float t = mod(iTime,PERIOD);    // 0..PERIOD
  33.  
  34.   vec3 col=                       // mix "movie frames"
  35.       frame(a,b,0.,2.,t)+
  36.       frame(b,c,2.,3.,t)+
  37.       frame(c,d,3.,5.,t)+
  38.       frame(d,e,5.,7.,t)+
  39.       frame(e,a,7.,9.,t)+         // back to first color, so it loops smoothly
  40.       vec3(0,0,0);                // so we can copypaste previous lines and keep + at the end
  41.  
  42.   fragColor = vec4(col,1);
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement