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
2
Question by Paul Ripley · Sep 30, 2013 at 09:45 AM · surface shaderdepth-bufferlinearsurfaceshader

why does IN.screenPos in a surface shader have different Z values on Windows and Android?

I'm using a surface shader that basically does this :

 struct Input 
 {
     float2 uv_MainTex;
      float4 screenPos;
 };
       
 void surf (Input IN, inout SurfaceOutput o)
 {
     float3 normScreenPos = IN.screenPos.xyz / IN.screenPos.w;
 
     float fragmentDepth = Linear01Depth(UNITY_SAMPLE_DEPTH(tex2D(_CameraDepthTexture, normScreenPos.xy))); 
     float decalDepth = Linear01Depth(normScreenPos.z);         
 
     ...
 }

This is useful for applying decals to surfaces that have already written to the depth buffer, and stopping the decals overlapping into space.

In the case of a decal triangle that completely maps onto a pre-existing surface, then fragment depth (read from the depth buffer image) should closely approximate the decal depth (interpolated from the triangle vertex positions).

This does indeed work in the Unity editor on Windows.

On Android however, it appears that IN.screenPos.z does not behave the same as it does on Windows, and appears to have an incorrect scaling. the xy values DO seem to be correct however.

Applying a scaling factor, eg. :

 float decalDepth = Linear01Depth(normScreenPos.z) * 1.6f;                             

Partly fixes the problem, but you would also need to determine an offset as well as a scale, and also determine the scaling factor to the required precision.

I'm guessing this is a Unity shader bug, so has anyone seen this before, and are there any plans to fix it?

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 Paul Ripley · Sep 30, 2013 at 10:44 AM 0
Share

I've also noticed that the values for sceneDepth and decalDepth match perfectly in the Editor when the active platform is PC, $$anonymous$$ac & Linux Standalone, but the values diverge slightly when the platform is set as Android (even when running in the Editor).

I don't have any graphics emulation enabled, so I'm not sure why I get different behavior here.

2 Replies

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

Answer by CHPedersen · Sep 30, 2013 at 11:54 AM

My theory is that this is because the Android device is running OpenGL and the Windows machine is running Direct3D. OpenGL and Direct3D do not map Z depths equally: The xyz mapping range of Normalized Device Coordinates in OpenGL is (-1,-1,-1) to (1,1,1) while it is (-1,-1,0) to (1,1,1) in Direct3D, i.e. the difference is the Z-coordinate: -1 to 1 in OpenGL, and 0 to 1 in Direct3D.

This discrepancy between the two systems is explained and elaborated on in Chapter 4 on Transformations in NVidia's Cg Tutorial, Section 4.1.9.

I'm not an expert on compiler directives in Cg, but a quick glance inside UnityCG.cginc reveals that you might be able to detect which shader api is in use by these compiler directives:

 #if (defined(SHADER_API_GLES) || defined(SHADER_API_GLES3)) && defined(SHADER_API_MOBILE)
 // Code for OpenGL here
 #else
 // Code for Direct3D here
 #endif

Maybe you can use those yourself to remap the Z-coordinate based on the device's shader api?

I also took a look at the Linear01Depth function while I had UnityCG.cginc open anyway. Judging by the function's name it looks like it's designed to make up for this exact issue, but it isn't immediately clear to me what it's doing. It's remapping Z based on a float4 called _ZBufferParams declared in UnityShaderVariables.cginc, but what the engine is actually storing in this variable from the CPU side, I don't know. :-/

Sorry I can't deliver a totally concrete answer; hopefully this at least contributes to your analysis of the problem.

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 Paul Ripley · Sep 30, 2013 at 12:20 PM 0
Share

Thanks for the quick reply! I'll take a look at that - it looks like you might be onto something. I'll try and visualize the values used by Linear01Depth and see what that is doing.

avatar image CHPedersen · Sep 30, 2013 at 12:34 PM 0
Share

I think that's a great way to debug it. :) It's very possible Linear01Depth doesn't give you what its name promises. Good luck!

avatar image
0

Answer by Paul Ripley · Sep 30, 2013 at 01:05 PM

Looks like you were spot on! The shader works on Android and Windows now with the following adjustment :

 #if (defined(SHADER_API_GLES) || defined(SHADER_API_GLES3)) && defined(SHADER_API_MOBILE)
     float decalDepth = Linear01Depth((normScreenPos.z + 1.0f) * 0.5f);                             
 #else
     float decalDepth = Linear01Depth(normScreenPos.z);             
 #endif  

I now think that Linear01Depth does give linear values on all platforms, because I'm using the abs delta of the 2 values I get back to alpha fragments out. This seems to work consistently for decals throughout the scene depth, and I wouldn't expect it to work so well if the values were non-linear. This surprises me, due to having just read this old thread : link http://forum.unity3d.com/threads/39332-_ZBufferParams-values, but I might have misunderstood that.

Thanks for your help!

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

15 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

Related Questions

uv-space to screenSpace in surface shader 0 Answers

Specular vs. Emission in Surface Shader? 2 Answers

How to not require normals in a surface shader 1 Answer

Using the Lightmap UV texcoords in a Unity 5 Surface Shader? 1 Answer

Access GameObject position and model position in surface shader? 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