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;
}
Can you made a tutorial please, im new to godot. Ty
The only problem with this is that my model has a skeleton aand this shader renders through itself.
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 );HMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM