- Home /
Runtime loading normal texture
Hello,
I'm currently trying to load textures at runtime through the WWW class and it is working perfectly for every texture except for normal textures. Unity does not convert these like they do when you put them in the editor. I've looked through Unity Anwsers but only found this solution: http://answers.unity3d.com/questions/47121/runtime-normal-map-import.html Which creates a grayscale version of my normal map. If I recall correctly this is how Unity used to work with normals.
When I use this runtime loaded and converted normal map with Unity 4.5 the lighting is all over the place. I did not find any other method to load a normal map at runtime.
Does somebody know a way to load the normals correctly? Like when I load normals in the editor and convert them to a normal map.
Runtime modified through previous anwser (grayscale):
Runtime unmodified:
Answer by Aras · Oct 09, 2014 at 06:59 AM
On PC & console platforms, Unity does "DXT5nm" encoding for normal maps. The X component (red channel of original normal map texture) should be put into alpha; and Y component (green channel of original normal map texture) should stay in the green channel. Red and blue channels are not used.
This is done so that DXT5 compression can be done for normal maps at acceptable quality, since if done on "regular normal map" the DXT5 quality is really, really bad.
On "mobile" platforms a regular normal map encoding is uses, where XYZ components are just put into RGB channels of the texture.
Thanks! This works. It makes the texture green ins$$anonymous$$d of greyscale but it does the trickt. Below I've added a method to convert a normal texture imported at runtime (modified from http://answers.unity3d.com/questions/47121/runtime-normal-map-import.html).
private Texture2D Normal$$anonymous$$ap(Texture2D source) {
Texture2D normalTexture = new Texture2D(source.width, source.height, TextureFormat.ARGB32, true);
Color theColour = new Color();
for (int x = 0; x < source.width; x++)
{
for (int y = 0; y < source.height; y++)
{
theColour.r = 0;
theColour.g = source.GetPixel(x, y).g;
theColour.b = 0;
theColour.a = source.GetPixel(x, y).r;
normalTexture.SetPixel(x, y, theColour);
}
}
normalTexture.Apply();
return normalTexture;
}
This explanation helped me save generated normal maps correctly. Thanks a bunch!