Created
January 1, 2025 17:37
-
-
Save maidopi-usagi/66aaaf63ff0814ddb8b0c44dac6696df to your computer and use it in GitHub Desktop.
Godot AtlasTexture Outline Shader
This file contains hidden or 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
| shader_type canvas_item; | |
| const vec2 VERTICES[4] = vec2[](vec2(0.0),vec2(0.0,1.0),vec2(1.0),vec2(1.0,0.0)); | |
| varying vec2 originalUV; | |
| varying flat vec4 regionInfo; | |
| uniform int outline_size:hint_range(0,10) = 1; | |
| uniform vec4 outline_color:source_color = vec4(1.0); | |
| void vertex() { | |
| originalUV= VERTICES[VERTEX_ID]; | |
| regionInfo.xy = VERTEX.xy/(VERTICES[0] - 0.5); | |
| regionInfo.zw = UV; | |
| VERTEX.xy += vec2(float(outline_size)*2.0) * (originalUV - vec2(0.5)); | |
| } | |
| vec2 region_from_texture(vec2 uv, vec2 size, vec4 region) { | |
| return region.zw + uv / size * region.xy; | |
| } | |
| vec2 texture_from_region(vec2 uv, vec2 size, vec4 region) { | |
| return uv * size / region.xy - region.zw; | |
| } | |
| vec4 sample_tex(sampler2D t, vec2 offset, vec2 size, vec2 uv_to_transform) | |
| { | |
| vec2 uv = uv_to_transform + offset; | |
| bool inside = uv.x <= 1.0 && uv.x >= 0.0 && uv.y >= 0.0 && uv.y <= 1.0; | |
| if (!inside) return vec4(0.0); | |
| vec2 restoredUV = region_from_texture(uv, size, regionInfo); | |
| return texture(t, restoredUV); | |
| } | |
| void fragment() { | |
| vec2 size = vec2(textureSize(TEXTURE, 0)); | |
| vec2 atlas_size = regionInfo.xy; | |
| vec2 scaled_ratio = (atlas_size + vec2(float(outline_size) * 2.0)) / atlas_size; | |
| vec2 uv = (originalUV - 0.5) * scaled_ratio + 0.5; | |
| float outline = 0.0; | |
| vec2 offset_ratio = float(outline_size) / atlas_size; | |
| outline = max(sample_tex(TEXTURE, vec2(0.0, -1.0) * offset_ratio, size, uv).a, outline); | |
| outline = max(sample_tex(TEXTURE, vec2(1.0, -0.0) * offset_ratio, size, uv).a, outline); | |
| outline = max(sample_tex(TEXTURE, vec2(-1.0, 0.0) * offset_ratio, size, uv).a, outline); | |
| outline = max(sample_tex(TEXTURE, vec2(0.0, 1.0) * offset_ratio, size, uv).a, outline); | |
| vec4 tex = sample_tex(TEXTURE, vec2(0.0), size, uv); | |
| COLOR = mix(vec4(outline_color.rgb, outline * outline_color.a), tex, tex.a); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A wip implementation