Higher Detail Outline Shader

The same as this: https://godotshaders.com/shader/2d-outline-stroke/ but with more detail.

feel free to add suggestions for improvement.

Shader code
shader_type canvas_item;

uniform vec4 line_color : hint_color = vec4(1);
uniform float line_thickness : hint_range(0, 10) = 1.0;

void fragment() {
	vec2 size = TEXTURE_PIXEL_SIZE * line_thickness;
	
	float outline = texture(TEXTURE, UV + vec2(-size.x, 0)).a;
	outline += texture(TEXTURE, UV + vec2(0, size.y)).a;
	
	outline += texture(TEXTURE, UV + vec2(size.x, 0)).a;
	
	outline += texture(TEXTURE, UV + vec2(0, -size.y)).a;
	
	
	outline += texture(TEXTURE, UV + vec2(-size.x, size.y)).a;
	
	outline += texture(TEXTURE, UV + vec2(-size.x, size.y * 0.5)).a;
	
	
	
	outline += texture(TEXTURE, UV + vec2(size.x, size.y)).a;
	outline += texture(TEXTURE, UV + vec2(size.x, size.y * 0.5)).a;
	
	
	outline += texture(TEXTURE, UV + vec2(-size.x, -size.y)).a;
	outline += texture(TEXTURE, UV + vec2(-size.x, -size.y * 0.5)).a;
	
	
	
	outline += texture(TEXTURE, UV + vec2(size.x, -size.y)).a;
	outline += texture(TEXTURE, UV + vec2(size.x, -size.y * 0.5)).a;
	
	
	outline = min(outline, 1.0);
	
	
	vec4 color = texture(TEXTURE, UV);
	COLOR = mix(color, line_color, outline - color.a);
}
The shader code and all code snippets in this post are under CC0 license and can be used freely without the author's permission. Images and videos, and assets depicted in those, do not fall under this license. For more info, see our License terms.

More from axilirate

Stylized Sky Shader With Clouds For Godot 4

Simple Energy Shield

Pixel Perfect outline Shader

Related shaders

2D Outline and Rainbow outline 2 in 1

Reupload of outline shader

Smooth outline 2D with enable disable system

Subscribe
Notify of
guest

2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Torguen
Torguen
1 year ago

Good shader for outlines, thanks!.

ArtcadeDev
1 year ago

How can I have this shader work on a ColorRect and draw an outline for anything underneath it instead of adding this shader to every object you need an outline for?