Unity3D shaderLab 使用BlinnPhong高光类型

上一篇我们实现了自定义高光类型,这一篇,我们说Blinn高光,它是另一种计算和估算高光更高效的方式,它是通过视线防线和光线方向,所形成的半角向量来完成。

这种方式比我们自己形成反射向量来进行计算更加高效。在UnityCG.cginc文件中内置的BlinnPhong光照模型就是半角矢量完成的。

首先还是创建一个shader,一个材质球,双击shader,打开编辑器。

1:Properties

Properties {

_MainTex ("Base (RGB)", 2D) = "white" {}

_MainTint("Diffuse Tint",Color)=(,,,)

_SpecularColor("Specular Color",Color)=(,,,)

_SpecularPower("Specular Power",Range(0.1,))=

}

2:SubShader申明变量

CGPROGRAM

#pragma surface surf MyBlinnPhong

sampler2D _MainTex;

float4 _SpecularColor;

float4 _MainTint;

float _SpecularPower;

3:LightingMyBlinnPhong光照模型实现

inline fixed4 LightingMyBlinnPhong(SurfaceOutput s,fixed3 lightDir,half3 viewDir, fixed atten){

float3 halfVector = normalize(lightDir+viewDir);

float diff = max(,dot(s.Normal, lightDir));

float nh = max(,dot(s.Normal, halfVector));

float spec = pow(nh,_SpecularPower)*_SpecularColor;

float4 c;

c.rgb = (s.Albedo*_LightColor0.rgb*diff)+(_LightColor0.rgb*_SpecularColor.rgb*spec)*(atten*);

c.a = s.Alpha;

return c;

}

4:surf函数

half4 c = tex2D (_MainTex, IN.uv_MainTex)*_MainTint;

最终效果如下:

最左侧是BlinnPhong,最右侧是自定义Phong,中间的是最基础的高光效果。

BlinnPhong高光模型和phong高光模型效果几乎一致,除了前者使用更少的代码更高效,他们的效果几乎一致。

在上面的 LightingMyBlinnPhong光照模型中,我们为了获得半角向量,视线方向和入射方向叠加,通过normalize获得了半角向量。

我们再简单的对顶点法线和新的半角向量进行点乘运算从而获得我们的高光值,再对它进行 SpecularPower次方求幂后乘以高光颜色值 SpecularColor。

虽然过程简化,但是同样能给我们带来出色的高光效果。

code start--------------------------------------------------------------------------------

Shader "91YGame/BlinnPhong" {
Properties {
_MainTex ("Base (RGB)", 2D) = "white" {}
_MainTint("Diffuse Tint",Color)=(,,,)
_SpecularColor("Specular Color",Color)=(,,,)
_SpecularPower("Specular Power",Range(0.1,))=
}
SubShader {
Tags { "RenderType"="Opaque" }
LOD CGPROGRAM
#pragma surface surf MyBlinnPhong sampler2D _MainTex;
float4 _SpecularColor;
float4 _MainTint;
float _SpecularPower; struct Input {
float2 uv_MainTex;
}; inline fixed4 LightingMyBlinnPhong(SurfaceOutput s,fixed3 lightDir,half3 viewDir, fixed atten){
float3 halfVector = normalize(lightDir+viewDir);
float diff = max(,dot(s.Normal, lightDir));
float nh = max(,dot(s.Normal, halfVector));
float spec = pow(nh,_SpecularPower)*_SpecularColor; float4 c;
c.rgb = (s.Albedo*_LightColor0.rgb*diff)+(_LightColor0.rgb*_SpecularColor.rgb*spec)*(atten*);
c.a = s.Alpha;
return c;
} void surf (Input IN, inout SurfaceOutput o) {
half4 c = tex2D (_MainTex, IN.uv_MainTex)*_MainTint;
o.Albedo = c.rgb;
o.Alpha = c.a;
}
ENDCG
}
FallBack "Diffuse"
}

code end --------------------------------------------------------------------------------

Unity3D ShaderLab 使用BlinnPhong高光类型的更多相关文章

  1. Unity3D ShaderLab 创建自定义高光类型

    Unity3D ShaderLab 创建自定义高光类型 在上一篇,我们认识了Unity基础的高光实现,本次主要是研究如何对Phong高光类型进行顶点操作,以及在表面着色器中使用Input结构体的新参数 ...

  2. Unity3D ShaderLab 基础的高光实现

    Unity3D ShaderLab 基础的高光实现 关于高光: 在一个物体表面的高光属性就是为了描述它是如何表现光泽.这种类型的效果在着色器的世界中通常称为视点相关效果. 之所以这样说,是因为为了实现 ...

  3. Unity3D ShaderLab 各向异性高光

    Unity3D ShaderLab 各向异性高光 各向异性时一种模拟物体表面沟槽方向性的高光反射类型,它会修改或延伸垂直方向上的高光.当我们想模拟金属拉丝高光的时候,它非常适合.下面就一步一步实现. ...

  4. Unity3D ShaderLab 使用贴图对模型的高光进行遮罩

    Unity3D ShaderLab 使用贴图对模型的高光进行遮罩 前面研究了高光效果的实现,再说说现很多游戏用到的高光贴图技术,因为它可以让3D美工更容易控制最终的视觉效果. 这也就为我们提供了另外的 ...

  5. Unity3D ShaderLab 立方体图的菲涅尔反射

    Unity3D ShaderLab 立方体图的菲涅尔反射 菲涅尔反射是反射类型中比较常见的一种类型,当我们的视线正对物体表面,那么反射量会明显增加, 我们几乎可以在任何支持反射类型的物体表面看到这种情 ...

  6. Unity3D ShaderLab 透明裁剪着色器

    Unity3D ShaderLab 透明裁剪着色器 上一篇,我们介绍了使用Alpha实现透明的手法,其实Unity为我们的#pragma提供了另一种参数,是我们能做出更高效 简单的透明效果,也就是裁剪 ...

  7. Unity3D ShaderLab 布料着色器

    Unity3D ShaderLab布料着色器 布料着色器是我们在虚拟现实中经常使用的着色器.本篇就来完成一个较为简单的布料着色器. 新建Shader,Material,InteractiveCloth ...

  8. Unity3D ShaderLab 漫反射卷积光照模型

    Unity3D ShaderLab 漫反射卷积光照模型 漫反射卷积[Diffuse convolution]是一个模糊立方体的过程,它保留了立方图的整体光照强度,只模糊了细节. 这种效果在我们要活得一 ...

  9. Unity3D ShaderLab 模拟纹理运动

    Unity3D ShaderLab 模拟纹理运动 这一篇,我们要说到着色器上的uv贴图的滚动效果,这样的场景可以用在河流,瀑布,熔岩等效果.算是创建纹理动画的基础技术之一. 所以 准备一个新的着色器文 ...

随机推荐

  1. template模版与Underscore.js

    template模版与Underscore.js 在项目中经常使用的模版是Underscore这个js框架的实用功能. 在html里面设定模板,然后js绑定数据,这样能避免在js中出现非常多的html ...

  2. ocument的createDocumentFragment()方法

    在<javascript高级程序设计>一书的6.3.5:创建和操作节点一节中,介绍了几种动态创建html节点的方法,其中有以下几种常见方法: · crateAttribute(name): ...

  3. Installing a single-server IBM FileNet P8 Platform system with IBM Content Navigator

    Product documentation Abstract You can install all of the IBM FileNet P8 Platform components on a si ...

  4. easyui layout 收缩的bug

    easyui layout提供collapse方法折叠指定的 panel,'region' 参数可能的值是:'north'.'south'.'east'.'west',但是在 IE6的环境下,调用这个 ...

  5. asp.net 页面局部刷新

    <asp:ScriptManager ID="ScriptManager1" runat="server"> </asp:ScriptMana ...

  6. jquery返回上一页面

    window.location.href=document.referrer;   返回然后刷新 window.history.back(-1);  返回不刷新

  7. linux下安装vtune_amplifier_xe_2015_update4

    说明:        linux系统: CentOS 6.0 Vtune版本: 2015 安装过程: 1.下载vtune_amplifier_xe_2015_update4.tar.gz(到官网去下载 ...

  8. C#窗体的加载等待(BackgroundWorker控件)实现

    窗体拉一个Button按钮和一个加载等待显示的label, label默认隐藏,点击按钮时显示这个label,加载完再隐藏 1.工具箱拉BackgroundWorker控件到窗体 2.backgrou ...

  9. Debugger Exception Notification

    --------------------------- Debugger Exception Notification --------------------------- Project PJSP ...

  10. KMP算法中next函数的理解

    首先要感谢http://blog.csdn.net/v_july_v/article/details/7041827以及http://blog.chinaunix.net/uid-27164517-i ...