Converting to world coordinates from projection coordinates, and then back

Hi,

I’m trying to write an image effect shader that works like this:

(For reference, projection space is similar to screen space, except (-1,1) refers to top-left, (1,-1) refers to bottom right, and the Z axis corresponds to values recorded in the depth buffer.)

A) v2f_img.uv for x,y and depth texture for z (projection space of original camera)
v
B) world coordinates
v
C) those coordinates transformed into another camera’s projection space (will be same as A if the same camera is used)

I remember coding using the DirectX API back in the day, and doing this sort of math was a piece of cake. Simply given the world coordinates, you could transform them by viewprojection to get where it will end up on the screen and the depth buffer. Then to do the opposite, you just multiplied that by inverse(viewprojection).

However, in Unity, it seems like this doesn’t work. I’ve got it to where a image effect shader can find the world coordinate of each pixel, but transforming it back into the same camera’s projection space using the same camera’s view and projection matrices doesn’t give the results I started with (and it should). Most notably, the resulting computed depth seems to somehow scale the x and y values which are supposed to be the resulting projection space coordinates. I tried to reverse this scaling by dividing x and y by z, by (z*w), and so on but nothing seems to work. I don’t know what math Unity is using to do their projections and how this interacts with shaderlab.

Can someone help me with this, please? Thank you. :slight_smile:

Calculate viewprojectioninverse from a script like:
Matrix4x4 mat = (camera.projectionMatrix * camera.worldToCameraMatrix).inverse;

and then pass it to your shader like:
material.SetMatrix(“_ViewProjectInverse”, mat);

if you define a matrix in your shader like:
uniform float4x4 _ViewProjectInverse;

it will work.

the multiplication order matters to get correct results.

I tried your code last night, and it worked fairly well. However, the global positions calculated for each pixel were incorrect:

For example, if you had a cube in the scene at (0,0,0) it’d show that the global (0,0,0) crosses through it’s center. For example, one half is red, corresponding to +x; the other black, corresponding to -x.
However as the camera strayed away from (0,0,0), the crossing point would offset away from (0,0,0) towards the camera. For example, three quarters of the cube would be red while only a quarter is black, meaning the shader believes (0,0,0) isn’t actually in the middle of the cube like it’s supposed to.

The technique I am implementing requires that projection->world conversions are accurate, as well as world->projection.

Perhaps the issue I’m having is with deriving the correct projection coords from v2f_img.uv and CameraDepthTexture, and then likewise doing the inverse.

My code for this conversion off hand is as follows:

float4 projectionCoord = float4(i.uv * 2 - 1, tex2D(_CameraDepthTexture, i.uv).x, 1);
float4 viewCoord = mul(_InvProjection, projectionCoord);
float4 worldCoord = mul(_InvView, viewCoord);
float4 newViewCoord = mul(_NewView, worldCoord);
float4 newProjectionCoord = mul(_NewProjection, newViewCoord);
float2 newImageUVCoord = (newProjectionCoord.xy + 1) / 2; // We just need resulting uv to sample from render target.

// Nice elegant code; hope it can stay this way. :) 
// BTW this code can be simplified with one mul() operation by multiplying the matrices together
// beforehand in a script and feeding the results into one float4x4. I broke down the steps here to
// show the process, though.

Where am I going wrong with this code?

Thanks.

EDIT: I tested the above code, with a single matrix of them all multiplied together (newProj * newView * oldView.inverse * oldProj.inverse). and tried using different math to go from imageUVCoords to projectionCoords and back, but it still wasn’t accurate and there largely was an issue in accurately computing the world position of objects in the camera-relative z direction among other things.

I think the main problem here is in deriving the projectionCoord from the imageUVCoord and depth texture sample. See, in order to get the correct world coordinate, the x, y, z and w values of the projectionCoord have to match what they’d be if you took that same world coordinate and passed them -forward- though the view and projection matrices. However, getting the correct projectionCoord apparently isn’t as simple as assigning (u, v, depth, 1).

For example, if we were to take a world coodinate at (0.75,0.75,1.5,1)…
With a camera using a 45 degree FOV, an aspect ratio of 5:4, a z range of 1-2, at world coordinate (0,0,0,1) and rotation (0,0,0)…

You get a view coordinate at (0.75, 0.75, -1.5, 1)…
And a projection coordinate at (1.4, 1.8, 0.5, 1.5)… wtf?

If I were using the correct math, then the projection coordinate would be (something less than 0.5, 0.5, something between 0 and 1, 1).

However, I don’t know what the correct math is in getting a projection coordinate from the image UV and a depth texture. THIS is the problem I am having - and I have no idea how to solve it. :frowning:

Since the problem was in converting image space to world coordinates, I looked over Unity’s global fog shader and borrowed the code in there used in calculating global coordinates. Then I fed them to the view and projection matrix, getting the values I needed. :slight_smile:

So, MY problem is solved - but it would still be interesting to see the solution outlined above work too.

the matrix is correct, your calculation of the pixel position might be wrong.
Anyhow, im glad if you solved the issue.