基础光照
兰伯特光照模型
逐顶点光照
Shader "Unlit/Lambert"
{
Properties
{
_Color("Color", color) = (1,1,1,1)
}
SubShader
{
Tags { "LightMode"="ForwardBase" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
#include "Lighting.cginc"
fixed4 _Color;
struct appdata
{
float4 vertex : POSITION;
float3 normal : NORMAL;
};
struct v2f
{
float4 pos : SV_POSITION;
fixed3 color : COLOR;
};
v2f vert (appdata v)
{
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
//环境光
fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;
//计算世界法线方向
fixed3 worldNormal = UnityObjectToWorldNormal(v.normal);
//计算世界灯光方向
fixed3 worldLight = normalize(UnityWorldSpaceLightDir(v.vertex));
//计算兰伯特光照
fixed3 diffuse = _LightColor0.rgb * _Color.rgb * saturate(dot(worldLight, worldNormal));
o.color = ambient + diffuse;
return o;
}
fixed4 frag (v2f i) : SV_Target
{
return fixed4(i.color, 1);
}
ENDCG
}
}
Fallback "Diffuse"
}
逐像素光照
Shader "Unlit/Lambert"
{
Properties
{
_Color("Color", color) = (1,1,1,1)
}
SubShader
{
Tags { "LightMode"="ForwardBase" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
#include "Lighting.cginc"
fixed4 _Color;
struct appdata
{
float4 vertex : POSITION;
float3 normal : NORMAL;
};
struct v2f
{
float4 pos : SV_POSITION;
fixed3 worldNormal : TEXCOORD0;
fixed3 worldLight : TEXCOORD1;
};
v2f vert (appdata v)
{
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
//计算世界法线方向
o.worldNormal = UnityObjectToWorldNormal(v.normal);
//计算世界灯光方向
o.worldLight =UnityWorldSpaceLightDir(v.vertex);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
//环境光
fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;
//归一化世界法线方向
fixed3 worldNormalDir = normalize(i.worldNormal);
//归一化世界灯光方向
fixed3 worldLightDir = normalize(i.worldLight);
//计算兰伯特光照
fixed3 diffuse = _LightColor0.rgb * _Color.rgb * saturate(dot(worldLightDir, worldNormalDir));
fixed3 color = ambient + diffuse;
return fixed4(color, 1);
}
ENDCG
}
}
Fallback "Diffuse"
}
半兰伯光照模型
Shader "Unlit/HalfLambert"
{
Properties
{
_Color("Color", color) = (1,1,1,1)
}
SubShader
{
Tags { "LightMode"="ForwardBase" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
#include "Lighting.cginc"
fixed4 _Color;
struct appdata
{
float4 vertex : POSITION;
float3 normal : NORMAL;
};
struct v2f
{
float4 pos : SV_POSITION;
fixed3 worldNormal : TEXCOORD0;
fixed3 worldLight : TEXCOORD1;
};
v2f vert (appdata v)
{
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
//计算世界法线方向
o.worldNormal = UnityObjectToWorldNormal(v.normal);
//计算世界灯光方向
o.worldLight = UnityWorldSpaceLightDir(v.vertex);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
//环境光
fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;
//归一化世界法线方向
fixed3 worldNormalDir = normalize(i.worldNormal);
//归一化世界灯光方向
fixed3 worldLightDir = normalize(i.worldLight);
//计算半兰伯特光照(把世界法线和世界灯光点乘后的标量乘0.5在加0.5,意为把点乘的结果从[-1,1]映射到[0,1]的范围内)
fixed halfLambert = dot(worldLightDir, worldNormalDir) * 0.5 +0.5;
fixed3 diffuse = _LightColor0.rgb * _Color.rgb * halfLambert;
fixed3 color = ambient + diffuse;
return fixed4(color, 1);
}
ENDCG
}
}
Fallback "Diffuse"
}
逐像素高光
Shader "Unlit/Speculart"
{
Properties
{
_Diffuse("Diffuse", color) = (1,1,1,1)
_Specular("Specular", color) = (1,1,1,1)
_Gloss("Gloss", Range(8, 256)) = 20
}
SubShader
{
Tags { "LightMode"="ForwardBase" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
#include "Lighting.cginc"
fixed4 _Diffuse;
fixed4 _Specular;
float _Gloss;
struct appdata
{
float4 vertex : POSITION;
float3 normal : NORMAL;
};
struct v2f
{
float4 pos : SV_POSITION;
fixed3 worldNormal : TEXCOORD0;
fixed3 worldPos : TEXCOORD1;
};
v2f vert (appdata v)
{
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
//计算世界法线方向
o.worldNormal = UnityObjectToWorldNormal(v.normal);
//计算顶点世界位置
o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
return o;
}
fixed4 frag (v2f i) : SV_Target
{
//环境光
fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;
//归一化世界法线方向
fixed3 worldNormalDir = normalize(i.worldNormal);
//计算世界灯光方向
fixed3 worldLightDir = normalize(UnityWorldSpaceLightDir(i.worldPos));
//计算半兰伯特光照(把世界法线和世界灯光点乘后的标量乘0.5在加0.5,意为把点乘的结果从[-1,1]映射到[0,1]的范围内)
fixed halfLambert = dot(worldLightDir, worldNormalDir) * 0.5 +0.5;
fixed3 diffuse = _LightColor0.rgb * _Diffuse.rgb * halfLambert;
//计算反射光,用到reflect函数
fixed3 reflectDir = normalize(reflect(-worldLightDir, worldNormalDir));
//计算世界空间下视角方向(世界空间下摄像机位置减去世界空间下顶点位置)
fixed3 viewDir = normalize(_WorldSpaceCameraPos.xyz - i.worldPos.xyz);
//计算高光
fixed3 specular = _LightColor0.rgb * _Specular.rgb * pow(saturate(dot(viewDir, reflectDir)), _Gloss);
return fixed4(ambient + diffuse + specular, 1);
}
ENDCG
}
}
Fallback "Diffuse"
}
Blinn-Phong光照模型
Shader "Unlit/Blinn-Phong"
{
Properties
{
_Diffuse("Diffuse", color) = (1,1,1,1)
_Specular("Specular", color) = (1,1,1,1)
_Gloss("Gloss", Range(8, 256)) = 20
}
SubShader
{
Tags { "LightMode"="ForwardBase" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
#include "Lighting.cginc"
fixed4 _Diffuse;
fixed4 _Specular;
float _Gloss;
struct appdata
{
float4 vertex : POSITION;
float3 normal : NORMAL;
};
struct v2f
{
float4 pos : SV_POSITION;
fixed3 worldNormal : TEXCOORD0;
fixed3 worldPos : TEXCOORD1;
};
v2f vert (appdata v)
{
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
//计算世界法线方向
o.worldNormal = UnityObjectToWorldNormal(v.normal);
//计算顶点世界位置
o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
return o;
}
fixed4 frag (v2f i) : SV_Target
{
//环境光
fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;
//归一化世界法线方向
fixed3 worldNormalDir = normalize(i.worldNormal);
//计算世界灯光方向
fixed3 worldLightDir = normalize(UnityWorldSpaceLightDir(i.worldPos));
//计算半兰伯特光照(把世界法线和世界灯光点乘后的标量乘0.5在加0.5,意为把点乘的结果从[-1,1]映射到[0,1]的范围内)
fixed halfLambert = dot(worldLightDir, worldNormalDir) * 0.5 +0.5;
fixed3 diffuse = _LightColor0.rgb * _Diffuse.rgb * halfLambert;
//计算世界空间下视角方向
fixed3 viewDir = normalize(UnityWorldSpaceViewDir(i.worldPos.xyz));
//计算视角方向和灯光方向的半角向量
fixed3 halfDir = normalize(worldLightDir + viewDir);
//计算高光
fixed3 specular = _LightColor0.rgb * _Specular.rgb * pow(saturate(dot(worldNormalDir, halfDir)), _Gloss);
return fixed4(ambient + diffuse + specular, 1);
}
ENDCG
}
}
Fallback "Diffuse"
}