## 一、Phong 高光反射原理

### 1、计算公式：

:最终的高光颜色
：光源颜色
：材质的高光反射颜色，它用于控制该材质对于高光反射的强度和颜色。
：材质的光泽度，它用于控制高光区域的 “亮点”有多宽，其值越大。亮点越小。反之，则亮点越小。
：视觉方向
：反射方向，反射方向r可以由表面法线方向n和光源方向l计算获得：

### 1、表面着色器实现Phong高光模型

Shader "Custom/Phong" {
Properties {
_Color ("Diffuse Color", Color) = (1,1,1,1)
_MainTex ("Base(RGB)", 2D) = "white" {}
_SpecularColor ("Specular Color", Color) = (1,1,1,1)
_SpecularPower ("Specular Power", Range(0,1)) = 1
}
Tags { "RenderType"="Opaque" }
LOD 200

CGPROGRAM
#pragma surface surf Phong
#pragma target 3.0

sampler2D _MainTex;
fixed4 _Color;
float4 _SpecularColor;
float _SpecularPower;
struct Input {
float2 uv_MainTex;
};

inline float4 LightingPhong(SurfaceOutput s,fixed3 lightDir,half3 viewDir,fixed atten ){
float diff=dot(s.Normal,lightDir);
float3 reflectionVector=normalize(2.0*s.Normal*diff-lightDir);
float spec=pow(max(0,dot(reflectionVector,viewDir)),_SpecularPower);
float3 finalSpec=_SpecularColor*spec;
fixed4 c;
c.rgb=(s.Albedo*_LightColor0.rgb*diff)+(_LightColor0.rgb*finalSpec);
c.a=1.0;
return c;
}
void surf (Input IN, inout SurfaceOutput o) {
fixed4 c = tex2D (_MainTex, IN.uv_MainTex)*_Color;
o.Albedo = c.rgb;
o.Alpha = c.a;
}
ENDCG
}
FallBack "Specular"
}

### 2、逐顶点光照实现Phong高光模型

Shader "Custom/Unlit/VertexPhong"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_Diffuse ("Diffuse",Color)=(1,1,1,1)
_Specular("Specular",Color)=(1,1,1,1)
_Gloss("Gloss",Range(8.0,256))=20
}
{
Tags { "RenderType"="Opaque" }
LOD 100

Pass
{
Tags{"LightMode"="ForwardBase"}
CGPROGRAM
#pragma vertex vert
#pragma fragment frag

#include "UnityCG.cginc"
#include "Lighting.cginc"

struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
float3 normal:NORMAL;
};

struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
fixed3 diffusecolor:COLOR;
fixed3 ambientcolor:COLOR2;
fixed3 specularcolor:COLOR1;

};

sampler2D _MainTex;
fixed4 _Diffuse;
fixed4 _Specular;
float _Gloss;
float4 _MainTex_ST;

v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);

o.uv = TRANSFORM_TEX(v.uv, _MainTex);

fixed3 ambient=UNITY_LIGHTMODEL_AMBIENT.xyz;

fixed3 worldNormal=UnityObjectToWorldNormal(v.normal);

fixed3 worldLightDir=normalize(_WorldSpaceLightPos0.xyz);

fixed3 diffuse=_LightColor0.rgb * _Diffuse.rgb*saturate(dot(worldNormal,worldLightDir));

fixed3 reflectDir=normalize(reflect(-worldLightDir,worldNormal));

fixed3 viewDir=normalize(_WorldSpaceCameraPos.xyz-mul(unity_ObjectToWorld,v.vertex).xyz);

fixed specular=_LightColor0.rgb*_Specular.rgb*pow(saturate(dot(reflectDir,viewDir)),_Gloss);

o.diffusecolor=diffuse;
o.specularcolor=specular;
o.ambientcolor=ambient;
return o;
}

fixed4 frag (v2f i) : SV_Target
{
fixed4 col = tex2D(_MainTex, i.uv);
fixed3 c=col.rgb*(i.ambientcolor+i.diffusecolor)+i.specularcolor;
return fixed4(c,1.0);
}
ENDCG
}
}
}

### 3、逐像素光照实现Phong高光模型

Shader "Custom/Unlit/FragmentPhong"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_Diffuse ("Diffuse",Color)=(1,1,1,1)
_Specular("Specular",Color)=(1,1,1,1)
_Gloss("Gloss",Range(8.0,256))=20
}
{
Tags { "RenderType"="Opaque" }
LOD 100

Pass
{
Tags{"LightMode"="ForwardBase"}
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_fog

#include "UnityCG.cginc"
#include "Lighting.cginc"

struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
float3 normal:NORMAL;
};

struct v2f
{
float2 uv : TEXCOORD0;
float3 worldNormal:TEXCOORD1;
float3 worldPos:TEXCOORD2;
float4 vertex : SV_POSITION;
};

sampler2D _MainTex;
fixed4 _Diffuse;
fixed4 _Specular;
float _Gloss;
float4 _MainTex_ST;

v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
o.worldNormal=mul(v.normal,(float3x3)unity_WorldToObject);
o.worldPos=mul(unity_ObjectToWorld,v.vertex).xyz;
return o;
}

fixed4 frag (v2f i) : SV_Target
{
fixed4 col = tex2D(_MainTex, i.uv);
fixed3 ambient=UNITY_LIGHTMODEL_AMBIENT*col.rgb;

fixed3 worldNormal=normalize(i.worldNormal);
fixed3 worldLightDir=normalize(_WorldSpaceLightPos0.xyz);
fixed3 diffuse=col.rgb*_LightColor0.rgb *_Diffuse.rgb*saturate(dot(worldNormal,worldLightDir));

fixed3 reflectDir=normalize(reflect(-worldLightDir,worldNormal));
fixed3 viewDir=normalize(_WorldSpaceCameraPos.xyz-i.worldPos.xyz);

fixed3 specular=_LightColor0.rgb*_Specular.rgb*pow(saturate(dot(reflectDir,viewDir)),_Gloss);
fixed3 color=ambient+diffuse+specular;

return fixed4(color,1.0);
}
ENDCG
}
}
FallBack "Diffuse"
}