Files
nunuhara_xsystem4/shaders/dilate.f.glsl
T
kichikuou b155b4b79f Add a parameter to gfx_draw_glyph() for controlling blending behavior
This adds a new parameter `blend` to `gfx_draw_glyph()`. When `blend` is
true, the function performs normal alpha blending (same as
`gfx_draw_glyph_to_pmap()`).

If `blend` is false, the resulting pixel color will be the text color
and the alpha value will be copied from the glyph texture. However, if
the alpha value is less than 0.01, the pixel will not be written.

If you are drawing on a transparent texture (which will later be blended
into another texture), `blend` should be false. If you are drawing on top
of an image, `blend` should be true.

`gfx_draw_glyph()` used to fill transparent pixels with text color, but
this step is no longer needed and has been removed.
2024-06-15 14:34:58 +09:00

50 lines
1.5 KiB
GLSL

/* Copyright (C) 2019 Nunuhara Cabbage <nunuhara@haniwa.technology>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://gnu.org/licenses/>.
*/
uniform sampler2D tex;
uniform float threshold;
uniform vec4 color; // .a is the discard threshold
in vec2 tex_coord;
out vec4 frag_color;
void main() {
// the fractional part of threshold becomes a weight value for the
// edge pixels
int size = int(floor(threshold));
int cutoff = size + 1;
float edge_weight = fract(threshold);
vec2 tex_size = vec2(textureSize(tex, 0).xy);
float a_out = 0.0;
for (int x = -cutoff; x <= cutoff; x++) {
for (int y = -cutoff; y <= cutoff; y++) {
float d = distance(vec2(x, y), vec2(0, 0));
if (d > threshold)
continue;
float a = texture(tex, tex_coord + vec2(x, y) / tex_size).r;
if (d > float(size))
a *= edge_weight;
a_out += a;
}
}
if (a_out < color.a)
discard;
frag_color = vec4(color.rgb, min(a_out, 1.0));
}