How to make a screen based outline shader?
Asked Answered
A

2

0

I found an outline shader that works but I have to manually attach it to every node or use a viewport to draw it over a set of nodes, However this messes up my mouse position and is a chore to deal with in general. How can I convert this shader to instead work on a ColorRect that covers the whole screen?
Note that my background only has 1 colour and can easily be selected as the colour to be transparent.

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);
}

I want to shader to draw an outline over everything that is being rendered including particles and text

Abrade answered 30/3, 2023 at 2:21 Comment(0)
B
0

For Godot 4.0, check this: https://docs.godotengine.org/en/4.0/tutorials/shaders/custom_postprocessing.html

Bigmouth answered 30/3, 2023 at 3:29 Comment(0)
S
0

Made by the tutorial from the previous version:

Scarron answered 30/3, 2023 at 15:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.