uniform float u_Time;
uniform mat4 u_Color;

varying vec2 v_TexCoord;
varying vec2 v_TexCoord2;
varying vec2 v_TexCoord3;

uniform sampler2D u_Tex0;
uniform sampler2D u_Tex1;

vec4 EmbossFull(sampler2D txt, vec2 uv, float angle, float dist, float intensity, float g, float o, vec4 color)
{
    angle = angle * 3.1415926;
    intensity = intensity * .25;
    #define rot(n)(n * mat2(cos(angle), -sin(angle), sin(angle), cos(angle)))

    float m1 = 0., m2 = 0., m3 = 0.;
    float m4 = 0., m5 = 1., m6 = 0.;
    float m7 = 0., m8 = 0., m9 = -1.;

    vec4 r = vec4(0.0);
    dist = dist * .005;

    vec4 rgb = texture2D(txt, uv);

    r += texture2D(txt, uv + rot(vec2(-dist,-dist))) * m1 * intensity;
    r += texture2D(txt, uv + rot(vec2( 0.0,-dist))) * m2 * intensity;
    r += texture2D(txt, uv + rot(vec2( dist,-dist))) * m3 * intensity;
    r += texture2D(txt, uv + rot(vec2(-dist, 0.0))) * m4 * intensity;
    r += texture2D(txt, uv)                    * m5 * intensity;
    r += texture2D(txt, uv + rot(vec2( dist, 0.0))) * m6 * intensity;
    r += texture2D(txt, uv + rot(vec2(-dist, dist))) * m7 * intensity;
    r += texture2D(txt, uv + rot(vec2( 0.0, dist))) * m8 * intensity;
    r += texture2D(txt, uv + rot(vec2( dist, dist))) * m9 * intensity;

    r = clamp(r, 0.0, 1.0);
    r = r * 6.0;
    r.a = rgb.a;

    return r * color;
}

vec4 EmbossOutline(sampler2D txt, vec2 uv, float angle, float dist, float intensity, float g, float o, vec4 color)
{
    angle = angle * 3.1415926;
    intensity = intensity * .25;
    #define rot(n)(n * mat2(cos(angle), -sin(angle), sin(angle), cos(angle)))

    float m1 = 0., m2 = 0., m3 = 0.;
    float m4 = 0., m5 = 1., m6 = 0.;
    float m7 = 0., m8 = 0., m9 = -1.;

    vec4 r = color;
    dist = dist * .005;

    vec4 rgb = texture2D(txt, uv);

    r += texture2D(txt, uv + rot(vec2(-dist,-dist))) * m1 * intensity;
    r += texture2D(txt, uv + rot(vec2( 0.0,-dist))) * m2 * intensity;
    r += texture2D(txt, uv + rot(vec2( dist,-dist))) * m3 * intensity;
    r += texture2D(txt, uv + rot(vec2(-dist, 0.0))) * m4 * intensity;
    r += texture2D(txt, uv)                    * m5 * intensity;
    r += texture2D(txt, uv + rot(vec2( dist, 0.0))) * m6 * intensity;
    r += texture2D(txt, uv + rot(vec2(-dist, dist))) * m7 * intensity;
    r += texture2D(txt, uv + rot(vec2( 0.0, dist))) * m8 * intensity;
    r += texture2D(txt, uv + rot(vec2( dist, dist))) * m9 * intensity;

    r = clamp(r, 0.0, 1.0);
    r = r * 6.0;
    r.a = rgb.a;

    return r;
}

vec4 ShadowLight(sampler2D source, vec2 uv, float precisao, float size, vec4 color, float intensity, float posx, float posy, float fade)
{
    float samples = precisao;
    float samples2 = samples * .5;

    vec4 ret = vec4(0.0);
    float count = 0.0;

    for(float y = -samples2; y < samples2; y++)
    {
        for(float x = -samples2; x < samples2; x++)
        {
            vec2 uv2 = vec2(x,y) / samples;
            uv2 *= size * .1;
            uv2 += vec2(-posx, posy);
            ret += texture2D(source, uv + uv2);
            count++;
        }
    }

    ret = mix(vec4(0.0), ret / count, intensity);
    ret.rgb = color.rgb;

    vec4 m = ret;
    gl_FragColor = texture2D(source, uv);

    vec4 texcolor = texture2D(u_Tex0, v_TexCoord2);

    if(texcolor.r > .9){
        gl_FragColor *= texcolor.g > .9 ? u_Color[0] : u_Color[1];
    } else if(texcolor.g > .9){
        gl_FragColor *= u_Color[2];
    } else if(texcolor.b > .9){
        gl_FragColor *= u_Color[3];
    }

    ret = mix(ret, gl_FragColor, gl_FragColor.a);
    ret = mix(m, ret, fade);

    return ret;
}

void main(void)
{
    gl_FragColor = texture2D(u_Tex0, v_TexCoord);

    float angle = u_Time;
    float dist = .3;
    float intensity = 3.0;
    float grayfade = 2.0;
    float original = 1.0;
    float spriteFade = 1.0;

    vec4 iceColor = vec4(
    0.5 + 0.5 * sin(u_Time),
    0.5 + 0.5 * sin(u_Time + 2.094),
    0.5 + 0.5 * sin(u_Time + 4.188),
    1.0);


    vec4 emboss = EmbossFull(
        u_Tex0,
        v_TexCoord,
        angle,
        dist,
        intensity,
        grayfade,
        original,
        iceColor
    );

    vec4 finalResult = emboss;
    finalResult.a *= spriteFade;

    float shadowPrec = 14.311;
    float shadowSize = .05;

    vec4 outline = EmbossOutline(
        u_Tex0,
        v_TexCoord,
        angle,
        dist,
        intensity,
        grayfade,
        original,
        iceColor
    );

    float shadowIntensity = 0.5;

    vec4 shadow = ShadowLight(
        u_Tex0,
        v_TexCoord,
        shadowPrec,
        shadowSize,
        outline,
        shadowIntensity,
        0.0,
        0.0,
        1.0
    );

    finalResult += shadow;
    finalResult.a *= spriteFade;

    gl_FragColor = finalResult;
}
