- Home /
How to import Normal map at Runtime in Unity 5?
For my project, I need to create materials at run time. When I create material, Normal map has no effect. I tried the two solutions about this but they did not work for me. Is something has changed about this in Unity 5 ?
The Links I checked :
http://answers.unity3d.com/questions/801670/runtime-loading-normal-texture.html http://answers.unity3d.com/questions/47121/runtime-normal-map-import.html
P.S: The weird thing is when I switch to "Scene View" inside Unity, If I expand material tab from "Inspector" Normal Map is being applied to the object.
My Code:
....
Material mat = new Material(Shader.Find("Standard (Specular setup)"));
mat.SetTexture("_MainTex", colortex);
//For source texture I both tried to use a file from external path and from
//Resources path which the image file marked as "Normal map".Result did not changed.
normaltex = getNormalTexture(source);
mat.SetTexture("_BumpMap", normaltex);
mat.SetFloat("_Glossiness", 0.1f);
mat.SetFloat("_BumpScale", 1.0f);
....
public static Texture2D getNormalTexture(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;
}
I am having this EXACT issue. For some reason the normal map doesn't seem to take effect until I flip the triangle down in the inspector (next to the material).
Sounds like an editor bug or a legit bug, try running a build to see if the normal map still fails to apply. If so, submit a bug report!
I was loading a $$anonymous$$aterial with (resource load) so I just set it up with a fake diffuse/normal, and that made it work. (I think it's how Unity "chooses the best shader" when you use "standard")
Answer by Gagan25 · Jun 21, 2017 at 08:14 AM
hello thankyou for your code. This code works perfectly on mac and windows. But on iOS normal map is not created properly. As you can see in screenshot. As i switch platform from iOS to pc it comes properly. Please see the screenshot for reference. Please help me in this case.alt text