Runtime Normal Map Import in Unity 5

For my project I need to create materials at runtime. When I create a material, the normal map does not work. I tried two solutions about this, but they didn't work for me. Has anything changed about this in Unity 5?

The links I have checked:

http://answers.unity3d.com/questions/801670/runtime-loading-normal-texture.html http://answers.unity3d.com/questions/47121/runtime-normal-map-import.html

PS: It's weird when I switch to "Scene View" inside Unity, if I expand the material tab from Inspector, the normal map is applied to the object.

My code:

            ....
Material mat = new Material(Shader.Find("Standard (Specular setup)"));
mat.SetTexture("_MainTex", colortex);

normaltex = getNormalTexture(Texture2D 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;
 }

      

+3


source to share


1 answer


Since at least Unity 4.x you have had to modify the shader to correctly display normal runtime maps. You just need to remove UnpackNormal () from your code.

Technical details: http://forum.unity3d.com/threads/creating-runtime-normal-maps-using-rendertotexture.135841/#post-924587



Built-in shader sources can be downloaded from: http://unity3d.com/get-unity/download/archive

0


source







All Articles