Skip to content

Instantly share code, notes, and snippets.

@belzecue
Created January 6, 2024 16:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save belzecue/e930f389fa6973f6131a7f36134f1e6e to your computer and use it in GitHub Desktop.
Save belzecue/e930f389fa6973f6131a7f36134f1e6e to your computer and use it in GitHub Desktop.
Godot 3 SDF shader for clear decal edges at low resolutions, as used by Valve
// Generate high-resolution white on black text decal in GIMP and apply Filters > Generic > Distance Map
// then scale down to e.g. 64 pixel height.
// Import decal image into Godot and set:
// Compression mode to lossless
// Filter ON
// Mipmaps ON
// Fix alpha border ON
// Invert color ON (if required)
shader_type spatial;
uniform sampler2D sdf_tex;
uniform vec2 uv_scale = vec2(1.0);
uniform vec2 uv_offset = vec2(0.0);
uniform float thickness : hint_range(0.0, 1.0) = 0.5;
uniform float softness : hint_range(0.0, 1.0) = 0.0;
uniform vec4 color : hint_color;
uniform vec4 outline_col : hint_color;
uniform float outline_thickness : hint_range(0.0, 1.0) = 0.0;
uniform float outline_softness : hint_range(0.0, 1.0) = 0.0;
void fragment() {
float a = texture(sdf_tex, UV * uv_scale + uv_offset).r;
float outline = smoothstep(outline_thickness - outline_softness, outline_thickness + outline_softness, a);
a = smoothstep(1.0 - thickness - softness, 1.0 - thickness + softness, a);
ALPHA = a;
ALBEDO = mix(outline_col.rgb, color.rgb, outline);
}
@belzecue
Copy link
Author

belzecue commented Jan 6, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment