Camera.cameraToWorldMatrix broken?

tl;dr: where can I get the correct matrix that gives me worldspace coords for viewspace coords?

I need to compute a lot of viewspace → worldspace points per frame and would like to do it on a dedicaded compute shader. While the shader part it self is not a problem, the view-to-worldspace matrix provied by Unity trought myCamera.cameraToWorldMatrix; seems to not take take the FOV in account. The points computed are all aligned on a straight line, no matter of depth.
The Function myCamera.ViewportToWorldPoint(myNormalizedViewportCoord); works as expected, but like I said, I need a matrix that I can pass to my compute shader.

It sounds like you’re looking for the projection to world matrix. For that you probably need to get the world to projection matrix then get the inverse.

The terminologies used for camera matrices is a bit confusing as stuff like the “viewportToWorld” in script isn’t the same thing as the “view” matrix in the shaders. The “camera” matrix in script is basically the same thing as the transform’s local matrix. The projection matrix also isn’t the same thing as the “viewport”, but is the matrix used to derive the viewport position.

See:

However that only gets you part of what you need. Look at how to calculate the UNITY_MATRIX_VP in script, or maybe the UNITY_MATRIX_MVP and leave out the “model” part (the local mesh to world matrix).

And see ComputeScreenPos in UnityCG.cginc and in Unity’s shaders to see how thats used.

Basically you’ll have to calculate all of that and then do the reverse.

1 Like

Thank you!

Matrix4x4 viewToWorld = (myCam.projectionMatrix * myCam.worldToCameraMatrix ).inverse;

Gives me what I wanted! The only thing thats wierd is the scale of the Z-axis, it seems to be logarithmic in some way, I’m looking into the shader code for LinearDepth01 right now…

1 Like

That’s because of the Projection matrix. I don’t know if it’s logarithmic but it’s not linear anyhow…