#version 420

layout(binding = 0) uniform sampler2D tex;
layout(binding = 0) uniform sampler2D temp_tex;
uniform float window_ice;
uniform vec2 tp;
uniform float thrust;
uniform float precip_intens;

layout(location = 0) out vec4 color_out;

float read_depth(vec2 offset)
{
    return texture(tex, (gl_FragCoord.xy + offset) / vec2(1024.0)).x;
}

float gold_noise(vec2 coordinate, float seed)
{
    return fract(sin(dot(coordinate * (seed + 0.1618033945560455322265625), vec2(0.1618033945560455322265625, 0.31415927410125732421875))) * 14142.1357421875);
}

void main()
{
    float temp = texture(temp_tex, gl_FragCoord.xy / vec2(1024.0)).x * 400.0;
    vec2 ice_displace = vec2(0.0);
    float window_ice_fact = sqrt(min(window_ice, 1.0));
    if (temp < 275.149993896484375)
    {
        float fact = smoothstep(271.149993896484375, 275.149993896484375, temp);
        vec2 param = vec2(0.0);
        float depth = pow(read_depth(param) / 3.0, 0.300000011920928955078125);
        fact *= depth;
        vec2 param_1 = gl_FragCoord.xy;
        float param_2 = 0.0;
        vec2 param_3 = gl_FragCoord.xy;
        float param_4 = 1.0;
        ice_displace = vec2((gold_noise(param_1, param_2) - 0.5) * fact, (gold_noise(param_3, param_4) - 0.5) * fact);
    }
    vec2 thrust_v = gl_FragCoord.xy - tp;
    thrust_v /= vec2(length(thrust_v));
    thrust_v *= ((20.0 * thrust) + 1.0);
    vec2 param_5 = vec2(-1.0, 0.0) * thrust_v;
    float depth_left = read_depth(param_5);
    vec2 param_6 = vec2(1.0, 0.0) * thrust_v;
    float depth_right = read_depth(param_6);
    vec2 param_7 = vec2(0.0, 1.0) * thrust_v;
    float depth_up = read_depth(param_7);
    vec2 param_8 = vec2(0.0, -1.0) * thrust_v;
    float depth_down = read_depth(param_8);
    float d_lr = ((atan(depth_left - depth_right) / 1.57079637050628662109375) * (1.0 + ice_displace.x)) + 0.5;
    d_lr = clamp(d_lr + (window_ice_fact * ice_displace.x), 0.0, 1.0);
    float d_ud = ((atan(depth_up - depth_down) / 1.57079637050628662109375) * (1.0 + ice_displace.y)) + 0.5;
    d_ud = clamp(d_ud + (window_ice_fact * ice_displace.y), 0.0, 1.0);
    color_out = vec4(d_lr, d_ud, 0.0, 1.0);
}

