56 lines
1.6 KiB
GLSL
56 lines
1.6 KiB
GLSL
#version 330 core
|
|
out vec4 pixel_color;
|
|
|
|
in vec4 frag_color;
|
|
in vec2 frag_dest_position;
|
|
in vec2 frag_dest_center;
|
|
in vec2 frag_dest_half_size;
|
|
in float frag_softness;
|
|
in float frag_border_radius;
|
|
in float frag_border_thickness;
|
|
|
|
float roundedRectSDF(vec2 sample_pos, vec2 rect_center, vec2 rect_half_size, float r) {
|
|
vec2 d2 = (abs(rect_center - sample_pos) - rect_half_size + vec2(r, r));
|
|
return min(max(d2.x, d2.y), 0.0) + length(max(d2, 0.0)) - r;
|
|
}
|
|
|
|
void main() {
|
|
vec2 softness_padding = vec2(
|
|
max(0, frag_softness*2-1),
|
|
max(0, frag_softness*2-1));
|
|
|
|
float border_factor = 1.0f;
|
|
if (frag_border_thickness != 0) {
|
|
vec2 interior_half_size = frag_dest_half_size - vec2(frag_border_thickness);
|
|
|
|
float interior_radius_reduce_f = min(
|
|
interior_half_size.x / frag_dest_half_size.x,
|
|
interior_half_size.y / frag_dest_half_size.y);
|
|
|
|
float interior_corner_radius = frag_border_radius * interior_radius_reduce_f * interior_radius_reduce_f;
|
|
|
|
float inside_d = roundedRectSDF(
|
|
frag_dest_position,
|
|
frag_dest_center,
|
|
interior_half_size - softness_padding,
|
|
interior_corner_radius);
|
|
|
|
|
|
float inside_f = smoothstep(0, 2*frag_softness, inside_d);
|
|
border_factor = inside_f;
|
|
}
|
|
|
|
float dist = roundedRectSDF(
|
|
frag_dest_position,
|
|
frag_dest_center,
|
|
frag_dest_half_size - softness_padding,
|
|
frag_border_radius);
|
|
|
|
// For texturing later
|
|
float sample = 1;
|
|
|
|
float sdf_factor = 1 - smoothstep(0, 2*frag_softness, dist);
|
|
|
|
pixel_color = frag_color * sample * sdf_factor * border_factor;
|
|
};
|