简体   繁体   中英

Runtime Normal Map Import 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

PS: 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);

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;
 }

At least with Unity 4.x you had to modify the shader to display runtime normal maps correctly. Just needed to remove UnpackNormal() from the code.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM