I have an unproject function I've written, which takes in x and y values between -1 and 1, and a z value between 0 and 1 (0 is near plane, 1 is far plane). I create a vector off of this, and multiply it onto Matrix.Invert(ViewMatrix * ProjectionMatrix) to get the world-space coordinates of that screen position at that depth. The problem I'm having is that the given depth values don't result in the right position (at least as I understand it).

Projection Problem

Above is a rough sketch of what I'm seeing. The black lines are the frustrum edges, red lines are near and far plane, and green line is the plane drawn at the given z depth. My assumption was that the depth would be linear, with z = 0.5 being halfway between the near and far plane, however the values all sit very close to the near plane until z is real close to 1, at which point it rapidly approaches the far plane. Am I doing my calculations wrong somewhere? Or did I just misunderstand how projections work?

  • \$\begingroup\$ Hi @Card. Welcome to stackoverflow. Post your actual code please. \$\endgroup\$ – craftworkgames Feb 3 '16 at 4:35
  • \$\begingroup\$ I was actually looking for more of the conceptual explanation behind Perspectives rather than a code fix, though I guess I could've made that more clear (probably by not tagging this as C# . . . ) sorry! \$\endgroup\$ – Card Feb 3 '16 at 7:24
  • \$\begingroup\$ The thing that threw me was the way you asked if you might be doing the calculations wrong but it wasn't clear what those calculations where. Anyway, I'm glad you got the answer you're after. \$\endgroup\$ – craftworkgames Feb 3 '16 at 7:38

The short answer is that the standard perspective projection has a side effect of focusing Z resolution near the camera, which is actually beneficial. A perspective projection turns out to be linear in 1/Z rather than Z, meaning that there are very large changes in Z near to the camera, but smaller changes as the distance increases. Here's an article explaining the effect.

Also here's another article that discusses alternatives to the traditional perspective transform, which has some graphs sort of depicting what's going on.

  • \$\begingroup\$ Thanks so much! This is exactly the explanation I was looking for! \$\endgroup\$ – Card Feb 3 '16 at 7:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.