Learn more about Russian war crimes in Ukraine.

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(
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.

What can computers do? What are the limits of mathematics? And just how busy can a busy beaver be? This year, I’m writing Busy Beavers, a unique interactive book on computability theory. You and I will take a practical and modern approach to answering these questions — or at least learning why some questions are unanswerable!

It’s only $19, and you can get 50% off if you find the discount code ... Not quite. Hackers use the console!

After months of secret toil, I and Andrew Carr released Everyday Data Science, a unique interactive online course! You’ll make the perfect glass of lemonade using Thompson sampling. You’ll lose weight with differential equations. And you might just qualify for the Olympics with a bit of statistics!

It’s $29, but you can get 50% off if you find the discount code ... Not quite. Hackers use the console!

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.