Unity shader学习之Blinn-Phong光照模型

Blinn-Phong光照模型不用计算反射方向,计算公式如下:

h = normalize(v + l);

Cspecular = Clight * mspecular * pow(max(0, dot(n, h), gloss))

转载请注明出处:http://www.cnblogs.com/jietian331/p/7097201.html

shader如下:

 Shader "Custom/Specular Blinn-Phong"
{
Properties
{
_Diffuse ("Diffuse Color", Color) = (,,,)
_Specular ("Specular Color", Color) = (,,,)
_Gloss ("Gloss", Range(, )) =
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD Pass
{
Tags { "LightMode"="ForwardBase" } CGPROGRAM
#pragma vertex vert
#pragma fragment frag #include "UnityCG.cginc"
#include "Lighting.cginc" struct appdata
{
float4 vertex : POSITION;
float3 normal : NORMAL;
}; struct v2f
{
float4 vertex : SV_POSITION;
float3 worldPos : TEXCOORD0;
float3 worldNormal : TEXCOORD1;
}; fixed4 _Diffuse;
fixed4 _Specular;
float _Gloss; v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.worldNormal = UnityObjectToWorldNormal(v.normal);
o.worldPos = mul(unity_ObjectToWorld, v.vertex);
return o;
} fixed4 frag (v2f i) : SV_Target
{
// specular
float3 worldNormal = normalize(i.worldNormal);
float3 lightDir = UnityWorldSpaceLightDir(i.worldPos);
lightDir = normalize(lightDir);
float3 viewDir = UnityWorldSpaceViewDir(i.worldPos);
viewDir = normalize(viewDir);
float3 halfDir = normalize(lightDir + viewDir);
float d = max(, dot(halfDir, worldNormal));
float3 spec = _LightColor0.rgb * _Specular.rgb * pow(d, _Gloss); // diffuse
float3 diff = _LightColor0.rgb * _Diffuse.rgb * max(, dot(lightDir, worldNormal)); float3 c = spec + diff + UNITY_LIGHTMODEL_AMBIENT.rgb;
return fixed4(c, );
}
ENDCG
}
}
}

效果如下:

Unity shader学习之Blinn-Phong光照模型

这种光照模型的高光反射看起来更大,更亮一些。在实际渲染中,绝大多数情况会选择 Blinn-Phong 光照模型,此种光照模型更符合实验结果。

上一篇:[学习笔记]设计模式之Composite


下一篇:__block的初步用法