码迷,mamicode.com
首页 > 编程语言 > 详细

unity5, make a custom PBS shader

时间:2015-04-06 06:12:57      阅读:232      评论:0      收藏:0      [点我收藏+]

标签:

unity5中引入了基于物理着色(PBS)的Standard shader。由于这种着色器通过调节参数和贴图可逼真模拟各种硬质表面,所以不必再像unity4时代那样需要对各种质感材质单独编写着色器,而且能得到更好的效果。这种“万能着色器”仿佛给人一种不再需要自己编写着色器的假象,但做游戏跟做虚拟现实不一样,除了真实性,还要追求趣味性和艺术夸张。所以老古语不过时:没有使用自定义着色器的游戏,不是好游戏。

但自己实现PBS是很困难的,如果我们想既继承Standard shader的PBS特性又加入自己的定制效果,最好我们的自定义shader能在Standard shader的基础上进行编写,即实现自定义PBS着色器(custom PBS shader)。

由于是新东西,资料不全,google了一整天也没能找到现成方法,unity官方文档中对此完全没有作说明(在surface shader自定义光照模型 部分只给了不带PBS的例子),unity论坛里有多个帖子问到类似问题,但都没有满意解答。最后在下面两个连接里找到了一点儿线索:

http://forum.unity3d.com/threads/for-those-in-u5-beta-is-pbr-really-that-good.283867/page-3#post-1886525

http://blogs.unity3d.com/2014/10/29/physically-based-shading-in-unity-5-a-primer/ (文章下面 MIG 的提问)

此线索是:“

  from the release notes for beta 12:

  • Shaders: Surface shaders can use physically based shading now; the same as Standard shader uses.
    • Use "Standard" lighting function, and "SurfaceOutputStandard" output structure.
    • Do an #include "UnityPBSLighting.cginc" in your shader to get it.
    • Default options require shader model 3.0, so add a "#pragma target 3.0" too.

然后又结合了UnityPBSLighting.cginc中的源代码(注1),当然,只是从UnityPBSLighting.cginc中拷贝一些代码出来(而不是修改它),最后终于把custom PBS shader试验成功了。

注1:UnityPBSLighting.cginc这个文件在哪儿?有三个途径获得:

(1),在 http://docs.unity3d.com/Manual/SL-SurfaceShaderLighting.html 中写道:“file inside Unity ({unity install path}/Data/CGIncludes/ on Windows, /Applications/Unity/Unity.app/Contents/CGIncludes/ on Mac)”,需要注意的是"/Applications/Unity/Unity.app/Contents/CGIncludes/"这个路径是在Unity.app的“包内容”里,所以这就是为什么在Mac上虽然UnityPBSLighting.cginc已经随unity一起安装了,但确不能通过文件搜素找到。

(2),通过网址http://docs.unity3d.com/Manual/StandardShaderMakeYourOwn.html进入在线的"Make your own"页面下载builtin_shaders的最新版本(也包括历史版本)。因为我的unity是最新的,所以通过此途径下载到的最新版的builtin_shaders与途径(1)中的是一致的,但如果你的unity不是最新的,一定要根据你的unity版本号下载相应版本的builtin_shaders,否则你拷贝其中的代码用到自定义shader中可能报错。

(3),在浏览器地址栏输入file:///Applications/Unity/Unity.app/Contents/Documentation/en/Manual/StandardShaderMakeYourOwn.html进入离线的"Make your own"页面(从地址上你可以看出这个页面实际上保存在你的电脑中),下载builtin_shaders,但要注意,通过这个离线文档下载的builtin_shaders可能不是最新的,我今天就被此坑了一回,通过此途径下载的UnityPBSLighting.cginc文件中的代码拷贝到自定义shader中报错,搞了半天没找到原因,直到我又通过途径(1)重新获得UnityPBSLighting.cginc。。。

综上,途径(1)是最好的。

下图第一个球用的是Standard shader,第二个球用的是“将法线当作颜色值”的自定义shader(不带PBS),第三个球是今天的试验成果:在Standard shader的PBS基础上添加了“将法线当作颜色值”效果的杂交shader。

技术分享

unity5, make a custom PBS shader

标签:

原文地址:http://www.cnblogs.com/wantnon/p/4395286.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!