# Depth to World Space Position problem

I am having a problem with turning depth to world space position. I am using GLSL.

What could go wrong?

Here is the code:

float x = (uv.x * 2.0) - 1.0;
float y = (uv.y * 2.0) - 1.0;
float z = (depth * 2.0) - 1.0;

vec4 pos_clip = vec4(x, y, z 1.0);

vec4 inverse_pos_view = InvCamProjMatrix * pos_clip;
inverse_pos_view.xyz /= inverse_pos_view.w;

vec4 pos_ws = InvCamViewMatrix * inverse_pos_view;

return pos_ws.xyz;


Thanks.

• I don't usually see the w division wedged between two matrix multiplications. Other solutions I've seen multiply by Inverse View Projection all in one go. What format are your depth values in? Is it possible they've been linearized before being sent as input to this code? – DMGregory Mar 28 at 14:31

vec4 pos_ws = InvCamViewMatrix * InvCamProjMatrix * pos_clip;