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.

I just released Vidrio, a free app for macOS and Windows to make your screen-sharing awesomely holographic. Vidrio shows your webcam video on your screen, just like a mirror. Then you just share or record your screen with Zoom, QuickTime, or any other app. Vidrio makes your presentations effortlessly engaging, showing your gestures, gazes, and expressions. #1 on Product Hunt. Available for macOS and Windows.

With Vidrio

With generic competitor

More by Jim

Tagged #programming, #graphics, #webgl. All content copyright James Fisher 2017. This post is not associated with my employer. Found an error? Edit this page.