#version 300 es
precision mediump int;
precision mediump float;

in vec4 ambientUV;
in vec3 ray;

out vec4 fragColour;

uniform vec4 invTexSize;
uniform sampler2D map;
uniform sampler2D geomMap;

#define NUM_BLUR_SAMPLES 8.0

void main()
{
    vec2 o = vec2(invTexSize.x, 0.0);
    vec4 sum = textureLod(map, ambientUV.xy, ambientUV.w) * (NUM_BLUR_SAMPLES + 1.0);
    float denom = NUM_BLUR_SAMPLES + 1.0;
    vec4 geom = textureLod(geomMap, ambientUV.xy, ambientUV.w);
    for (int i = 1; i <= int(NUM_BLUR_SAMPLES); ++i)
    {
        vec2 nuv = ambientUV.xy + o * float(i);
        vec4 nGeom = textureLod(geomMap, nuv, 0.0);
        float coef = (NUM_BLUR_SAMPLES + 1.0 - float(i)) * step(0.9, (dot(geom.xyz, nGeom.xyz)));
        sum += textureLod(map, nuv, 0.0) * coef;
        denom += coef;
    }
    for (int i = 1; i <= 4; ++i)
    {
        vec2 nuv = ambientUV.xy + o * float(-i);
        vec4 nGeom = textureLod(geomMap, nuv, 0.0);
        float coef = (NUM_BLUR_SAMPLES + 1.0 - float(i)) * step(0.9, (dot(geom.xyz, nGeom.xyz)));
        sum += textureLod(map, nuv, 0.0) * coef;
        denom += coef;
    }
    fragColour = sum / denom;
}
