uniform sampler2D tex0;
uniform sampler2D texMask;
uniform float fade;

void main(void)
{
	vec4 texel = texture2D(tex0, gl_TexCoord[0]);
	vec4 mask = texture2D(texMask, gl_TexCoord[0]);

	//gl_FragData[0] = vec4(texel.rgb * gl_Color.rgb, texel.a * gl_Color.a);

	float window = 0.05;

	if (mask.r < fade - window * 0.5)
	{
		gl_FragData[0] = texel;		
	}
	else if (mask.r >= fade - window * 0.5 && mask.r <= fade + window * 0.5)
	{
		gl_FragData[0] = texel * vec4(1.0,1.0,1.0, (1.0 - (mask.r - (fade - window * 0.5)) / window)) * gl_Color;
	}
	else
	{
		discard;
	}
}


