Distance Checker
A shader that checks the distance between a pixel and a target location.
Shader code
shader_type spatial;
uniform float player_fade_radius : hint_range(0,50);
uniform vec3 player_position;
uniform vec3 regular : source_color;
uniform vec3 faded : source_color;
uniform float alpha : hint_range(0.0, 1.0, 0.1);
void fragment() {
float depth = FRAGCOORD.z;
vec2 frag_ndc = ((FRAGCOORD.xy / VIEWPORT_SIZE) * 2.0) - 1.0;
vec4 frag_view_space_position = INV_PROJECTION_MATRIX * vec4(frag_ndc, depth, 1.0);
frag_view_space_position /= frag_view_space_position.w;
vec4 frag_world_space = INV_VIEW_MATRIX * frag_view_space_position;
ALBEDO = regular;
float posx = pow(player_position.x - frag_world_space.x,2);
float posy = pow(player_position.y - frag_world_space.y,2);
float posz = pow(player_position.z - frag_world_space.z,2);
float distance_from_player = floor(sqrt(posx+posy+posz));
if (abs(distance_from_player) < player_fade_radius){
//INSERT YOUR CODE HERE
}
}