Unity 5中的运行时法线贴图导入

6

对于我的项目,我需要在运行时创建材料。当我创建材料时,法线贴图没有效果。我尝试了两种解决方案,但它们对我不起作用。在Unity 5中是否有所改变?

我查看的链接:

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

附言:奇怪的是,当我在Unity中切换到“场景视图”时,如果我从“检查器”扩展材质选项卡,则法线贴图将应用于对象。

我的代码:

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

你是通过什么方式访问mat的?我想到了一些场景,你可能正在编辑错误的材质实例。如果还没有这样做,您应该双重检查您的着色器是否需要一个名为“_BumpMap”的纹理变量。 - rutter
@rutter 我使用Shader.Find()函数创建了新的材质(我已经更新了上面的代码)。法线贴图和颜色纹理被添加到正确的位置。然而,当我在Unity编辑器的场景视图中选择对象并展开材质组件时,法线贴图会激活。问题是Unity将法线纹理保存在一个名为DXTnm的不同格式中,除非我在编辑器上单击它,否则它无法识别我的纹理。当我单击材质时,它可能会将法线纹理转换为正确的格式。 - Berke Cagkan Toptas
1个回答

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接