Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 ikelaiah · Aug 27, 2013 at 01:49 AM · shadershaderlabcgshader writing

Linearising the inversion of camera depth effect

I have the following code, slightly modified the the original, as per Unity's documentation on depth. My aim is to create a Kinect / Primesense effect, where object appear white as closer to the camera and darker when far away. With the following code, I was able to get a reasonably goo effect. However, I get full white for any object between 0-2 units from the camera. Looking at the gradient, I don't think it is a linear falloff.

How can I display the (inverted) depth fall off linearly?

My code:

 Shader "Custom/Render Depth2" {
 
 
 SubShader {
 
     Tags { "RenderType"="Opaque" }
     Pass {
             Fog { Mode Off }
             CGPROGRAM
             #pragma vertex vert
             #pragma fragment frag
             #include "UnityCG.cginc"
     
             struct v2f {
                 float4 pos : SV_POSITION;
                 float2 depth : TEXCOORD0;
     
             };
     
         v2f vert (appdata_base v) {
             v2f o;
             o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
             UNITY_TRANSFER_DEPTH(o.depth);
             
             return o;
         }
     
      
     
         half4 frag(v2f i) : COLOR {
             // this is where the depth inversion take place
             UNITY_OUTPUT_DEPTH(1.0f - (smoothstep(0.4f, 8.0f, i.depth)));
         }
     
         ENDCG
         }
     }
 }

My screenshot: alt text

You see from the screenshot, I can't tell the difference between 2 cubes, as the closest point, 0.5 unit away and 1.0 unit away are all white. I'm hoping someone with better knowledge in shaderlab can help me to linearise the (inverted) depth fall off.

helpunityshader.png (66.3 kB)
Comment
Add comment
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

1 Reply

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

Answer by tanoshimi · Sep 06, 2013 at 01:29 PM

I believe you need to use the built-in (though not obviously documented) LinearEyeDepth() function, which will give you distance from the eye in world units. There's an example in the following blog post: http://chrismflynn.wordpress.com/2012/09/06/fun-with-shaders-and-the-depth-buffer/

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

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to save a variable from the shader? 0 Answers

Addition to Standard Shader stopping it from batching? 0 Answers

Flat Shading / No Vertex Interpolation 2 Answers

Can a CG shader fail to work on hardware? 1 Answer

Simple Cg shader not compiling 1 Answer


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