#include "depth_normal.fsh"
precision mediump float;

uniform sampler2D texUnit;

uniform vec2 range;
uniform vec2 perTexelDis;
uniform float maxRadius;
uniform float density;

in vec2 UV_f;

layout(location = 0) out vec4 fragColor;

const int kTapCount = 6;
const float kOffsets[] = float[6](
   -5.176470588235294, -3.2941176470588234, -1.411764705882353,
    1.411764705882353,  3.2941176470588234,  5.176470588235294
);
const float kCoeffs[] = float[6](
    0.010381362401148057, 0.09447039785044732, 0.2969069646728344,
    0.2969069646728344, 0.09447039785044732, 0.010381362401148057
);

void main()
{
    float depth = _SampleDepth(UV_f);
    vec4 color = texture(texUnit, UV_f);
    float coc = clamp((depth - range.x) / range.y, 0.0, 1.0);
    float smoothCoc = smoothstep(0.0, 1.0, coc);
    float alpha = clamp(smoothCoc + step(perTexelDis.y, 0.0), 0.0, 1.0);
    vec2 offset = perTexelDis * maxRadius * density;
    vec3 acc = color.rgb * 0.1964825501511404;
    for (int i = 0; i < kTapCount; i++)
    {
        vec2 sampCoord = UV_f + kOffsets[i] * offset * alpha;
        vec3 sampColor = texture(texUnit, sampCoord).rgb;
        acc += sampColor * kCoeffs[i];
    }
    fragColor = vec4(acc, color.a) * alpha;
}
