您的位置:首页 > 移动开发 > Unity3D

Unity3D ShaderLab 使用BlinnPhong高光类型

2015-01-18 20:07 316 查看
Unity3D shaderLab 使用BlinnPhong高光类型

上一篇我们实现了自定义高光类型,这一篇,我们说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 --------------------------------------------------------------------------------
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: