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

runtime normal map created.

Discussion in 'Editor & General Support' started by xxxoooggg0519, Feb 9, 2015.

  1. xxxoooggg0519

    xxxoooggg0519

    Joined:
    Sep 5, 2013
    Posts:
    2
    Hey Guys~
    I wanna create runtime normal map texture.
    But I couldn't set normal map option on created texture2d.
    anyonw knows about this?
     
  2. fffMalzbier

    fffMalzbier

    Joined:
    Jun 14, 2011
    Posts:
    3,276
  3. CWolf

    CWolf

    Joined:
    Oct 24, 2011
    Posts:
    106
    There is no switch when scripting like when in Editor mode. You need to do the process manually.

    You convert the texture's red and blue colour channel values to the green and the alpha channel value to the red. See the code we use (edited to be more generic) below.

    Code (CSharp):
    1. // Process normal map
    2. Texture2D normalTexture = new Texture2D(512, 512, TextureFormat.RGBA32, true);
    3. normalTexture.filterMode = FilterMode.Trilinear;
    4. normalTexture.wrapMode = TextureWrapMode.Clamp;
    5. Color[] pixels = yourTexture.GetPixels(0, 0, width, height);
    6. float r, g, b, a;
    7. for (int i = pixels.Length - 1; i >= 0; i--) {
    8.     Color c = pixels[i];
    9.     r = g = b = c.g;
    10.     a = c.r;
    11.     pixels[i] = new Color(r, g, b, a);
    12. }
    13. normalTexture.SetPixels(pixels);
    14. normalTexture.name = "a normal map";
    15. normalTexture.Apply(true);
     
    Last edited: Feb 9, 2015
    mgreter and fffMalzbier like this.
  4. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    8,936