//.template
//.name /*: name*/
//.category Operators; /*: category*/
//.param float opacity; 0.0; 1.0; Opacity

//.input sampler2D tex0; Bottom layer
//.input sampler2D tex1; Top layer
//.optional input sampler2D tex2; mask; Alpha mask

uniform sampler2D tex0;
uniform sampler2D tex1;
uniform sampler2D tex2;
uniform int mask;

uniform float opacity;

/*: functions*/

void main(void)
{
	vec4 t0 = texture2D(tex0, gl_TexCoord[0].st);
	vec4 t1 = texture2D(tex1, gl_TexCoord[0].st);
	vec4 t2 = texture2D(tex2, gl_TexCoord[0].st);

	vec3 a = t0.rgb;
	vec3 b = t1.rgb;

	
	float af = (mask > 0 ? opacity * (t2.r + t2.g + t2.b) / 3.0 * t2.a : opacity) * t1.a;
	float ab = t0.a;
	float ar = af + (1.0 - af) * ab;

	gl_FragColor = vec4(mix(a * ab, clamp(
  
  
  1.0 - ((1.0 - a) * (1.0 - b))
  
  
  , 0.0, 1.0), af), ar);
}
