#version 420

layout(binding = 0) uniform sampler2D screenshot_tex;
uniform vec4 vp;
uniform vec2 wiper_pivot[2];
uniform float wiper_radius_outer[2];
uniform float wiper_radius_inner[2];
uniform float wiper_pos[2];
layout(binding = 0) uniform sampler2D depth_tex;
layout(binding = 0) uniform sampler2D norm_tex;
uniform int num_wipers;

layout(location = 1) in vec2 tex_coord;
layout(location = 0) out vec4 color_out;
layout(location = 0) in vec3 tex_norm;

vec4 get_pixel(inout vec2 pos)
{
    vec2 sz = vec2(textureSize2D(screenshot_tex, 0));
    pos /= sz;
    pos = clamp(pos, vec2(0.0), vec2(vp.zw / sz) - vec2(0.001000000047497451305389404296875));
    return texture(screenshot_tex, pos);
}

float dir2hdg(vec2 v)
{
    if (v.y >= 0.0)
    {
        return asin(v.x / length(v));
    }
    else
    {
        if (v.x >= 0.0)
        {
            return 3.1415927410125732421875 - asin(v.x / length(v));
        }
        else
        {
            return (-3.1415927410125732421875) - asin(v.x / length(v));
        }
    }
}

bool check_wiper(int i)
{
    vec2 wiper2pos = tex_coord - wiper_pivot[i];
    float dist = length(wiper2pos);
    bool _129 = abs(dist - wiper_radius_outer[i]) < 0.00200000009499490261077880859375;
    bool _141;
    if (!_129)
    {
        _141 = abs(dist - wiper_radius_inner[i]) < 0.00200000009499490261077880859375;
    }
    else
    {
        _141 = _129;
    }
    if (_141)
    {
        return true;
    }
    vec2 param = wiper2pos;
    float hdg = dir2hdg(param);
    bool _154 = dist <= wiper_radius_outer[i];
    bool _162;
    if (_154)
    {
        _162 = dist >= wiper_radius_inner[i];
    }
    else
    {
        _162 = _154;
    }
    bool _174;
    if (_162)
    {
        _174 = abs(hdg - wiper_pos[i]) <= 0.01745329238474369049072265625;
    }
    else
    {
        _174 = _162;
    }
    if (_174)
    {
        return true;
    }
    return false;
}

void main()
{
    float depth = texture(depth_tex, tex_coord).x;
    vec2 norm_v = (texture(norm_tex, tex_coord).xy * 2.0) - vec2(1.0);
    vec2 displace = norm_v * vec2(200.0);
    vec2 param = gl_FragCoord.xy + displace;
    vec4 _212 = get_pixel(param);
    vec4 bg_pixel = _212;
    float displace_fract = length(displace) / 800.0;
    bg_pixel *= (1.0 - ((displace_fract / 2.0) * (displace_fract / 2.0)));
    color_out = vec4(bg_pixel.xyz, 1.0);
    if (num_wipers > 0)
    {
        for (int i = 0; i < num_wipers; i++)
        {
            int param_1 = i;
            if (check_wiper(param_1))
            {
                color_out += vec4(0.60000002384185791015625, 0.0, 0.0, 0.0);
                break;
            }
        }
    }
}

