- Home /
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");
}
Answer by SophieH · Apr 16, 2011 at 08:11 PM
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 :)
LOVE YOU!!!! i've been looking for the solution to this for months with no real solution! it even works on the shader side real-time :)
Thank you very much. Generating a normalmap-atlas for my tile-atlas would work much less convenient without your code.
Since the question got already bumped i just want to say that this conversion is extremely inefficient. GetPixel has a bad performance. Also calling GetPixel two times per pixel doubles the overhead.
You should do something like this:
loadedTexture = www.texture;
normalTexture = new Texture2D(loadedTexture.width,loadedTexture.height,TextureFormat.ARGB32,false);
Color32[] colours = loadedTexture.GetPixels32();
for (int i=0; i < colours.Length; i++){
Color32 c = colours[i];
c.a = c.r;
c.r = c.b = c.g;
colours[i] = c;
}
normalTexture.SetPixels32(colours);
normalTexture.Apply();
Get and SetPixels is way faster than reading one pixel at a time. Color32 is also a bit faster as it's a more compact format and the actual format used in memory.
This works great on laptop/Editor, but does not work on iOS and Android. The result looks faded compared to the one with correct normal map. Below are the comparison between correct and non-correct one. Is there chance that you might know why @Bunny83? I'm using Standard $$anonymous$$aterial. Any leads will be helpful. Thanks!
Answer by tnageleweb · May 30, 2016 at 02:47 PM
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 [i].a = myColors [i].r;
myColors [i].r = myColors [i].g;
myColors [i].b = myColors [i].g;
//myColors [i].g = myColors [i].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 [i].a = myColors [i].r;
myColors [i].r = 0;
myColors [i].b = 0;
//myColors [i].g = myColors [i].g;
}
normalMap.SetPixels (myColors);
normalMap.Apply();
Hope that helps!
Answer by Michael_Swan · 5 days ago
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/