先開個坑 慢慢填
漫反射
蘭伯特光照(Lambert)
fixed4 LightingLambert (SurfaceOutput s, UnityGI gi)
半蘭伯特光照(Half-Lambert)
鏡面反射
Phong高光模型
Blinn-Phong
fixed4 LightingBlinnPhong (SurfaceOutput s, half3 viewDir, UnityGI gi)
高級光照模型
Cook_Torrance光照模型
inline fixed4 LightingCookTorrance (SurfaceOutput s, fixed3 lightDir, fixed3 viewDir, fixed atten)
{
clip ( s.Alpha - _Cutoff );
viewDir = normalize ( viewDir );
lightDir = normalize ( lightDir );
float3 h = normalize ( lightDir + viewDir );
float NdotL_unsat = dot ( s.Normal, lightDir );
float NdotH_unsat = dot ( s.Normal, h );
float NdotL = saturate( NdotL_unsat );
float NdotH = saturate( NdotH_unsat );
float NdotV = saturate( dot ( s.Normal, viewDir ) );
float VdotH = saturate( dot ( viewDir, h ) );
float geo_numerator = 2.0 * NdotH;
float geo_b = ( geo_numerator * NdotV ) / VdotH;
float geo_c = ( geo_numerator * NdotL ) / VdotH;
float geo = min( 1.0f, min( geo_b, geo_c ) );
fixed roughness = tex2D( _Beckmann, float2 ( NdotH_unsat * 0.5 + 0.5, s.Specular ) ).r;
float fresnel = pow( 1.0 - VdotH, 5.0 );
fresnel *= ( 1.0 - _Fresnel );
fresnel += _Fresnel;
float3 spec = float3 ( fresnel * geo * roughness ) / ( NdotV * NdotL );
fixed4 c;
c.rgb = NdotL * ( _LightColor0.rgb * spec + s.Albedo ) * atten;
c.a = s.Alpha;
return c;
}
BRDF
雙向反射分佈函數(bidirectional reflectance distribution function)
Shader "Bank-BRDF" {
Properties {
_AmbiColor ("Main Color", Color) = (1, 1, 1, 1)
_Ak ("Ambient Coef", float) = 1
_DiffColor ("Diff Color", Color) = (1, 1, 1, 1)
_Dk ("Diff Coef", float) = 1
_SpecColor ("Spec Color", Color) = (1, 1, 1, 1)
_Sk ("Sk", float) = 1
_Sp ("Sp", Range(0, 5)) = 1
}
SubShader {
Pass {
CGPROGRAM
#include "UnityCG.cginc"
#pragma vertex vert
#pragma fragment frag
float4 _AmbiColor;
float _Ak;
float4 _DiffColor;
float _Dk;
float4 _SpecColor;
float _Sk;
float _Sp;
struct v2f {
float4 pos : POSITION;
float3 normal : TEXCOORD0;
float3 light : TEXCOORD1;
float3 view : TEXCOORD2;
float3 targent : TEXCOORD3;
};
v2f vert(appdata_base v) {
v2f o;
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
o.normal = v.normal;
o.light = ObjSpaceLightDir(v.vertex);
o.view = ObjSpaceViewDir(v.vertex);
o.targent = cross(v.normal, o.view);
return o;
}
float4 frag(v2f i) : COLOR {
float3 l = normalize(i.light);
float3 t = normalize(i.targent);
float3 v = normalize(i.view);
float3 n = normalize(i.normal);
float lt = dot(l, t);
float vt = dot(v, t);
float nl = dot(n, l);
float nv = dot(n, v);
float4 ambi = _AmbiColor * _Ak;
float4 diff = _Dk * _DiffColor * saturate(nl);
if (nl <= 0 || nv <= 0)
return ambi + diff;
float p = sqrt(1 - lt * lt) * sqrt(1 - vt * vt) - lt * vt;
float f = _Sk * pow(p, _Sp);
float spec = f * _SpecColor * saturate(dot(l, n));
return ambi + diff + spec;
}
ENDCG
}
}
FallBack "Diffuse"
}