Outline Silhouette Shader

This is outline silhouette shader. It needs two shaders for a object.

Warning: This shader code has two shaders. needs to separate two files.

How to use:

  • Add ShaderMaterial with pass #1 to original material’s next pass.
    • Set 126 to render priority
  • Add ShaderMaterial with pass #2 to pass #1’s next pass.
    • Set 127 to render priority
Shader code
/*
	アウトラインシルエット Pass #1 シェーダー by あるる(きのもと 結衣)
	Outline Silhouette Pass #1 Shader by Yui Kinomoto @arlez80

	MIT License
*/

shader_type spatial;
render_mode depth_draw_always, unshaded;

uniform float bias = -1.0;

varying mat4 camera_matrix;

void vertex( )
{
	camera_matrix = CAMERA_MATRIX;
}

void fragment( )
{
	vec4 screen_pixel_vertex = vec4( vec3( SCREEN_UV, textureLod( DEPTH_TEXTURE, SCREEN_UV, 0.0 ).x ) * 2.0 - 1.0, 1.0 );
	vec4 screen_pixel_coord = INV_PROJECTION_MATRIX * screen_pixel_vertex;
	screen_pixel_coord.xyz /= screen_pixel_coord.w;
	float depth = -screen_pixel_coord.z;

	float z = -VERTEX.z;

	ALPHA = 0.0;
	DEPTH = 1.0 - float( depth < z + bias );
}

//
// --------------------------------------------------------------------------------
//

/*
	アウトラインシルエット Pass #2 シェーダー by あるる(きのもと 結衣)
	Outline Silhouette Pass #2 Shader by Yui Kinomoto @arlez80

	MIT License
*/

shader_type spatial;
render_mode unshaded, cull_disabled;

uniform vec4 color : hint_color = vec4( 0.0, 0.0, 0.0, 1.0 );
uniform float bias = -1.0;
uniform float thickness = 0.02;

varying mat4 camera_matrix;

void vertex( )
{
	VERTEX += NORMAL * thickness;
	camera_matrix = CAMERA_MATRIX;
}

void fragment( )
{
	vec4 screen_pixel_vertex = vec4( vec3( SCREEN_UV, textureLod( DEPTH_TEXTURE, SCREEN_UV, 0.0 ).x ) * 2.0 - 1.0, 1.0 );
	vec4 screen_pixel_coord = INV_PROJECTION_MATRIX * screen_pixel_vertex;
	screen_pixel_coord.xyz /= screen_pixel_coord.w;
	float depth = -screen_pixel_coord.z;

	float z = -VERTEX.z;

	ALBEDO = color.rgb;
	ALPHA = color.a * float( depth < z + bias );
	DEPTH = 0.00001;
}

Tags
outline, silhouette
The shader code and all code snippets in this post are under MIT license and can be used freely. Images and videos, and assets depicted in those, do not fall under this license. For more info, see our License terms.

More from arlez80

Procedural Burberry Check Shader

Magical Shield Shader #3

Procedural Wang Tiling Shader

Related shaders

Fake stencil silhouette/outline (object-based but without depth test)

2D Outline and Rainbow outline 2 in 1

The simplest outline shader (via material)

Subscribe
Notify of
guest

4 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Lordesca
Lordesca
4 years ago

Can you made a tutorial please, im new to godot. Ty

DriftWare.exe
DriftWare.exe
2 years ago

The only problem with this is that my model has a skeleton aand this shader renders through itself.

DrManatee
2 years ago

For anyone attempting to use this shader in Godot 4:

CAMERA_MATRIX has been renamed to INV_VIEW_MATRIXhint_color has been renamed to source_colorNo need to normalize the screen_pixel_vertex on lines 22 and 61: vec4 screen_pixel_vertex = vec4( vec3( SCREEN_UV, textureLod( DEPTH_TEXTURE, SCREEN_UV, 0.0 ).x ) * 2.0 – 1.0, 1.0 );

Last edited 2 years ago by DrManatee
fdrerfered
fdrerfered
2 years ago

HMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM

Last edited 2 years ago by fdrerfered