1）Phong公式介绍

2）高光反射的逐顶点光照模型

3）高光反射的逐像素光照模型

4）高光反射的Blinn-Phong光照模型

## 一.Phong公式介绍

Phong公式：高光反射颜色=光的颜色*高光反射系数*max（0，反射光在视角方向的投影）^反射度

## 二.高光反射的逐顶点光照

Properties
{
_Specular("反射系数",Color) = (1.0,1.0,1.0,1.0)
_Gloss("高光区域",Range(8, 256)) = 20
}

Pass
{
Tags{ "LightMode" = "ForwardBase"}
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include"Lighting.cginc"
float4 _Diffuse;
float4 _Specular;
float _Gloss;
struct a2v
{
float4 pos : POSITION;
float3 normal :NORMAL;
};
struct v2f
{
float4 pos : SV_POSITION;
float3 color :COLOR;
};

v2f vert(a2v v)
{
v2f o;
o.pos = UnityObjectToClipPos(v.pos);
float3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;
float3 normal2 = (mul(v.normal, (float3x3)unity_WorldToObject));

float3 lightDir = (_WorldSpaceLightPos0.xyz);
float3 reflectDir = -(reflect(lightDir, normal2));
float3 viewDir = normalize(_WorldSpaceCameraPos.xyz - o.pos);
float3 specular = _LightColor0.rgb*_Specular.rgb*pow(saturate(dot(reflectDir, viewDir)), _Gloss);

o.color = ambient + specular;
return o;
}

lightDir 获取光照方向、reflectDir 通过法线与光照方向获取反射方向、_WorldSpaceCameraPos代表世界坐标下摄像机位向量，再减去世界坐标下的顶点向量，得到的就是视角方向viewDir

Shader"sony/Shader150"
{
Properties
{
_Specular("反射系数",Color) = (1.0,1.0,1.0,1.0)
_Gloss("高光区域",Range(8, 256)) = 20
}
{
Pass
{
Tags{ "LightMode" = "ForwardBase"}
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include"Lighting.cginc"
float4 _Diffuse;
float4 _Specular;
float _Gloss;
struct a2v
{
float4 pos : POSITION;
float3 normal :NORMAL;
};
struct v2f
{
float4 pos : SV_POSITION;
float3 color :COLOR;
};
v2f vert(a2v v)
{
v2f o;
o.pos = UnityObjectToClipPos(v.pos);
float3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;
float3 normal2 = (mul(v.normal, (float3x3)unity_WorldToObject));

float3 lightDir = (_WorldSpaceLightPos0.xyz);
float3 reflectDir = -(reflect(lightDir, normal2));
float3 viewDir = normalize(_WorldSpaceCameraPos.xyz - o.pos);//必须加normalize
float3 specular = _LightColor0.rgb*_Specular.rgb*pow(saturate(dot(viewDir,reflectDir)), _Gloss);

o.color = ambient + specular;
return o;
}
fixed4 frag(v2f i) : SV_Target
{
return fixed4(i.color,1.0);
}
ENDCG
}
}
FallBack "Diffuse"
}

## 三.逐像素光照

struct v2f
{
float4 pos : SV_POSITION;
float3 texcoord :TEXCOORD0;
float3 texcoord1 :TEXCOORD1;
};

v2f vert(a2v v)
{
v2f o;
o.pos = UnityObjectToClipPos(v.pos);
o.texcoord = (mul(v.normal, (float3x3)unity_WorldToObject));
o.texcoord1 = mul(unity_ObjectToWorld, v.normal).xyz;
return o;
}
fixed4 frag(v2f i) : SV_Target
{
float3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;

float3 lightDir = (_WorldSpaceLightPos0.xyz);
float3 reflectDir = -(reflect(lightDir, i.texcoord));
float3 viewDir = normalize(_WorldSpaceCameraPos.xyz - i.texcoord1);//必须加normalize
float3 specular = _LightColor0.rgb*_Specular.rgb*pow(saturate(dot(viewDir,reflectDir)), _Gloss);
return fixed4(ambient + specular,1.0);
}

Shader"sony/Shader152"
{
Properties
{
_Specular("反射系数",Color) = (1.0,1.0,1.0,1.0)
_Gloss("高光区域",Range(8, 256)) = 20
}
{
Pass
{
Tags{ "LightMode" = "ForwardBase" }
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include"Lighting.cginc"
float4 _Diffuse;
float4 _Specular;
float _Gloss;
struct a2v
{
float4 pos : POSITION;
float3 normal :NORMAL;
};
struct v2f
{
float4 pos : SV_POSITION;
float3 texcoord :NORMAL;
float3 texcoord1 :TEXCOORD1;
};
v2f vert(a2v v)
{
v2f o;
o.pos = UnityObjectToClipPos(v.pos);
o.texcoord = (mul(v.normal, (float3x3)unity_WorldToObject));
o.texcoord1 = mul(unity_ObjectToWorld, v.normal).xyz;
return o;
}
fixed4 frag(v2f i) : SV_Target
{
float3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;

float3 lightDir = (_WorldSpaceLightPos0.xyz);
float3 reflectDir = -(reflect(lightDir, i.texcoord));
//必须加normalize
float3 viewDir = normalize(_WorldSpaceCameraPos.xyz - i.texcoord1);
float3 specular = _LightColor0.rgb*_Specular.rgb*pow(saturate(dot(viewDir,reflectDir)), _Gloss);
return fixed4(ambient + specular,1.0);
}
ENDCG
}
}
FallBack "Diffuse"
}

## 四.Blinn-Phong光照模型

float3 halfDir = normalize(lightDir + viewDir);
float3 specular=_LightColor0.rgb*_Specular.rgb*pow(saturate(dot(halfDir,reflectDir)), _Gloss);

Shader"sony/Shader154"
{
Properties
{
_Specular("反射系数",Color) = (1.0,1.0,1.0,1.0)
_Gloss("高光区域",Range(8, 256)) = 20
}
{
Pass
{
Tags{ "LightMode" = "ForwardBase" }
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include"Lighting.cginc"
float4 _Diffuse;
float4 _Specular;
float _Gloss;
struct a2v
{
float4 pos : POSITION;
float3 normal :NORMAL;
};
struct v2f
{
float4 pos : SV_POSITION;
float3 texcoord :NORMAL;
float3 texcoord1 :TEXCOORD1;
};
v2f vert(a2v v)
{
v2f o;
o.pos = UnityObjectToClipPos(v.pos);
o.texcoord = (mul(v.normal, (float3x3)unity_WorldToObject));
o.texcoord1 = mul(unity_ObjectToWorld, v.normal).xyz;
return o;
}
fixed4 frag(v2f i) : SV_Target
{
float3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;

float3 lightDir = (_WorldSpaceLightPos0.xyz);
float3 reflectDir = -(reflect(lightDir, i.texcoord));
float3 viewDir = normalize(_WorldSpaceCameraPos.xyz - i.texcoord1);//必须加normalize
float3 specular = _LightColor0.rgb*_Specular.rgb*pow(saturate(dot(viewDir,reflectDir)), _Gloss);
return fixed4(ambient + specular,1.0);
}
ENDCG
}
}
FallBack "Diffuse"
}

Excelsior！

10-12 2万+

06-04 2456
02-22 1676
07-11 2178