![原理](https://i-blog.csdnimg.cn/blog_migrate/27ba7b2dcd8db9da75d601e0e88a9988.png#pic_center)
Phong光照模型
Shader "QStudyShader/Phong"
{
Properties
{
_BaseColor("Base Color",Color) = (1.0,1.0,1.0,1.0)
_Gloss("Gloss",Range(8,100)) = 20.0
}
SubShader
{
Pass{
Tags { "LightModel" = "ForwardBase"}
CGPROGRAM
#include "Lighting.cginc"
#pragma vertex Vertex
#pragma fragment Fragment
struct a2v {
float4 vertex : POSITION;
float3 normal : NORMAL;
};
struct v2f {
float4 pos : SV_POSITION;
float3 worldNormal : TEXCOORD0;
float3 worldPos : TEXCOORD1;
};
fixed4 _BaseColor;
float _Gloss;
v2f Vertex(a2v v) {
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
o.worldNormal = UnityObjectToWorldNormal(v.normal);
o.worldPos = mul(unity_ObjectToWorld, v.vertex);
return o;
}
fixed4 Fragment(v2f i) : SV_TARGET {
fixed3 worldNormal = normalize(i.worldNormal);
fixed3 worldLightDir = normalize(_WorldSpaceLightPos0.xyz);
fixed3 reflectDir = reflect(-worldLightDir, worldNormal);
fixed3 viewDir = normalize(UnityWorldSpaceViewDir(i.worldPos));
fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz * _BaseColor.xyz;
fixed3 diffuse = _LightColor0.xyz * _BaseColor.xyz * saturate(dot(worldNormal,worldLightDir));
fixed3 specular = _LightColor0.xyz * pow(saturate(dot(reflectDir,worldNormal)),_Gloss);
return fixed4(specular + ambient + diffuse, 1.0);
}
ENDCG
}
}
}
BelinnPhong光照模型
Shader "QStudyShader/BelinnPhong"
{
Properties
{
_BaseColor("Base Color",Color) = (1.0,1.0,1.0,1.0)
_Gloss("Gloss",Range(8,100)) = 20.0
}
SubShader
{
Pass{
Tags{ "LightModel" = "FrowardBase"}
CGPROGRAM
#include "Lighting.cginc"
#pragma vertex Vertex
#pragma fragment Fragment
struct a2v {
float4 vertex : POSITION;
float3 normal : NORMAL;
};
struct v2f {
float4 pos : SV_POSITION;
float3 worldNormal : TEXCOORD0;
float3 worldPos : TEXCOORD1;
};
fixed4 _BaseColor;
float _Gloss;
v2f Vertex(a2v v) {
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
o.worldNormal = UnityObjectToWorldNormal(v.normal);
o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
return o;
}
fixed4 Fragment(v2f i) : SV_TARGET{
fixed3 worldNormal = normalize(i.worldNormal);
fixed3 worldLightDir = normalize(_WorldSpaceLightPos0.xyz);
fixed3 viewDir = normalize(UnityWorldSpaceViewDir(i.worldPos));
fixed3 halfDir = normalize(viewDir + worldLightDir);
fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz * _BaseColor.xyz;
fixed3 diffuse = _LightColor0.xyz * _BaseColor.xyz * saturate(dot(worldLightDir,worldNormal));
fixed3 specular = _LightColor0.xyz * pow(saturate(dot(halfDir, worldNormal)), _Gloss);
return fixed4(ambient + diffuse + specular,1.0);
}
ENDCG
}
}
}