precision highp float;

// these are our textures
uniform sampler2D msdf_0;
uniform sampler2D msdf_1;

uniform vec4 fontColor;
uniform float msdf_mix;
uniform float msdf_pxratio;

// this comes from the vertex shader
varying vec2 texCoordVarying;

float median(float r, float g, float b) {
    return max(min(r, g), min(max(r, g), b));
}

float screenPxRange(float ratio) {
    return ratio * 2.0;
}

void main()
{
    vec4 bgColor = vec4(fontColor.rgb,0);
    vec4 fgColor = fontColor;

    vec3 msd_0 = texture2D(msdf_0, texCoordVarying).rgb;
    vec3 msd_1 = texture2D(msdf_1, texCoordVarying).rgb;
    vec3 msd = mix(msd_0, msd_1,msdf_mix);
    float sd = median(msd.r, msd.g, msd.b);
    float screenPxDistance = screenPxRange(msdf_pxratio)*(sd - 0.5);
    float opacity = clamp(screenPxDistance + 0.5, 0.0, 1.0);
    gl_FragColor = mix(bgColor, fgColor, opacity);
}
