My favourite shader made this week was this neat ripple effect created using techniques learned in the patterns video, shaping functions video, as well as my own explorations into the sin function.

I first began by modifying the code from the grid example, and created a series of if else statements targeting each column individually.

In each of these conditions, I drew circles with the size being based off a sin function using u_time, which created an effect where the circle grew and shrunk over time.

I modified the speed of the effect by multiplying u_time, effectively changing the period of the function, which resulted in a shorter cycle and animation. This can be seen below in my experiments in graphtoy, the orange wave is sin(x) while the shorter yellow wave is sin(x*2).

In order to offset the animation of each circle to create the ripple effect, I added a varying horizontal shift to each function, so that the cycles would occur quickly in succession. Once again, this was verified using graphtoy.

Overall, making this shader refreshed a lot of what I learned about sin functions and their modifiers, and it was especially interesting using sin functions with u_time to create an animated shader.

#ifdef GL_ES precision mediump float; #endif uniform vec2 u_resolution; uniform vec2 u_mouse; uniform float u_time; vec2 createGrid( in vec2 st, in vec2 grid, out vec2 indices ){ // multiply by the number of cells // [0,1] range => [0,10] (for example) st *= grid; // get the cell indices // for example, in a grid of 10x10: // 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 indices = floor(st); // use fract to get the fractional amount of st // 9.5 => 0.5 // shows the xy coordinate in each cell st = fract(st); return st; } float drawCircle (vec2 st, vec2 pos, float size){ float result = distance (st, pos); result=1.-step(size,result); return result; } void main() { vec2 st = gl_FragCoord.xy/u_resolution.xy; st.x *= u_resolution.x/u_resolution.y; // good practice to take a copy of the original st // before you change it vec2 st0 = st; vec2 indices; // This divides the result of the sin function, changing the final size of the circle float size = 4.; // This changes the period of the sin function, changing the speed at which the circles animate float speed = 5.; // Create grid for circles st = createGrid( st, vec2(5., 5.), indices); // Set bg colour vec3 color = vec3(0.995,0.764,0.451); // Change the colour and sin function of each column if(indices.x == 0.){ float r = drawCircle(st, vec2(0.5), sin(u_time*speed)/size); color = mix( color, vec3(0.683,0.289,1.000), r); }else if(indices.x == 1.){ float r = drawCircle(st, vec2(0.5), sin(u_time*speed+1.)/size); color = mix( color, vec3(1.000,0.243,0.831), r); } else if(indices.x == 2.){ float r = drawCircle(st, vec2(0.5), sin(u_time*speed+2.)/size); color = mix( color, vec3(1.000,0.118,0.380), r); } else if(indices.x == 3.){ float r = drawCircle(st, vec2(0.5), sin(u_time*speed+3.)/size); color = mix( color, vec3(1.000,0.188,0.236), r); } else if(indices.x == 4.){ float r = drawCircle(st, vec2(0.5), sin(u_time*speed+4.)/size); color = mix( color, vec3(1.000,0.237,0.122), r); } gl_FragColor = vec4(color,1.0); }