Files
OpenVulkano/openVulkanoCpp/Shader/msdfText.frag
2025-01-05 02:05:10 +01:00

41 lines
1002 B
GLSL

#version 450
layout(location = 0) in vec4 color;
layout(location = 1) in vec4 bgColor;
layout(location = 2) in vec2 texCoord;
layout(location = 0) out vec4 outColor;
layout(set = 2, binding = 0) uniform sampler2D texSampler;
float median(float r, float g, float b)
{
return max(min(r, g), min(max(r, g), b));
}
// this parameter should be same as FontAtlasGeneratorConfig::pixelRange
const float pxRange = 3;
float screenPxRange()
{
vec2 unitRange = vec2(pxRange) / vec2(textureSize(texSampler, 0));
vec2 screenTexSize = vec2(1.0) / fwidth(texCoord);
return max(0.5 * dot(unitRange, screenTexSize), 1.0);
}
void main()
{
vec3 msd = texture(texSampler, texCoord).rgb;
float sd = median(msd.r, msd.g, msd.b);
float screenPxDistance = screenPxRange() * (sd - 0.5);
float opacity = clamp(screenPxDistance + 0.5, 0.0, 1.0);
if (bgColor.a != 0)
{
outColor = mix(bgColor, color, opacity);
}
else
{
outColor = vec4(vec3(color), opacity);
}
}