Files
Hazel/Editor/assets/shaders/Renderer2D_text.glsl
2025-10-30 23:32:20 +08:00

69 lines
1.7 KiB
GLSL

// Basic MSDF Text Texture Shader
#type vertex
#version 450 core
layout(location = 0) in vec3 a_Position;
layout(location = 1) in vec4 a_Color;
layout(location = 2) in vec2 a_TexCoord;
layout(location = 3) in int a_EntityID;
layout(std140, binding = 0) uniform Camera
{
mat4 u_ViewProjection;
};
layout (location = 0) out vec4 o_Color;
layout (location = 1) out vec2 o_TexCoord;
layout (location = 2) out flat int v_EntityID;
void main()
{
o_Color = a_Color;
o_TexCoord = a_TexCoord;
v_EntityID = a_EntityID;
gl_Position = u_ViewProjection * vec4(a_Position, 1.0);
}
#type fragment
#version 450 core
layout(location = 0) out vec4 o_Color;
layout(location = 1) out int o_EntityID;
layout (location = 0) in vec4 i_Color;
layout (location = 1) in vec2 i_TexCoord;
layout (location = 2) in flat int v_EntityID;
layout (binding = 0) uniform sampler2D u_FontAtlas;
float screenPxRange() {
const float pxRange = 2.0; // set to distance field's pixel range
vec2 unitRange = vec2(pxRange)/vec2(textureSize(u_FontAtlas, 0));
vec2 screenTexSize = vec2(1.0)/fwidth(i_TexCoord); // 使用 i_TexCoord
return max(0.5*dot(unitRange, screenTexSize), 1.0);
}
float median(float r, float g, float b) {
return max(min(r, g), min(max(r, g), b));
}
void main()
{
vec4 texColor = i_Color * texture(u_FontAtlas, i_TexCoord); // 使用 i_Color 和 i_TexCoord
vec3 msd = texture(u_FontAtlas, i_TexCoord).rgb; // 使用 i_TexCoord
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 (opacity == 0.0)
discard;
vec4 bgColor = vec4(0.0);
o_Color = mix(bgColor, i_Color, opacity); // 使用 i_Color
if (o_Color.a == 0.0)
discard;
o_EntityID = v_EntityID;
}