#version 450

layout (binding = 1) uniform sampler2D samplerColor;

struct Gui {
	vec4 offset;
	vec4 color;
	int mode;
	float depth;
	int sdf;
	int shadowbox;
	vec4 stroke;
	float weight;
	int spread;
	float scale;
};

layout (location = 0) in vec2 inUv;
layout (location = 1) in flat Gui inGui;
layout (location = 0) out vec4 outFragColor;

void main() {
	if ( inUv.x < inGui.offset.x ) discard;
	if ( inUv.y < inGui.offset.y ) discard;
	if ( inUv.x > inGui.offset.z ) discard;
	if ( inUv.y > inGui.offset.w ) discard;

	if ( inGui.shadowbox == 1 ) {
		outFragColor = inGui.color;
		return;
	}
	float dist = texture(samplerColor, inUv).r;
//	outFragColor = mix(vec4(inGui.color) * dist, vec4(1.0f, 0.0f, 1.0f, 0.5f), 0.9);

	if ( inGui.sdf == 1 ) {
		float smoothing = ( inGui.spread > 0 && inGui.scale > 0 ) ? 0.25 / (inGui.spread * inGui.scale) : 0.25 / (4 * 1.5);
		float outlining = smoothstep(0.5 - smoothing, 0.5 + smoothing, dist);
		float alpha = smoothstep(inGui.weight - smoothing, inGui.weight + smoothing, dist);
		vec4 c = inGui.color;
		outFragColor = mix(inGui.stroke, c, outlining);
		outFragColor.a = inGui.color.a * alpha;
		if ( alpha < 0.001 ) discard;
		if ( alpha > 1 ) discard;
	} else {
		outFragColor = vec4(inGui.color) * dist;
		if ( dist < 0.001 ) discard;
		if ( dist > 1 ) discard;
	}
}