Runtime Normal Map Import

Hi, I got the same problem as Jan 18 in his `Runtime Loaded Bump/Normal Texture into "Bumped Diffuse" Shader`-topic.

When loading in a NormalMap via the WWW loader Unity just doesn't convert the texture to the Normal Map DXnm format. This makes it not possible for the Shaders to read the Normal Map properly.

So my question to the Unity Developers? Why is a conversion to the DXnm not possible?!

Regards, Thomas

var DiffuseMap:Texture2D;
var NormalMap:Texture2D;
var HeightMap:Texture2D;
private var loader:WWW;
var PathDiffuse:String;
var PathNormal:String;
var PathHeight:String;
var textureImporter:TextureImporter;
    
function Start()
{
    Shader.globalMaximumLOD=1000000;
    DiffuseMap=new Texture2D(10,10,TextureFormat.ARGB32, false);
    NormalMap=new Texture2D(10,10,TextureFormat.ARGB32, false);
    HeightMap= new Texture2D(10,10,TextureFormat.ARGB32, false);
    StartCoroutine(LoadTextures());
    SetShader();
}

function LoadTextures ()
{
    loader= new WWW(PathDiffuse);
    yield loader;
    loader.LoadImageIntoTexture(DiffuseMap);
    loader = new WWW(PathNormal);
    yield loader;
    loader.LoadImageIntoTexture(NormalMap);
    loader = new WWW(PathHeight);
    yield loader;
    loader.LoadImageIntoTexture(HeightMap);
    print("Loaded");
}
    
function SetShader()
{
    var m:Material;
    m=new Material(Shader.Find("Bumped Diffuse"));
    if(DiffuseMap!=null) m.SetTexture("_MainTex",DiffuseMap);
    if(NormalMap!=null) m.SetTexture("_BumpMap",NormalMap);
    //if(HeightMap!=null) m.SetTexture("_ParallaxMap",HeightMap); // Not using for Bumped Diffuse
    renderer.material=m;
    print("Shader set");
}

just spent a few hours trying to wrestle with this myself and finally got it to work (I think) so here y'are:

it seems when you import a normal map to unity, it intenally has the setup of RGB = y normal, A = x normal.

so when you load a texture at runtime, you should do something like (in C# sorry):

loadedTexture = www.texture;
normalTexture = new Texture2D(loadedTexture.width,loadedTexture.height,TextureFormat.ARGB32,false);
Color theColour = new Color();
for (int x=0; x<loadedTexture.width; x++){
for (int y=0; y<loadedTexture.height; y++){
   theColour.r = loadedTexture.GetPixel(x,y).g;
   theColour.g = theColour.r;
   theColour.b = theColour.r;
   theColour.a = loadedTexture.GetPixel(x,y).r;
   normalTexture.SetPixel(x,y, theColour);
}
}
normalTexture.Apply();

hope that helps :)

Thanks for this post and solution, that fix my issue!

However, instead of using double loop operations you might use a faster method as following:


    Color [] myColors; // or define it globally
    normalMap= new Texture2D( wwwTex.texture.width,  wwwTex.texture.height, TextureFormat.ARGB32, true);
    myColors= wwwTex.texture.GetPixels ();
   for (int i = 0; i < myColors.Length; i++) {
    	myColors _.a = myColors *.r;*_

myColors .r = myColors .g;
myColors .b = myColors .g;
//myColors .g = myColors .g;
* }*
normalMap.SetPixels (myColors);
normalMap.Apply();
----------
That to get same suggested results above. But, to get exact results as unity you may need to clear R and B component as follows:
----------

Color [] myColors; // or define it globally
normalMap= new Texture2D( wwwTex.texture.width, wwwTex.texture.height, TextureFormat.ARGB32, true);
myColors= wwwTex.texture.GetPixels ();
for (int i = 0; i < myColors.Length; i++) {
myColors .a = myColors .r;
_ myColors .r = 0;
myColors .b = 0;
//myColors .g = myColors .g;
}
normalMap.SetPixels (myColors);
normalMap.Apply();
----------
Hope that helps!_

Apparently you don’t have to this anymore (2021+?) - Normal maps need to be linear rather than sRGB as described here: https://forum.unity.com/threads/runtime-normal-map-formats.1192042/