//-----------------------------------------------------------------------------------------------
// Basic Text-rendering Fragment (Pixel) shader
//
#version 140


//-----------------------------------------------------------------------------------------------
uniform sampler2D u_diffuseTexture;
uniform float u_time;
uniform float u_edgeThreshold;
uniform float u_softness;


//-----------------------------------------------------------------------------------------------
in vec4 v_surfaceColor;
in vec2 v_texCoords;
in float v_stringFraction;
in float v_charIndex;
in float v_cellHeight;


//-----------------------------------------------------------------------------------------------
out vec4 o_fragColor;


//-----------------------------------------------------------------------------------------------
void main()
{
	vec4 diffuseTexel = texture( u_diffuseTexture, v_texCoords );
	o_fragColor = diffuseTexel * v_surfaceColor;

//	float threshold = 0.5; // 0.5 + 0.5 * sin( u_time );
//	float outerThreshold = 0.6;// + 0.1 * sin( u_time );
//	float innerThreshold = 1.0;// + 0.1 * sin( u_time );
	if( diffuseTexel.a < u_edgeThreshold )
		discard;

	float innerThreshold = (1.0 * u_softness) + (1.0 - u_softness) * (u_edgeThreshold + 0.01);
	float alpha = (diffuseTexel.a - u_edgeThreshold) / (innerThreshold - u_edgeThreshold);
	alpha = clamp( alpha, 0.0, 1.0 );
	alpha = (1.0 - alpha);
	alpha *= alpha;
	alpha = (1.0 - alpha);
	o_fragColor = vec4( v_surfaceColor.rgb, alpha * v_surfaceColor.a );
/*
	float threshold = 0.5 + 0.5 * sin( u_time );
	if( diffuseTexel.g < threshold )
		discard;

	float alpha = (diffuseTexel.g - threshold) / (1.0 - threshold);
	alpha = (1.0 - alpha);
	alpha *= alpha;
	alpha *= alpha;
	alpha *= alpha;
	alpha = (1.0 - alpha);

//	float fraction = v_charIndex - floor( 0.1 * v_charIndex + 3.0 * v_stringFraction );
//	float darkening = 0.5 + 0.5 * sin( 8.0 * u_time + 3.0 * v_cellHeight + 30.0 * v_stringFraction );

	float charIndex = floor( v_charIndex );
	float cyclicTime = mod( 12.0 * u_time, 50.0 );
//	if( charIndex > cyclicTime )
//		discard;

	float charAge = 0.1 * (cyclicTime - charIndex);
	float brightening = 1.0 - clamp( charAge, 0.0, 1.0 );
	float darkening = 1.0 - brightening;
	vec3 color = (brightening * vec3( 1, 1, 1 ) + darkening * v_surfaceColor.rgb);
	
//	float timeFloor = 0.5 * floor( mod( u_time * 2.0, 50.0 ) );

//	o_fragColor = vec4( v_surfaceColor.rgb, alpha );
//	o_fragColor = vec4( color, alpha );
//	o_fragColor.r *= darkening;
	o_fragColor = v_surfaceColor;
*/
}

