Back BRDF是一种 模拟 金属表面 的 各向异性 光泽的经验模型,具体公式参照 GPU 编程与CG 语言之阳春白雪下里巴人 中120页的公式(10-14)
1 Shader "Custom/Bank-BRDF" { 2 Properties { 3 _AmbiColor ("Main Color", Color) = (1, 1, 1, 1) 4 _Ak ("Ambient Coef", float) = 1 5 _DiffColor ("Diff Color", Color) = (1, 1, 1, 1) 6 _Dk ("Diff Coef", float) = 1 7 _SpecColor ("Spec Color", Color) = (1, 1, 1, 1) 8 _Sk ("Sk", float) = 1 9 _Sp ("Sp", Range(0, 5)) = 1 10 } 11 12 SubShader { 13 Pass { 14 CGPROGRAM 15 #include "UnityCG.cginc" 16 #pragma vertex vert 17 #pragma fragment frag 18 19 float4 _AmbiColor; 20 float _Ak; 21 float4 _DiffColor; 22 float _Dk; 23 float4 _SpecColor; 24 float _Sk; 25 float _Sp; 26 27 struct v2f { 28 float4 pos : POSITION; 29 float3 normal : TEXCOORD0; 30 float3 light : TEXCOORD1; 31 float3 view : TEXCOORD2; 32 float3 targent : TEXCOORD3; 33 }; 34 35 v2f vert(appdata_base v) { 36 v2f o; 37 o.pos = mul(UNITY_MATRIX_MVP, v.vertex); 38 o.normal = v.normal; 39 o.light = ObjSpaceLightDir(v.vertex); 40 o.view = ObjSpaceViewDir(v.vertex); 41 o.targent = cross(v.normal, o.view); 42 43 return o; 44 } 45 46 float4 frag(v2f i) : COLOR { 47 float3 l = normalize(i.light); 48 float3 t = normalize(i.targent); 49 float3 v = normalize(i.view); 50 float3 n = normalize(i.normal); 51 52 float lt = dot(l, t); 53 float vt = dot(v, t); 54 float nl = dot(n, l); 55 float nv = dot(n, v); 56 57 float4 ambi = _AmbiColor * _Ak; 58 float4 diff = _Dk * _DiffColor * saturate(nl); 59 60 if (nl <= 0 || nv <= 0) 61 return ambi + diff; 62 63 float p = sqrt(1 - lt * lt) * sqrt(1 - vt * vt) - lt * vt; 64 float f = _Sk * pow(p, _Sp); 65 float spec = f * _SpecColor * saturate(dot(l, n)); 66 67 return ambi + diff + spec; 68 } 69 ENDCG 70 } 71 } 72 FallBack "Diffuse" 73 }
效果如下:
时间: 2024-10-11 23:59:22