-
Notifications
You must be signed in to change notification settings - Fork 6
/
SH.shader
112 lines (98 loc) · 3.74 KB
/
SH.shader
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
Shader "BASICxSHADER/Lighting/SH" {
Properties {
_Albedo ("Albedo", Color) = (1, 1, 1, 1)
_Roughness ("Roughness", Range(0, 1)) = 0.5
_Metallic ("Metallic", Range(0, 1)) = 0.5
}
SubShader {
Pass {
Tags { "LightMode" = "ForwardBase" }
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#define PI 3.14159265359f
// Properties
uniform fixed4 _LightColor0;
uniform fixed4 _Albedo;
uniform half _Roughness;
uniform half _Metallic;
// Vertex Input
struct appdata {
float4 vertex : POSITION;
float3 normal : NORMAL;
};
// Vertex to Fragment
struct v2f {
float4 pos : SV_POSITION;
float3 normal : NORMAL;
float4 posWorld : TEXCOORD0;
};
//------------------------------------------------------------------------
// Vertex Shader
//------------------------------------------------------------------------
v2f vert(appdata v) {
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
o.normal = normalize(mul(v.normal, unity_WorldToObject).xyz);
o.posWorld = mul(unity_ObjectToWorld, v.vertex);
return o;
}
//------------------------------------------------------------------------
// Fragment Shader
//------------------------------------------------------------------------
fixed4 frag(v2f i) : SV_Target {
// Vector
half3 normal = normalize(i.normal);
half3 lightDir = normalize(_WorldSpaceLightPos0.xyz);
half3 viewDir = normalize(_WorldSpaceCameraPos.xyz - i.posWorld);
half3 halfDir = normalize(lightDir + viewDir);
// Dot
half NdotL = saturate(dot(normal, lightDir));
half NdotV = saturate(dot(normal, viewDir));
half NdotH = saturate(dot(normal, halfDir));
half LdotH = saturate(dot(lightDir, halfDir));
half NdotHSqr = NdotH * NdotH;
// Roughness
half roughness = _Roughness * _Roughness;
half roughnessSqr = roughness * roughness;
// Oren-Nayar
half A = 1.0 - 0.5 * (roughnessSqr / (roughnessSqr + 0.33));
half B = 0.45 * (roughnessSqr / (roughnessSqr + 0.09));
half C = saturate(dot(normalize(viewDir - normal * NdotV), normalize(lightDir - normal * NdotL)));
half angleL = acos(NdotL);
half angleV = acos(NdotV);
half alpha = max(angleL, angleV);
half beta = min(angleL, angleV);
fixed3 diffuse = _Albedo.rgb * (A + B * C * sin(alpha) * tan(beta)) * _LightColor0.rgb * NdotL;
// Cook-Torrance
half D = roughnessSqr / (PI * pow(NdotHSqr * (roughnessSqr - 1.0) + 1.0, 2.0));
half k = roughness * 0.5;
half gl = NdotL / (NdotL * (1.0 - k) + k);
half gv = NdotV / (NdotV * (1.0 - k) + k);
half G = gl * gv;
half F = _Metallic + (1.0 - _Metallic) * pow(1.0 - LdotH, 5.0);
fixed3 specular = saturate((D * G * F) / (4.0 * NdotV)) * PI * _LightColor0.rgb;
// SH
half3 sh;
half4 nSHA = half4(normal, 1.0);
sh.r = dot(unity_SHAr, nSHA);
sh.g = dot(unity_SHAg, nSHA);
sh.b = dot(unity_SHAb, nSHA);
half4 nSHB = normal.xyzz * normal.yzzx;
sh.r += dot(unity_SHBr, nSHB);
sh.g += dot(unity_SHBg, nSHB);
sh.b += dot(unity_SHBb, nSHB);
half nSHC = normal.x * normal.x - normal.y * normal.y;
sh += unity_SHC.rgb * nSHC;
#if defined(UNITY_COLORSPACE_GAMMA)
sh = pow(sh, 1.0 / 2.2);
#endif
fixed3 ambient = sh * _Albedo.rgb;
// Color
fixed4 color = fixed4(ambient + lerp(diffuse, specular, _Metallic), 1.0);
return color;
}
ENDCG
}
}
}