#version 330

in vec4 pass_color;
in vec2 pass_tex;
in vec2 pass_pos;

out vec4 frag_color;

uniform sampler2D texture;
uniform float use_texture;
uniform vec4 bounds;
uniform float roundness;

const vec4 no_texture = vec4(1.0, 1.0, 1.0, 1.0);
const float cornerSmoothFactor = 0.55;

float square(float val) {
	return val * val;
}

float distanceSquared(vec2 p1, vec2 p2) {
	vec2 vector = p2 - p1;
	return dot(vector, vector);
}

float calcRoundedCorners() {
	if (roundness <= 0.0) return 1.0;
	return smoothstep(square(roundness + cornerSmoothFactor), square(roundness - cornerSmoothFactor), distanceSquared(pass_pos, clamp(pass_pos, vec2(bounds.x + roundness, bounds.y + roundness), vec2(bounds.z - roundness, bounds.w - roundness))));
}

void main()
{
	frag_color = pass_color * mix(no_texture, texture2D(texture, pass_tex), use_texture);
	frag_color.a *= calcRoundedCorners();
}