这一次学习各向异性高光类型,名字真拗口,Anisotropic 这个英文单词也很拗口。

各向异性是模拟物体表面 沟槽方向性的高光反射类型,他会修改或延伸垂直方向上的高光。

比如模拟金属拉丝的效果,就可以使用各向异性来模拟。

转自http://blog.csdn.net/huutu http://www.thisisgame.com.cn

首先需要准备一张各向异性 的法线贴图,代表各向异性镜面高光的方向性。

注意法线贴图导入到Unity之后要在属性面板中勾选类型为 Normal Map。

首先在 Properties 块中添加相应的属性

 Properties {_MainTex ("Base (RGB)", 2D) = "white" {}_MainTint("Diffuse Tint",Color)=(1,1,1,1)_SpecularColor("Specular Color",Color)=(1,1,1,1)_Specular("Specular Amount",Range(0,1))=0.5_SpecularPower("Specular Power",Range(0,1))=0.5_AnisoDir("Anisotropic Direction",2D)=""{}_AnisoOffset("Anisotropic Offset",Range(-1,1))=-0.2}

然后声明对应的变量供Subshader 使用

     sampler2D _MainTex;float4 _MainTint;float4  _SpecularColor;float _Specular;float _SpecularPower;sampler2D _AnisoDir;float _AnisoOffset;

其中的 _AnisoDir  是留给法线贴图的。

因为需要读取法线贴图的数据,所以修改 Input 结构体,把法线贴图的 UV 变量加入。

     struct Input {float2 uv_MainTex;float2 uv_AnisoDir;};

Unity 中默认的 SurfaceOutput 不符合我们这个Shader 的需求了,所以需要自定义一个

     struct SurfaceAnisoOutput{fixed3 Albedo;fixed3 Normal;fixed3 Emission;fixed3 AnisoDirection;half Specular;fixed Gloss;fixed Alpha;};

然后在 Surf 函数中,使用 UnpackNormal 读取 法线贴图中的数据。通过 SurfaceAnisoOutput 传递到 光照模型函数中

     void surf (Input IN, inout SurfaceAnisoOutput o) {half4 c = tex2D (_MainTex, IN.uv_MainTex) * _MainTint;float3 anisoTex=UnpackNormal(tex2D(_AnisoDir,IN.uv_AnisoDir));o.AnisoDirection=anisoTex;o.Specular=_Specular;o.Gloss=_SpecularPower;o.Albedo = c.rgb;o.Alpha = c.a;}

添加光照模型函数

     inline fixed4 LightingAnisotropic(SurfaceAnisoOutput s,fixed3 lightDir,half3 viewDir,fixed atten){//计算半角向量fixed3 halfVector=normalize(normalize(lightDir)+normalize(viewDir));//计算法线和光照方向的cos值float NdotL=saturate(dot(s.Normal,lightDir));//fixed HdotA=dot(normalize(s.Normal + s.AnisoDirection) , halfVector);float aniso=max(0,sin(radians((HdotA + _AnisoOffset)*180)));float spec=saturate(pow(aniso,s.Gloss*128) * s.Specular);fixed4 c;c.rgb=( (s.Albedo * _LightColor0.rgb * NdotL)  + (_LightColor0.rgb * _SpecularColor.rgb * spec)) * (atten * 2);c.a=1.0;return c;}

Shader 编写好之后,创建 Material,在场景中创建3D GameObject,设置 Material,然后把法线贴图 赋值到 Material 中,最终效果如下 转自http://blog.csdn.net/huutu http://www.thisisgame.com.cn

实现原理:

在光照模型函数中,首先计算半角向量

//计算半角向量fixed3 halfVector=normalize(normalize(lightDir)+normalize(viewDir));

然后将 顶点法线 与 各向异性法线贴图的每个像素求和,然后进行normalize,再与 半角向量 进行点积运算。

fixed HdotA=dot(normalize(s.Normal + s.AnisoDirection) , halfVector);

这样得到的浮点值 HdotA。

当HdotA ==1,表示物体表面法线 与 halfVector 平行。

当 HdotA==0,表示物体表面法线 与 halfVector 垂直。

然后,使用 sin() 函数对这个值进行修改。

loat aniso=max(0,sin(radians((HdotA + _AnisoOffset)*180)));

最后,对 aniso 值产生的效果进行放大,对它进行 s.Gloss 方求幂,再乘上 s.Specular 值,在全局范围内降低它的强度。

float spec=saturate(pow(aniso,s.Gloss*128) * s.Specular);

书上提到,需要指定着色器使用 Shader model 3.0 模式。

#pragma target 3.0

我测试后法线不指定也是可以的。

到wiki上找到关于 target 的一些比较

2.0 or default

Compiles the shader under shader model 2. Model 2 has more limitations than 3 but is more compatible. Uses shader model 1.1 for vertex shaders.

Vertex: 128 instruction limit. Fragment: 96 instruction limit (32 texture + 64 arithmetic), 16 temporary registers and 4 texture indirections.

3.0

Compiles the shader under shader model 3. Model 3 is more powerful and flexible than 2 but is less compatible.

Vertex: no instruction limit.

Fragment: 1024 instruction limit (512 texture + 512 arithmetic), 32 temporary registers and 4 texture indirections.

It is possible to override these limits using #pragma profileoption directive.

For example, #pragma profileoption MaxTexIndirections=256 raises texture indirections limit to 256.

See #pragma profileoption for more information. Note that some shader model 3.0 features, like derivative instructions, aren't supported by vertex or fragment shaders.

You can use #pragma glsl to translate to GLSL instead which has fewer restrictions. See #pragma glsl for more information.

查看相关网页

http://www.ceeger.com/Components/SL-ShaderPrograms.htmlhttp://wiki.unity3d.com/index.php?title=Shader_Code

示例项目打包下载:

http://pan.baidu.com/s/1eSK1rl4

Unity Shaders and Effects Cookbook (3-6) 创建各向异性高光类型(Anisotropic) 模拟金属拉丝效果相关推荐

  1. Unity Shaders and Effects Cookbook (7-2) Surface Shader 中实现 顶点动画

    上一节中说了,在 Surface Shader 中,添加顶点函数,我们可以在 顶点函数中获取到 顶点数据,比如顶点颜色.顶点坐标等. 这一节学习获取顶点坐标,并且修改顶点坐标,来实现顶点动画. 简单介 ...

  2. Unity Shaders and Effects Cookbook (3-4) 使用高光贴图

    在学习完上一节之后.已经了解了在Unity 中怎样实现一个高光 Shader ,可是会有一个问题.就是效果看起来不切实际,如以下的问题 我用一张图片贴到了Cube上面.然后用了一个高光材质,得到了下图 ...

  3. Unity Shaders and Effects Cookbook (4-5)Cubemap与菲涅尔反射

    菲涅尔是人名,他发现,当我们站在水中,直直的往下看水面,是看不到反射的太阳光的.而当我们往远处看时,就能看到很强的反光,也就是成语波光粼粼所体现的.这一现象就被命名为 菲涅尔反射. 刚看这一节的时候觉 ...

  4. Unity Shaders and Effects Cookbook (2-7)实现 Photoshop 色阶效果

    看完了这一书上的代码,然后在网上查找 Photoshop 色阶的一些解释,大致理解为:调整色阶就是调整亮度,也就是调整纹理 R.G.B 通道的数值大小. 下面是 Photoshop 中的直方图转自ht ...

  5. 《Unity着色器和屏幕特效开发秘笈》—— 3.4 创建BlinnPhong高光类型

    本节书摘来自华章出版社<Unity着色器和屏幕特效开发秘笈>一 书中的第3章,第3.4节,作者:(美)Kenny Lammers,更多章节内容可以访问云栖社区"华章计算机&quo ...

  6. 【《Unity 2018 Shaders and Effects Cookbook》翻译提炼】(八)BlinnPhong模型和 Anisotropic 高光(可用于金属性,头发)

    这篇文章最好与前一篇连着看,自己可做比较. 1.BlinnPhong 模型 Blinn是计算和估计镜面反射的另一种更有效的方法.这是通过从 视图方向和光方向获得半矢量来完成饿.JimBlinn将它带入 ...

  7. Unity Shaders and Effects

    https://www.baidu.com/link?url=o85V5QLsraCjMOmzJc6081Z3bsQRw4_hjoPki5igyrvrJExygnt2Ex-_oGmH60-wm2VU5 ...

  8. 【《Unity 2018 Shaders and Effects Cookbook》翻译提炼】(九)Physically - Based Rendering

    制作过程中最重要的方面时效率,实时着色很昂贵,而Lambertian或BlinnPhong等技术是计算成本和现实之间的折中. 拥有更   强大的GPU允许我们逐步写更强大的光照模型和渲染引擎,目的是模 ...

  9. 22.01.23 《Unity 2018 Shaders and Effects Cookbook》笔记Chapter7

    7.Fragment Shaders and Grab Passes vertex and fragment shader 对光照如何影响物体表面不感兴趣,不受物理限制,对非真实感的效果很好 使用#p ...

最新文章

  1. electron-vue模仿网易云桌面应用体验
  2. [蓝桥杯2016决赛]一步之遥-枚举
  3. 专访图灵奖得主:完全自动驾驶是计算机接近人类智能的一大步
  4. BlueCatTools-域名批量查询软件介绍
  5. 10.14. Network
  6. [Spring Boot核心功能]1. SpringApplication 启动引导类(2)
  7. 【WCF】无法加载协定为“ServiceReference1.xxxxxx”的终结点配置部分,因为找到了该协定的多个终结点配置。请按名称指示首选的终结点配置部分.
  8. Qt QMake详解
  9. 2023河海大学计算机考研信息汇总
  10. 一种定位激光在摄像头画面中位置的方法(附安卓源码)
  11. NodeMCU ESP8266+Arduino:将宿舍老式门锁改造为简易密码锁
  12. 卸载WPS后Office文档图标显示异常
  13. 【跟我一起学Unity3D】做一个2D的90坦克大战之各种各样的墙附上项目源码
  14. 联想笔记本电脑开机无法修复计算机,联想笔记本开机没反应怎么办 笔记本无法开机的解决方法...
  15. 优秀logo,最基础的设计技巧(五)
  16. quartz动态任务调度实现
  17. Veritas NetBackup 10.0 (Unix, Linux, Windows)
  18. 学计算机的3个奋斗目标,我的学习方法和奋斗目标
  19. 在 Chrome 中清除、启用和管理 Cookie
  20. @click=handleClick

热门文章

  1. 【蓄电池充电】蓄电池如何正确充电 蓄电池充电方法详解 - 转
  2. 深入理解计算机系统第五章------优化程序性能
  3. NOT NULL约束
  4. 养生保健的误区有哪些?
  5. Text 1 Do We Need Extra Vitamins?
  6. 如何把模糊的照片还原?
  7. RT-Thread 隐藏的宝藏之 data_queue
  8. erp系统主要功能和作用
  9. 【验证码逆向专栏】某验“初代”滑块验证码逆向分析
  10. 电商网站搭建——什么是Nginx?常见web服务器有哪些?