Created
January 6, 2024 16:39
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Valve's 2007 paper on this technique is here: https://steamcdn-a.akamaihd.net/apps/valve/2007/SIGGRAPH2007_AlphaTestedMagnification.pdf