Search Unity

Normal map not working when adding Standard in Always Included Shaders

Discussion in 'Shaders' started by kevin-masson, Dec 10, 2018.

  1. kevin-masson

    kevin-masson

    Joined:
    Sep 24, 2018
    Posts:
    73
    If i add Standard Shader in Graphic Settings > Always Included Shaders the normal map is not working properly and lighting fails (shadows, etc...) on WebGL.

    The problem occurs with WebGL builds and does not occur with standalone builds. Tested with Unity 2018.2.14f1. It happens in both Firefox and Chrome. The problem also occurs if the normal map is in the Secondary Maps.

    Video showing the bug:


    Video showing expected result:


    I need the Standard Shader in Always Included Shaders because I want to do Variant Stripping (note that I'm not stripping anything in this scene, I created it for test purpose because I noticed the bug while writing a stripper).

    I wrote a shader to display only the normal map, world normals and lambert coefficient and everything works correctly. I guess the problem must come from the standard shader.

    Maybe I'm missing something about variants but I haven't noticed anything relevant in the docs and online.

    Here is the unity scene reproducing the bug:
    https://www.dropbox.com/s/s8kjupajqppjf2c/WebGLNormalMapBug.unitypackage?dl=1
    (Don't forget to add Standard in Graphic Settings > Always Included Shaders to reproduce the bug).

    Here is my settings:

     
  2. kevin-masson

    kevin-masson

    Joined:
    Sep 24, 2018
    Posts:
    73
    The bug doesn't show up when using the Lightweight Rendering Pipeline.
     
  3. kevin-masson

    kevin-masson

    Joined:
    Sep 24, 2018
    Posts:
    73
    I think this should be added to the issue tracker.
     
    Twyker_gp likes this.
  4. kevin-masson

    kevin-masson

    Joined:
    Sep 24, 2018
    Posts:
    73
    Nobody else encountered this issue ?
     
  5. kevin-masson

    kevin-masson

    Joined:
    Sep 24, 2018
    Posts:
    73
    The bug also appears with different shaders like "Standard (Specular Setup)" or "Mobile/Bumped Diffuse (1 directional light)".
    I suppose it is a normal map bug, when I see the code that extract the normal in "Mobile/Bumped Diffuse", I'm wondering why the normal isn't being transformed before being used for lighting.
    Code (JavaScript):
    1.  
    2. o.Normal = UnpackNormal (tex2D(_BumpMap, IN.uv_MainTex));
    3. ...
    4. inline fixed3 UnpackNormal(fixed4 packednormal)
    5. {
    6. #if defined(UNITY_NO_DXT5nm)
    7.     return packednormal.xyz * 2 - 1;
    8. #else
    9.     return UnpackNormalmapRGorAG(packednormal);
    10. #endif
    11. }
    But that doesn't makes sense because the bug doesn't appear when the shader isn't in "Always Included Shaders", which shouldn't affect the code. Moreover, I have extracted the list of variants that are being compiled in both case and lists are identical.
     
  6. kevin-masson

    kevin-masson

    Joined:
    Sep 24, 2018
    Posts:
    73
  7. TheZkip

    TheZkip

    Joined:
    Feb 21, 2013
    Posts:
    3
    Hi, we've been having a similar issue with normals on WebGL except in our case they break when we put the shader (and the objects that want to use that shader) in an asset bundle. The shader in question is not included in the 'Always Included shaders' list. Now, if we override the UnpackNormal function from above with a version that always calls UnpackNormalmapRGorAG (and doesn't care about the define) and put that in the asset bundle everything works and shading looks correct.

    I tried to build with shader variant stripping disabled from graphics settings (and without the override in the shader) but that results into broken shading as well.

    So, I'm thinking here that something that is same for the 'Always Included Shaders' list and asset bundles is causing 'UNITY_NO_DXTnm' to be defined which is not the correct behaviour for the shader on WebGL.
     
    Twyker_gp, KasperSP and kevin-masson like this.
  8. kevin-masson

    kevin-masson

    Joined:
    Sep 24, 2018
    Posts:
    73
    Really weird things are happening with this 'Always Included Shaders' list...
     
    KasperSP likes this.
  9. KasperSP

    KasperSP

    Joined:
    Mar 7, 2017
    Posts:
    4
    You guys just saved my day :)

    [EDIT]: To clarify what we did to solve our issue (since I had a few PMs): We simply called UnpackNormalmapRGorAG() instead of UnpackNormal(). This way we're skipping the UNITY_NO_DXTnm check.
     
    Last edited: Sep 14, 2019
  10. Twyker_gp

    Twyker_gp

    Joined:
    Dec 4, 2018
    Posts:
    29
    We are having the same problem, even with asset bundles that were built with the addressable asset system.
    Wrongly putting the normal maps into "default" format instead of normal maps also seems to help with this issue, but is by no means a fix obviously.
     
  11. jukka_j

    jukka_j

    Unity Technologies

    Joined:
    May 4, 2018
    Posts:
    953
    Reproduced the issue, and working to fix this up. Thanks for the bug report!

    It looks like the issue is that shaders in the Always Included Shaders list are getting miscompiled without the appropriate compilation options being present in the shader, which results in wrong set of #defines being present.

    To work around the issue, removing the shaders from Always Included Shaders list, or cloning the shader and making sure that the clone is not placed in the Always Included Shaders list should work.

    I have not yet been able to check out the issue regarding addressables/asset bundles, but the issue is likely related.
     
    kevin-masson and Twyker_gp like this.
  12. Twyker_gp

    Twyker_gp

    Joined:
    Dec 4, 2018
    Posts:
    29
    @jukka_j Cool, so glad to hear! :)
    The solution here did however not work in my case. :/

    Thanks for checking this out though, this is an immense problem as I had to go back to Legacy Shaders in WebGL atm.
     
  13. flipddr

    flipddr

    Joined:
    Jan 30, 2019
    Posts:
    3
    Spent a couple days trying to fix it. I'm using the Universal RP, and changing the material shader to URP/Baked Lit instead of URP/Lit did it for me. Hope this helps someone.
     
  14. firala

    firala

    Joined:
    Mar 17, 2015
    Posts:
    2
    Try setting your normalmap texture types not to normalmap, but texture and set sRGB to false, that got it to work for us (URP/Lit materials on prefab loaded by addressables)
     
  15. VortexInCortex

    VortexInCortex

    Joined:
    Apr 13, 2019
    Posts:
    12
    where is that?
     
  16. VortexInCortex

    VortexInCortex

    Joined:
    Apr 13, 2019
    Posts:
    12
    Actually idk why I can't do that :/