linear depth (4.3+)

Get a linear depth value from the DEPTH_TEXTURE (4.3+).

Apply this shader to a quad, which is attached to the camera.

 

Shader code
shader_type spatial;
render_mode unshaded, depth_draw_never;

uniform float depth_factor: hint_range(0.0, 256.0, 0.1) = 80.0;

uniform sampler2D DEPTH_TEXTURE: hint_depth_texture;

void fragment() {
	float depth = texture(DEPTH_TEXTURE, SCREEN_UV).r;
	float linear_depth = 1.0 / (depth * INV_PROJECTION_MATRIX[2].w + INV_PROJECTION_MATRIX[3].w);
	
	ALBEDO = vec3(linear_depth / depth_factor);
}
Tags
depth, depth_texture, linear
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 Firerabbit

Related shaders

guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments