Copy depth texture to regular texture?

Godot Version

4.3

Question

I want to render the depth of a scene into a SubViewport. How can I do that?

I put this shader into a ShaderMaterial, make that ShaderMaterial every object’s surface override material, and render it with a camera. The viewport remains all black and I don’t understand why. Forward+ renderer btw.

shader_type spatial;
render_mode unshaded;
render_mode cull_disabled;

uniform sampler2D depth_texture: hint_depth_texture;

void fragment() {
    ALBEDO = vec3(texture(depth_texture, SCREEN_UV).r, 0.0, 0.0 );
}

Mapping the depth texture to values you expect is more complicated than that

1 Like

Right, thank you for your response, but I’m not just getting unexpected values, I’m getting literally zero. It’s black, completely, but the geometry close to the near plane should be red. Could this be a bug?

My goal here is just to write the nonlinear depth values (i.e. 0.0 to 1.0) directly to the colour channel. I don’t need to undo the perspective divide or multiply by the inverse projection matrix here.