in vec2 texCoord;
out vec4 fragColor;

#define M_PI 3.1415926535897932384626433832795

uniform sampler2D texture0;
uniform float time;
uniform vec4 color;

uniform float transition;

uniform float colorCycleTransition;
uniform float shiftHue;
uniform float shiftSaturation;
uniform float shiftValue;
uniform float centerize;

vec2 centerizeFn(vec2 coord) {
    return (2.0 * coord - 1.0);
}

uniform float kaleidoscopeTransition;
uniform float kaleidoscopeXangle;

vec2 kaleidoscope(vec2 coord) {
    if (kaleidoscopeTransition <= 0.0) {
        return texCoord;
    }
    
    coord.x = mod(coord.x, 1.0);
    coord.y = mod(coord.y, 1.0);
    coord = centerizeFn(coord);
    float angle = M_PI / kaleidoscopeXangle;

    float a = abs(mod(atan(coord.s,coord.t),angle*2.0)-angle);
    float dist = length(coord);
    coord.s = sin(a)*dist;
    coord.t = cos(a)*dist;
    return coord;
}

uniform float deformTransition;

vec2 deform(vec2 coord) {
    if (deformTransition <= 0.0) {
        return texCoord;
    }

    float x = coord.x * 2.0 - 1.0;
    float y = coord.y * 2.0 - 1.0;
    
    float dist = sqrt(x*x + y*y);
    
    float radius = 0.8;   
    float factor = 1.0 - smoothstep(0.0, radius, dist);
    float newDist = dist * (1.0 -deformTransition * factor);
    
    if (dist > 0.0) {
        x = x * (newDist / dist);
        y = y * (newDist / dist);
    }
    coord.x = (x + 1.0) * 0.5;
    coord.y = (y + 1.0) * 0.5;

    return coord;
}


uniform float waveTransition;
uniform float amp;
uniform float freq;
uniform float scrollSpeed;
uniform float dir;

vec2 waveDistortion(vec2 texCoord)
{
    if (waveTransition <= 0.0) {
        return texCoord;
    }

    vec2 uv = texCoord.st;

    float wave = sin(uv.y * freq + time * scrollSpeed) * amp;
    float waveCross = sin(uv.x * freq + time * scrollSpeed) * amp;

    uv.x += mix(wave, 0.0, dir);
    uv.y += mix(0.0, waveCross, dir);
    return uv;
}

uniform float borderSkewTransition;
uniform float borderSkewValue;

vec2 borderSkew(vec2 tc)
{
  if (borderSkewTransition <= 0.0) {
      return tc;
  }
  float borderSkewPowY = 8.0;
  float borderSkewPowX = 8.0;
  vec2 uv = 2.0 * tc - 1.0;
  float skew = borderSkewValue;
  vec2 offset = uv * skew * vec2(pow(uv.y, borderSkewPowY), pow(uv.x, borderSkewPowX));
  return tc + offset;
}

uniform float twirlTransition;

vec2 twirl(vec2 tc)
{	
    vec2 uv = tc;
    float rot = radians(sin(time * 0.5) * (mod(distance(uv, vec2(0.5, 0.5)), 1.0) / 1.0) * 200.0);
    return mat2(cos(rot), -sin(rot), sin(rot), cos(rot)) * (uv - vec2(0.5)) + vec2(0.5);
}

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);
}

vec4 colorcycle(vec2 coord)
{
    if (colorCycleTransition <= 0.0) {
        return vec4(0.0);
    }
    vec3 hsv = rgb2hsv(fragColor.rgb)*255.0;

    float distanceToCenter = distance(coord.xy, vec2(0.5,0.5))*255.0;

    hsv.x = mod(hsv.x + shiftHue*255.0 + centerize*distanceToCenter, 256.0);
    hsv.y = mod(hsv.y + shiftSaturation*255.0 + centerize*distanceToCenter, 256.0);
    hsv.z = mod(hsv.z + shiftValue*255.0 + centerize*distanceToCenter, 256.0);
    hsv /= 255.0;

    return vec4(hsv2rgb(hsv), 1.0);
}

void main()
{
    vec4 unmodifiedColor = texture2D(texture0, texCoord) * color;
    if (transition <= 0.0) {
        fragColor = unmodifiedColor;
        return;
    }

    vec2 uv = texCoord;

    uv = mix(uv, deform(uv), deformTransition);
    uv = mix(uv, kaleidoscope(uv), kaleidoscopeTransition);
    uv = mix(uv, borderSkew(uv), borderSkewTransition);
    uv = mix(uv, waveDistortion(uv), waveTransition);
    uv = mix(uv, twirl(uv), twirlTransition);

    fragColor = texture2D(texture0, uv);
    vec4 colorCycleResult = colorcycle(uv);
    fragColor = mix(fragColor, colorCycleResult, colorCycleTransition);

    fragColor *= color;
    fragColor = mix(unmodifiedColor, fragColor, transition);
    //fragColor.r *= transition;
}
