Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Resolved What happens to colors of a normal map when we sample them in shadergraph?

Discussion in 'General Graphics' started by od3098, Jun 24, 2022.

  1. od3098

    od3098

    Joined:
    Apr 29, 2015
    Posts:
    56
    My original tangent space normal map is like this (light blue):
    upload_2022-6-24_15-44-39.png

    when I sample that in shadergraph or amplify shader editor it becomes more dark blue and its still in tangent space:
    upload_2022-6-24_15-46-2.png

    and when I try to bake this (or any other normal maps) from shader and save it to a file, the darker version will be saved.
    P.S. The process of saving is done by applying normal result to BaseColor slot and Graphics.Blit into a renderTexture.

    So what is the reason of darker colors and how I can have access to the light blue version to save it?
     
  2. od3098

    od3098

    Joined:
    Apr 29, 2015
    Posts:
    56
    Part of the probable answer based on this video in OpenGL

    RGB Colors are between 0 and 1.
    Normal vectors are between -1 and 1.
    So to convert RGB data to normal vectors, we need to multiply RGB values by 2 and subtract by 1.
    This is probably what Sample node does to a normal map in shadergraph or amplify shader editor and in order to have the original texture data, we need to convert it back to values between 0 and 1 (add one and divide by 2)

    upload_2022-6-24_22-58-25.png

    this will get a close results to the original texture, but not the exact same one.
    Not sure why..
     
    Lex4art likes this.
  3. Oxeren

    Oxeren

    Joined:
    Aug 14, 2013
    Posts:
    120
    You can just set the Type of the Sample Texture 2D node to Default.
     
  4. od3098

    od3098

    Joined:
    Apr 29, 2015
    Posts:
    56
    Yes, but I want to keep it in normal mode cuz this method only works if the texture is not set to Normal map in import setting. And this is not always desired.
     
  5. od3098

    od3098

    Joined:
    Apr 29, 2015
    Posts:
    56
    Ok, I have found the solution.
    The result of the formula is the correct one. when I tried to save the texture and compare, that I was using:
    Code (CSharp):
    1. RenderTexture.GetTemporary(resolution.x, resolution.y);
    This renderTexture is in sRGB color space and the original texture was made in linear space.
    so when I render in Linear space using:
    Code (CSharp):
    1. new RenderTexture(resolution.x, resolution.y, 24, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Linear);
    the rendered png texture is almost exactly the same as the original one.