Ber*_*tas 6 c# unity-game-engine normals
对于我的项目,我需要在运行时创建材质。创建材质时,法线贴图不起作用。我尝试了这两种解决方案,但它们对我不起作用。在 Unity 5 中对此有什么改变吗?
我检查的链接:
http://answers.unity3d.com/questions/801670/runtime-loading-normal-texture.html http://answers.unity3d.com/questions/47121/runtime-normal-map-import.html
PS:奇怪的是,当我在 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;
}
Run Code Online (Sandbox Code Playgroud)
至少在 Unity 4.x 中,您必须修改着色器才能正确显示运行时法线贴图。只需从代码中删除 UnpackNormal() 即可。
内置着色器源可以从以下位置下载: http ://unity3d.com/get-unity/download/archive