Fade by distance to character

This is a simple shader that fades each pixel based on distance from character (or any object). Built on godot 3.x

Pass the shader the characters position with:

texture.set_shader_param(“character_position”, character_position)

Shader code
shader_type spatial;

uniform vec3 character_position;
uniform float fade_distance;

varying vec3 world_vertex;

uniform vec4 color : hint_color = vec4(0.94, 0.54, 0.15, 1.0);
void vertex() {
	world_vertex = (WORLD_MATRIX * vec4(VERTEX, 1.0)).xyz;
}

void fragment() {
// Calculate the distance between the fragment and the character
	float chardistance = length(character_position - world_vertex);
    
// Calculate the alpha value based on the distance
	float alpha = 1.0 - smoothstep(0.0, fade_distance, chardistance);
	ALBEDO = color.rgb;
	ALPHA = alpha;
// Discard the fragment if the alpha is zero
	if (alpha <= 0.0) {
		discard;
	}
}
Live Preview
Tags
dissolve, Distance, fade, proximity, vicinity
The shader code and all code snippets in this post are under CC0 license and can be used freely without the author's permission. Images and videos, and assets depicted in those, do not fall under this license. For more info, see our License terms.

More from mackatap

Related shaders

guest

5 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
SSebigo
SSebigo
2 years ago

How is it supposed to be used ? Is it post-processing ? As a child of a camera ?

koleok
2 years ago
Reply to  SSebigo

Yeah wondering the same!

FindersGrove
FindersGrove
4 months ago

needs an update for godot 4+

rburing
2 months ago
Reply to  FindersGrove

WORLD_MATRIX was renamed to MODEL_MATRIX and hint_color was renamed to source_color, that’s about it for this shader.