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

Related shaders

Pixelized (by distance) Shader

Camera Distance UV Scaling

Fade by distance to character

Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments