Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ErwinTerpstra · May 04, 2017 at 11:07 AM · cameramatrixprojectionperspectivetransformation

How do projection matrices work in Unity?

I'm working on a screen-space reflection shader for Unity but I'm running into some incorrect values when transforming from clip space to camera space.

To get a better grasp on how this transformation works I've ran some tests:

 Matrix4x4 projectionMatrix = GL.GetGPUProjectionMatrix(targetCamera.projectionMatrix, false);
 
 Vector4 clipSpace = projectionMatrix * new Vector4(0.0f, 0.0f, -targetCamera.nearClipPlane, 1.0f);
 float depth = clipSpace.z / clipSpace.w;

Result: depth = 0.0.

 Matrix4x4 projectionMatrix = GL.GetGPUProjectionMatrix(targetCamera.projectionMatrix, false);
 
 Vector4 clipSpace = projectionMatrix * new Vector4(0.0f, 0.0f, -targetCamera.farClipPlane, 1.0f);
 float depth = clipSpace.z / clipSpace.w;

Result: depth = 1.0.

So far, this makes sense. In clip space the Z component represents linear depth from the near clip plane to the far clip plane.

But then I ran this test, expecting the result to be about 0.5f:

 Matrix4x4 projectionMatrix = GL.GetGPUProjectionMatrix(targetCamera.projectionMatrix, false);
 
 Vector4 clipSpace = projectionMatrix * new Vector4(0.0f, 0.0f, -targetCamera.farClipPlane * 0.5f, 1.0f);
 float depth = clipSpace.z / clipSpace.w;

Result: depth = 0.995.

Apparently my assumption was not correct. It seems the Z value in clip space follows some sort of curve where it rapidly increases to values above 0.9 and then slowly increases to 1.0 at the far plane. What am I missing here? Is my test case incorrect?

Note: In my test setup I used a perspective camera with the near plane at 0.1 and the far plane at 20

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Owen-Reynolds · May 04, 2017 at 01:38 PM 0
Share

It's been a while (my Red OpenGL book has been a cat pillow for years) but I thought that was correct - those z values are purposely more fine-grained at near values, since that's where bad-looking overlaps tend to appear (but I wouldn't have guessed that 99% is used for the 1st half of the range.)

For the standard stuff, Unity tends to copy it exactly.

The test looks a tiny bit off (should be (near+(far-near)*0.5,) but it's only a difference from 10.0 to 10.05.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by FortisVenaliter · May 04, 2017 at 08:19 PM

What you're expecting would be linear depth mapping. That hasn't been used for a while because it does cause clipping issues, as Owen mentioned. Instead, the calculation is based on the reciprocal. Read this article from NVIDIA for a pretty good description of why it works that way, including equations for how it's calculated.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

SSI Gold Box/Divine Divinity perspective? 1 Answer

How can I make a skybox respect oblique frustum? 1 Answer

Changing camera.projectionMatrix doesn't affect skyboxes? 0 Answers

Drawing on the same screen with different cameras? 1 Answer

changing camera view 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges