-
-
Notifications
You must be signed in to change notification settings - Fork 21.8k
/
Copy pathscene.glsl
2411 lines (1850 loc) · 73.7 KB
/
scene.glsl
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
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* clang-format off */
[vertex]
#if defined(IS_UBERSHADER)
uniform highp int ubershader_flags;
#endif
#define M_PI 3.14159265359
#define SHADER_IS_SRGB false
/*
from VisualServer:
ARRAY_VERTEX=0,
ARRAY_NORMAL=1,
ARRAY_TANGENT=2,
ARRAY_COLOR=3,
ARRAY_TEX_UV=4,
ARRAY_TEX_UV2=5,
ARRAY_BONES=6,
ARRAY_WEIGHTS=7,
ARRAY_INDEX=8,
*/
// hack to use uv if no uv present so it works with lightmap
/* INPUT ATTRIBS */
layout(location = 0) in highp vec4 vertex_attrib;
/* clang-format on */
#ifdef ENABLE_OCTAHEDRAL_COMPRESSION //ubershader-skip
layout(location = 2) in vec4 normal_tangent_attrib;
#else //ubershader-skip
layout(location = 1) in vec3 normal_attrib;
#endif //ubershader-skip
#if defined(ENABLE_TANGENT_INTERP) || defined(ENABLE_NORMALMAP) || defined(LIGHT_USE_ANISOTROPY)
#ifdef ENABLE_OCTAHEDRAL_COMPRESSION //ubershader-skip
// packed into normal_attrib zw component
#else //ubershader-skip
layout(location = 2) in vec4 normal_tangent_attrib; //ubershader-skip
#endif //ubershader-skip
#endif
#if defined(ENABLE_COLOR_INTERP)
layout(location = 3) in vec4 color_attrib;
#endif
#if defined(ENABLE_UV_INTERP)
layout(location = 4) in vec2 uv_attrib;
#endif
#if defined(ENABLE_UV2_INTERP)
layout(location = 5) in vec2 uv2_attrib;
#else
#ifdef USE_LIGHTMAP //ubershader-skip
layout(location = 5) in vec2 uv2_attrib;
#endif //ubershader-skip
#endif
#ifdef USE_SKELETON //ubershader-skip
layout(location = 6) in uvec4 bone_indices; // attrib:6
layout(location = 7) in highp vec4 bone_weights; // attrib:7
#endif //ubershader-skip
#ifdef USE_INSTANCING //ubershader-skip
layout(location = 8) in highp vec4 instance_xform0;
layout(location = 9) in highp vec4 instance_xform1;
layout(location = 10) in highp vec4 instance_xform2;
layout(location = 11) in lowp vec4 instance_color;
#if defined(ENABLE_INSTANCE_CUSTOM)
layout(location = 12) in highp vec4 instance_custom_data;
#endif
#endif //ubershader-skip
layout(std140) uniform SceneData { // ubo:0
highp mat4 projection_matrix;
highp mat4 inv_projection_matrix;
highp mat4 camera_inverse_matrix;
highp mat4 camera_matrix;
mediump vec4 ambient_light_color;
mediump vec4 bg_color;
mediump vec4 fog_color_enabled;
mediump vec4 fog_sun_color_amount;
mediump float ambient_energy;
mediump float bg_energy;
mediump float z_offset;
mediump float z_slope_scale;
highp float shadow_dual_paraboloid_render_zfar;
highp float shadow_dual_paraboloid_render_side;
highp vec2 viewport_size;
highp vec2 screen_pixel_size;
highp vec2 shadow_atlas_pixel_size;
highp vec2 directional_shadow_pixel_size;
highp float time;
highp float z_far;
mediump float reflection_multiplier;
mediump float subsurface_scatter_width;
mediump float ambient_occlusion_affect_light;
mediump float ambient_occlusion_affect_ao_channel;
mediump float opaque_prepass_threshold;
bool fog_depth_enabled;
highp float fog_depth_begin;
highp float fog_depth_end;
mediump float fog_density;
highp float fog_depth_curve;
bool fog_transmit_enabled;
highp float fog_transmit_curve;
bool fog_height_enabled;
highp float fog_height_min;
highp float fog_height_max;
highp float fog_height_curve;
int view_index;
};
uniform highp mat4 world_transform;
#ifdef USE_LIGHTMAP //ubershader-skip
uniform highp vec4 lightmap_uv_rect;
#endif //ubershader-skip
#ifdef USE_LIGHT_DIRECTIONAL //ubershader-skip
layout(std140) uniform DirectionalLightData { //ubo:3
highp vec4 light_pos_inv_radius;
mediump vec4 light_direction_attenuation;
mediump vec4 light_color_energy;
mediump vec4 light_params; // cone attenuation, angle, specular, shadow enabled,
mediump vec4 light_clamp;
mediump vec4 shadow_color_contact;
highp mat4 shadow_matrix1;
highp mat4 shadow_matrix2;
highp mat4 shadow_matrix3;
highp mat4 shadow_matrix4;
mediump vec4 shadow_split_offsets;
};
#endif //ubershader-skip
#ifdef USE_VERTEX_LIGHTING //ubershader-skip
//omni and spot
struct LightData {
highp vec4 light_pos_inv_radius;
mediump vec4 light_direction_attenuation;
mediump vec4 light_color_energy;
mediump vec4 light_params; // cone attenuation, angle, specular, shadow enabled,
mediump vec4 light_clamp;
mediump vec4 shadow_color_contact;
highp mat4 shadow_matrix;
};
layout(std140) uniform OmniLightData { //ubo:4
LightData omni_lights[MAX_LIGHT_DATA_STRUCTS];
};
layout(std140) uniform SpotLightData { //ubo:5
LightData spot_lights[MAX_LIGHT_DATA_STRUCTS];
};
#ifdef USE_FORWARD_LIGHTING //ubershader-skip
uniform int omni_light_indices[MAX_FORWARD_LIGHTS];
uniform int omni_light_count;
uniform int spot_light_indices[MAX_FORWARD_LIGHTS];
uniform int spot_light_count;
#endif //ubershader-skip
out vec4 diffuse_light_interp;
out vec4 specular_light_interp;
void light_compute(vec3 N, vec3 L, vec3 V, vec3 light_color, float roughness, inout vec3 diffuse, inout vec3 specular) {
float NdotL = dot(N, L);
float cNdotL = max(NdotL, 0.0); // clamped NdotL
float NdotV = dot(N, V);
float cNdotV = max(NdotV, 0.0);
#if defined(DIFFUSE_OREN_NAYAR)
vec3 diffuse_brdf_NL;
#else
float diffuse_brdf_NL; // BRDF times N.L for calculating diffuse radiance
#endif
#if defined(DIFFUSE_LAMBERT_WRAP)
// energy conserving lambert wrap shader
diffuse_brdf_NL = max(0.0, (NdotL + roughness) / ((1.0 + roughness) * (1.0 + roughness)));
#elif defined(DIFFUSE_OREN_NAYAR)
{
// see http://mimosa-pudica.net/improved-oren-nayar.html
float LdotV = dot(L, V);
float s = LdotV - NdotL * NdotV;
float t = mix(1.0, max(NdotL, NdotV), step(0.0, s));
float sigma2 = roughness * roughness; // TODO: this needs checking
vec3 A = 1.0 + sigma2 * (-0.5 / (sigma2 + 0.33) + 0.17 * diffuse / (sigma2 + 0.13));
float B = 0.45 * sigma2 / (sigma2 + 0.09);
diffuse_brdf_NL = cNdotL * (A + vec3(B) * s / t) * (1.0 / M_PI);
}
#else
// lambert by default for everything else
diffuse_brdf_NL = cNdotL * (1.0 / M_PI);
#endif
diffuse += light_color * diffuse_brdf_NL;
if (roughness > 0.0) {
// D
float specular_brdf_NL = 0.0;
#if !defined(SPECULAR_DISABLED)
//normalized blinn always unless disabled
vec3 H = normalize(V + L);
float cNdotH = max(dot(N, H), 0.0);
float shininess = exp2(15.0 * (1.0 - roughness) + 1.0) * 0.25;
float blinn = pow(cNdotH, shininess);
blinn *= (shininess + 2.0) * (1.0 / (8.0 * M_PI));
specular_brdf_NL = blinn;
#endif
specular += specular_brdf_NL * light_color;
}
}
#ifdef USE_PHYSICAL_LIGHT_ATTENUATION
float get_omni_attenuation(float distance, float inv_range, float decay) {
float nd = distance * inv_range;
nd *= nd;
nd *= nd; // nd^4
nd = max(1.0 - nd, 0.0);
nd *= nd; // nd^2
return nd * pow(max(distance, 0.0001), -decay);
}
#endif
void light_process_omni(int idx, vec3 vertex, vec3 eye_vec, vec3 normal, float roughness, inout vec3 diffuse, inout vec3 specular) {
vec3 light_rel_vec = omni_lights[idx].light_pos_inv_radius.xyz - vertex;
float light_length = length(light_rel_vec);
#ifdef USE_PHYSICAL_LIGHT_ATTENUATION
vec3 light_attenuation = vec3(get_omni_attenuation(light_length, omni_lights[idx].light_pos_inv_radius.w, omni_lights[idx].light_direction_attenuation.w));
#else
float normalized_distance = light_length * omni_lights[idx].light_pos_inv_radius.w;
vec3 light_attenuation = vec3(pow(max(1.0 - normalized_distance, 0.0), omni_lights[idx].light_direction_attenuation.w));
#endif
light_compute(normal, normalize(light_rel_vec), eye_vec, omni_lights[idx].light_color_energy.rgb * light_attenuation, roughness, diffuse, specular);
}
void light_process_spot(int idx, vec3 vertex, vec3 eye_vec, vec3 normal, float roughness, inout vec3 diffuse, inout vec3 specular) {
vec3 light_rel_vec = spot_lights[idx].light_pos_inv_radius.xyz - vertex;
float light_length = length(light_rel_vec);
#ifdef USE_PHYSICAL_LIGHT_ATTENUATION
vec3 light_attenuation = vec3(get_omni_attenuation(light_length, spot_lights[idx].light_pos_inv_radius.w, spot_lights[idx].light_direction_attenuation.w));
#else
float normalized_distance = light_length * spot_lights[idx].light_pos_inv_radius.w;
vec3 light_attenuation = vec3(pow(max(1.0 - normalized_distance, 0.001), spot_lights[idx].light_direction_attenuation.w));
#endif
vec3 spot_dir = spot_lights[idx].light_direction_attenuation.xyz;
float spot_cutoff = spot_lights[idx].light_params.y;
float scos = max(dot(-normalize(light_rel_vec), spot_dir), spot_cutoff);
float spot_rim = (1.0 - scos) / (1.0 - spot_cutoff);
light_attenuation *= 1.0 - pow(max(spot_rim, 0.001), spot_lights[idx].light_params.x);
light_compute(normal, normalize(light_rel_vec), eye_vec, spot_lights[idx].light_color_energy.rgb * light_attenuation, roughness, diffuse, specular);
}
#endif //ubershader-skip
#ifdef ENABLE_OCTAHEDRAL_COMPRESSION //ubershader-skip
vec3 oct_to_vec3(vec2 e) {
vec3 v = vec3(e.xy, 1.0 - abs(e.x) - abs(e.y));
float t = max(-v.z, 0.0);
v.xy += t * -sign(v.xy);
return normalize(v);
}
#endif //ubershader-skip
/* Varyings */
out highp vec3 vertex_interp;
out vec3 normal_interp;
#if defined(ENABLE_COLOR_INTERP)
out vec4 color_interp;
#endif
#if defined(ENABLE_UV_INTERP)
out vec2 uv_interp;
#endif
#if defined(ENABLE_UV2_INTERP)
out vec2 uv2_interp;
#else
#ifdef USE_LIGHTMAP //ubershader-skip
out vec2 uv2_interp;
#endif //ubershader-skip
#endif
#if defined(ENABLE_TANGENT_INTERP) || defined(ENABLE_NORMALMAP) || defined(LIGHT_USE_ANISOTROPY)
out vec3 tangent_interp;
out vec3 binormal_interp;
#endif
#if defined(USE_MATERIAL)
/* clang-format off */
layout(std140) uniform UniformData { // ubo:1
MATERIAL_UNIFORMS
};
/* clang-format on */
#endif
/* clang-format off */
VERTEX_SHADER_GLOBALS
/* clang-format on */
#ifdef RENDER_DEPTH_DUAL_PARABOLOID //ubershader-skip
out highp float dp_clip;
#endif //ubershader-skip
#define SKELETON_TEXTURE_WIDTH 256
#ifdef USE_SKELETON //ubershader-skip
uniform highp sampler2D skeleton_texture; // texunit:-1
#endif //ubershader-skip
out highp vec4 position_interp;
// FIXME: This triggers a Mesa bug that breaks rendering, so disabled for now.
// See GH-13450 and https://bugs.freedesktop.org/show_bug.cgi?id=100316
//invariant gl_Position;
void main() {
highp vec4 vertex = vertex_attrib; // vec4(vertex_attrib.xyz * data_attrib.x,1.0);
highp mat4 world_matrix = world_transform;
#ifdef USE_INSTANCING //ubershader-runtime
{
highp mat4 m = mat4(instance_xform0, instance_xform1, instance_xform2, vec4(0.0, 0.0, 0.0, 1.0));
world_matrix = world_matrix * transpose(m);
}
#endif //ubershader-runtime
vec3 normal;
#ifdef ENABLE_OCTAHEDRAL_COMPRESSION //ubershader-runtime
normal = oct_to_vec3(normal_tangent_attrib.xy);
#else //ubershader-runtime
normal = normal_attrib;
#endif //ubershader-runtime
#if defined(ENABLE_TANGENT_INTERP) || defined(ENABLE_NORMALMAP) || defined(LIGHT_USE_ANISOTROPY)
vec3 tangent;
float binormalf;
#ifdef ENABLE_OCTAHEDRAL_COMPRESSION //ubershader-runtime
tangent = oct_to_vec3(vec2(normal_tangent_attrib.z, abs(normal_tangent_attrib.w) * 2.0 - 1.0));
binormalf = sign(normal_tangent_attrib.w);
#else //ubershader-runtime
tangent = normal_tangent_attrib.xyz;
binormalf = normal_tangent_attrib.a;
#endif //ubershader-runtime
#endif
#if defined(ENABLE_COLOR_INTERP)
color_interp = color_attrib;
#ifdef USE_INSTANCING //ubershader-runtime
color_interp *= instance_color;
#endif //ubershader-runtime
#endif
#if defined(ENABLE_TANGENT_INTERP) || defined(ENABLE_NORMALMAP) || defined(LIGHT_USE_ANISOTROPY)
vec3 binormal = normalize(cross(normal, tangent) * binormalf);
#endif
#if defined(ENABLE_UV_INTERP)
uv_interp = uv_attrib;
#endif
#ifdef USE_LIGHTMAP //ubershader-runtime
uv2_interp = lightmap_uv_rect.zw * uv2_attrib + lightmap_uv_rect.xy;
#else //ubershader-runtime
#if defined(ENABLE_UV2_INTERP)
uv2_interp = uv2_attrib;
#endif
#endif //ubershader-runtime
#if defined(OVERRIDE_POSITION)
highp vec4 position;
#endif
vec4 instance_custom;
#ifdef USE_INSTANCING //ubershader-runtime
#if defined(ENABLE_INSTANCE_CUSTOM)
instance_custom = instance_custom_data;
#else
instance_custom = vec4(0.0);
#endif
#else //ubershader-runtime
instance_custom = vec4(0.0);
#endif //ubershader-runtime
highp mat4 local_projection = projection_matrix;
//using world coordinates
#if !defined(SKIP_TRANSFORM_USED) && defined(VERTEX_WORLD_COORDS_USED)
vertex = world_matrix * vertex;
#if defined(ENSURE_CORRECT_NORMALS)
mat3 normal_matrix = mat3(transpose(inverse(world_matrix)));
normal = normal_matrix * normal;
#else
normal = normalize((world_matrix * vec4(normal, 0.0)).xyz);
#endif
#if defined(ENABLE_TANGENT_INTERP) || defined(ENABLE_NORMALMAP) || defined(LIGHT_USE_ANISOTROPY)
tangent = normalize((world_matrix * vec4(tangent, 0.0)).xyz);
binormal = normalize((world_matrix * vec4(binormal, 0.0)).xyz);
#endif
#endif
float roughness = 1.0;
//defines that make writing custom shaders easier
#define projection_matrix local_projection
#define world_transform world_matrix
#ifdef USE_SKELETON //ubershader-runtime
{
//skeleton transform
ivec4 bone_indicesi = ivec4(bone_indices); // cast to signed int
ivec2 tex_ofs = ivec2(bone_indicesi.x % 256, (bone_indicesi.x / 256) * 3);
highp mat4 m;
m = mat4(
texelFetch(skeleton_texture, tex_ofs, 0),
texelFetch(skeleton_texture, tex_ofs + ivec2(0, 1), 0),
texelFetch(skeleton_texture, tex_ofs + ivec2(0, 2), 0),
vec4(0.0, 0.0, 0.0, 1.0)) *
bone_weights.x;
tex_ofs = ivec2(bone_indicesi.y % 256, (bone_indicesi.y / 256) * 3);
m += mat4(
texelFetch(skeleton_texture, tex_ofs, 0),
texelFetch(skeleton_texture, tex_ofs + ivec2(0, 1), 0),
texelFetch(skeleton_texture, tex_ofs + ivec2(0, 2), 0),
vec4(0.0, 0.0, 0.0, 1.0)) *
bone_weights.y;
tex_ofs = ivec2(bone_indicesi.z % 256, (bone_indicesi.z / 256) * 3);
m += mat4(
texelFetch(skeleton_texture, tex_ofs, 0),
texelFetch(skeleton_texture, tex_ofs + ivec2(0, 1), 0),
texelFetch(skeleton_texture, tex_ofs + ivec2(0, 2), 0),
vec4(0.0, 0.0, 0.0, 1.0)) *
bone_weights.z;
tex_ofs = ivec2(bone_indicesi.w % 256, (bone_indicesi.w / 256) * 3);
m += mat4(
texelFetch(skeleton_texture, tex_ofs, 0),
texelFetch(skeleton_texture, tex_ofs + ivec2(0, 1), 0),
texelFetch(skeleton_texture, tex_ofs + ivec2(0, 2), 0),
vec4(0.0, 0.0, 0.0, 1.0)) *
bone_weights.w;
world_matrix = world_matrix * transpose(m);
}
#endif //ubershader-runtime
float point_size = 1.0;
highp mat4 modelview = camera_inverse_matrix * world_matrix;
{
/* clang-format off */
VERTEX_SHADER_CODE
/* clang-format on */
}
gl_PointSize = point_size;
// using local coordinates (default)
#if !defined(SKIP_TRANSFORM_USED) && !defined(VERTEX_WORLD_COORDS_USED)
vertex = modelview * vertex;
#if defined(ENSURE_CORRECT_NORMALS)
mat3 normal_matrix = mat3(transpose(inverse(modelview)));
normal = normal_matrix * normal;
#else
normal = normalize((modelview * vec4(normal, 0.0)).xyz);
#endif
#if defined(ENABLE_TANGENT_INTERP) || defined(ENABLE_NORMALMAP) || defined(LIGHT_USE_ANISOTROPY)
tangent = normalize((modelview * vec4(tangent, 0.0)).xyz);
binormal = normalize((modelview * vec4(binormal, 0.0)).xyz);
#endif
#endif
//using world coordinates
#if !defined(SKIP_TRANSFORM_USED) && defined(VERTEX_WORLD_COORDS_USED)
vertex = camera_inverse_matrix * vertex;
normal = normalize((camera_inverse_matrix * vec4(normal, 0.0)).xyz);
#if defined(ENABLE_TANGENT_INTERP) || defined(ENABLE_NORMALMAP) || defined(LIGHT_USE_ANISOTROPY)
tangent = normalize((camera_inverse_matrix * vec4(tangent, 0.0)).xyz);
binormal = normalize((camera_inverse_matrix * vec4(binormal, 0.0)).xyz);
#endif
#endif
vertex_interp = vertex.xyz;
normal_interp = normal;
#if defined(ENABLE_TANGENT_INTERP) || defined(ENABLE_NORMALMAP) || defined(LIGHT_USE_ANISOTROPY)
tangent_interp = tangent;
binormal_interp = binormal;
#endif
#ifdef RENDER_DEPTH //ubershader-runtime
#ifdef RENDER_DEPTH_DUAL_PARABOLOID //ubershader-runtime
vertex_interp.z *= shadow_dual_paraboloid_render_side;
normal_interp.z *= shadow_dual_paraboloid_render_side;
dp_clip = vertex_interp.z; //this attempts to avoid noise caused by objects sent to the other parabolloid side due to bias
//for dual paraboloid shadow mapping, this is the fastest but least correct way, as it curves straight edges
highp vec3 vtx = vertex_interp + normalize(vertex_interp) * z_offset;
highp float distance = length(vtx);
vtx = normalize(vtx);
vtx.xy /= 1.0 - vtx.z;
vtx.z = (distance / shadow_dual_paraboloid_render_zfar);
vtx.z = vtx.z * 2.0 - 1.0;
vertex_interp = vtx;
#else //ubershader-runtime
float z_ofs = z_offset;
z_ofs += (1.0 - abs(normal_interp.z)) * z_slope_scale;
vertex_interp.z -= z_ofs;
#endif //RENDER_DEPTH_DUAL_PARABOLOID //ubershader-runtime
#endif //RENDER_DEPTH //ubershader-runtime
#if defined(OVERRIDE_POSITION)
gl_Position = position;
#else
gl_Position = projection_matrix * vec4(vertex_interp, 1.0);
#endif
position_interp = gl_Position;
#ifdef USE_VERTEX_LIGHTING //ubershader-runtime
diffuse_light_interp = vec4(0.0);
specular_light_interp = vec4(0.0);
#ifdef USE_FORWARD_LIGHTING //ubershader-runtime
for (int i = 0; i < omni_light_count; i++) {
light_process_omni(omni_light_indices[i], vertex_interp, -normalize(vertex_interp), normal_interp, roughness, diffuse_light_interp.rgb, specular_light_interp.rgb);
}
for (int i = 0; i < spot_light_count; i++) {
light_process_spot(spot_light_indices[i], vertex_interp, -normalize(vertex_interp), normal_interp, roughness, diffuse_light_interp.rgb, specular_light_interp.rgb);
}
#endif //ubershader-runtime
#ifdef USE_LIGHT_DIRECTIONAL //ubershader-runtime
vec3 directional_diffuse = vec3(0.0);
vec3 directional_specular = vec3(0.0);
light_compute(normal_interp, -light_direction_attenuation.xyz, -normalize(vertex_interp), light_color_energy.rgb, roughness, directional_diffuse, directional_specular);
float diff_avg = dot(diffuse_light_interp.rgb, vec3(0.33333));
float diff_dir_avg = dot(directional_diffuse, vec3(0.33333));
if (diff_avg > 0.0) {
diffuse_light_interp.a = diff_dir_avg / (diff_avg + diff_dir_avg);
} else {
diffuse_light_interp.a = 1.0;
}
diffuse_light_interp.rgb += directional_diffuse;
float spec_avg = dot(specular_light_interp.rgb, vec3(0.33333));
float spec_dir_avg = dot(directional_specular, vec3(0.33333));
if (spec_avg > 0.0) {
specular_light_interp.a = spec_dir_avg / (spec_avg + spec_dir_avg);
} else {
specular_light_interp.a = 1.0;
}
specular_light_interp.rgb += directional_specular;
#endif //USE_LIGHT_DIRECTIONAL //ubershader-runtime
#endif // USE_VERTEX_LIGHTING //ubershader-runtime
}
/* clang-format off */
[fragment]
#if defined(IS_UBERSHADER)
uniform highp int ubershader_flags;
// These are more performant and make the ubershaderification simpler
#define VCT_QUALITY_HIGH
#define USE_LIGHTMAP_FILTER_BICUBIC
#endif
/* texture unit usage, N is max_texture_unity-N
1-skeleton
2-radiance
3-radiance_array
4-reflection_atlas
5-directional_shadow
6-shadow_atlas
7-irradiance
8-screen
9-depth
10-probe1, lightmap
11-probe2, lightmap_array
*/
uniform highp mat4 world_transform;
/* clang-format on */
#define M_PI 3.14159265359
#define SHADER_IS_SRGB false
/* Varyings */
#if defined(ENABLE_COLOR_INTERP)
in vec4 color_interp;
#endif
#if defined(ENABLE_UV_INTERP)
in vec2 uv_interp;
#endif
#if defined(ENABLE_UV2_INTERP)
in vec2 uv2_interp;
#else
#ifdef USE_LIGHTMAP //ubershader-skip
in vec2 uv2_interp;
#endif //ubershader-skip
#endif
#if defined(ENABLE_TANGENT_INTERP) || defined(ENABLE_NORMALMAP) || defined(LIGHT_USE_ANISOTROPY)
in vec3 tangent_interp;
in vec3 binormal_interp;
#endif
in highp vec3 vertex_interp;
in vec3 normal_interp;
/* PBR CHANNELS */
#ifdef USE_RADIANCE_MAP //ubershader-skip
layout(std140) uniform Radiance { // ubo:2
mat4 radiance_inverse_xform;
float radiance_ambient_contribution;
};
#define RADIANCE_MAX_LOD 5.0
uniform sampler2D irradiance_map; // texunit:-7
#ifdef USE_RADIANCE_MAP_ARRAY //ubershader-skip
uniform sampler2DArray radiance_map_array; // texunit:-3
vec3 textureDualParaboloidArray(sampler2DArray p_tex, vec3 p_vec, float p_roughness) {
vec3 norm = normalize(p_vec);
norm.xy /= 1.0 + abs(norm.z);
norm.xy = norm.xy * vec2(0.5, 0.25) + vec2(0.5, 0.25);
// we need to lie the derivatives (normg) and assume that DP side is always the same
// to get proper texture filtering
vec2 normg = norm.xy;
if (norm.z > 0.0) {
norm.y = 0.5 - norm.y + 0.5;
}
// thanks to OpenGL spec using floor(layer + 0.5) for texture arrays,
// it's easy to have precision errors using fract() to interpolate layers
// as such, using fixed point to ensure it works.
float index = p_roughness * RADIANCE_MAX_LOD;
int indexi = int(index * 256.0);
vec3 base = textureGrad(p_tex, vec3(norm.xy, float(indexi / 256)), dFdx(normg), dFdy(normg)).xyz;
vec3 next = textureGrad(p_tex, vec3(norm.xy, float(indexi / 256 + 1)), dFdx(normg), dFdy(normg)).xyz;
return mix(base, next, float(indexi % 256) / 256.0);
}
#else //ubershader-skip
uniform sampler2D radiance_map; // texunit:-2
vec3 textureDualParaboloid(sampler2D p_tex, vec3 p_vec, float p_roughness) {
vec3 norm = normalize(p_vec);
norm.xy /= 1.0 + abs(norm.z);
norm.xy = norm.xy * vec2(0.5, 0.25) + vec2(0.5, 0.25);
if (norm.z > 0.0) {
norm.y = 0.5 - norm.y + 0.5;
}
return textureLod(p_tex, norm.xy, p_roughness * RADIANCE_MAX_LOD).xyz;
}
#endif //ubershader-skip
#endif //ubershader-skip
/* Material Uniforms */
#if defined(USE_MATERIAL)
/* clang-format off */
layout(std140) uniform UniformData {
MATERIAL_UNIFORMS
};
/* clang-format on */
#endif
layout(std140) uniform SceneData {
highp mat4 projection_matrix;
highp mat4 inv_projection_matrix;
highp mat4 camera_inverse_matrix;
highp mat4 camera_matrix;
mediump vec4 ambient_light_color;
mediump vec4 bg_color;
mediump vec4 fog_color_enabled;
mediump vec4 fog_sun_color_amount;
mediump float ambient_energy;
mediump float bg_energy;
mediump float z_offset;
mediump float z_slope_scale;
highp float shadow_dual_paraboloid_render_zfar;
highp float shadow_dual_paraboloid_render_side;
highp vec2 viewport_size;
highp vec2 screen_pixel_size;
highp vec2 shadow_atlas_pixel_size;
highp vec2 directional_shadow_pixel_size;
highp float time;
highp float z_far;
mediump float reflection_multiplier;
mediump float subsurface_scatter_width;
mediump float ambient_occlusion_affect_light;
mediump float ambient_occlusion_affect_ao_channel;
mediump float opaque_prepass_threshold;
bool fog_depth_enabled;
highp float fog_depth_begin;
highp float fog_depth_end;
mediump float fog_density;
highp float fog_depth_curve;
bool fog_transmit_enabled;
highp float fog_transmit_curve;
bool fog_height_enabled;
highp float fog_height_min;
highp float fog_height_max;
highp float fog_height_curve;
int view_index;
};
/* clang-format off */
FRAGMENT_SHADER_GLOBALS
/* clang-format on */
//directional light data
#ifdef USE_LIGHT_DIRECTIONAL //ubershader-skip
layout(std140) uniform DirectionalLightData {
highp vec4 light_pos_inv_radius;
mediump vec4 light_direction_attenuation;
mediump vec4 light_color_energy;
mediump vec4 light_params; // cone attenuation, angle, specular, shadow enabled,
mediump vec4 light_clamp;
mediump vec4 shadow_color_contact;
highp mat4 shadow_matrix1;
highp mat4 shadow_matrix2;
highp mat4 shadow_matrix3;
highp mat4 shadow_matrix4;
mediump vec4 shadow_split_offsets;
};
uniform highp sampler2DShadow directional_shadow; // texunit:-5
#endif //ubershader-skip
#ifdef USE_VERTEX_LIGHTING //ubershader-skip
in vec4 diffuse_light_interp;
in vec4 specular_light_interp;
#endif //ubershader-skip
// omni and spot
struct LightData {
highp vec4 light_pos_inv_radius;
mediump vec4 light_direction_attenuation;
mediump vec4 light_color_energy;
mediump vec4 light_params; // cone attenuation, angle, specular, shadow enabled,
mediump vec4 light_clamp;
mediump vec4 shadow_color_contact;
highp mat4 shadow_matrix;
};
layout(std140) uniform OmniLightData { // ubo:4
LightData omni_lights[MAX_LIGHT_DATA_STRUCTS];
};
layout(std140) uniform SpotLightData { // ubo:5
LightData spot_lights[MAX_LIGHT_DATA_STRUCTS];
};
uniform highp sampler2DShadow shadow_atlas; // texunit:-6
struct ReflectionData {
mediump vec4 box_extents;
mediump vec4 box_offset;
mediump vec4 params; // intensity, 0, interior , boxproject
mediump vec4 ambient; // ambient color, energy
mediump vec4 atlas_clamp;
highp mat4 local_matrix; // up to here for spot and omni, rest is for directional
// notes: for ambientblend, use distance to edge to blend between already existing global environment
};
layout(std140) uniform ReflectionProbeData { //ubo:6
ReflectionData reflections[MAX_REFLECTION_DATA_STRUCTS];
};
uniform mediump sampler2D reflection_atlas; // texunit:-4
#ifdef USE_FORWARD_LIGHTING //ubershader-skip
uniform int omni_light_indices[MAX_FORWARD_LIGHTS];
uniform int omni_light_count;
uniform int spot_light_indices[MAX_FORWARD_LIGHTS];
uniform int spot_light_count;
uniform int reflection_indices[MAX_FORWARD_LIGHTS];
uniform int reflection_count;
#endif //ubershader-skip
#if defined(SCREEN_TEXTURE_USED)
uniform highp sampler2D screen_texture; // texunit:-8
#endif
layout(location = 0) out vec4 frag_color;
#ifdef USE_MULTIPLE_RENDER_TARGETS //ubershader-skip
#define diffuse_buffer frag_color
layout(location = 1) out vec4 specular_buffer;
layout(location = 2) out vec4 normal_mr_buffer;
#if defined(ENABLE_SSS)
layout(location = 3) out float sss_buffer;
#endif
#endif //ubershader-skip
in highp vec4 position_interp;
uniform highp sampler2D depth_buffer; // texunit:-9
#ifdef USE_CONTACT_SHADOWS //ubershader-skip
float contact_shadow_compute(vec3 pos, vec3 dir, float max_distance) {
if (abs(dir.z) > 0.99)
return 1.0;
vec3 endpoint = pos + dir * max_distance;
vec4 source = position_interp;
vec4 dest = projection_matrix * vec4(endpoint, 1.0);
vec2 from_screen = (source.xy / source.w) * 0.5 + 0.5;
vec2 to_screen = (dest.xy / dest.w) * 0.5 + 0.5;
vec2 screen_rel = to_screen - from_screen;
if (length(screen_rel) < 0.00001)
return 1.0; // too small, don't do anything
/*
float pixel_size; // approximate pixel size
if (screen_rel.x > screen_rel.y) {
pixel_size = abs((pos.x - endpoint.x) / (screen_rel.x / screen_pixel_size.x));
} else {
pixel_size = abs((pos.y - endpoint.y) / (screen_rel.y / screen_pixel_size.y));
}
*/
vec4 bias = projection_matrix * vec4(pos + vec3(0.0, 0.0, max_distance * 0.5), 1.0);
vec2 pixel_incr = normalize(screen_rel) * screen_pixel_size;
float steps = length(screen_rel) / length(pixel_incr);
steps = min(2000.0, steps); // put a limit to avoid freezing in some strange situation
//steps = 10.0;
vec4 incr = (dest - source) / steps;
float ratio = 0.0;
float ratio_incr = 1.0 / steps;
while (steps > 0.0) {
source += incr * 2.0;
bias += incr * 2.0;
vec3 uv_depth = (source.xyz / source.w) * 0.5 + 0.5;
if (uv_depth.x > 0.0 && uv_depth.x < 1.0 && uv_depth.y > 0.0 && uv_depth.y < 1.0) {
float depth = texture(depth_buffer, uv_depth.xy).r;
if (depth < uv_depth.z) {
if (depth > (bias.z / bias.w) * 0.5 + 0.5) {
return min(pow(ratio, 4.0), 1.0);
} else {
return 1.0;
}
}
ratio += ratio_incr;
steps -= 1.0;
} else {
return 1.0;
}
}
return 1.0;
}
#endif //ubershader-skip
// This returns the G_GGX function divided by 2 cos_theta_m, where in practice cos_theta_m is either N.L or N.V.
// We're dividing this factor off because the overall term we'll end up looks like
// (see, for example, the first unnumbered equation in B. Burley, "Physically Based Shading at Disney", SIGGRAPH 2012):