shader_type canvas_item;
render_mode blend_premul_alpha;

/*
Shader that applies a stroke to the existing result.
*/

uniform sampler2D base;
uniform sampler2D stroke;
uniform sampler2D seams;
uniform sampler2D mask;
uniform bool erase;

void fragment() {
	vec2 uv = texture(mask, UV).a > 0.0 ? texture(seams, UV).xy : UV;;
	uv = UV; // disable seams
	vec4 stroke_color = texture(stroke, uv);
	vec4 base_color = texture(base, UV);
	if (erase) {
		COLOR = base_color;
		COLOR.a -= stroke_color.a;
		if (stroke_color.a > 0.001) {
			COLOR.rgb *= COLOR.a;
		}
	} else {
		COLOR.rgb = mix(base_color.rgb, stroke_color.rgb, stroke_color.a);
		COLOR.a = max(base_color.a, stroke_color.a);
	}
	//COLOR.a *= texture(mask, UV).a;
	COLOR.rgb /= COLOR.a;
}