Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Reversed Z

Discussion in 'Shaders' started by Phantom_X, Nov 13, 2021.

  1. Phantom_X

    Phantom_X

    Joined:
    Jul 11, 2013
    Posts:
    305
    Hey,
    I'm trying to debug an issue that I believe is related to the reversed / not reversed Z, but I can't get the traditional Z to be used.

    I use the command
    -force-opengl
    to force unity in OpenGL to get the traditional z direction.
    I can see the < Open GL 4.5 > on the title bar.
    However, using the macro UNITY_REVERSED_Z seems to be defined in both OpenGL and DX11.

    This gives me a black color in all cases.

    Code (CSharp):
    1.  
    2. float z = 1;
    3.  
    4. #if defined(UNITY_REVERSED_Z)
    5.   z = 1.0f - z;
    6. #endif
    7.  
    8. return z;
    9.  
    How else can I force unity to use the traditional Z ?
    Thanks!
     
  2. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    2,852
    Hi!
    It shouldn't be defined for OpenGL. If it is, it's a bug. Can you please report it if that's the case?
    Thank you!
     
  3. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,243
    Unless they're using the SRPs, in which case it is defined, but you need to check if
    #if !UNITY_REVERSED_Z
    for OpenGL (not
    #ifdef
    ) or
    #if UNITY_REVERSED_Z
    for everything else as it gets defined as
    #define UNITY_REVERSED_Z 0
    for OpenGL.
     
  4. Phantom_X

    Phantom_X

    Joined:
    Jul 11, 2013
    Posts:
    305
    Hey,

    I am on URP and I used the code from Unity's documentation found here:
    https://docs.unity3d.com/Manual/SL-PlatformDifferences.html

    that gave me no difference between OpenGL and DirectX, however using #if UNITY_REVERSED_Z like you suggested does give me a difference between the two.

    Thanks!
     
    MiguelLobo and aleksandrk like this.