Outline shader for 3d

Godot Version

4.2.1

Question

I’m trying to apply shader to create intractable outlines. It should be simple, but I have spent 3 hours with no result.

Me test scene is very simple: MeshInstance3D, BoxSize. I added New StandartMaterial3D in Surface Material Override section, than created New ShaderMaterial for Next Pass and simply pasted shader code from godotshaders.com

shader_type spatial;
render_mode unshaded;

uniform sampler2D SCREEN_TEXTURE : hint_screen_texture;
uniform vec4 outline_color : source_color;
uniform float outline_width = 2.0;

void fragment() {
	vec4 pixelatedtext = texture(SCREEN_TEXTURE, SCREEN_UV);
	vec2 pixel_size = 1.0 / VIEWPORT_SIZE;
	ALBEDO = pixelatedtext.rgb;
	for(int y = -1*int(outline_width); y <= 1*int(outline_width); y++)
	for(int x = -1*int(outline_width); x <= 1*int(outline_width); x++)
	{
		vec4 tex = texture(SCREEN_TEXTURE, SCREEN_UV + vec2(float(x),float(y)) * pixel_size );
		if(tex.a == 0.0 && SCREEN_UV.x + float(x) * pixel_size.x < 1.0 && SCREEN_UV.x + float(x) * pixel_size.x > 0.0 && SCREEN_UV.y + float(y) * pixel_size.y < 1.0 && SCREEN_UV.y + float(y) * pixel_size.y > 0.0)
		{
			ALBEDO = outline_color.rgb;
			ALPHA = 1.0;
		}
	}
	
}

There is no effect neither in preview mode nor running the scene.

It tried several alternative shaders and double checked tutorials, but I faced similar issues. It looks like I’m missing some settings, simple apply shaders wrong way

I would try removimg the ALPHA assignment, since transparency isn’t enabled. If you want.to enable it add blend_mix to the render mode.

There is a note in the docs that transparency is drawn in a specific order.

1 Like

I found another shader, it works well for me