Godot Version
4.4.1
Question
I’ve been trying to display the depth texture to the screen, but it doesn’t work. I’m not really sure how. My screen is just white the entire time.
Here’s the code in GDScript getting the matrices:
var cam_tr = render_scene_data.get_cam_transform()
var view_proj = render_scene_data.get_view_projection(view)
var inv_view_mat = [
cam_tr.basis.x.x, cam_tr.basis.x.y, cam_tr.basis.x.z, 0.0,
cam_tr.basis.y.x, cam_tr.basis.y.y, cam_tr.basis.y.z, 0.0,
cam_tr.basis.z.x, cam_tr.basis.z.y, cam_tr.basis.z.z, 0.0,
cam_tr.origin.x, cam_tr.origin.y, cam_tr.origin.z, 1.0,
]
var proj_mat = [
view_proj.x.x, view_proj.x.y, view_proj.x.z, view_proj.x.w,
view_proj.y.x, view_proj.y.y, view_proj.y.z, view_proj.y.w,
view_proj.z.x, view_proj.z.y, view_proj.z.z, view_proj.z.w,
view_proj.w.x, view_proj.w.y, view_proj.w.z, view_proj.w.w,
]
var mat_buffer := PackedByteArray()
mat_buffer.append_array(PackedFloat32Array(inv_view_mat).to_byte_array())
mat_buffer.append_array(PackedFloat32Array(proj_mat).to_byte_array())
And here’s the code in the shader:
layout(rgba16f, set = 0, binding = 0) uniform image2D render_texture;
layout(set = 0, binding = 1) uniform sampler2D depth_sampler;
layout(binding = 2) uniform uniformBufferMat {
mat4 inv_view;
mat4 proj;
} mat;
layout(push_constant, std430) uniform Params {
vec2 raster_size;
};
void main() {
ivec2 pixel = ivec2(gl_GlobalInvocationID.xy);
ivec2 size = ivec2(raster_size);
vec2 uv = pixel / size;
if (pixel.x >= size.x || pixel.y >= size.y) return;
float depth = texture(depth_sampler, uv).x;
vec3 ndc = vec3(uv * 2.0 - 1.0, depth);
vec4 view = inverse(mat.proj) * vec4(ndc, 1.0);
view.xyz /= view.w;
float linear_depth = -view.z;
imageStore(render_texture, pixel, vec4(vec3(fract(linear_depth)), 1.0));
}
The code sampling the depth is from the docs, but I’m not sure it carries over to compositor effects. Does this code carry over, or is there something that I’m missing?