precision mediump float;

uniform sampler2D texture;
uniform float useCustomColor;
uniform vec4 color;
uniform float alpha;
uniform float roundValue;
uniform float visibleAmount;

varying vec2 pass_TexCoords;

float isInRoundedRange();
float isInVisiblePart();

void main() {

    if(isInRoundedRange() == 1.0 && isInVisiblePart() == 1.0){
        if(useCustomColor == 1.0){
            gl_FragColor = color;
        } else {
            gl_FragColor = texture2D(texture, pass_TexCoords);
        }
        gl_FragColor.a = min(gl_FragColor.a, alpha);
        gl_FragColor.rgb = gl_FragColor.rgb / gl_FragColor.a;
    } else {
        gl_FragColor = vec4(0.0, 0.0, 0.0, 0.0);
    }

}

float isInRoundedRange(){
    if(roundValue == 0.0)
        return 1.0;
    float relativeX = abs((pass_TexCoords.x- 0.5));
    float relativeY = abs(pass_TexCoords.y - 0.5);
    float circleX = abs(roundValue * 0.5 - 0.5);
    float circleY = abs(roundValue * 0.5 - 0.5);
    if(relativeX < circleX || relativeY < circleY)
        return 1.0;
    float radius = roundValue * 0.5;
    float distX = relativeX - circleX;
    float distY = relativeY - circleY;
    if(distX * distX + distY * distY > radius * radius)
        return 0.0;
    return 1.0;
}

float isInVisiblePart(){
    if(pass_TexCoords.x >= visibleAmount)
        return 1.0;
    return 0.0;
}