Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Normal maps generated using encodeToPng are orange

Discussion in 'Editor & General Support' started by radiantboy, Aug 19, 2019.

  1. radiantboy

    radiantboy

    Joined:
    Nov 21, 2012
    Posts:
    1,633
    Im converting some files to png in an editor script (using encodeToPng()), everything seems ok, but the normal maps seem to come out orange rather than blue. They are marked as normal maps still in the inspector. Anyone know why this may be ?

    It seems like I can fix it manually by ticking "create form greyscale" (which I shouldnt need to do, they didnt use that originally) but I cannot find a way to call this from a script. Any thoughts on how to call it from a script?
     
  2. Domas_L

    Domas_L

    Unity Technologies

    Joined:
    Nov 27, 2018
    Posts:
    111
    Hello! Could you please submit a bug report with a minimal reproduction project for this issue and reply in here with the issue ID? Also, please make sure to include the .PNG file which results in this behavior.
     
  3. radiantboy

    radiantboy

    Joined:
    Nov 21, 2012
    Posts:
    1,633
    Thanks! Actually I think my code normal maps it again by setting the texture type (thats my current theory). Is there a way to set that without unity doing any work to it? I need the equivalnt of setting it in the editor, when I do it that way everything is fine.
     
  4. radiantboy

    radiantboy

    Joined:
    Nov 21, 2012
    Posts:
    1,633
    Infuriating.. Everything works fine so long as I dont convert a file marked as a normal map. If I do its orange. If it is not marked as a normal map then all is fine and if I mark it after in the editor it works fine.

    All I am trying to do is convert .tiff norma maps to .png, but when I do they go orange.
     
    Last edited: Aug 20, 2019
  5. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    Normal maps (as well as lightmaps, HDR textures and perhaps a few other types) inside unity might get "encoded" into special formats, with the shader doing unpacking and decoding. This is done mostly for quality improvement reasons, e.g. for normal maps, unity stores just two (x & y) components instead of all three (x, y, z) because under GPU texture compression schemes that looks much better.

    If you want to get "decoded" pixel values out of a texture in unity, you'd have to do the same unpacking as what the shaders do when using them for rendering. Specifically for normal maps, the relevant shader code is this from UnityCG.cginc:

    Code (CSharp):
    1. inline fixed3 UnpackNormalDXT5nm (fixed4 packednormal)
    2. {
    3.     fixed3 normal;
    4.     normal.xy = packednormal.wy * 2 - 1;
    5.     normal.z = sqrt(1 - saturate(dot(normal.xy, normal.xy)));
    6.     return normal;
    7. }
    8.  
    9. // Unpack normal as DXT5nm (1, y, 1, x) or BC5 (x, y, 0, 1)
    10. // Note neutral texture like "bump" is (0, 0, 1, 1) to work with both plain RGB normal and DXT5nm/BC5
    11. fixed3 UnpackNormalmapRGorAG(fixed4 packednormal)
    12. {
    13.     // This do the trick
    14.    packednormal.x *= packednormal.w;
    15.  
    16.     fixed3 normal;
    17.     normal.xy = packednormal.xy * 2 - 1;
    18.     normal.z = sqrt(1 - saturate(dot(normal.xy, normal.xy)));
    19.     return normal;
    20. }
    21. inline fixed3 UnpackNormal(fixed4 packednormal)
    22. {
    23. #if defined(UNITY_NO_DXT5nm)
    24.     return packednormal.xyz * 2 - 1;
    25. #else
    26.     return UnpackNormalmapRGorAG(packednormal);
    27. #endif
    28. }
    29.  
    Which, as far as I remember, basically means that the normal map texture might have Red=1, Green=Y, Blue=1, Alpha=X (for DXT5 format), or Red=X, Green=Y, Blue=0, Alpha=1 (for BC5 format). Z component can be computed from X & Y.
     
  6. stevphie123

    stevphie123

    Joined:
    Mar 24, 2021
    Posts:
    81
    All our normal maps in latest 2021.2 beta turned into pink/orange... The mapped texture isn't accurate as well..

    This triggers Nan and black flickers across our screen

    Can somebody give us some advice on how to solve this, reason we upgraded to latest 2021.2 beta bcos of some features that lacking in hdrp 2020 lts
     
  7. cecarlsen

    cecarlsen

    Joined:
    Jun 30, 2006
    Posts:
    858
    I have the same issue in Unity 2021.2. Orange normal maps. They come from Quixel.