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

Glitch Effect Shader

Soap Bubble Shader

Procedural Brick Shader

Related shaders

Post Effect Outline Shader

Screen-Space Edge Detection Outline Shader

Reupload of outline shader

guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Lordesca
Lordesca
5 months ago

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