The Code Therapy

rolling shutter

simple rolling shutter simulator

Created by jihyunyu on Thu, 11 Aug 2022 16:03:36 GMT.

#version 300 es
precision highp float;

uniform vec2 resolution;
uniform float time;
out vec4 fragColor;

#define PI     3.14159265

vec4 image(vec2 coord) {
  float size = 20.0;
  if ((coord.x < size && coord.x >= -size) || (coord.y < size && coord.y >= -size)) {
    return vec4(1.0, 1.0, 1.0, 1.);
  }
  return vec4(0, 0, 0, 1);
}

float scantime = 1.0 / 1.0;
// rotation per second, rpm / 60
float speed = 1.0;

void main(void)
{
    // normalized vertical pos
    float scanpos = gl_FragCoord.y / resolution.y;
    // (0, 0) centered coordinate, for rotation
    vec2 uv = vec2(gl_FragCoord.x - resolution.x/2.0, gl_FragCoord.y - resolution.y/2.0);
    
    float phase = (time + scanpos * scantime) * PI * 2.0 * speed;
    float t = phase;// + scantime * scanpos;

    vec2 coord = vec2(sin(t) * uv.x + cos(t) * uv.y, cos(t) * uv.x - sin(t) * uv.y);
    fragColor = image(coord);
}