Unity3D ShaderLab 使用BlinnPhong高光类型
2015-01-18 20:07
316 查看
Unity3D shaderLab 使用BlinnPhong高光类型
上一篇我们实现了自定义高光类型,这一篇,我们说Blinn高光,它是另一种计算和估算高光更高效的方式,它是通过视线防线和光线方向,所形成的半角向量来完成。
这种方式比我们自己形成反射向量来进行计算更加高效。在UnityCG.cginc文件中内置的BlinnPhong光照模型就是半角矢量完成的。
首先还是创建一个shader,一个材质球,双击shader,打开编辑器。
1:Properties
2:SubShader申明变量
3:LightingMyBlinnPhong光照模型实现
4:surf函数
最终效果如下:
最左侧是BlinnPhong,最右侧是自定义Phong,中间的是最基础的高光效果。
BlinnPhong高光模型和phong高光模型效果几乎一致,除了前者使用更少的代码更高效,他们的效果几乎一致。
在上面的 LightingMyBlinnPhong光照模型中,我们为了获得半角向量,视线方向和入射方向叠加,通过normalize获得了半角向量。
我们再简单的对顶点法线和新的半角向量进行点乘运算从而获得我们的高光值,再对它进行 SpecularPower次方求幂后乘以高光颜色值 SpecularColor。
虽然过程简化,但是同样能给我们带来出色的高光效果。
code start--------------------------------------------------------------------------------
code end --------------------------------------------------------------------------------
上一篇我们实现了自定义高光类型,这一篇,我们说Blinn高光,它是另一种计算和估算高光更高效的方式,它是通过视线防线和光线方向,所形成的半角向量来完成。
这种方式比我们自己形成反射向量来进行计算更加高效。在UnityCG.cginc文件中内置的BlinnPhong光照模型就是半角矢量完成的。
首先还是创建一个shader,一个材质球,双击shader,打开编辑器。
1:Properties
Properties { _MainTex ("Base (RGB)", 2D) = "white" {} _MainTint("Diffuse Tint",Color)=(1,1,1,1) _SpecularColor("Specular Color",Color)=(1,1,1,1) _SpecularPower("Specular Power",Range(0.1,30))=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(0,dot(s.Normal, lightDir)); float nh = max(0,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*2); 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)=(1,1,1,1) _SpecularColor("Specular Color",Color)=(1,1,1,1) _SpecularPower("Specular Power",Range(0.1,30))=1 } SubShader { Tags { "RenderType"="Opaque" } LOD 200 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(0,dot(s.Normal, lightDir)); float nh = max(0,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*2); 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 使用贴图对模型的高光进行遮罩
- Unity3D ShaderLab 创建自定义高光类型
- Unity3D ShaderLab 创建自定义高光类型
- Unity3D ShaderLab 各向异性高光
- Unity3D C# List数据类型使用
- 【转】使用SYSTEMINFO类获取UNITY3D运行设备的各类信息(CPU类型,显卡类型等)
- 高光反射-逐像素光照(Blinn-Phong)
- 看书笔记——《Unity3d脚本编程使用C#语言开发跨平台游戏》——值类型和引用类型
- Unity3D ShaderLab 基础的高光实现
- blinn-phong高光反向穿透问题
- unity3d Shader镜面高光反射(使用fresenl), bolinPhone是没有物理依据的
- Unity|ShaderLab笔记整理-五(逐像素漫反射+环境光 +高光反射(Phone+ BlinnPhong))
- Unity3D ShaderLab 使用渐变纹理着色
- Unity3D ShaderLab 使用alpha参数创建透明效果
- 看书笔记——《Unity3d脚本编程使用C#语言开发跨平台游戏》——U3d游戏脚本中的值类型
- 使用SYSTEMINFO类获取UNITY3D运行设备的各类信息(CPU类型,显卡类型等)
- CHAR,VARCHAR,VARCHAR2类型的区别与使用
- 在Asp中使用存储过程 数值类型值
- 使用DLL时如何处理“System.NullReferenceException”类型的异常
- 应该使用c# 预定义类型 还是绝对不要使用预定义类型。