summaryrefslogtreecommitdiff
path: root/assets/shaders/glyph.frag
blob: 986792596e6a842eae420fa46c752cd8de6d4782 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# version 450

layout(location = 0) in vec4 in_fg_color;
layout(location = 1) in vec4 in_bg_color;
layout(location = 2) in vec2 in_bounding_region_min;
layout(location = 3) in vec2 in_bounding_region_max;
layout(location = 4) in vec2 in_uv;
layout(location = 5) in vec2 in_screen_px;
layout(location = 6) in float in_width;

layout(location = 0) out vec4 out_color;

layout(binding = 0) uniform sampler2D mtsdf_sampler;

float median(float r, float g, float b) {
	return max(min(r, g), min(max(r, g), b));
}

void main() {
	if (in_screen_px.x < in_bounding_region_min.x) discard;
	if (in_screen_px.y < in_bounding_region_min.y) discard;
	if (in_screen_px.x > in_bounding_region_max.x) discard;
	if (in_screen_px.y > in_bounding_region_max.y) discard;
	vec4 msd = texture(mtsdf_sampler, in_uv);
	float sd = median(msd.r, msd.g, msd.b);
	float screenPxDistance = in_width * (sd - 0.5);
	float opacity = clamp(screenPxDistance + 0.5, 0.0, 1.0);
	out_color = mix(in_bg_color, in_fg_color, opacity);
}