// __multiversion__
// This signals the loading code to prepend either #version 100 or #version 300 es as apropriate.

#include "shaders/fragmentVersionCentroidUV.h"
#include "shaders/uniformMacro.h"

varying vec4 ColorOut;
varying vec4 AdditionalOut;

uniform sampler2D TEXTURE_0;

// On Text:
// x - glyphFactor
uniform vec4 PRIM_PROPS_0;

// Keep in sync w/ SDFGenerator
#define DISTANCE_FIELD_MULTIPLIER 7.96875
#define DISTANCE_FIELD_MULTIPLIER_DIV2 3.984375
#define DISTANCE_FIELD_THRESHOLD 0.50196078431

#define SHOW_DF 0

float GetLuminance(vec3 color) {
	return 0.2126 * color.r + 0.7152 * color.g + 0.0722 * color.b;
}

void main() {
	float dfValue = texture2D(TEXTURE_0, AdditionalOut.xy).r;
#if SHOW_DF
	gl_FragColor = vec4(dfValue.xxx, 1);
#else
	// Values should be in [-4, 4]
	dfValue = (dfValue * DISTANCE_FIELD_MULTIPLIER) - DISTANCE_FIELD_MULTIPLIER_DIV2;
	dfValue = smoothstep(-DISTANCE_FIELD_THRESHOLD / PRIM_PROPS_0.x, DISTANCE_FIELD_THRESHOLD / PRIM_PROPS_0.x, dfValue);

	float lum = 1.45 - GetLuminance(ColorOut.xyz);
	gl_FragColor = ColorOut * pow(dfValue, lum);
#endif
}
