Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by FinchWorks · Mar 08, 2017 at 05:00 PM · unity 5shaderz-buffer

Reversed Z-Buffer in 5.5 Update Breaking a Shader; Any Advice?

Hey, thanks for looking at this topic!

I recently updated a project to 5.5 and I noticed that a shader I'm building no longer works as intended, I think, due to the fact that the Z-Buffer near/far values have been reversed. I am using the z-buffer to create a ring of foam around objects sitting in water.

I'm using something similar to this shader by Chris Flynn. Specifically this section,

 //Get the distance to the camera from the depth buffer for this point
 float sceneZ = LinearEyeDepth (tex2Dproj(_CameraDepthTexture, UNITY_PROJ_COORD(i.projPos)).r);
 
 //Actual distance to the camera
 float partZ = i.projPos.z;
 
 //If the two are similar, then there is an object intersecting with our object
 float diff = (abs(sceneZ - partZ)) / _HighlightThresholdMax;
 
 if(diff <= 1)
 {
       finalColor = lerp(_HighlightColor, _RegularColor, float4(diff, diff, diff, diff));
  }

However, with the depth buffer reversed, it's doing the opposite, and rendering foam in the space closer to the camera. I've been messing with the values for quite a while without any real success. Can anyone give me advice on how to get the desired effect with the new 5.5 Depth Buffer?

The above is what I'm trying to accomplish, and below is what's happening with the same code now, but with a much larger foam threshold.

alt text alt text

capture.png (44.1 kB)
capture2.png (230.7 kB)
Comment
Add comment · Show 2
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 Namey5 · Mar 09, 2017 at 09:19 AM 0
Share

Even though the documentation says it should work the same way, I spent about a month developing a water shader, and 2 days after I finished I updated to 5.5 only to have this exact problem. If Unity think it should be identical in values (which it isn't), I guess we just kinda have to wait and see what they do.

avatar image FinchWorks Namey5 · Mar 09, 2017 at 02:46 PM 0
Share

Yeh, I'm not sure why LinearEyeDepth and Linear01Depth are acting strange, when Unity says they should work the same. When I get home from work, I think I might try getting the depth manually and flipping it with 1 - z like it says here in the update guide

2 Replies

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

Answer by FinchWorks · Mar 10, 2017 at 05:57 AM

Interesting! I did some digging in the forums and found someone who had found a solution! Keep the original LinearEyeDepth equation but change the screen position to compare it to i.projPos.w instead of i.projPos.z

 float partZ = i.projPos.w;

I can't really say that I understand why this change works, but my shader is working as intended again. Hope this helps someone else, and thanks for the replies!

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 Namey5 · Mar 10, 2017 at 05:59 AM 1
Share

This is the answer I have been looking for. It honestly makes no sense, but I'm so glad you found it. Gonna take a bit of a deeper look into changes to ScreenPos to see if anything's out of the ordinary.

avatar image FinchWorks Namey5 · Mar 10, 2017 at 02:11 PM 0
Share

Glad it worked for you too!

avatar image
0

Answer by IgorAherne · Mar 09, 2017 at 11:30 PM

if Z-depth returns inverted results (0 for far away and 1 for closest), just invert it during sampling

  //Get the inverted distance to the camera from the depth buffer for this point
  float sceneZ = LinearEyeDepth ( 1- tex2Dproj(_CameraDepthTexture, UNITY_PROJ_COORD(i.projPos)).r);
Comment
Add comment · Show 1 · 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 FinchWorks · Mar 10, 2017 at 02:48 AM 0
Share

See, I had the same thought and tried that the other day, but it still doesn't seem to work properly.

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

7 People are following this question.

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

Related Questions

How to make a 2d shader with edge waves 0 Answers

Issue with standard Unity 5 shader on ios8 1 Answer

XCOm Style Visibility Bubble Shader 0 Answers

How to change the reflection according to skybox-material? 2 Answers

Vertex Displacement Shader Graph With The Camera Moving Issue 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