• 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
2
Question by pragmascript · Jul 13, 2011 at 11:56 AM · depth-buffer

compute distance from depth value

given a depth value taken from a depth texture I want to calculate the actual distance of a texel to the camera. the documentation tells me:

http://unity3d.com/support/documentation/Components/SL-DepthTextures.html

Pixel values in the depth texture range from 0 to 1 with a nonlinear distribution. Precision is usually 24 or 16 bits, depending on depth buffer used. When reading from depth texture, a high precision value in 0..1 range is returned. If you need to get distance from the camera, or otherwise linear value, you should compute that manually.

my question is how to compute that manually.

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 Waz · Jul 13, 2011 at 12:13 PM 0
Share

I think that documentation is basically saying "don't". By "manually" (a word as meaningless and overused as "automatically" in reference to software), I think they mean "not using the depth texture", presumably because of the platform- and hardware-dependent nature of the values.

If you get a good answer, or even if you don't, be sure to submit a documentation Bug Report from within Unity.

1 Reply

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

Answer by Bunny83 · Jul 13, 2011 at 12:43 PM

According to this nice post, the zbuffer values are ditributed like this:

 a = zFar / ( zFar - zNear )
 b = zFar * zNear / ( zNear - zFar )
 z = distance from the eye to the object

 z_buffer_value =  a + b / z 

If you reverse the calculation you will get:

 z = b / (z_buffer_value-a)

I'm not sure if that's right for all hardware out there but if the article isn't completely wrong it should work. Don't expect a good precision since the stored value is non linear and those calculations won't help to make the result more accurate ;) At least it should produce a value near the original distance.

edit

the documentation states that there are some macros that compute the distance for you. Read the page carefully, i guess DECODE_EYEDEPTH is what you want.

If you just need a single point distance and not inside a shader you should use a Raycast instead ;)

Comment
Add comment · Show 2 · 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
avatar image pragmascript · Jul 13, 2011 at 09:31 PM 0
Share

Hey, thank you for your answer :). I will try that calculation. I can't use DECODE_EYEDEPTH because I'm not inside a shader. I'm doing (many) Raycasts at the moment but this has become a bottleneck.

avatar image FuzzyQuills · Nov 01, 2013 at 12:20 AM 0
Share

Hey you guys! thanks for this answer, since i am after a way of calculating depth from script too!

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

Custom Transparency shader occasionally losing transparency? 1 Answer

How to get the depth values of the camera view? 1 Answer

CommandBuffer.SetRenderTarget becomes strange with BuiltinRenderTextureType.Depth 0 Answers

Debug Rendering using the depth buffer 3 Answers

Scene View Camera Depth Texture? 0 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