Search Unity

Runtime Normal Map import - Please help

Discussion in 'Scripting' started by Southridge1985, Feb 9, 2016.

  1. Southridge1985

    Southridge1985

    Joined:
    Jul 17, 2012
    Posts:
    17
    Hi everyone, I have been working for sometime on a app that is ever closer to its finished state.
    Although close i have tripped up on one function that i cant seem to get clarity on.

    Importing a normal map via www. I have successfully applied it to the _BumpMap but it isnt correctly imported for a normal map tangent space texture. I have researched other threads that reveal its simple fix of taking the red into the alpha channel and only keeping the green. Correct me if I'm wrong but this seems pretty simple.

    The problem i have is the resulting converted normal map doesn't reflect what i get if import directly into the editor and set its texture type to normal map. Even the compression despite being set to RGBA32 still looks crunched and the normal's are all over the place.

    Is it actually possible and if so on a mobile device? My app and its design really depend on this element working correctly.

    I should have come forward with this call out for help earlier but i really wanted to do my own research before asking this rather silly question :p

    Any guidance really appreciated.

    A link to the app store alpha stage - opt in link (yet to be updated with this massively overdue updated beta stage).
    https://play.google.com/apps/testing/com.SouthRidge.Interactive
    If yo want to actually have a play with the app you just need to punch in a pin via system cog icon, either 1008, 1010, 1011 will do for testing.( alpha version have no bump map and standard spec shader)
    This is what i want the result of (beta stage snapshots).




    Let me know if you need more info thanks.
     
  2. Southridge1985

    Southridge1985

    Joined:
    Jul 17, 2012
    Posts:
    17
    HI everyone, i know this question may have an obvious answer but i am really at a loss figuring this one out. Here is some code that i use to convert the texture to a bump texture. It just turns out grey and doesnt work in the new standard shader. You can the difference of from the left model runtime downloaded to the one on the right imported in the editor and the tangent space texture set as a normal map. Really need help please

    result of imported normal at runtime converted and applied

    result of normal imported at runtime with no conversion.

    Result when imported into editor and set texture type to normal map. (this is the result i want)

    Actual tangent space texture that i download during runtime


    The code is use.

    Code (JavaScript):
    1. public function bumpConvert(){
    2.     if(convertBump){
    3.  
    4.     var normalTexture : Texture2D = new Texture2D(2048,2048,TextureFormat.RGBA32, false);
    5.     normalTexture.filterMode = FilterMode.Trilinear;
    6.     normalTexture.wrapMode = TextureWrapMode.Clamp;
    7.     var pixels : Color[] = bumpTexture.GetPixels(0,0,2048,2048);
    8.  
    9.     var r : float;
    10.     var g : float;
    11.     var b : float;
    12.     var a : float;
    13.  
    14.     for(var i: int = pixels.Length - 1; i >= 0; i--){
    15.         var colour : Color = pixels[i];
    16.         r = g = b = colour.g;
    17.         a = colour.r;
    18.         pixels[i] = new Color(r,g,b,a);
    19.     }
    20.     normalTexture.SetPixels(pixels);
    21.  
    22.     normalTexture.Apply(true);
    23.     bumpTexture = normalTexture;
    24.  
    25.  
    26.     var go : GameObject = GameObject.Find("Chasis");
    27.     go.GetComponent.<Renderer>().material.SetTexture("_BumpMap",bumpTexture);
    28.  
    29.  
    30.  
    31.     Debug.Log("the normals have been converted");
    32.     StopAllCoroutines();
    33.     this.enabled = false;
    34.     }
    35.  
    36.  
    37. }
    38.  
     
    Last edited: Feb 16, 2016
  3. Teravisor

    Teravisor

    Joined:
    Dec 29, 2014
    Posts:
    654
    Southridge1985 likes this.
  4. Southridge1985

    Southridge1985

    Joined:
    Jul 17, 2012
    Posts:
    17
    Last edited: Feb 16, 2016