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. Dismiss Notice

How to pack a procedural self created normal map for standard unity shaders?

Discussion in 'Shaders' started by Quatum1000, Feb 22, 2015.

  1. Quatum1000

    Quatum1000

    Joined:
    Oct 5, 2014
    Posts:
    888
    Hi everyone,

    I have created a procedural normal map that is unpacked. Now I want to use in the new U5 standard shaders. The standard shaders use unpacknormal() for sure.

    How can I pack my "unpacked normal map" in my shader, so it's usable in standard Unity5 shaders or others?

    Or would it be possible to flag my RT as normal unpacked, so that the standard shaders can work with internally?

    Thanks and kind regards
     
  2. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,415
    From what I can see how UnpackNormal is implemented in UnityGC.inc, unpacking depends on the UNITY_NO_DXT5nm define. I guess UNITY_NO_DXT5nm is to detect whether DXT5nm is available on a certain platform. If that's the case, then I'd say:

    If DXT5nm is supported, x is stored in the alpha channel, y is stored in the green channel and z is being reconstructed on the fly.
    If DXT5nm is not supported, the normal map simply holds the xyz information in its rgb channels.

    In order to use your generated normal map, using the UnpackNormal macro where UNITY_NO_DXT5nm is active, you'd generate the texture data like red=0, green=y, blue=0, alpha=x (where x,y correspond to the normal components).

    I hope it makes sense!
     
    Last edited: Feb 24, 2015
    Quatum1000 likes this.
  3. Quatum1000

    Quatum1000

    Joined:
    Oct 5, 2014
    Posts:
    888
    I use the result of the my normal in a frag shader. This works well so far.
    :
    Code (CSharp):
    1.  
    2. float4 n1 = tex2D(myN1, IN.myN1UV.xy);
    3. o.Normal = n1*2 - 1;
    4.  
    But to use it with UnpackNormal() "(you'd store red=0, green=y, blue=0, alpha=x.)"
    I tried this, but that doesn't work.

    :
    Code (CSharp):
    1.  
    2. half4 n1 = half4(0, IN.myN1UV.x, 0, IN.myN1UV.y);
    3. o.Normal = UnpackNormal(n1);
    4.  
    Can you drop me some line of code how to? :)
     
  4. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,415
    It looks like myN1UV holds your texture coordinates, rather than the unpacked normal data. It should be something like this, I guess:
    Code (CSharp):
    1.  
    2. half4 sample = tex2D(myN1, IN.myN1UV.xy); // unpacked normal
    3. half4 packedNormal = half4(0, sample.y, 0, sample.x);
    4. o.Normal = UnpackNormal(packedNormal);
    5.  
     
    Quatum1000 likes this.
  5. Quatum1000

    Quatum1000

    Joined:
    Oct 5, 2014
    Posts:
    888
    Thanks a lot Peter!