uniform float time;

uniform float hueRotate;
uniform float enabled;
uniform float saturation;
vec3 rgb2hsv(vec3 c) {
    vec4 K = vec4(0.0, -1.0/3.0, 2.0/3.0, -1.0);
    vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g));
    vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r));
    float d = q.x - min(q.w, q.y);
    float e = 1.0e-10;
    return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x);
}

vec3 hsv2rgb(vec3 c) {
    vec4 K = vec4(1.0, 2.0/3.0, 1.0/3.0, 3.0);
    vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www);
    return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);
}

float rand() {
  return fract(sin(time+dot(gl_FragCoord.xy, vec2(12.9898, 78.233))) * 43758.5453);
}

void drawLaser()
{


    //vec4 color1 = vec4(1.,0.4,0.4,1.);
    vec2 coord=vMapUv;

    // sin wavy
    coord.s = coord.s + sin(time*20.0 + coord.t * 15.0) * 0.05;

    float y = coord.t * 2.0;
    if (y > 1.0) {
      y = 2.0 - y;
    }

    float x = coord.s * 2.0;
    if (x > 1.0) {
      x = 2.0 - x;
    }
    gl_FragColor.rgb = gl_FragColor.rgb * 0.8 + vec3(x*y) * 0.2;
    if(enabled < 0.9) return;
    vec3 hsv = rgb2hsv(gl_FragColor.rgb);
    hsv.x = fract(hsv.x + hueRotate);
    hsv.y = clamp(hsv.y + saturation, 0.0, 1.0);
    gl_FragColor.rgb = hsv2rgb(hsv);

    //gl_FragColor.rgb = vec3(x*y);
    float originalAlpha = gl_FragColor.a;
    gl_FragColor.a = (0.6 + 0.4*rand());

    // moire pattern
    vec2 gridCoord = floor(coord * 3000.0 / 100.0);
    if (mod(gridCoord.x + gridCoord.y, 2.0) == 0.0) {
        gl_FragColor.rgb *= 0.7;
        //discard;
    }

    //gl_FragColor.rgba *= color1;
    gl_FragColor.a *= originalAlpha;


    //gl_FragColor.rgb = vec3(1.0,0.,0.);
}
