簡介
學完了蘭伯特光照模型,再來學習一個更加高級一點的光照模型-Phong光照模型。光除了漫反射,還有鏡面反射。一些金屬類型的材質,往往表現出一種高光效果,用蘭伯特模型是模擬不出來的,所以就有了Phong模型。Phong模型主要有三部分構成,第一部分是上一篇中介紹了的Diffuse,也就是漫反射,第二部分是環境光,在非全局光照的情況下,我們一般是通過一個環境光來模擬物體的間接照明,這個值在shader中可以通過一個宏來直接獲取,而第三部分Specular,也就是高光部分的計算,是一種模擬鏡面反射的效果,也是本篇文章重點介紹的內容。
在現實世界中,粗糙的物體一般會是漫反射,而光滑的物體呈現得較多的就是鏡面反射,最明顯的現象就是光線照射的反射方向有一個亮斑。再來複習一下鏡面反射的概念:當平行入射的光線射到這個反射面時,仍會平行地向一個方向反射出來,這種反射就屬於鏡面反射,其反射波的方向與反射平面的法線夾角(反射角),與入射波方向與該反射平面法線的夾角(入射角)相等,且入射波、反射波,及平面法線同處於一個平面內。反射光的亮度不僅與光線的入射角有關,還與觀察者視線和物體表面之間的角度有關。鏡面反射通常會造成物體表面上的“閃爍”和“高光”現象,鏡面反射的強度也與物體的材質有關,無光澤的木材很少會有鏡面反射發生,而高光澤的金屬則會有大量鏡面反射。
Phong光照模型
Phong光照模型在Unity中的實現
Shader "ApcShader/SpecularPerPixel"
{
//屬性
Properties
{
_Diffuse("Diffuse", Color) = (1,1,1,1)
_Specular("Specular", Color) = (1,1,1,1)
_Gloss("Gloss", Range(1.0, 255)) = 20
}
//子着色器
SubShader
{
Pass
{
//定義Tags
Tags{ "LightingMode" = "ForwardBase" }
CGPROGRAM
//引入頭文件
#include "Lighting.cginc"
//定義函數
#pragma vertex vert
#pragma fragment frag
fixed4 _Diffuse;
fixed4 _Specular;
float _Gloss;
//定義結構體:應用階段到vertex shader階段的數據
struct a2v
{
float4 vertex : POSITION;
float3 normal : NORMAL;
};
//定義結構體:vertex shader階段輸出的內容
struct v2f
{
float4 pos : SV_POSITION;
float3 worldNormal : NORMAL;
float3 worldPos : TEXCOORD1;
};
//頂點shader
v2f vert(a2v v)
{
v2f o;
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
//法線轉化到世界空間
o.worldNormal = normalize(mul(v.normal, (float3x3)_World2Object));
//頂點位置轉化到世界空間
o.worldPos = mul(_Object2World, v.vertex).xyz;
return o;
}
//片元shader
fixed4 frag(v2f i) : SV_Target
{
//環境光
fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz * _Diffuse;
//歸一化光方向
fixed3 worldLight = normalize(_WorldSpaceLightPos0.xyz);
//再次歸一化worldNorml
fixed3 worldNormal = normalize(i.worldNormal);
//diffuse
fixed3 diffuse = _LightColor0.rgb * _Diffuse.rgb * saturate(dot(worldNormal, worldLight));
//計算反射方向R,worldLight表示光源方向(指向光源),入射光線方向爲-worldLight,通過reflect函數(入射方向,法線方向)獲得反射方向
fixed3 reflectDir = normalize(reflect(-worldLight, worldNormal));
//計算該像素對應位置(頂點計算過後傳給像素經過插值後)的觀察向量V,相機座標-像素位置
fixed3 viewDir = normalize(_WorldSpaceCameraPos.xyz - i.worldPos.xyz);
//計算高光值,高光值與反射光方向與觀察方向的夾角有關,夾角爲dot(R,V),最後根據反射係數計算的反射值爲pow(dot(R,V),Gloss)
fixed3 specular = _LightColor0.rgb * _Specular.rgb * pow(max(0.0,dot(reflectDir, viewDir)), _Gloss);
//馮氏模型:Diffuse + Ambient + Specular
fixed3 color = diffuse + ambient + specular;
return fixed4(color, 1.0);
}
ENDCG
}
}
//前面的Shader失效的話,使用默認的Diffuse
FallBack "Diffuse"
}
Blinn-Phong光照模型
I(spcular) = I * k * pow(max(0,dot(N,H)), gloss) ,其中I爲入射光顏色向量,k爲鏡面反射係數,gloss爲光滑程度。
Blinn-Phong光照在Unity中的實現
Shader "ApcShader/BlinnPhongPerPixel"
{
//屬性
Properties
{
_Diffuse("Diffuse", Color) = (1,1,1,1)
_Specular("Specular", Color) = (1,1,1,1)
_Gloss("Gloss", Range(1.0, 256)) = 20
}
//子着色器
SubShader
{
Pass
{
//定義Tags
Tags{ "LightingMode" = "ForwardBase" }
CGPROGRAM
//引入頭文件
#include "Lighting.cginc"
//定義函數
#pragma vertex vert
#pragma fragment frag
//定義Properties中的變量
fixed4 _Diffuse;
fixed4 _Specular;
float _Gloss;
//定義結構體:應用階段到vertex shader階段的數據
struct a2v
{
float4 vertex : POSITION;
float3 normal : NORMAL;
};
//定義結構體:vertex shader階段輸出的內容
struct v2f
{
float4 pos : SV_POSITION;
float3 worldNormal : NORMAL;
float3 worldPos : TEXCOORD1;
};
//頂點shader
v2f vert(a2v v)
{
v2f o;
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
//法線轉化到世界空間
o.worldNormal = normalize(mul(v.normal, (float3x3)_World2Object));
//頂點位置轉化到世界空間
o.worldPos = mul(_Object2World, v.vertex).xyz;
return o;
}
//片元shader
fixed4 frag(v2f i) : SV_Target
{
//環境光
fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz * _Diffuse;
//世界空間下光線方向
fixed3 worldLight = normalize(_WorldSpaceLightPos0.xyz);
//需要再次normalize
fixed3 worldNormal = normalize(i.worldNormal);
//計算Diffuse
fixed3 diffuse = _LightColor0.rgb * _Diffuse.rgb * saturate(dot(worldNormal, worldLight));
//計算視線方向(相機位置-像素對應位置)
fixed3 viewDir = normalize(_WorldSpaceCameraPos.xyz - i.worldPos.xyz);
//計算半角向量(光線方向 + 視線方向,結果歸一化)
fixed3 halfDir = normalize(worldLight + viewDir);
//計算Specular(Blinn-Phong計算的是)
fixed3 specular = _LightColor0.rgb * _Specular.rgb * pow(saturate(dot(halfDir, worldNormal)), _Gloss);
//結果爲diffuse + ambient + specular
fixed3 color = diffuse + ambient + specular;
return fixed4(color, 1.0);
}
ENDCG
}
}
//前面的Shader失效的話,使用默認的Diffuse
FallBack "Diffuse"
}
我們再放一個球,使用Blinn-Phong光照,與Phong光照模型進行對比:
帶紋理的Phong光照Shader
Shader "ApcShader/BlinnPhongWithTex"
{
//屬性
Properties
{
_Diffuse("Diffuse", Color) = (1,1,1,1)
_Specular("Specular", Color) = (1,1,1,1)
_SpecularScale("SpecularScale", Range(0.0, 5.0)) = 1.0
_Gloss("Gloss", Range(0.0, 1)) = 20
_MainTex("RGBSpecular", 2D) = "white"{}
}
//子着色器
SubShader
{
Pass
{
//定義Tags
Tags{ "LightingMode" = "ForwardBase" }
CGPROGRAM
//引入頭文件
#include "Lighting.cginc"
//定義函數
#pragma vertex vert
#pragma fragment frag
//定義Properties中的變量
fixed4 _Diffuse;
fixed4 _Specular;
float _Gloss;
float _SpecularScale;
sampler2D _MainTex;
float4 _MainTex_ST;
//定義結構體:應用階段到vertex shader階段的數據
struct a2v
{
float4 vertex : POSITION;
float3 normal : NORMAL;
float4 texcoord : TEXCOORD0;
};
//定義結構體:vertex shader階段輸出的內容
struct v2f
{
float4 pos : SV_POSITION;
float3 worldNormal : NORMAL;
float3 worldPos : TEXCOORD0;
float2 uv : TEXCOORD1;
};
//頂點shader
v2f vert(a2v v)
{
v2f o;
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
//法線轉化到世界空間
o.worldNormal = normalize(mul(v.normal, (float3x3)_World2Object));
//頂點位置轉化到世界空間
o.worldPos = mul(_Object2World, v.vertex).xyz;
//轉化uv
o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
return o;
}
//片元shader
fixed4 frag(v2f i) : SV_Target
{
//環境光
fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;
//世界空間下光線方向
fixed3 worldLight = normalize(_WorldSpaceLightPos0.xyz);
//需要再次normalize
fixed3 worldNormal = normalize(i.worldNormal);
//計算Diffuse
fixed3 diffuse = _LightColor0.rgb * (dot(worldNormal, worldLight) * 0.5 + 0.5);
//計算視線方向(相機位置-像素對應位置)
fixed3 viewDir = normalize(_WorldSpaceCameraPos.xyz - i.worldPos.xyz);
//計算半角向量(光線方向 + 視線方向,結果歸一化)
fixed3 halfDir = normalize(worldLight + viewDir);
//計算Specular(Blinn-Phong計算的是)
fixed3 specular = _LightColor0.rgb * _Specular.rgb * pow(saturate(dot(halfDir, worldNormal)), _Gloss);
//紋理採樣
fixed4 tex = tex2D(_MainTex, i.uv);
//紋理中rgb爲正常顏色,a爲一個高光的mask圖,非高光部分a值爲0,高光部分根據a的值控制高光強弱
fixed3 color = (diffuse + ambient + specular * tex.a * _SpecularScale) * tex.rgb;
return fixed4(color, 1.0);
}
ENDCG
}
}
//前面的Shader失效的話,使用默認的Diffuse
FallBack "Diffuse"
}
看一下最終效果,左側爲使用了高光的效果,右側爲普通diffuse的效果,可以看出,使用了高光貼圖,我們只有上面的刀才表現出了高光,其他部分仍然是正常的。Shader的優化
//blinn-phong shader
//puppet_master
//2016.12.11
Shader "ApcShader/BlinnPhongPerPixel"
{
//屬性
Properties
{
_Diffuse("Diffuse", Color) = (1,1,1,1)
_Specular("Specular", Color) = (1,1,1,1)
_Gloss("Gloss", Range(1.0, 256)) = 20
}
//子着色器
SubShader
{
Pass
{
//定義Tags
Tags{ "LightingMode" = "ForwardBase" }
CGPROGRAM
//引入頭文件
#include "Lighting.cginc"
//定義函數
#pragma vertex vert
#pragma fragment frag
//定義Properties中的變量
fixed4 _Diffuse;
fixed4 _Specular;
float _Gloss;
//定義結構體:應用階段到vertex shader階段的數據
struct a2v
{
float4 vertex : POSITION;
float3 normal : NORMAL;
};
//定義結構體:vertex shader階段輸出的內容
struct v2f
{
float4 pos : SV_POSITION;
float3 worldNormal : NORMAL;
float3 viewDir : TEXCOORD1;
};
//頂點shader
v2f vert(a2v v)
{
v2f o;
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
//法線轉化到世界空間
o.worldNormal = normalize(mul(v.normal, (float3x3)_World2Object));
//頂點位置轉化到世界空間
float3 worldPos = mul(_Object2World, v.vertex).xyz;
//計算視線方向(相機位置 - 像素對應位置)
o.viewDir = _WorldSpaceCameraPos - worldPos;
return o;
}
//片元shader
fixed4 frag(v2f i) : SV_Target
{
//環境光
fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz * _Diffuse;
//世界空間下光線方向
fixed3 worldLight = normalize(_WorldSpaceLightPos0.xyz);
//需要再次normalize
fixed3 worldNormal = normalize(i.worldNormal);
//計算Diffuse
fixed3 diffuse = _LightColor0.rgb * _Diffuse.rgb * saturate(dot(worldNormal, worldLight));
//normalize
fixed3 viewDir = normalize(i.viewDir);
//計算半角向量(光線方向 + 視線方向,結果歸一化)
fixed3 halfDir = normalize(worldLight + viewDir);
//計算Specular(Blinn-Phong計算的是)
fixed3 specular = _LightColor0.rgb * _Specular.rgb * pow(saturate(dot(halfDir, worldNormal)), _Gloss);
//結果爲diffuse + ambient + specular
fixed3 color = diffuse + ambient + specular;
return fixed4(color, 1.0);
}
ENDCG
}
}
//前面的Shader失效的話,使用默認的Diffuse
FallBack "Diffuse"
}
在優化前後,沒有特別明顯的變化: