-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathcustom_pbr.frag
More file actions
599 lines (492 loc) · 22.2 KB
/
custom_pbr.frag
File metadata and controls
599 lines (492 loc) · 22.2 KB
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
#version 450
#extension GL_ARB_separate_shader_objects : enable
#pragma import_defines (VSG_POINT_SPRITE, VSG_DIFFUSE_MAP, VSG_GREYSCALE_DIFFUSE_MAP, VSG_DETAIL_MAP, VSG_EMISSIVE_MAP, VSG_LIGHTMAP_MAP, VSG_NORMAL_MAP, VSG_METALLROUGHNESS_MAP, VSG_SPECULAR_MAP, VSG_TWO_SIDED_LIGHTING, VSG_WORKFLOW_SPECGLOSS, VSG_SHADOWS_PCSS, VSG_SHADOWS_SOFT, VSG_SHADOWS_HARD, SHADOWMAP_DEBUG, VSG_ALPHA_TEST, CUSTOM_HIGHLIGHT)
// define by default for backwards compatibility
#define VSG_SHADOWS_HARD
#define VIEW_DESCRIPTOR_SET 1
#define MATERIAL_DESCRIPTOR_SET 2
#define CUSTOM_DESCRIPTOR_SET 0
#define HIGHLIGHT_DESCRIPTOR_SET 3
const float PI = 3.14159265359;
const float RECIPROCAL_PI = 0.31830988618;
const float RECIPROCAL_PI2 = 0.15915494;
const float EPSILON = 1e-6;
const float c_MinRoughness = 0.04;
#ifdef VSG_DIFFUSE_MAP
layout(set = MATERIAL_DESCRIPTOR_SET, binding = 0) uniform sampler2D diffuseMap;
#endif
#ifdef VSG_DETAIL_MAP
layout(set = MATERIAL_DESCRIPTOR_SET, binding = 1) uniform sampler2D detailMap;
#endif
#ifdef VSG_NORMAL_MAP
layout(set = MATERIAL_DESCRIPTOR_SET, binding = 2) uniform sampler2D normalMap;
#endif
#ifdef VSG_LIGHTMAP_MAP
layout(set = MATERIAL_DESCRIPTOR_SET, binding = 3) uniform sampler2D aoMap;
#endif
#ifdef VSG_EMISSIVE_MAP
layout(set = MATERIAL_DESCRIPTOR_SET, binding = 4) uniform sampler2D emissiveMap;
#endif
#ifdef VSG_SPECULAR_MAP
layout(set = MATERIAL_DESCRIPTOR_SET, binding = 5) uniform sampler2D specularMap;
#endif
#ifdef VSG_METALLROUGHNESS_MAP
layout(set = MATERIAL_DESCRIPTOR_SET, binding = 6) uniform sampler2D mrMap;
#endif
layout(set = MATERIAL_DESCRIPTOR_SET, binding = 10) uniform PbrData
{
vec4 baseColorFactor;
vec4 emissiveFactor;
vec4 diffuseFactor;
vec4 specularFactor;
float metallicFactor;
float roughnessFactor;
float alphaMask;
float alphaMaskCutoff;
} pbr;
// ViewDependentState
layout(constant_id = 3) const int lightDataSize = 256;
layout(set = VIEW_DESCRIPTOR_SET, binding = 0) uniform LightData
{
vec4 values[lightDataSize];
} lightData;
// Custom state
layout(set = CUSTOM_DESCRIPTOR_SET, binding = 0) uniform Fog
{
vec3 color;
float density;
float start;
float end;
float exponent;
} fog;
#ifdef CUSTOM_HIGHLIGHT
layout(set = HIGHLIGHT_DESCRIPTOR_SET, binding = 0) uniform Highlight
{
vec3 color;
bool highlighted;
} highlight;
#endif
layout(location = 0) in vec3 eyePos;
layout(location = 1) in vec3 normalDir;
layout(location = 2) in vec4 vertexColor;
#ifndef VSG_POINT_SPRITE
layout(location = 3) in vec2 texCoord0;
#endif
layout(location = 5) in vec3 viewDir;
layout(location = 0) out vec4 outColor;
// Encapsulate the various inputs used by the various functions in the shading equation
// We store values in this struct to simplify the integration of alternative implementations
// of the shading terms, outlined in the Readme.MD Appendix.
struct PBRInfo
{
float NdotL; // cos angle between normal and light direction
float NdotV; // cos angle between normal and view direction
float NdotH; // cos angle between normal and half vector
float LdotH; // cos angle between light direction and half vector
float VdotH; // cos angle between view direction and half vector
float VdotL; // cos angle between view direction and light direction
float perceptualRoughness; // roughness value, as authored by the model creator (input to shader)
float metalness; // metallic value at the surface
vec3 reflectance0; // full reflectance color (normal incidence angle)
vec3 reflectance90; // reflectance color at grazing angle
float alphaRoughness; // roughness mapped to a more linear change in the roughness (proposed by [2])
vec3 diffuseColor; // color contribution from diffuse lighting
vec3 specularColor; // color contribution from specular lighting
};
float rcp(const in float value)
{
return 1.0 / value;
}
float pow5(const in float value)
{
return value * value * value * value * value;
}
// include the calculateShadowCoverageForDirectionalLight(..) implementation
#include "shadows.glsl"
// Find the normal for this fragment, pulling either from a predefined normal map
// or from the interpolated mesh normal and tangent attributes.
vec3 getNormal()
{
vec3 result;
#ifdef VSG_NORMAL_MAP
// Perturb normal, see http://www.thetenthplanet.de/archives/1180
vec3 tangentNormal = texture(normalMap, texCoord0).xyz * 2.0 - 1.0;
//tangentNormal *= vec3(2,2,1);
vec3 q1 = dFdx(eyePos);
vec3 q2 = dFdy(eyePos);
vec2 st1 = dFdx(texCoord0);
vec2 st2 = dFdy(texCoord0);
vec3 N = normalize(normalDir);
vec3 T = normalize(q1 * st2.t - q2 * st1.t);
vec3 B = -normalize(cross(N, T));
mat3 TBN = mat3(T, B, N);
result = normalize(TBN * tangentNormal);
#else
result = normalize(normalDir);
#endif
#ifdef VSG_TWO_SIDED_LIGHTING
if (!gl_FrontFacing)
result = -result;
#endif
return result;
}
// Basic Lambertian diffuse
// Implementation from Lambert's Photometria https://archive.org/details/lambertsphotome00lambgoog
// See also [1], Equation 1
vec3 BRDF_Diffuse_Lambert(PBRInfo pbrInputs)
{
return pbrInputs.diffuseColor * RECIPROCAL_PI;
}
vec3 BRDF_Diffuse_Custom_Lambert(PBRInfo pbrInputs)
{
return pbrInputs.diffuseColor * RECIPROCAL_PI * pow(pbrInputs.NdotV, 0.5 + 0.3 * pbrInputs.perceptualRoughness);
}
// [Gotanda 2012, "Beyond a Simple Physically Based Blinn-Phong Model in Real-Time"]
vec3 BRDF_Diffuse_OrenNayar(PBRInfo pbrInputs)
{
float a = pbrInputs.alphaRoughness;
float s = a;// / ( 1.29 + 0.5 * a );
float s2 = s * s;
float VoL = 2 * pbrInputs.VdotH * pbrInputs.VdotH - 1; // double angle identity
float Cosri = pbrInputs.VdotL - pbrInputs.NdotV * pbrInputs.NdotL;
float C1 = 1 - 0.5 * s2 / (s2 + 0.33);
float C2 = 0.45 * s2 / (s2 + 0.09) * Cosri * ( Cosri >= 0 ? 1.0 / max(pbrInputs.NdotL, pbrInputs.NdotV) : 1 );
return pbrInputs.diffuseColor / PI * ( C1 + C2 ) * ( 1 + pbrInputs.perceptualRoughness * 0.5 );
}
// [Gotanda 2014, "Designing Reflectance Models for New Consoles"]
vec3 BRDF_Diffuse_Gotanda(PBRInfo pbrInputs)
{
float a = pbrInputs.alphaRoughness;
float a2 = a * a;
float F0 = 0.04;
float VoL = 2 * pbrInputs.VdotH * pbrInputs.VdotH - 1; // double angle identity
float Cosri = VoL - pbrInputs.NdotV * pbrInputs.NdotL;
float a2_13 = a2 + 1.36053;
float Fr = ( 1 - ( 0.542026*a2 + 0.303573*a ) / a2_13 ) * ( 1 - pow( 1 - pbrInputs.NdotV, 5 - 4*a2 ) / a2_13 ) * ( ( -0.733996*a2*a + 1.50912*a2 - 1.16402*a ) * pow( 1 - pbrInputs.NdotV, 1 + rcp(39*a2*a2+1) ) + 1 );
//float Fr = ( 1 - 0.36 * a ) * ( 1 - pow( 1 - NoV, 5 - 4*a2 ) / a2_13 ) * ( -2.5 * Roughness * ( 1 - NoV ) + 1 );
float Lm = ( max( 1 - 2*a, 0 ) * ( 1 - pow5( 1 - pbrInputs.NdotL ) ) + min( 2*a, 1 ) ) * ( 1 - 0.5*a * (pbrInputs.NdotL - 1) ) * pbrInputs.NdotL;
float Vd = ( a2 / ( (a2 + 0.09) * (1.31072 + 0.995584 * pbrInputs.NdotV) ) ) * ( 1 - pow( 1 - pbrInputs.NdotL, ( 1 - 0.3726732 * pbrInputs.NdotV * pbrInputs.NdotV ) / ( 0.188566 + 0.38841 * pbrInputs.NdotV ) ) );
float Bp = Cosri < 0 ? 1.4 * pbrInputs.NdotV * pbrInputs.NdotL * Cosri : Cosri;
float Lr = (21.0 / 20.0) * (1 - F0) * ( Fr * Lm + Vd + Bp );
return pbrInputs.diffuseColor * RECIPROCAL_PI * Lr;
}
vec3 BRDF_Diffuse_Burley(PBRInfo pbrInputs)
{
float energyBias = mix(pbrInputs.perceptualRoughness, 0.0, 0.5);
float energyFactor = mix(pbrInputs.perceptualRoughness, 1.0, 1.0 / 1.51);
float fd90 = energyBias + 2.0 * pbrInputs.VdotH * pbrInputs.VdotH * pbrInputs.perceptualRoughness;
float f0 = 1.0;
float lightScatter = f0 + (fd90 - f0) * pow(1.0 - pbrInputs.NdotL, 5.0);
float viewScatter = f0 + (fd90 - f0) * pow(1.0 - pbrInputs.NdotV, 5.0);
return pbrInputs.diffuseColor * lightScatter * viewScatter * energyFactor;
}
vec3 BRDF_Diffuse_Disney(PBRInfo pbrInputs)
{
float Fd90 = 0.5 + 2.0 * pbrInputs.perceptualRoughness * pbrInputs.VdotH * pbrInputs.VdotH;
vec3 f0 = vec3(0.1);
vec3 invF0 = vec3(1.0, 1.0, 1.0) - f0;
float dim = min(invF0.r, min(invF0.g, invF0.b));
float result = ((1.0 + (Fd90 - 1.0) * pow(1.0 - pbrInputs.NdotL, 5.0 )) * (1.0 + (Fd90 - 1.0) * pow(1.0 - pbrInputs.NdotV, 5.0 ))) * dim;
return pbrInputs.diffuseColor * result;
}
// The following equation models the Fresnel reflectance term of the spec equation (aka F())
// Implementation of fresnel from [4], Equation 15
vec3 specularReflection(PBRInfo pbrInputs)
{
//return pbrInputs.reflectance0 + (pbrInputs.reflectance90 - pbrInputs.reflectance0) * pow(clamp(1.0 - pbrInputs.VdotH, 0.0, 1.0), 5.0);
return pbrInputs.reflectance0 + (pbrInputs.reflectance90 - pbrInputs.reflectance90*pbrInputs.reflectance0) * exp2((-5.55473 * pbrInputs.VdotH - 6.98316) * pbrInputs.VdotH);
}
// This calculates the specular geometric attenuation (aka G()),
// where rougher material will reflect less light back to the viewer.
// This implementation is based on [1] Equation 4, and we adopt their modifications to
// alphaRoughness as input as originally proposed in [2].
float geometricOcclusion(PBRInfo pbrInputs)
{
float NdotL = pbrInputs.NdotL;
float NdotV = pbrInputs.NdotV;
float r = pbrInputs.alphaRoughness * pbrInputs.alphaRoughness;
float attenuationL = 2.0 * NdotL / (NdotL + sqrt(r + (1.0 - r) * (NdotL * NdotL)));
float attenuationV = 2.0 * NdotV / (NdotV + sqrt(r + (1.0 - r) * (NdotV * NdotV)));
return attenuationL * attenuationV;
}
// The following equation(s) model the distribution of microfacet normals across the area being drawn (aka D())
// Implementation from "Average Irregularity Representation of a Roughened Surface for Ray Reflection" by T. S. Trowbridge, and K. P. Reitz
// Follows the distribution function recommended in the SIGGRAPH 2013 course notes from EPIC Games [1], Equation 3.
float microfacetDistribution(PBRInfo pbrInputs)
{
float roughnessSq = pbrInputs.alphaRoughness * pbrInputs.alphaRoughness;
float f = (pbrInputs.NdotH * roughnessSq - pbrInputs.NdotH) * pbrInputs.NdotH + 1.0;
return roughnessSq / (PI * f * f);
}
vec3 BRDF(vec3 u_LightColor, vec3 v, vec3 n, vec3 l, vec3 h, float perceptualRoughness, float metallic, vec3 specularEnvironmentR0, vec3 specularEnvironmentR90, float alphaRoughness, vec3 diffuseColor, vec3 specularColor, float ao)
{
float unclmapped_NdotL = dot(n, l);
vec3 reflection = -normalize(reflect(v, n));
reflection.y *= -1.0f;
float NdotL = clamp(unclmapped_NdotL, 0.001, 1.0);
float NdotV = clamp(abs(dot(n, v)), 0.001, 1.0);
float NdotH = clamp(dot(n, h), 0.0, 1.0);
float LdotH = clamp(dot(l, h), 0.0, 1.0);
float VdotH = clamp(dot(v, h), 0.0, 1.0);
float VdotL = clamp(dot(v, l), 0.0, 1.0);
PBRInfo pbrInputs = PBRInfo(NdotL,
NdotV,
NdotH,
LdotH,
VdotH,
VdotL,
perceptualRoughness,
metallic,
specularEnvironmentR0,
specularEnvironmentR90,
alphaRoughness,
diffuseColor,
specularColor);
// Calculate the shading terms for the microfacet specular shading model
vec3 F = specularReflection(pbrInputs);
float G = geometricOcclusion(pbrInputs);
float D = microfacetDistribution(pbrInputs);
// Calculation of analytical lighting contribution
vec3 diffuseContrib = (1.0 - F) * BRDF_Diffuse_Disney(pbrInputs);
vec3 specContrib = F * G * D / (4.0 * NdotL * NdotV);
// Obtain final intensity as reflectance (BRDF) scaled by the energy of the light (cosine law)
vec3 color = NdotL * u_LightColor * (diffuseContrib + specContrib);
color *= ao;
#ifdef VSG_EMISSIVE_MAP
vec3 emissive = texture(emissiveMap, texCoord0).rgb * pbr.emissiveFactor.rgb;
#else
vec3 emissive = pbr.emissiveFactor.rgb;
#endif
color += emissive;
return color;
}
float convertMetallic(vec3 diffuse, vec3 specular, float maxSpecular)
{
float perceivedDiffuse = sqrt(0.299 * diffuse.r * diffuse.r + 0.587 * diffuse.g * diffuse.g + 0.114 * diffuse.b * diffuse.b);
float perceivedSpecular = sqrt(0.299 * specular.r * specular.r + 0.587 * specular.g * specular.g + 0.114 * specular.b * specular.b);
if (perceivedSpecular < c_MinRoughness)
{
return 0.0;
}
float a = c_MinRoughness;
float b = perceivedDiffuse * (1.0 - maxSpecular) / (1.0 - c_MinRoughness) + perceivedSpecular - 2.0 * c_MinRoughness;
float c = c_MinRoughness - perceivedSpecular;
float D = max(b * b - 4.0 * a * c, 0.0);
return clamp((-b + sqrt(D)) / (2.0 * a), 0.0, 1.0);
}
void main()
{
float brightnessCutoff = 0.001;
#ifdef VSG_POINT_SPRITE
vec2 texCoord0 = gl_PointCoord.xy;
#endif
float perceptualRoughness = 0.0;
float metallic;
vec3 diffuseColor;
vec4 baseColor;
float ambientOcclusion = 1.0;
vec3 f0 = vec3(0.04);
#ifdef VSG_DIFFUSE_MAP
#ifdef VSG_GREYSCALE_DIFFUSE_MAP
float v = texture(diffuseMap, texCoord0.st).s * pbr.baseColorFactor;
baseColor = vertexColor * vec4(v, v, v, 1.0);
#else
baseColor = vertexColor * texture(diffuseMap, texCoord0) * pbr.baseColorFactor;
#endif
#else
baseColor = vertexColor * pbr.baseColorFactor;
#endif
#ifdef VSG_DETAIL_MAP
vec4 detailColor = texture(detailMap, texCoord0.st);
baseColor.rgb = mix(baseColor.rgb, detailColor.rgb, detailColor.a);
#endif
#ifdef VSG_ALPHA_TEST
if (pbr.alphaMask == 1.0f && baseColor.a < pbr.alphaMaskCutoff) discard;
#endif
#ifdef VSG_WORKFLOW_SPECGLOSS
#ifdef VSG_DIFFUSE_MAP
vec4 diffuse = texture(diffuseMap, texCoord0);
#else
vec4 diffuse = vec4(1.0);
#endif
#ifdef VSG_SPECULAR_MAP
vec4 specular_texel = texture(specularMap, texCoord0);
vec3 specular = specular_texel.rgb;
perceptualRoughness = 1.0 - specular_texel.a;
#else
vec3 specular = vec3(0.0);
perceptualRoughness = 0.0;
#endif
float maxSpecular = max(max(specular.r, specular.g), specular.b);
// Convert metallic value from specular glossiness inputs
metallic = convertMetallic(diffuse.rgb, specular, maxSpecular);
const float epsilon = 1e-6;
vec3 baseColorDiffusePart = diffuse.rgb * ((1.0 - maxSpecular) / (1 - c_MinRoughness) / max(1 - metallic, epsilon)) * pbr.diffuseFactor.rgb;
vec3 baseColorSpecularPart = specular - (vec3(c_MinRoughness) * (1 - metallic) * (1 / max(metallic, epsilon))) * pbr.specularFactor.rgb;
baseColor = vec4(mix(baseColorDiffusePart, baseColorSpecularPart, metallic * metallic), diffuse.a);
#else
perceptualRoughness = pbr.roughnessFactor;
metallic = pbr.metallicFactor;
#ifdef VSG_METALLROUGHNESS_MAP
vec4 mrSample = texture(mrMap, texCoord0);
perceptualRoughness = mrSample.g * perceptualRoughness;
metallic = mrSample.b * metallic;
#endif
#endif
#ifdef VSG_LIGHTMAP_MAP
ambientOcclusion = texture(aoMap, texCoord0).r;
#endif
diffuseColor = baseColor.rgb * (vec3(1.0) - f0);
diffuseColor *= 1.0 - metallic;
float alphaRoughness = perceptualRoughness * perceptualRoughness;
vec3 specularColor = mix(f0, baseColor.rgb, metallic);
// Compute reflectance.
float reflectance = max(max(specularColor.r, specularColor.g), specularColor.b);
// For typical incident reflectance range (between 4% to 100%) set the grazing reflectance to 100% for typical fresnel effect.
// For very low reflectance range on highly diffuse objects (below 4%), incrementally reduce grazing reflecance to 0%.
float reflectance90 = clamp(reflectance * 25.0, 0.0, 1.0);
vec3 specularEnvironmentR0 = specularColor.rgb;
vec3 specularEnvironmentR90 = vec3(1.0, 1.0, 1.0) * reflectance90;
vec3 n = getNormal();
vec3 v = normalize(viewDir); // Vector from surface point to camera
float shininess = 100.0f;
vec3 color = vec3(0.0, 0.0, 0.0);
vec4 lightNums = lightData.values[0];
int numAmbientLights = int(lightNums[0]);
int numDirectionalLights = int(lightNums[1]);
int numPointLights = int(lightNums[2]);
int numSpotLights = int(lightNums[3]);
int lightDataIndex = 1;
if (numAmbientLights>0)
{
// ambient lights
for(int i = 0; i<numAmbientLights; ++i)
{
vec4 ambient_color = lightData.values[lightDataIndex++];
color += (baseColor.rgb * ambient_color.rgb) * (ambient_color.a * ambientOcclusion);
}
}
// index used to step through the shadowMaps array
int shadowMapIndex = 0;
if (numDirectionalLights>0)
{
vec3 q1 = dFdx(eyePos);
vec3 q2 = dFdy(eyePos);
vec2 st1 = dFdx(texCoord0);
vec2 st2 = dFdy(texCoord0);
vec3 N = normalize(normalDir);
vec3 T = normalize(q1 * st2.t - q2 * st1.t);
vec3 B = -normalize(cross(N, T));
// directional lights
for(int i = 0; i<numDirectionalLights; ++i)
{
vec4 lightColor = lightData.values[lightDataIndex++];
vec3 direction = -lightData.values[lightDataIndex++].xyz;
float brightness = lightColor.a;
// if light is too dim to effect the rendering skip it
if (brightness <= brightnessCutoff ) continue;
int shadowMapCount = int(lightData.values[lightDataIndex].r);
if (shadowMapCount > 0)
{
if (brightness > brightnessCutoff)
brightness *= (1.0-calculateShadowCoverageForDirectionalLight(lightDataIndex, shadowMapIndex, T, B, color));
lightDataIndex += 1 + 8 * shadowMapCount;
shadowMapIndex += shadowMapCount;
}
else
lightDataIndex++;
// if light is too shadowed to effect the rendering skip it
if (brightness <= brightnessCutoff ) continue;
vec3 l = direction; // Vector from surface point to light
vec3 h = normalize(l+v); // Half vector between both l and v
float scale = brightness;
color.rgb += BRDF(lightColor.rgb * scale, v, n, l, h, perceptualRoughness, metallic, specularEnvironmentR0, specularEnvironmentR90, alphaRoughness, diffuseColor, specularColor, ambientOcclusion);
}
}
if (numPointLights>0)
{
// point light
for(int i = 0; i<numPointLights; ++i)
{
vec4 lightColor = lightData.values[lightDataIndex++];
vec3 position = lightData.values[lightDataIndex++].xyz;
vec3 delta = position - eyePos;
float distance2 = delta.x * delta.x + delta.y * delta.y + delta.z * delta.z;
vec3 direction = delta / sqrt(distance2);
vec3 l = direction; // Vector from surface point to light
vec3 h = normalize(l+v); // Half vector between both l and v
float scale = lightColor.a / distance2;
color.rgb += BRDF(lightColor.rgb * scale, v, n, l, h, perceptualRoughness, metallic, specularEnvironmentR0, specularEnvironmentR90, alphaRoughness, diffuseColor, specularColor, ambientOcclusion);
}
}
if (numSpotLights>0)
{
vec3 q1 = dFdx(eyePos);
vec3 q2 = dFdy(eyePos);
vec2 st1 = dFdx(texCoord0);
vec2 st2 = dFdy(texCoord0);
vec3 N = normalize(normalDir);
vec3 T = normalize(q1 * st2.t - q2 * st1.t);
vec3 B = -normalize(cross(N, T));
// spot light
for(int i = 0; i<numSpotLights; ++i)
{
vec4 lightColor = lightData.values[lightDataIndex++];
vec4 position_cosInnerAngle = lightData.values[lightDataIndex++];
vec4 lightDirection_cosOuterAngle = lightData.values[lightDataIndex++];
float brightness = lightColor.a;
// if light is too dim to effect the rendering skip it
if (brightness <= brightnessCutoff ) continue;
vec3 delta = position_cosInnerAngle.xyz - eyePos;
float distance2 = delta.x * delta.x + delta.y * delta.y + delta.z * delta.z;
float dist = sqrt(distance2);
vec3 direction = delta / dist;
float dot_lightdirection = dot(lightDirection_cosOuterAngle.xyz, -direction);
int shadowMapCount = int(lightData.values[lightDataIndex].r);
if (shadowMapCount > 0)
{
if (lightDirection_cosOuterAngle.w > dot_lightdirection)
brightness *= (1.0-calculateShadowCoverageForSpotLight(lightDataIndex, shadowMapIndex, T, B, dist, color));
lightDataIndex += 1 + 8 * shadowMapCount;
shadowMapIndex += shadowMapCount;
}
else
lightDataIndex++;
// if light is too shadowed to effect the rendering skip it
if (brightness <= brightnessCutoff ) continue;
vec3 l = direction; // Vector from surface point to light
vec3 h = normalize(l+v); // Half vector between both l and v
float scale = (brightness * smoothstep(lightDirection_cosOuterAngle.w, position_cosInnerAngle.w, dot_lightdirection)) / distance2;
color.rgb += BRDF(lightColor.rgb * scale, v, n, l, h, perceptualRoughness, metallic, specularEnvironmentR0, specularEnvironmentR90, alphaRoughness, diffuseColor, specularColor, ambientOcclusion);
}
}
if (fog.exponent != 0.0)
{
float fogCoord = -eyePos.z;
const float e = 2.71828;
float f = pow(e, -fog.density * pow(fogCoord, fog.exponent));
color = mix(fog.color, color, f);
}
else
{
// linear
float fogCoord = -eyePos.z;
if (fogCoord > fog.start)
{
if (fogCoord < fog.end)
{
float f = (fog.end - fogCoord) / (fog.end - fog.start);
color = mix(fog.color, color, f);
}
else
{
color = fog.color;
}
}
}
#ifdef CUSTOM_HIGHLIGHT
if (highlight.highlighted)
color += highlight.color;
#endif
outColor = vec4(color, baseColor.a);
}