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;
	}
}
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

Diamond-based Screen Transition

Related shaders

Pixelized (by distance) Shader

Camera Distance UV Scaling

Subscribe
Notify of
guest

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
SSebigo
SSebigo
7 days ago

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