Learn more about Israeli war crimes in Gaza, funded by the USA, Germany, the UK and others.

Generated normal-mapped ripples

Above you see a ripply surface. There’s something bobbing in the middle, making waves. As you move the cursor around, you illuminate these waves. With the light in the middle, it’s quite hypnotic! This is another WebGL fragment shader:

precision mediump float;
const float PI_2 = 1.57079632679489661923;
uniform mediump vec2 mouse_pos;
uniform mediump float shift;
mat4 rotateZ(float ang) {
    return mat4(
        cos(ang),sin(ang),0.,0,
        -sin(ang),cos(ang),0.,0.,
        0.,0.,1.,0.,
        0.,0.,0.,1.);
}
mat4 rotateY(float ang) {
  return mat4(
    cos(ang), 0.0, -sin(ang), 0.0,
    0.0,      1.0, 0.0,       0.0,
    sin(ang), 0.0, cos(ang),  0.0,
    0.0,      0.0, 0.0,       1.0
  );
}
void main(void) {
  vec4 surface_pos_cs = vec4(vec2(gl_FragCoord)/256.0 - 1.0, 0.0, 1.0);
  vec4 light_pos_cs = vec4(mouse_pos*2.0 - 1.0, 0.5, 1.0);
  float ang = atan(surface_pos_cs.y, surface_pos_cs.x);
  float dist = length(vec2(surface_pos_cs));
  vec4 normal_direction = rotateZ(ang) * rotateY(-PI_2) * normalize(vec4(1.0, 0.0, sin((dist-shift)*20.0)/4.0, 0.0));
  vec4 from_light_dir = normalize(surface_pos_cs-light_pos_cs);
  vec4 reflection_dir = reflect(from_light_dir, normal_direction);
  vec4 camera_dir = normalize(vec4(0.0, 0.0, 1.0, 0.0));
  float intensity = dot(reflection_dir, camera_dir);
  gl_FragColor = vec4(intensity, intensity, intensity, 1.0);
}

Next, I want to simulate refraction. This ripply surface will be the surface of a pool, refracting the image of the pool bottom.

Tagged #programming, #graphics, #webgl.

Similar posts

More by Jim

Want to build a fantastic product using LLMs? I work at Granola where we're building the future IDE for knowledge work. Come and work with us! Read more or get in touch!

This page copyright James Fisher 2017. Content is not associated with my employer. Found an error? Edit this page.