Extra2D/Extra2D/shaders/backends/opengl/builtin/sdf_font.frag

40 lines
1.0 KiB
GLSL
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#version 300 es
precision highp float;
in vec2 v_texCoord;
in vec4 v_color;
uniform sampler2D u_texture;
uniform float u_opacity;
uniform float u_sdfThreshold;
uniform float u_sdfSmoothness;
uniform vec2 u_textureSize;
out vec4 fragColor;
void main() {
// 采样 SDF 纹理SDF 值存储在 alpha 通道,范围 0-255 已映射到 0-1
float sdfValue = texture(u_texture, v_texCoord).a;
// 使用 fwidth 计算屏幕空间的变化率
float fw = fwidth(sdfValue);
// 平衡的抗锯齿:根据屏幕空间变化率调整平滑范围
// 在放大时更平滑,缩小时更锐利
float smoothRange = max(u_sdfSmoothness, fw * 0.5);
// 使用 smoothstep 进行抗锯齿
float alpha = smoothstep(u_sdfThreshold - smoothRange,
u_sdfThreshold + smoothRange,
sdfValue);
// 应用颜色和透明度
fragColor = v_color;
fragColor.a *= alpha * u_opacity;
// 丢弃完全透明的像素
if (fragColor.a < 0.001) {
discard;
}
}