Search Unity

Bug ZWrite can't be enabled after upgrading the "Standard" shader to "Universal Render Pipeline/Lit"

Discussion in 'Editor & General Support' started by Lozoo501, May 26, 2023.

  1. Lozoo501

    Lozoo501

    Joined:
    Sep 12, 2014
    Posts:
    11
    I have a draw order problem with my models in Unity, the URP shader in particular. This happened after having upgraded the shaders that were used, to the URP variant: "Standard" to "Universal Render Pipeline/Lit". URP shaders do not have the "Fade" render mode, which I was using before the upgrade and so I got told to use the "Transparent" render mode instead. They said that it works the same but maybe that has something to do with it?

    Anyway the issue is that _ZWrite has been turned off on the URP shader and normally you can turn it on by giving it the value 1 (and in the new Unity versions you would also need to increase the value of the _Mode property to make _ZWrite editable). But now even if I change _Mode to some high(er) value, the _ZWrite property still doesn't accept changes, it resets back to zero immediately. I'm using the debug mode of the inspector to edit these values.

    I'm using Unity 2021.3.23f1 (LTS version).
     
    Last edited: May 31, 2023
  2. Lozoo501

    Lozoo501

    Joined:
    Sep 12, 2014
    Posts:
    11
    I found the solution! "ZWrite[_ZWrite]" in the first pass had to become "ZWrite On".

    That variable is "on" with opaque and "off" with transparent, it's the Lit material inspector that sets the value for you automatically. At the end of the shader, you can see this line:
    CustomEditor "UnityEditor.Rendering.Universal.ShaderGUI.LitShader"

    If you went to see the source code of this class, you would find some code that says "if type == opaque then [_ZWrite] = 1 else [_ZWrite] = 0". You just don't want that, so you hardcode the value "on".