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. ![]()