Search Unity

help with Texture arrays of normal maps

Discussion in 'Shaders' started by dudester, Jul 10, 2020.

  1. dudester

    dudester

    Joined:
    Oct 25, 2014
    Posts:
    371
    Hi there I'm creating a Tri-planar shader with 13 textures in total , naturally I need texture arrays,
    This works perfectly with just textures , however once i try using normal maps the shader breaks and the maps end up being incorrect .

    Normal mapping works perfectly if I don't use texture arrays , So my question is :
    Is there some special way to create texture arrays if they contain normal maps?

    Thanks so much for the help.

    Heres the code example of how I make texture arrays
    Code (CSharp):
    1.  //format = texture format
    2. //I tried every single Texture format as far as i know
    3. Texture2DArray textureArrayN = new Texture2DArray(1024, 1024, numberOfTextures, format, true,false );
    4.    for (int i = 0; i < texturesNormal.Length; i++)
    5.    {
    6.  
    7.     textureArray.SetPixels32(texturesNormal[i].GetPixels32(0), i, 0);
    8.    }
    9.    textureArrayN.Apply();
    10.    m_material.SetTexture("_MainTexNorm",textureArray);
    The tri planar shader works fine with non textureArray textures,So I cant imagine thats the problem , unless there is some special way to get the texture from a normal map besides "UNITY_SAMPLE_TEX2DARRAY(_MainTex , uv.xyz);"


    Again thanks so much Im completly stumped.

    see attached file for example of normals not working
     

    Attached Files:

    Last edited: Jul 11, 2020
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    Normal maps textures need to be linear.
    Texture2DArray textureArrayN = new Texture2DArray(1024, 1024, numberOfTextures, format, true, true );
     
  3. dudester

    dudester

    Joined:
    Oct 25, 2014
    Posts:
    371
    Ok I tried linear space , still the problem persists.
    I think when unity does its getpixels it ignores the fact that it's a normal map , but I already tried using a regular texture not marked as a normal map then doing SampleTex (text, uv)*2-1 , nothing works , I read somewhere that the texture format for the arrays if using normal maps should be ARGB32 but even that doesn't work
     
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    GetPixels/SetPixels doesn't care if the texture is linear or not. Those functions are getting and setting the raw data.

    However you shouldn't really be using those functions anyway. You should be using CopyTexture so you can use compressed formats.

    When sampling a normal map in the shader, you'll still want to use the UnpackNormal function, and other than using the texture array sampler it should be no different than a non-array texture.
     
    kosowski and dudester like this.
  5. dudester

    dudester

    Joined:
    Oct 25, 2014
    Posts:
    371
    Ok sorry for that turns out I was setting the normal maps to the regular textures in the array .
    It was my mistake. thanks for your help though it led me to the correct answer
     
  6. dudester

    dudester

    Joined:
    Oct 25, 2014
    Posts:
    371
    For anyone else coming here ,bgolus is right , you should use graphics.copyTexture,
    Especially for normal maps otherwise they come out white and are unusable.
     
  7. ownself

    ownself

    Joined:
    Mar 18, 2013
    Posts:
    16
    Even though I knew normal texture need to be in linear space, but often so I still stuck into issues led by this T_T
    Just spent 3 hours to dig into space transforming, dot product and this post eventually rings a bell to me. sign...
    Thanks!