Untitled
Loading...
Searching...
No Matches
dynamic.frag
Go to the documentation of this file.
1
6#extension GL_ARB_bindless_texture : require
7
8
27
32struct Light
33{
34
35 vec3 pos;
36 uint entity_id;
37
38 vec3 dir;
39 float outer_cos;
40
41 vec3 color;
42 float cos_diff;
43
44 float intensity;
45 uint light_type;
46 float inner_angle;
47 float outer_angle;
48
49 float radius;
50 uint layer;
51 uint mesh;
52 uint tri;
53
54 float area;
55 uint pad0;
56 uint pad1;
57 uint pad2;
58
59};
60
65struct Material
66{
67
68 vec4 ambient;
69 vec4 diffuse;
70 vec4 specular;
71 vec4 emissive;
72
74 uint pad0;
75 uint pad1;
77 uint pad2;
78 uint pad3;
79
80 sampler2D albedo_tex;
81 sampler2D normal_tex;
82 sampler2D arm_tex;
83 sampler2D emissive_tex;
84
85 float metallic;
86 float roughness;
87 float ao_strength;
88 float normal_scale;
89 uint alpha_mode;
90 float alpha_cutoff;
91 uint double_sided;
93
94};
95
100struct RProbe
101{
102 vec3 center;
103 uint pad0;
104 vec3 min;
105 uint pad1;
106 vec3 max;
107 uint pad2;
108
109};
110
115struct Cluster
116{
117
118 uint offset;
119 uint count;
120};
121
122
123
124layout(location = 0)out vec4 fragColor;
125layout(std140, binding = 0) uniform CameraData {
126 vec4 cam_pos;
127 vec4 cam_vel;
129 mat4 unjittered_projection;
130 mat4 view;
131 mat4 inv_projection;
132 mat4 unjittered_inv_projection;
133 mat4 inv_view;
134};
135layout(std140, binding = 1) uniform RenderingData {
136 ivec4 resolution;
137};
138layout(std140, binding = 2) uniform LightCameraData {
139 mat4 light_proj[4];
140 mat4 light_view[4];
141};
142
143layout(std140, binding = 3) uniform RProbeData {
144
145 uint rprobe_indices[4];
146 uint rprobe_count;
147 uint pad0;
148 uint pad1;
149 uint pad2;
150
151};
152
153
154layout(std140, binding = 6) uniform ProbeVolumeData{
155 OBB pvs[6];
156};
157
158
159
160layout(std430, binding = 0) buffer LightData {
161
162 Light lights[];
163
164};
165
166layout(std430, binding = 1) buffer MeshLightData {
167
168 uint light_indices[];
169
170};
171
172layout(std430, binding = 2) buffer MaterialData {
173
174 Material materials[];
175
176};
177layout(std430, binding = 3) buffer LightmapData {
178
179 vec4 uvs[];
180
181};
182layout(std430, binding = 10) buffer RProbeSSBO {
183
184 RProbe rprobes[10];
185
186};
187
188layout(std430, binding = 13) buffer GlobalLightIndexData {
189 uint global_light_indices[];
190};
191
192layout(std430, binding = 14) buffer ClusterData {
193 Cluster clusters[];
194};
195
198in float TangentSign;
207
211
212flat in uint Static;
213flat in uint Mat;
214
215uniform int material;
216uniform sampler2D uLightmap;
217uniform sampler2D uDirectionalmap;
218uniform sampler2DArrayShadow uShadowmap;
219uniform sampler2D gAlbedo;
220uniform sampler2D gNormal;
221uniform sampler2D gExtra;
222uniform samplerCubeArray uRProbe;
223uniform samplerCubeArrayShadow uPLSCMA;
224uniform sampler2D uBRDFLUT;
225uniform samplerCube uSkybox;
226uniform sampler2D uFBM;
227uniform sampler2D uSSAO;
228uniform sampler2D uDepth;
229uniform sampler2D uSSR;
230uniform sampler2D gUV;
232uniform float probe_spacing;
234uniform int index_offset;
235uniform int probe_bake;
236
237
238
239uniform uint uLPVDepthPadded;
240uniform sampler3D probe_tex;
241
242
245uniform float uNear;
246uniform float uFar;
247uniform float uLogScale;
248
249const float height_scale = 0.02f;
250
251
252
253vec2 parallax_mapping(sampler2D normal_map, vec2 texCoords, vec3 viewDir, out float final_depth)
254{
255
256 vec2 dx = dFdx(texCoords);
257 vec2 dy = dFdy(texCoords);
258 const float minLayers = 2.0;
259 const float maxLayers = 16.0;
260
261 float tx_width = float(textureSize(normal_map, 0).x);
262
263 vec2 dx2 = max(dx * dx * tx_width * tx_width, dy * dy * tx_width * tx_width);
264 float mip_level = 0.5 * log2(max(dx2.x, dx2.y));
265
266 float fade = clamp(1.0 - (mip_level / 3.0), 0.0, 1.0);
267
268
269 float current_max_layers = mix(minLayers, maxLayers, fade);
270
271
272 float numLayers = mix(current_max_layers, minLayers, abs(dot(vec3(0.0, 0.0, 1.0), viewDir)));
273
274 float layerDepth = 1.0 / numLayers;
275 float currentLayerDepth = 0.0;
276
277
278 float uv_scale = length(fwidth(texCoords));
279 vec2 P = viewDir.xy *height_scale;
280 vec2 deltaTexCoords = vec2(P.x, -P.y) / numLayers;
281
282 vec2 currentTexCoords = texCoords;
283 float freq = 10;
284 float currentDepthMapValue = textureGrad(normal_map, currentTexCoords, dx, dy).a;
285
286 int safety = 100;
287 int cur = 0;
288 while(currentLayerDepth < currentDepthMapValue && safety > cur)
289 {
290 currentTexCoords -= deltaTexCoords;
291
292
293 currentDepthMapValue = textureGrad(normal_map, currentTexCoords, dx, dy).a;
294 currentLayerDepth += layerDepth;
295 cur++;
296 }
297
298 vec2 prevTexCoords = currentTexCoords + deltaTexCoords;
299 float afterDepth = currentLayerDepth - currentDepthMapValue;
300 float current_depth = textureGrad(normal_map, currentTexCoords, dx, dy).a;
301 float prev_depth = textureGrad(normal_map, prevTexCoords, dx, dy).a;
302 float beforeDepth = (prev_depth) - currentLayerDepth + layerDepth;
303
304 float weight = afterDepth / (afterDepth + beforeDepth);
305 vec2 finalTexCoords = mix(currentTexCoords, prevTexCoords, weight);
306 final_depth = mix(current_depth, prev_depth, weight);
307
308 return finalTexCoords;
309}
310
311vec3 get_parallax_corrected_vector(vec3 worldPos, vec3 viewDir, vec3 normal, vec3 boxMin, vec3 boxMax, vec3 probeCenter) {
312
313 vec3 reflectionDir = reflect(-viewDir, normal);
314
315 vec3 firstPlaneIntersect = (boxMax - worldPos) / reflectionDir;
316 vec3 secondPlaneIntersect = (boxMin - worldPos) / reflectionDir;
317
318 vec3 furthestPlane = max(firstPlaneIntersect, secondPlaneIntersect);
319
320 float distance = min(min(furthestPlane.x, furthestPlane.y), furthestPlane.z);
321
322 vec3 intersectPosition = worldPos + reflectionDir * distance;
323
324 vec3 correctedReflDir = intersectPosition - probeCenter;
325
326 return correctedReflDir;
327}
328
329
330
331float shadow_calculation(uint layer, vec4 pos, float diff)
332{
333 vec3 proj_coords = pos.xyz / pos.w;
334
335 proj_coords = proj_coords * 0.5 + 0.5;
336 if (proj_coords.z > 1.0f) return 0;
337
338 float bias = max(0.005f * (diff), 0.0005f);
339
340
341 float current_depth = proj_coords.z;
342
343 float shadow = texture(uShadowmap, vec4(proj_coords.xy, layer, current_depth));
344
345
346 return shadow;
347
348}
349
350
351const vec3 DIELECTRIC_F0 = vec3(0.04);
352const float PI = 3.14159265359;
353
354float distribution_GGX(float NdotH, float roughness)
355{
356 float a = roughness * roughness;
357 float a2 = a * a;
358 float d = (NdotH * a2 - NdotH) * NdotH + 1.0;
359 return a2 / (PI * d * d + 0.00001);
360}
361
369vec3 fresnel_schlick(float CosTheta, vec3 F0)
370{
371 float fc = 1.0 - CosTheta;
372 float fc2 = fc * fc;
373 float fc5 = fc2 * fc2 * fc;
374 return F0 + (1.0 - F0) * fc5;
375}
376
377vec3 fresnel_schlick_roughness(float cosTheta, vec3 albedo, float roughness, float metallic)
378{
379
380 vec3 F0 = mix(DIELECTRIC_F0, albedo, metallic);
381
382 vec3 F = fresnel_schlick(cosTheta, F0);
383 return F;
384}
385
386float visibility(float NdotV, float NdotL, float roughness)
387{
388 float k = roughness * 0.5;
389 float gV = NdotV * (1.0 - k) + k;
390 float gL = NdotL * (1.0 - k) + k;
391 return 0.25 / (gV * gL + 0.00001);
392}
393
394vec3 calculate_light(vec3 N, vec3 V, vec3 L, vec3 albedo, float roughness, float metallic, vec3 light_color)
395{
396 vec3 H = normalize(V + L);
397
398 float NdotL = max(dot(N, L), 0.0);
399 float NdotV = max(dot(N, V), 0.0);
400 float NdotH = max(dot(N, H), 0.0);
401 float LdotH = max(dot(L, H), 0.0);
402
403 if (NdotL <= 0.0) return vec3(0.0);
404
405 vec3 F0 = mix(DIELECTRIC_F0, albedo, metallic);
406
407 float D = distribution_GGX(NdotH, roughness);
408 vec3 F = fresnel_schlick(LdotH, F0);
409 float vis = visibility(NdotV, NdotL, roughness);
410
411 vec3 specular = D * F * vis;
412
413 vec3 kS = F;
414 vec3 kD = vec3(1.0) - kS;
415 kD *= 1.0 - metallic;
416
417 vec3 diffuse = kD * albedo / PI;
418
419 return (diffuse + specular) * light_color * NdotL;
420}
421
422
423
424float normal_filtering(vec3 N, float roughness) {
425 vec3 dNdx = dFdx(N);
426 vec3 dNdy = dFdy(N);
427
428 float variance = dot(dNdx, dNdx) + dot(dNdy, dNdy);
429
430 float kernel_roughness = min(variance * 0.5, 0.5);
431
432 float alpha = roughness * roughness;
433 float filtered_alpha = clamp(alpha + kernel_roughness, 0.0, 1.0);
434
435 return sqrt(filtered_alpha);
436}
437
438
439
440vec3 palette(in float t, in vec3 a, in vec3 b, in vec3 c, in vec3 d) {
441 return a + b * cos(6.28318 * (c * t + d));
442}
443
444float hash(vec3 p) {
445 p = fract(p * 0.3183099 + vec3(0.1));
446 p *= 17.0;
447 return fract(p.x * p.y * p.z * (p.x + p.y + p.z));
448}
449
450float compute_probe_weight(vec3 world_pos, RProbe rp) {
451
452 vec3 dist_min = world_pos - rp.min;
453 vec3 dist_max = rp.max - world_pos;
454
455 vec3 min_dist = min(dist_min, dist_max);
456 float closest_wall_dist = min(min(min_dist.x, min_dist.y), min_dist.z);
457
458 if (closest_wall_dist <= 0.0f) {
459 return 0.0f;
460 }
461
462 vec3 fade_margin = vec3(5, 5, 5);
463
464 float margin = min(min(fade_margin.x, fade_margin.y), fade_margin.z);
465 float weight = closest_wall_dist / margin;
466
467 if (weight > 1.0f) weight = 1.0f;
468 if (weight < 0.0f) weight = 0.0f;
469
470
471 return weight;
472}
473
474
475vec3 sample_weighted_rprobes(vec3 pos, vec3 view_dir, vec3 normal, float roughness_lod)
476{
477
478 float weights[4];
479 float sum_weights = 0.0f;
480 for (int i = 0; i < rprobe_count; i++)
481 {
482 RProbe rp = rprobes[rprobe_indices[i]];
483 weights[i] = clamp(compute_probe_weight(pos, rp), 0.0, 1.0);
484
485 sum_weights += weights[i];
486 }
487 vec3 sky_color = vec3(1,0,0);
488 vec3 color = vec3(0.0,0.0,0.0);
489 for (int i = 0; i < rprobe_count; i++)
490 {
491
492 float w_norm = weights[i] / sum_weights;
493 w_norm = clamp(w_norm, 0.0, 1.0);
494 RProbe rp = rprobes[rprobe_indices[i]];
495 vec3 corrected = get_parallax_corrected_vector(pos, view_dir, normal, rp.min, rp.max, rp.center);
496 corrected = vec3(corrected.x, corrected.z, -corrected.y);
497 color += textureLod(uRProbe, vec4(corrected, rprobe_indices[i]), roughness_lod).rgb * w_norm;
498 }
499
500
501 return color;
502
503
504}
505
506
507
508float calculate_attenuation(float distance, float radius, float intensity) {
509 float attenuation = intensity / (distance + 1.0);
510
511 float factor = distance / (radius * radius);
512 float window = clamp(1.0 - factor * factor * factor * factor, 0.0, 1.0);
513 window = window * window;
514
515 return attenuation * window;
516}
517
519 float depth = max(-ViewPos.z, uNear);
520
521 uint z_tile = uint(max(0.0, log2(depth / uNear) * uLogScale));
522 z_tile = min(z_tile, uGridSize.z - 1);
523
524 uint x_tile = uint(gl_FragCoord.x / (uScreenRes.x / float(uGridSize.x)));
525 uint y_tile = uint(gl_FragCoord.y / (uScreenRes.y / float(uGridSize.y)));
526
527 x_tile = clamp(x_tile, 0u, uGridSize.x - 1u);
528 y_tile = clamp(y_tile, 0u, uGridSize.y - 1u);
529
530 uint cluster_index = x_tile + (y_tile * uGridSize.x) + (z_tile * uGridSize.x * uGridSize.y);
531
532 return cluster_index;
533}
534
535
536float calculate_specular_occlusion(float NdotV, float ao, float roughness)
537{
538 return clamp(pow(NdotV + ao, exp2(-16.0 * roughness - 1.0)) - 1.0 + ao, 0.0, 1.0);
539}
540
541
542
543float linearize(float z)
544{
545 float near = 0.1;
546 float far = 100.0;
547 return (2.0 * near) / (far + near - z * (far - near));
548}
549
562{
563
564 vec3 local_pos = pos - obb.center;
565 mat3 rot = mat3(obb.axis_x, obb.axis_y, obb.axis_z);
566 vec3 unrotated = inverse(rot) * local_pos;
567 vec3 norm = unrotated / (obb.extents * 2.0f);
568 return norm + 0.5f;
569}
570
571#define LPV_SLOTS 6
572#define SH_MAX 9
573void main()
574{
575
576 vec2 texelSize = 1.0 / uScreenRes.xy;
577 vec2 screenUV = gl_FragCoord.xy * texelSize;
578 uint cluster_index = get_cluster_index();
579 float final_depth;
580 Material mat = materials[Mat];
581 vec3 viewDir = normalize(ViewDirTanSpace);
582 #ifdef POM_VDIR
583 fragColor = vec4(viewDir, 1.0);
584 return;
585 #endif
586 vec2 uv = UV;
587 #ifdef POM
588 uv = parallax_mapping(mat.normal_tex, UV, viewDir, final_depth);
589 #endif
590
591 #ifdef DVIEW_UV
592 fragColor = vec4(uv.x, uv.y, 0.0, 1.0);
593 return;
594 #endif
595
596 vec4 albedo = texture(mat.albedo_tex, uv) * mat.diffuse;
597 #ifdef ALBEDO
598 fragColor = albedo;
599 return;
600 #endif
601
602 vec3 arm = texture(mat.arm_tex, uv).rgb;
603 #ifdef ARM
604 fragColor = vec4(arm, 1.0);
605 return;
606 #endif
607
608 vec4 map_normal = texture(mat.normal_tex, uv) * 2.0 - 1.0;
609 map_normal.xyz = normalize(map_normal.xyz);
610 vec3 N = normalize(TBN * map_normal.xyz);
611 #ifdef NORMAL
612 fragColor = vec4(N, 1.0);
613 return;
614 #endif
615
616 vec3 emissive = texture(mat.emissive_tex, uv).rgb;
617 #ifdef EMISSIVE
618 fragColor = vec4(emissive * mat.emissive.rgb * 3.0, 1.0);
619 return;
620 #endif
621 vec3 original_normal = normalize(Normal);
622 #ifdef ORIGINAL_NORMAL
623 fragColor = vec4(original_normal, 1.0);
624 return;
625 #endif
626
627
628 vec3 sun_dir = vec3(-0.5, 0.2, 0.9);
629 vec3 L = normalize(sun_dir);
630
631 vec3 view_dir = normalize(cam_pos.xyz - Pos);
632
633 float cosTheta = max(dot(N, view_dir), 0.0);
634
635 float ao = arm.r * texture(uSSAO, screenUV).r;
636 vec4 SSR = texture(uSSR, screenUV);
637
638 float roughness = arm.g * mat.roughness;
639 float metallic = arm.b * mat.metallic;
640 vec3 acc = vec3(0,0,0);
641 vec3 ddir = vec3(0,0,1);
642
643 uint directional_light_offset = DirectionalLight.x;
644 uint directional_light_count = DirectionalLight.y;
645 uint spot_light_offset = SpotLight.x;
646 uint spot_light_count = SpotLight.y;
647
648 Cluster cluster = clusters[cluster_index];
649 float depth = max(-ViewPos.z, uNear);
650
651 uint z_tile = uint(max(0.0, log2(depth / uNear) * uLogScale));
652 z_tile = min(z_tile, uGridSize.z - 1);
653
654 uint x_tile = uint(gl_FragCoord.x / (uScreenRes.x / float(uGridSize.x)));
655 uint y_tile = uint(gl_FragCoord.y / (uScreenRes.y / float(uGridSize.y)));
656
657 x_tile = clamp(x_tile, 0u, uGridSize.x - 1u);
658 y_tile = clamp(y_tile, 0u, uGridSize.y - 1u);
659
660 #ifdef CLUSTERS
661 fragColor = vec4(float(x_tile) / uGridSize.x, float(y_tile) / uGridSize.y, depth * (float(z_tile) / uGridSize.z), 1.0);
662 return;
663 #endif
664
665 for (int i = 0; i < cluster.count; i++)
666 {
667 uint index = global_light_indices[cluster.offset + i];
668 Light l = lights[index];
669 vec3 diff = l.pos - Pos;
670 vec3 dir = normalize(diff);
671
672 float nbias = max(0.1 * (1.0 - dot(original_normal, dir)), 0.05);
673 float d2 = dot(diff, diff);
674
675
676 vec3 lighting = calculate_light(N, view_dir, dir, albedo.rgb, roughness, metallic, l.color);
677 lighting *= calculate_attenuation(d2, l.radius, l.intensity);
678
679 vec3 opp = (Pos + original_normal * nbias) - l.pos;
680 float current_depth = length(opp) / 100;
681
682 opp = vec3(opp.x, opp.z, -opp.y);
683 float bias = max(0.005 * (1.0 - dot(N, dir)), 0.0005);
684
685 float s = texture(uPLSCMA, vec4(opp, index), current_depth - bias).r;
686
687 lighting *= s;
688
689
690
691 acc += lighting;
692
693 }
694 for (int i = 0; i < directional_light_count; i++)
695 {
696 Light l = lights[light_indices[directional_light_offset + i]];
697 vec3 lighting = calculate_light(N, view_dir, l.dir, albedo.rgb, roughness, metallic, l.color);
698
699 float shadow = shadow_calculation(l.layer, FragPosLightSpace[l.layer], 0);
700 lighting *= l.intensity;
701 lighting *= shadow;
702 acc += lighting;
703 }
704 for (int i = 0; i < spot_light_count; i++)
705 {
706
707 Light l = lights[light_indices[spot_light_offset + i]];
708 vec3 diff = l.pos - Pos;
709 vec3 dir = normalize(diff);
710 float d2 = dot(diff, diff);
711
712
713 vec3 lighting = calculate_light(N, view_dir, dir, albedo.rgb, roughness, metallic, l.color);
714 float theta = dot(-dir, l.dir);
715
716 float intensity = clamp((theta - l.outer_cos) * l.cos_diff, 0.0f, 1.0f);
717 lighting /= max(d2, 0.001);
718 lighting *= intensity;
719 lighting *= l.intensity;
720 acc += lighting;
721 }
722 vec3 direct_diffuse = albedo.rgb * acc;
723 #ifdef DIRECT
724 fragColor = vec4(direct_diffuse, albedo.a);
725 return;
726 #endif
727
728 vec3 irradiance = vec3(0,0,0);
729 float ff;
730 if (Static == 0)
731 {
732 vec3 tex_local = vec3(0.0);
733 float closest = 10000000;
734
735 uint closest_pv = 0;
736 for (int i = 0; i < 6; i++)
737 {
738
739 if (pvs[i].loaded == 0) continue;
740 vec3 local = pos_to_pv_local(pvs[i], Pos);
741 vec3 clamped_local = clamp(local, vec3(0.0), vec3(1.0));
742 float dist = length(clamped_local - local);
743 if (dist < closest)
744 {
745 closest_pv = i;
746 closest = dist;
747 tex_local = local;
748 }
749 if (dist <= 0.0f)
750 {
751 break;
752 }
753 }
754
755 // pad out to dilate into empty space on top and bottom z to avoid leaking
756 float full_z = uLPVDepthPadded * SH_MAX * LPV_SLOTS;
757 float grid_size_div = uLPVDepthPadded / full_z;
758 float z_offset = (uLPVDepthPadded * closest_pv * SH_MAX) / full_z;
759 float local_z = ((tex_local.z + 1.0 / float(uLPVDepthPadded)) / (1.0 + 2.0 / float(uLPVDepthPadded))) / SH_MAX / LPV_SLOTS + z_offset;
760 vec3 coord0 = vec3(tex_local.x, tex_local.y, local_z + grid_size_div * 0);
761 vec3 coord1 = vec3(tex_local.x, tex_local.y, local_z + grid_size_div * 1);
762 vec3 coord2 = vec3(tex_local.x, tex_local.y, local_z + grid_size_div * 2);
763 vec3 coord3 = vec3(tex_local.x, tex_local.y, local_z + grid_size_div * 3);
764 vec3 coord4 = vec3(tex_local.x, tex_local.y, local_z + grid_size_div * 4);
765 vec3 coord5 = vec3(tex_local.x, tex_local.y, local_z + grid_size_div * 5);
766 vec3 coord6 = vec3(tex_local.x, tex_local.y, local_z + grid_size_div * 6);
767 vec3 coord7 = vec3(tex_local.x, tex_local.y, local_z + grid_size_div * 7);
768 vec3 coord8 = vec3(tex_local.x, tex_local.y, local_z + grid_size_div * 8);
769 vec3 p0 = texture(probe_tex, coord0).rgb;
770 vec3 p1 = texture(probe_tex, coord1).rgb;
771 vec3 p2 = texture(probe_tex, coord2).rgb;
772 vec3 p3 = texture(probe_tex, coord3).rgb;
773 vec3 p4 = texture(probe_tex, coord4).rgb;
774 vec3 p5 = texture(probe_tex, coord5).rgb;
775 vec3 p6 = texture(probe_tex, coord6).rgb;
776 vec3 p7 = texture(probe_tex, coord7).rgb;
777 vec3 p8 = texture(probe_tex, coord8).rgb;
778
779
780 vec3 pc0 = p0 * 0.282095;
781 vec3 pc1 = p1 * 0.488603 * N.y;
782 vec3 pc2 = p2 * 0.488603 * N.z;
783 vec3 pc3 = p3 * 0.488603 * N.x;
784 vec3 pc4 = p4 * 1.092548 * N.x * N.y;
785 vec3 pc5 = p5 * 1.092548 * N.y * N.z;
786 vec3 pc6 = p6 * 0.315392 * (3 * (N.z * N.z) - 1);
787 vec3 pc7 = p7 * 1.092548 * (N.x * N.z);
788 vec3 pc8 = p8 * 0.546274 * (N.x * N.x - N.y * N.y);
789
790
791 irradiance = max(pc0 + pc1 + pc2 + pc3 + pc4 + pc5 + pc6 + pc7 + pc8, vec3(0.0));
792 }
793 else
794 {
795
796 vec4 lm = texture(uLightmap, LMUV);
797 vec4 dir_data = texture(uDirectionalmap, LMUV);
798
799 float focus_factor = dir_data.a;
800
801 vec3 dominant_dir = normalize(dir_data.xyz * 2.0f - 1.0f);
802
803 float ndotl = max(0.0, dot(N, dominant_dir));
804
805 float lam = dot(N, dominant_dir) * 0.5f + 0.5f;
806
807 ff = (lam / max(focus_factor, 0.00001f));
808 #ifdef LM_DIRECTIONAL
809 fragColor = vec4(ff, ff, ff, 1.0f);
810 return;
811 #endif
812 // ff = focus_factor;
813 // ff = mix(1.0, ndotl, focus_factor);
814 // ff = mix(1.0, lam, focus_factor);
815 // fragColor = vec4(dominant_dir, 1.0);
816 // return;
817 // irradiance = lm.rgb * ff;
818 irradiance = lm.rgb;
819 // irradiance = lm.rgb;
820
821 ao *= lm.a;
822
823 }
824 #ifdef AO
825 fragColor = vec4(ao, ao, ao, 1.0f);
826 return;
827 #endif
828
829 #ifdef LM_IRRADIANCE
830 fragColor = vec4(irradiance, 1.0f);
831 return;
832 #endif
833
834 const float PI = 3.14159265359;
835 float filtered_roughness = normal_filtering(N, roughness);
836
837 vec3 F0 = vec3(0.04);
838 F0 = mix(F0, albedo.rgb, metallic);
839
840 vec3 kS = fresnel_schlick_roughness(cosTheta, albedo.rgb, filtered_roughness, metallic);
841 vec3 kD_indirect = (vec3(1.0) - kS) * (1.0 - metallic);
842 vec3 indirect_diffuse = kD_indirect * irradiance * albedo.rgb;
843 #ifdef INDIRECT
844 fragColor = vec4(indirect_diffuse, 1.0f);
845 return;
846 #endif
847
848 const float MAX_REFLECTION_LOD = 4.0f;
849
850 float normal_blend = filtered_roughness;
851 vec3 parallax_N = normalize(mix(original_normal, N, clamp(normal_blend + 0.1, 0.2, 1.0)));
852
853 float temp_lod = 0;
854
855
856 vec3 rprobe_color = vec3(0.0);
857 vec2 brdf = texture(uBRDFLUT, vec2(cosTheta, filtered_roughness)).rg;
858 vec3 specular = vec3(0,0,0);
859 float spec_occ = calculate_specular_occlusion(cosTheta, ao, filtered_roughness);
860 if (probe_bake == 0)
861 {
862 rprobe_color = sample_weighted_rprobes(Pos, view_dir, N, filtered_roughness * MAX_REFLECTION_LOD);
863 #ifdef RPROBE
864 fragColor = vec4(rprobe_color, 1.0f);
865 return;
866 #endif
867
868 specular = rprobe_color * (F0 * brdf.x + brdf.y);
869 #ifdef SPECULAR
870 fragColor = vec4(specular, 1.0f);
871 return;
872 #endif
873 }
874 else
875 {
876 rprobe_color = vec3(0,0,0);
877
878
879
880 }
881 vec3 final_indirect = (indirect_diffuse + specular) * ao;
882
883 vec3 ambient = vec3(0.005, 0.005, 0.005);
884
885 vec3 ambient_light = ambient * albedo.rgb;
886 float z_scale = z_tile / 24.0f;
887 vec3 tiles = vec3(x_tile / 16.0f, y_tile /9.0f, depth / uFar);
888
889
890
891
892 fragColor = vec4(direct_diffuse + final_indirect + ambient_light + mat.emissive.rgb * emissive * mat.emissive_strength, albedo.a);
893 // fragColor = vec4(1,1,1,1);
894
895}
layout(local_size_x=16, local_size_y=16) in
void main()
out vec4 fragColor
Definition bloom.frag:6
const float weights[5]
vec3 rot(vec3 M[3], vec3 a)
Refer to Ken Perlin's 2 joint IK implmentation.
Definition camera.c:1004
float linearize(float z)
Definition coc.frag:30
uniform float uFar
uniform uvec3 uGridSize
uniform float uNear
uniform sampler2D uDepth
in vec3 Normal
in vec3 ViewDirTanSpace
in vec2 UV
flat in uint Mat
in vec3 ViewPos
const float height_scale
in mat3 TBN
in vec3 Pos
Definition dynamic.frag:199
uniform samplerCubeArray uRProbe
Definition dynamic.frag:222
in vec2 LMUV
Definition dynamic.frag:202
flat in uvec2 DirectionalLight
Definition dynamic.frag:209
uniform int index_offset
Definition dynamic.frag:234
uniform sampler2DArrayShadow uShadowmap
Definition dynamic.frag:218
float distribution_GGX(float NdotH, float roughness)
Definition dynamic.frag:354
float hash(vec3 p)
Definition dynamic.frag:444
vec3 fresnel_schlick(float CosTheta, vec3 F0)
Computes Schlick's approximation for Fresnel reflectance.
Definition dynamic.frag:369
float calculate_specular_occlusion(float NdotV, float ao, float roughness)
Definition dynamic.frag:536
uniform vec3 probe_origin
Definition dynamic.frag:231
uniform sampler2D uFBM
Definition dynamic.frag:226
uniform sampler2D uDirectionalmap
Definition dynamic.frag:217
in vec4 FragPosLightSpace[4]
Definition dynamic.frag:204
flat in uvec2 SpotLight
Definition dynamic.frag:210
vec3 get_parallax_corrected_vector(vec3 worldPos, vec3 viewDir, vec3 normal, vec3 boxMin, vec3 boxMax, vec3 probeCenter)
Definition dynamic.frag:311
uniform vec2 uScreenRes
Definition dynamic.frag:243
uniform sampler2D uSSAO
Definition dynamic.frag:227
uniform ivec3 probe_grid_size
Definition dynamic.frag:233
vec3 palette(in float t, in vec3 a, in vec3 b, in vec3 c, in vec3 d)
Definition dynamic.frag:440
float compute_probe_weight(vec3 world_pos, RProbe rp)
Definition dynamic.frag:450
float normal_filtering(vec3 N, float roughness)
Definition dynamic.frag:424
in vec3 Color
Definition dynamic.frag:203
vec3 fresnel_schlick_roughness(float cosTheta, vec3 albedo, float roughness, float metallic)
Definition dynamic.frag:377
vec3 calculate_light(vec3 N, vec3 V, vec3 L, vec3 albedo, float roughness, float metallic, vec3 light_color)
Definition dynamic.frag:394
uniform samplerCubeArrayShadow uPLSCMA
Definition dynamic.frag:223
uniform sampler3D probe_tex
Definition dynamic.frag:240
const vec3 DIELECTRIC_F0
Definition dynamic.frag:351
uniform sampler2D gAlbedo
Definition dynamic.frag:219
uniform sampler2D gNormal
Definition dynamic.frag:220
flat in uint Static
Definition dynamic.frag:212
in float TangentSign
Definition dynamic.frag:198
vec2 parallax_mapping(sampler2D normal_map, vec2 texCoords, vec3 viewDir, out float final_depth)
Definition dynamic.frag:253
float calculate_attenuation(float distance, float radius, float intensity)
Definition dynamic.frag:508
uniform sampler2D uBRDFLUT
Definition dynamic.frag:224
uniform sampler2D uLightmap
Definition dynamic.frag:216
vec3 sample_weighted_rprobes(vec3 pos, vec3 view_dir, vec3 normal, float roughness_lod)
Definition dynamic.frag:475
uniform uint uLPVDepthPadded
Definition dynamic.frag:239
uniform sampler2D gUV
Definition dynamic.frag:230
flat in ivec2 PointLight
Definition dynamic.frag:208
uniform float probe_spacing
Definition dynamic.frag:232
float visibility(float NdotV, float NdotL, float roughness)
Definition dynamic.frag:386
uniform int probe_bake
Definition dynamic.frag:235
uniform float uLogScale
Definition dynamic.frag:247
float shadow_calculation(uint layer, vec4 pos, float diff)
Definition dynamic.frag:331
in vec3 Tangent
Definition dynamic.frag:197
uint get_cluster_index()
Definition dynamic.frag:518
uniform int material
Definition dynamic.frag:215
uniform sampler2D gExtra
Definition dynamic.frag:221
uniform sampler2D uSSR
Definition dynamic.frag:229
#define LPV_SLOTS
#define PI
The metadata for the lightmap settings.
The GPU side SSBO structure.
The structure for RProbes sent to the GPU.
in vec2 uv
#define SH_MAX
vec3 pos_to_pv_local(OBB *obb, vec3 pos)
Converts a position to local OBB space.
Definition audio.c:920
float intensity
The intensity of the Light.
sampler2D albedo_tex
vec3 pos
Position of light.
vec3 extents
Definition dynamic.frag:23
uint32_t layer
Which layer a DIRECTIONAL_LIGHT corresponds to in the SHADOW_MAP_FBO.
LightType light_type
The type of Light.
float normal_scale
Unused - Used to determine how much of the texture normal to use.
sampler2D emissive_tex
vec3 center
Definition dynamic.frag:15
vec3 axis_z
Definition dynamic.frag:21
vec3 dir
Direction for lights that are not POINT_LIGHTs.
float inner_angle
Starting angle for SPOT_LIGHTs.
uint loaded
Definition dynamic.frag:16
vec3 center
int tri
The MeshFace this corresponds to.
float outer_angle
Outer angle for SPOT_LIGHTs.
uint32_t pad1
float outer_cos
float emissive_strength
vec3 axis_y
Definition dynamic.frag:19
uint32_t pad0
float radius
The radius of the Light.
int mesh
The MeshNode this corresponds to.
uint pad3
Definition dynamic.frag:22
sampler2D arm_tex
uint pad2
Definition dynamic.frag:20
vec3 axis_x
Definition dynamic.frag:17
uint pad4
Definition dynamic.frag:24
sampler2D normal_tex
uint pad1
Definition dynamic.frag:18
uint32_t pad2
AABB area
The physical area of AREA_LIGHTs.
uint32_t entity_id
Entity ID into ENTITIES.
float alpha_cutoff
Used to discard transparent Materials.
float absorption_coefficients[6]
How much audio is absorbed when hitting this Material.
float scattering_coefficients[6]
How much audio specularly reflects when hitting this Material.
GPU-side Clusters for clustered forward.
The CPU side structure for Lights.
The Materials parsed in from scene files that determine rendering pipeline setup, uploads,...
Oriented Bounding Boxes.
Definition dynamic.frag:14
The structure for ReflectionProbes padded for the GPU.
uniform samplerCube uSkybox
SDL_Window * window
Definition main.c:519
float clamp(float a, float b, float c)
Definition mathf.h:379
uniform mat4 projection
Definition shadow.vert:15
uniform mat4 view
Definition shadow.vert:16
float x
Definition types.h:11
float w
Definition types.h:13
@ D
Definition types.h:26
float x
Definition types.h:12
uint32_t x
Definition types.h:18
float y
Definition types.h:11
float y
Definition types.h:12
uint32_t z
Definition types.h:18
uint32_t y
Definition types.h:18
float z
Definition types.h:12
uint32_t x
Definition types.h:17
uint32_t y
Definition types.h:17
Definition types.h:14
Definition types.h:15
Definition types.h:16
Definition types.h:20
Definition types.h:21
Definition types.h:17
Definition types.h:18
Definition types.h:11
Definition types.h:12
Definition types.h:13
uniform vec3 color
Definition wireframe.frag:6