Project depth texture on a mesh

Create meshinstance -> assign mesh ex: sphere, size 200000.0, add shader material.
In the shader set the far_dist to your need, ex 100000.0.

depth_test_disabled make it draw in front of everyting.

Shader code
shader_type spatial;
render_mode unshaded, cull_front, depth_test_disabled;

uniform sampler2D DEPTH_TEXTURE : hint_depth_texture;
uniform float far_dist = 1.0;

void fragment() {
    float raw = texture(DEPTH_TEXTURE, SCREEN_UV).r;

    vec4 view_pos = INV_PROJECTION_MATRIX * vec4(SCREEN_UV * 2.0 - 1.0, raw, 1.0);
    view_pos /= view_pos.w;
    float dist = -view_pos.z;

    float t = clamp(dist / far_dist, 0.0, 1.0);
    ALBEDO = vec3(t);   // near = black, far = white
}
Live Preview
Tags
atmosheric, Data/Generation, depth, Fog, texture
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 spaghetti

Related shaders

Comments 0

No comments yet.