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 MaT227 · Oct 07, 2014 at 12:29 PM · shadercgdepthsurface shader

ZBuffer and Object Depth

I can easily retrieve the depth of an object using camera's depth but this is dependent of the camera's z position. I would like to "normalize" the depth and not to be linked with the camera distance from the object.

Here is a simple example of applying depth of an object but this is view dependent. Maybe there's a matrix trick to normalize the view position.

 void vert(inout appdata_full v, out Input o)
 {
     UNITY_INITIALIZE_OUTPUT(Input, o);
     o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
     o.screenPos = ComputeScreenPos(o.pos);
 }
  
 void surf(Input IN, inout MyOutput OUT)
 {
     half4 c = tex2D (_MainTex, IN.uv_MainTex);
     OUT.Albedo = c.rgb;
     OUT.Alpha = c.a;
  
     float Depth = Linear01Depth(tex2Dproj(_CameraDepthTexture, UNITY_PROJ_COORD(IN.screenPos)).r);
     half4 edgeBlendFactors = half4(1.0, 0.0, 0.0, 0.0);
     edgeBlendFactors = saturate((Depth - IN.screenPos.w + _A) * _B); // _A = 7 and _B = 0.2
     Depth = lerp(1.0, 0.0, edgeBlendFactors);  
  
     OUT.Albedo = Depth;
 }
  

Thank you !

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
0

Answer by Bunny83 · Oct 07, 2014 at 12:47 PM

I'M not sure what you actually want to know. The "depth" as seen on the screen is relative to the camera as the camera represents the projection matrix. The camera is the view point. Your view starts at the camera's position. There is nothing like a "global" or "worldspace" view as this would mean your camera is at 0,0,0 and not rotated.

Comment
Add comment · Show 3 · 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 MaT227 · Oct 09, 2014 at 05:54 PM 0
Share

I understand what you say but is it maybe possible to constrain the camera position. Of course depth is dependent to camera orientation but I want to constraint the distance to the object and keep the same distance.

avatar image Bunny83 · Oct 10, 2014 at 07:56 AM 0
Share

I don't get what you want. depth is the distance from the camera. A distance is a length between two points. While one point is your object's vertex the other is the camera's position. If you want a different distance, tell us what distance you mean, relative to what?

Is it possible that all you actually want is to move the camera with your object? That obviously can't be done inside a shader. If you can't describe what behaviour you actually want we can'T really help you.

avatar image MaT227 · Oct 10, 2014 at 09:45 AM 0
Share

When I am talking about depth I should probably use thickness. To calculate thickness I think that the best way is to use depth (in my opinion) that's why I want to constrain the depth. I've made another question here. $$anonymous$$aybe You'll better understand what I mean. Thickness Surface Shader

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

29 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 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

Thickness Surface Shader 0 Answers

Shader - Object Depth 0 Answers

How to add Bump/Normal Map to surface shader? (CG) 0 Answers

Depth issues with a shader 1 Answer

How to control Ambient Lighting and Spherical Harmonics 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