summaryrefslogtreecommitdiffstats
path: root/lighting.cginc
blob: 8a60e66c3078f44287f2565a454e250edcba0598 (plain)
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#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);
	return float4(_LightColor0.xyz, _LightColor0.w);
}

float3 getIndirectSpecular(v2f i, float perceptual_roughness, float3 view_dir, float3 reflect_dir) {
  UnityGIInput data = InitialiseUnityGIInput(i.worldPos, view_dir);
  float3 env_refl = UnityGI_prefilteredRadiance(data, perceptual_roughness, reflect_dir);
  return env_refl;
}

// Geomerics SH evaluation
// https://community.arm.com/cfs-file/__key/telligent-evolution-components-attachments/01-2066-00-00-00-01-27-70/Simplifying_2D00_Spherical_2D00_Harmonics_2D00_for_2D00_Lighting.pdf
float shEvaluateDiffuseL1Geomerics(float L0, float3 L1, float3 n) {
  // average energy
  float R0 = max(L0, 0);

  // avg direction of incoming light
  float3 R1 = 0.5f * L1;

  // directional brightness
  float lenR1 = length(R1);

  // linear angle between normal and direction 0-1
  float q = dot(normalize(R1), n) * 0.5 + 0.5;
  q = saturate(q);

  // power for q
  // lerps from 1 (linear) to 3 (cubic) based on directionality
  float p = 1.0f + 2.0f * lenR1 / R0;

  // dynamic range constant
  // should vary between 4 (highly directional) and 0 (ambient)
  float a = (1.0f - lenR1 / R0) / (1.0f + lenR1 / R0);

  return R0 * (a + (1.0f - a) * (p + 1.0f) * pow(q, p));
}

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));
  }

  // 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

  // Equation 13 from "An Efficient Representation for Irradiance Environment
  // Maps" by Ramamoorthi and Hanrahan. Normalization constants have been
  // premultiplied by Unity into the coefficient buffers.
  //
  // L0+L1: dot4 per channel (n.w=1 picks up the L0 term from SHA*.w)
  // L2: four quadratic terms packed into vB via swizzle multiply, plus L22
  float4 n4 = float4(n.xyz, 1.0);
  float3 L0 = float3(unity_SHAr.w, unity_SHAg.w, unity_SHAb.w);
  float3 L01 = float3(dot(unity_SHAr, n4), dot(unity_SHAg, n4), dot(unity_SHAb, n4));
  float4 vB = n4.xyzz * n4.yzzx;
  float3 L2 = float3(dot(unity_SHBr, vB), dot(unity_SHBg, vB), dot(unity_SHBb, vB))
            + unity_SHC * (n.x * n.x - n.y * n.y);

  light.L00 = L0;
  light.L01r = unity_SHAr.xyz;
  light.L01g = unity_SHAg.xyz;
  light.L01b = unity_SHAb.xyz;

  return L01 + L2;
}

float4 getIndirectDiffuse(v2f i, Pbr pbr, inout LightIndirect light) {
  float4 diffuse = 0;
#if defined(FORWARD_BASE_PASS)
#if defined(_BENT_NORMALS)
  diffuse.xyz += max(0, yumSH9(float4(pbr.bent_normal, 1.0), i.worldPos, light));
#else
  diffuse.xyz += max(0, yumSH9(float4(i.normal, 1.0), i.worldPos, light));
#endif
#endif

#if defined(_AMBIENT_OCCLUSION)
  diffuse.xyz *= pbr.ao;
#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 * 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_perceptual, view_dir, -data.indirect.dir);
#if defined(_AMBIENT_OCCLUSION) || defined(_BENT_NORMALS)
  float ao_vis = 1.0;
#if defined(_AMBIENT_OCCLUSION)
  ao_vis = pbr.ao;
#endif
#if defined(_BENT_NORMALS)
  float3 spec_ao_normal = pbr.bent_normal;
#else
  float3 spec_ao_normal = pbr.normal;
#endif
  //float NoV_geom = saturate(dot(i.normal, data.common.V));
  float spec_ao = computeSpecularAO(data.common.NoV, ao_vis, pbr.roughness, spec_ao_normal, -data.indirect.dir);
#if defined(_BENT_NORMALS)
  spec_ao = saturate(lerp(1.0, spec_ao, _Bent_Normals_Strength));
#endif
  data.indirect.specular *= spec_ao;
#endif

  // Didn't see a big difference in testbed, so commented out.
  // Bent normals get us most of what we want.
#if 0
  // Horizon fading
  // https://marmosetco.tumblr.com/post/81245981087
  // The goal is to attenuate anything which points into the mesh.
  // TODO expose fade strength as a parameter.
  float horizon_fade_str = 1.3;
  float horizon_fade = saturate(1.0f + horizon_fade_str * dot(pbr.normal, -reflect_dir));
  horizon_fade *= horizon_fade;
  data.indirect.specular *= horizon_fade;
#endif

#if defined(_CLEARCOAT)
  data.indirect.specular_cc = getIndirectSpecular(i, saturate(sqrt(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