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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
|
#ifndef __LIGHTING_INC
#define __LIGHTING_INC
#include "UnityCG.cginc"
#include "AutoLight.cginc"
#include "UnityPBSLighting.cginc"
#include "UnityLightingCommon.cginc"
#include "UnityStandardCoreMinimal.cginc"
#include "features.cginc"
#include "filamented.cginc"
#include "interpolators.cginc"
#include "LightVolumes.cginc"
#include "pbr.cginc"
struct LightCommon {
float3 V;
float3 N;
float NoV;
#if defined(_CLEARCOAT)
float NoV_cc;
#endif
};
struct LightDirect {
float3 dir;
float3 H;
float NoH;
float NoL;
#if defined(_CLEARCOAT)
float NoH_cc;
float NoL_cc;
#endif
float LoH;
float LoV;
float double_LoV;
float3 color;
};
struct LightIndirect {
float3 dir;
float3 H;
float NoH;
float NoL;
#if defined(_CLEARCOAT)
float LoH_cc;
#endif
float LoH;
float LoV;
float double_LoV;
float3 specular;
#if defined(_CLEARCOAT)
float3 specular_cc;
#endif
float3 diffuse;
float3 L00;
float3 L01r;
float3 L01g;
float3 L01b;
};
struct LightData {
LightCommon common;
LightDirect direct;
LightIndirect indirect;
};
float3 getDirectLightDirection(v2f i) {
#if defined(POINT) || defined(POINT_COOKIE) || defined(SPOT)
return normalize((_WorldSpaceLightPos0 - i.worldPos).xyz);
#else
return _WorldSpaceLightPos0;
#endif
}
float getShadowAttenuation(v2f i)
{
UNITY_LIGHT_ATTENUATION(attenuation, i, i.worldPos);
return attenuation;
}
float4 getDirectLightColorIntensity() {
// Properly separate light color from intensity like filamented
if (_LightColor0.w <= 0) return float4(0, 0, 0, 0);
_LightColor0 += 1e-6f;
return float4(_LightColor0.xyz / _LightColor0.w, _LightColor0.w);
}
float3 getIndirectSpecular(v2f i, float roughness, float3 view_dir, float3 reflect_dir) {
UnityGIInput data = InitialiseUnityGIInput(i.worldPos, view_dir);
float3 env_refl = UnityGI_prefilteredRadiance(data, roughness, reflect_dir);
return env_refl;
}
float3 yumSH9(float4 n, float3 worldPos, inout LightIndirect light) {
[branch]
if (_UdonLightVolumeEnabled) {
LightVolumeSH(worldPos, light.L00, light.L01r, light.L01g, light.L01b);
return light.L00 + float3(
dot(light.L01r, n.xyz),
dot(light.L01g, n.xyz),
dot(light.L01b, n.xyz));
}
// No light volumes - use SH9
// Unity gives us the first three bands (L0-L2) of SH coefficients as follows:
// unity_SHA*.w: L0 coefficients
// unity_SHA*.xyz: L1 coefficients
// unity_SHB*: first four of the L2 coefficients
// unity_SHC: last L2 coefficient
// L0 band
float3 L0 = float3(unity_SHAr.w, unity_SHAg.w, unity_SHAb.w);
// L1 band
float3 L1 = float3(
dot(unity_SHAr.xyz, n.xyz),
dot(unity_SHAg.xyz, n.xyz),
dot(unity_SHAb.xyz, n.xyz)
);
// L2 band
float4 v = float4(n.x * n.y, n.y * n.z, n.z * n.z, n.x * n.z);
float3 L2 =
float3(dot(unity_SHBr.xyzw, v), dot(unity_SHBg.xyzw, v), dot(unity_SHBb.xyzw, v)) +
unity_SHC.xyz * (n.x * n.x - n.y * n.y);
// TODO expose this as a parameter
float wrap_term = 0.0f;
// Original coefficients: 1, 2/3, 1/4.
// Wrapped coefficients: 1, (2-w)/3, ((1-w)^2)/4.
float l1_wrap = 1.0f - wrap_term * 0.75f;
L1 *= l1_wrap;
float l2_wrap_base = 1.0f - wrap_term;
float l2_wrap = l2_wrap_base * l2_wrap_base;
L2 *= l2_wrap;
light.L00 = L0;
light.L01r = unity_SHAr.xyz * l1_wrap;
light.L01g = unity_SHAg.xyz * l1_wrap;
light.L01b = unity_SHAb.xyz * l1_wrap;
return L0 + L1 + L2;
}
float4 getIndirectDiffuse(v2f i, Pbr pbr, inout LightIndirect light) {
float4 diffuse = 0;
#if defined(FORWARD_BASE_PASS)
diffuse.xyz += max(0, yumSH9(float4(pbr.normal, 0), i.worldPos, light));
#endif
return diffuse;
}
void GetLighting(v2f i, Pbr pbr, out LightData data) {
data = (LightData) 0;
float3 view_dir = normalize(i.eyeVec.xyz);
data.common.V = -view_dir;
data.common.N = pbr.normal;
data.common.NoV = saturate(dot(pbr.normal, data.common.V));
#if defined(_CLEARCOAT)
data.common.NoV_cc = saturate(dot(i.normal, data.common.V));
#endif
// Direct lighting
data.direct.dir = getDirectLightDirection(i);
data.direct.H = normalize(data.common.V + data.direct.dir);
data.direct.NoL = saturate(dot(pbr.normal, data.direct.dir));
data.direct.NoH = saturate(dot(pbr.normal, data.direct.H));
data.direct.LoH = saturate(dot(data.direct.dir, data.direct.H));
#if defined(_CLEARCOAT)
data.direct.NoH_cc = saturate(dot(i.normal, data.direct.H));
data.direct.NoL_cc = saturate(dot(i.normal, data.direct.dir));
#endif
float direct_LoV = dot(data.direct.dir, data.common.V);
data.direct.LoV = saturate(direct_LoV);
data.direct.double_LoV = saturate(2.0f * direct_LoV * direct_LoV - 1.0f);
float4 lightColorIntensity = getDirectLightColorIntensity();
data.direct.color = lightColorIntensity.rgb * lightColorIntensity.w * getShadowAttenuation(i);
// Indirect lighting
float3 reflect_dir = -reflect(data.common.V, pbr.normal);
float3 dominant_dir = getSpecularDominantDirection(pbr.normal, reflect_dir, pbr.roughness);
data.indirect.dir = normalize(dominant_dir);
data.indirect.H = normalize(data.common.V + data.indirect.dir);
data.indirect.NoL = saturate(dot(pbr.normal, data.indirect.dir));
data.indirect.NoH = saturate(dot(pbr.normal, data.indirect.H));
#if defined(_CLEARCOAT)
float3 dir_cc = -reflect(data.common.V, i.normal);
float3 H_cc = normalize(data.common.V + dir_cc);
data.indirect.LoH_cc = saturate(dot(dir_cc, H_cc));
#endif
data.indirect.LoH = saturate(dot(data.indirect.dir, data.indirect.H));
float indirect_LoV = dot(data.indirect.dir, data.common.V);
data.indirect.LoV = saturate(indirect_LoV);
data.indirect.double_LoV = saturate(2.0f * indirect_LoV * indirect_LoV - 1.0f);
data.indirect.diffuse = getIndirectDiffuse(i, pbr, data.indirect);
data.indirect.specular = getIndirectSpecular(i, pbr.roughness, view_dir, data.indirect.dir);
#if defined(_CLEARCOAT)
data.indirect.specular_cc = getIndirectSpecular(i, pbr.cc_roughness, view_dir, dir_cc);
#if defined(_CLEARCOAT_MASK)
float cc_mask = _Clearcoat_Mask.Sample(bilinear_clamp_s, i.uv01.xy).r;
data.indirect.specular_cc *= cc_mask;
#endif
#endif
}
#endif // __LIGHTING_INC
|