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;
}
}
How is it supposed to be used ? Is it post-processing ? As a child of a camera ?
Yeah wondering the same!
It is a spatial shader, you use it as a spatial material on any spatial node, such as a meshinstance