
vec2 GetOffsetFromCenter(vec2 screenCoords, vec2 screenSize)
{
    vec2 halfScreenSize = screenSize / 2.0;
    
	return (screenCoords.xy - halfScreenSize) / min(halfScreenSize.x, halfScreenSize.y);
}


float EffectDuration = 1.0;
float EffectFadeInTimeFactor = 0.5;
float EffectWidth = 0.4;
float EffectMaxTexelOffset = 20.0;

vec2 GetDistortionTexelOffset(vec2 offsetDirection, float offsetDistance, float time)
{
    float progress = mod(time, EffectDuration) / EffectDuration;
    
    float halfWidth = EffectWidth / 2.0;
    float lower = 1.0 - smoothstep(progress - halfWidth, progress, offsetDistance);
    float upper = smoothstep(progress, progress + halfWidth, offsetDistance);
    
    float band = 1.0 - (upper + lower);
    
    
    float strength = 1.0 - progress;
    float fadeStrength = smoothstep(0.0, EffectFadeInTimeFactor, progress);
    
    float distortion = band * strength * fadeStrength;
    
    
    return distortion * offsetDirection * EffectMaxTexelOffset;
}


vec3 GetTextureOffset(vec2 coords, vec2 textureSize, vec2 texelOffset)
{
    vec2 texelSize = 1.0 / textureSize;
    vec2 offsetCoords = coords + texelSize * texelOffset;
    
    vec2 halfTexelSize = texelSize / 2.0;
    vec2 clampedOffsetCoords = clamp(offsetCoords, halfTexelSize, 1.0 - halfTexelSize);
    
    return texture(InputTexture, clampedOffsetCoords).rgb;
}


//void mainImage(out vec4 result, in vec2 TexCoord)
void main()
{
    float time = NewTime;
    float time_scale = 0.0035;
	time *= time_scale;
    //vec2 screenCoords = fragCoord.xy;
    //vec2 screenSize = iResolution.xy;
	vec2 screenCoords = TexCoord;
    vec2 screenSize = textureSize(InputTexture, TexCoord);
    
    
    vec2 offsetFromCenter = GetOffsetFromCenter(screenCoords, screenSize);
    vec2 offsetDirection = normalize(-offsetFromCenter);
    float offsetDistance = length(offsetFromCenter);
    
    
    vec2 offset = GetDistortionTexelOffset(offsetDirection, offsetDistance, time);
    
    
    vec2 coords = (TexCoord / screenSize);
    coords.y = 1.0 - coords.y;
    
    vec3 background = GetTextureOffset(coords, screenSize, offset);
    
	FragColor = vec4(background, 1.0);
}
