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

c# – Unity 5中的运行时法线贴图导入,第1张

概述对于我的项目,我需要在运行时创建材料.当我创建材质时,法线贴图无效.我尝试了两个解决方案,但它们对我不起作用.在Unity 5中有什么变化吗? 我检查的链接: http://answers.unity3d.com/questions/801670/runtime-loading-normal-texture.html http://answers.unity3d.com/questions/471 对于我的项目,我需要在运行时创建材料.当我创建材质时,法线贴图无效.我尝试了两个解决方案,但它们对我不起作用.在Unity 5中有什么变化吗?

我检查的链接:

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

P.S:奇怪的是当我切换到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; }
解决方法 至少在Unity 4.x中,您必须修改着色器才能正确显示运行时法线贴图.只需要从代码中删除Unpacknormal().

技术细节:
http://forum.unity3d.com/threads/creating-runtime-normal-maps-using-rendertotexture.135841/#post-924587

内置着色器源可以从以下位置下载:
http://unity3d.com/get-unity/download/archive

总结

以上是内存溢出为你收集整理的c# – Unity 5中的运行时法线贴图导入全部内容,希望文章能够帮你解决c# – Unity 5中的运行时法线贴图导入所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/langs/1226509.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-05
下一篇 2022-06-05

发表评论

登录后才能评论

评论列表(0条)

保存