Debian Codegraph @

drivers/gles3/shaders/scene.glsl

raw
1 /* clang-format off */
2 #[modes]
3
4 mode_color =
5 mode_color_instancing = \n#define USE_INSTANCING
6 mode_depth = #define MODE_RENDER_DEPTH
7 mode_depth_instancing = #define MODE_RENDER_DEPTH \n#define USE_INSTANCING
8
9 #[specializations]
10
11 DISABLE_LIGHTMAP = false
12 DISABLE_LIGHT_DIRECTIONAL = false
13 DISABLE_LIGHT_OMNI = false
14 DISABLE_LIGHT_SPOT = false
15 DISABLE_REFLECTION_PROBE = true
16 DISABLE_FOG = false
17 USE_DEPTH_FOG = false
18 USE_SUN_SCATTER = false
19 USE_RADIANCE_MAP = true
20 USE_LIGHTMAP = false
21 USE_SH_LIGHTMAP = false
22 USE_LIGHTMAP_CAPTURE = false
23 USE_MULTIVIEW = false
24 RENDER_SHADOWS = false
25 RENDER_SHADOWS_LINEAR = false
26 SHADOW_MODE_PCF_5 = false
27 SHADOW_MODE_PCF_13 = false
28 LIGHT_USE_PSSM2 = false
29 LIGHT_USE_PSSM4 = false
30 LIGHT_USE_PSSM_BLEND = false
31 BASE_PASS = true
32 USE_ADDITIVE_LIGHTING = false
33 APPLY_TONEMAPPING = true
34 // We can only use one type of light per additive pass. This means that if USE_ADDITIVE_LIGHTING is defined, and
35 // these are false, we are doing a directional light pass.
36 ADDITIVE_OMNI = false
37 ADDITIVE_SPOT = false
38 RENDER_MATERIAL = false
39 SECOND_REFLECTION_PROBE = false
40 LIGHTMAP_BICUBIC_FILTER = false
41 RENDER_MOTION_VECTORS = false
42
43
44 #[vertex]
45
46 #define M_PI 3.14159265359
47
48 #define SHADER_IS_SRGB true
49 #define SHADER_SPACE_FAR -1.0
50
51 #ifdef USE_MULTIVIEW
52 #define OUTPUT_IS_MULTIVIEW true
53 #else
54 #define OUTPUT_IS_MULTIVIEW false
55 #endif
56
57 #if defined(RENDER_SHADOWS) || defined(RENDER_SHADOWS_LINEAR)
58 #define IN_SHADOW_PASS true
59 #else
60 #define IN_SHADOW_PASS false
61 #endif
62
63 #include "stdlib_inc.glsl"
64
65 #if !defined(MODE_RENDER_DEPTH) || defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED) ||defined(LIGHT_CLEARCOAT_USED)
66 #ifndef NORMAL_USED
67 #define NORMAL_USED
68 #endif
69 #endif
70
71 #ifdef MODE_UNSHADED
72 #ifdef USE_ADDITIVE_LIGHTING
73 #undef USE_ADDITIVE_LIGHTING
74 #endif
75 #endif // MODE_UNSHADED
76
77 /*
78 from RenderingServer:
79 ARRAY_VERTEX = 0, // RGB32F or RGBA16
80 ARRAY_NORMAL = 1, // RG16 octahedral compression or RGBA16 normal + angle
81 ARRAY_TANGENT = 2, // RG16 octahedral compression, sign stored in sign of G
82 ARRAY_COLOR = 3, // RGBA8
83 ARRAY_TEX_UV = 4, // RG32F
84 ARRAY_TEX_UV2 = 5, // RG32F
85 ARRAY_CUSTOM0 = 6, // Depends on ArrayCustomFormat.
86 ARRAY_CUSTOM1 = 7,
87 ARRAY_CUSTOM2 = 8,
88 ARRAY_CUSTOM3 = 9,
89 ARRAY_BONES = 10, // RGBA16UI (x2 if 8 weights)
90 ARRAY_WEIGHTS = 11, // RGBA16UNORM (x2 if 8 weights)
91 */
92
93 /* INPUT ATTRIBS */
94
95 // Always contains vertex position in XYZ, can contain tangent angle in W.
96 layout(location = 0) in highp vec4 vertex_angle_attrib;
97 /* clang-format on */
98
99 #ifdef NORMAL_USED
100 // Contains Normal/Axis in RG, can contain tangent in BA.
101 layout(location = 1) in vec4 axis_tangent_attrib;
102 #endif
103
104 // location 2 is unused.
105
106 #if defined(COLOR_USED)
107 layout(location = 3) in vec4 color_attrib;
108 #endif
109
110 #ifdef UV_USED
111 layout(location = 4) in vec2 uv_attrib;
112 #endif
113
114 #if defined(UV2_USED) || defined(USE_LIGHTMAP) || defined(RENDER_MATERIAL)
115 layout(location = 5) in vec2 uv2_attrib;
116 #endif
117
118 #if defined(CUSTOM0_USED)
119 layout(location = 6) in vec4 custom0_attrib;
120 #endif
121
122 #if defined(CUSTOM1_USED)
123 layout(location = 7) in vec4 custom1_attrib;
124 #endif
125
126 #if defined(CUSTOM2_USED)
127 layout(location = 8) in vec4 custom2_attrib;
128 #endif
129
130 #if defined(CUSTOM3_USED)
131 layout(location = 9) in vec4 custom3_attrib;
132 #endif
133
134 #if defined(BONES_USED)
135 layout(location = 10) in uvec4 bone_attrib;
136 #endif
137
138 #if defined(WEIGHTS_USED)
139 layout(location = 11) in vec4 weight_attrib;
140 #endif
141
142 vec3 oct_to_vec3(vec2 e) {
143 vec3 v = vec3(e.xy, 1.0 - abs(e.x) - abs(e.y));
144 float t = max(-v.z, 0.0);
145 v.xy += t * -sign(v.xy);
146 return normalize(v);
147 }
148
149 void axis_angle_to_tbn(vec3 axis, float angle, out vec3 tangent, out vec3 binormal, out vec3 normal) {
150 float c = cos(angle);
151 float s = sin(angle);
152 vec3 omc_axis = (1.0 - c) * axis;
153 vec3 s_axis = s * axis;
154 tangent = omc_axis.xxx * axis + vec3(c, -s_axis.z, s_axis.y);
155 binormal = omc_axis.yyy * axis + vec3(s_axis.z, c, -s_axis.x);
156 normal = omc_axis.zzz * axis + vec3(-s_axis.y, s_axis.x, c);
157 }
158
159 #ifdef USE_INSTANCING
160 layout(location = 12) in highp vec4 instance_xform0;
161 layout(location = 13) in highp vec4 instance_xform1;
162 layout(location = 14) in highp vec4 instance_xform2;
163 layout(location = 15) in highp uvec4 instance_color_custom_data; // Color packed into xy, Custom data into zw.
164 #endif
165
166 #if defined(RENDER_MOTION_VECTORS)
167 layout(location = 16) in highp vec4 prev_vertex_attrib;
168 layout(location = 17) in highp vec4 prev_normal_attrib;
169 #ifdef USE_INSTANCING
170 layout(location = 18) in highp vec4 prev_instance_xform0;
171 layout(location = 19) in highp vec4 prev_instance_xform1;
172 layout(location = 20) in highp vec4 prev_instance_xform2;
173 layout(location = 21) in highp uvec4 prev_instance_color_custom_data;
174 #endif // USE_INSTANCING
175 #endif // RENDER_MOTION_VECTORS
176
177 #define FLAGS_NON_UNIFORM_SCALE (1 << 4)
178
179 layout(std140) uniform GlobalShaderUniformData { //ubo:1
180 vec4 global_shader_uniforms[MAX_GLOBAL_SHADER_UNIFORMS];
181 };
182
183 struct SceneData {
184 highp mat4 projection_matrix;
185 highp mat4 inv_projection_matrix;
186 highp mat4 inv_view_matrix;
187 highp mat4 view_matrix;
188
189 // Used for billboards to cast correct shadows.
190 highp mat4 main_cam_inv_view_matrix;
191
192 vec2 viewport_size;
193 vec2 screen_pixel_size;
194
195 mediump vec4 ambient_light_color_energy;
196
197 mediump float ambient_color_sky_mix;
198 uint directional_shadow_count;
199 float emissive_exposure_normalization;
200 bool use_ambient_light;
201
202 bool use_ambient_cubemap;
203 bool use_reflection_cubemap;
204 float fog_aerial_perspective;
205 float time;
206
207 mat3x4 radiance_inverse_xform;
208
209 uint directional_light_count;
210 float z_far;
211 float z_near;
212 float IBL_exposure_normalization;
213
214 bool fog_enabled;
215 uint fog_mode;
216 float fog_density;
217 float fog_height;
218
219 float fog_height_density;
220 float fog_depth_curve;
221 float fog_sun_scatter;
222 float fog_depth_begin;
223
224 vec3 fog_light_color;
225 float fog_depth_end;
226
227 float shadow_bias;
228 float luminance_multiplier;
229 uint camera_visible_layers;
230 bool pancake_shadows;
231 };
232
233 // The containing data block is for historic reasons.
234 layout(std140) uniform SceneDataBlock { // ubo:2
235 SceneData data;
236 }
237 scene_data_block;
238
239 #ifdef RENDER_MOTION_VECTORS
240 layout(std140) uniform PrevSceneDataBlock { // ubo:12
241 SceneData data;
242 }
243 prev_scene_data_block;
244 #endif
245
246 #ifndef RENDER_MOTION_VECTORS
247 #ifdef USE_ADDITIVE_LIGHTING
248
249 #if defined(ADDITIVE_OMNI) || defined(ADDITIVE_SPOT)
250 struct PositionalShadowData {
251 highp mat4 shadow_matrix;
252 highp vec3 light_position;
253 highp float shadow_normal_bias;
254 vec3 pad;
255 highp float shadow_atlas_pixel_size;
256 };
257
258 layout(std140) uniform PositionalShadows { // ubo:9
259 PositionalShadowData positional_shadows[MAX_LIGHT_DATA_STRUCTS];
260 };
261
262 uniform lowp uint positional_shadow_index;
263
264 #else // ADDITIVE_DIRECTIONAL
265
266 struct DirectionalShadowData {
267 highp vec3 direction;
268 highp float shadow_atlas_pixel_size;
269 highp vec4 shadow_normal_bias;
270 highp vec4 shadow_split_offsets;
271 highp mat4 shadow_matrix1;
272 highp mat4 shadow_matrix2;
273 highp mat4 shadow_matrix3;
274 highp mat4 shadow_matrix4;
275 mediump float fade_from;
276 mediump float fade_to;
277 mediump vec2 pad;
278 };
279
280 layout(std140) uniform DirectionalShadows { // ubo:10
281 DirectionalShadowData directional_shadows[MAX_DIRECTIONAL_LIGHT_DATA_STRUCTS];
282 };
283
284 uniform lowp uint directional_shadow_index;
285
286 #endif // !(defined(ADDITIVE_OMNI) || defined(ADDITIVE_SPOT))
287 #endif // USE_ADDITIVE_LIGHTING
288
289 #ifdef USE_VERTEX_LIGHTING
290
291 out vec3 diffuse_light_interp;
292 out vec3 specular_light_interp;
293
294 #ifdef USE_ADDITIVE_LIGHTING
295 out vec3 additive_diffuse_light_interp;
296 out vec3 additive_specular_light_interp;
297 #endif // USE_ADDITIVE_LIGHTING
298
299 // Directional light data.
300 #if !defined(DISABLE_LIGHT_DIRECTIONAL) || (!defined(ADDITIVE_OMNI) && !defined(ADDITIVE_SPOT) && defined(USE_ADDITIVE_LIGHTING))
301
302 struct DirectionalLightData {
303 mediump vec3 direction;
304 mediump float energy;
305 mediump vec3 color;
306 mediump float size;
307 lowp uint enabled_bake_mode;
308 mediump float shadow_opacity;
309 mediump float specular;
310 highp uint mask;
311 };
312
313 layout(std140) uniform DirectionalLights { // ubo:7
314 DirectionalLightData directional_lights[MAX_DIRECTIONAL_LIGHT_DATA_STRUCTS];
315 };
316
317 #define DIRECTIONAL_LIGHT_ENABLED uint(1 << 0)
318 #define DIRECTIONAL_LIGHT_BAKE_STATIC uint(1 << 1)
319 #define DIRECTIONAL_LIGHT_BAKE_DYNAMIC uint(1 << 2)
320 #endif // !DISABLE_LIGHT_DIRECTIONAL
321
322 // Omni and spot light data.
323 #if !defined(DISABLE_LIGHT_OMNI) || !defined(DISABLE_LIGHT_SPOT) || (defined(ADDITIVE_OMNI) || defined(ADDITIVE_SPOT) && defined(USE_ADDITIVE_LIGHTING))
324
325 struct LightData { // This structure needs to be as packed as possible.
326 highp vec3 position;
327 highp float inv_radius;
328
329 mediump vec3 direction;
330 highp float size;
331
332 mediump vec3 color;
333 mediump float attenuation;
334
335 mediump float cone_attenuation;
336 mediump float cone_angle;
337 mediump float specular_amount;
338 mediump float shadow_opacity;
339
340 lowp vec3 pad;
341 lowp uint bake_mode;
342 };
343
344 #if !defined(DISABLE_LIGHT_OMNI) || defined(ADDITIVE_OMNI)
345 layout(std140) uniform OmniLightData { // ubo:5
346 LightData omni_lights[MAX_LIGHT_DATA_STRUCTS];
347 };
348 #ifdef BASE_PASS
349 uniform uint omni_light_indices[MAX_FORWARD_LIGHTS];
350 uniform uint omni_light_count;
351 #endif // BASE_PASS
352 #endif // DISABLE_LIGHT_OMNI
353
354 #if !defined(DISABLE_LIGHT_SPOT) || defined(ADDITIVE_SPOT)
355 layout(std140) uniform SpotLightData { // ubo:6
356 LightData spot_lights[MAX_LIGHT_DATA_STRUCTS];
357 };
358 #ifdef BASE_PASS
359 uniform uint spot_light_indices[MAX_FORWARD_LIGHTS];
360 uniform uint spot_light_count;
361 #endif // BASE_PASS
362 #endif // DISABLE_LIGHT_SPOT
363 #endif // !defined(DISABLE_LIGHT_OMNI) || !defined(DISABLE_LIGHT_SPOT) || (defined(ADDITIVE_OMNI) || defined(ADDITIVE_SPOT) && defined(USE_ADDITIVE_LIGHTING))
364
365 #ifdef USE_ADDITIVE_LIGHTING
366 #ifdef ADDITIVE_OMNI
367 uniform lowp uint omni_light_index;
368 #endif
369 #ifdef ADDITIVE_SPOT
370 uniform lowp uint spot_light_index;
371 #endif
372 #endif // USE_ADDITIVE_LIGHTING
373
374 #if !defined(MODE_RENDER_DEPTH) && !defined(MODE_UNSHADED) && defined(USE_VERTEX_LIGHTING)
375
376 // Eyeballed approximation of `exp2(15.0 * (1.0 - roughness) + 1.0) * 0.25`.
377 // Uses slightly more FMA instructions (2x rate) to avoid special instructions (0.25x rate).
378 // Range is reduced to [0.64,4977] from [068,2,221,528] which makes mediump feasible for the rest of the shader.
379 mediump float roughness_to_shininess(mediump float roughness) {
380 mediump float r = 1.2 - roughness;
381 mediump float r2 = r * r;
382 return r * r2 * r2 * 2000.0;
383 }
384
385 void light_compute(vec3 N, vec3 L, vec3 V, vec3 light_color, bool is_directional, float roughness,
386 inout vec3 diffuse_light, inout vec3 specular_light) {
387 float NdotL = min(dot(N, L), 1.0);
388 float cNdotL = max(NdotL, 0.0); // clamped NdotL
389
390 #if defined(DIFFUSE_LAMBERT_WRAP)
391 // Energy conserving lambert wrap shader.
392 // https://web.archive.org/web/20210228210901/http://blog.stevemcauley.com/2011/12/03/energy-conserving-wrapped-diffuse/
393 float diffuse_brdf_NL = max(0.0, (cNdotL + roughness) / ((1.0 + roughness) * (1.0 + roughness))) * (1.0 / M_PI);
394 #else
395 // lambert
396 float diffuse_brdf_NL = cNdotL * (1.0 / M_PI);
397 #endif
398
399 diffuse_light += light_color * diffuse_brdf_NL;
400
401 #if !defined(SPECULAR_DISABLED)
402 float specular_brdf_NL = 0.0;
403 // Normalized blinn always unless disabled.
404 vec3 H = normalize(V + L);
405 float cNdotH = clamp(dot(N, H), 0.0, 1.0);
406 float shininess = roughness_to_shininess(roughness);
407 float blinn = pow(cNdotH, shininess);
408 blinn *= (shininess + 2.0) * (1.0 / (8.0 * M_PI)) * cNdotL;
409 specular_brdf_NL = blinn;
410 specular_light += specular_brdf_NL * light_color;
411 #endif
412 }
413
414 float get_omni_spot_attenuation(float distance, float inv_range, float decay) {
415 float nd = distance * inv_range;
416 nd *= nd;
417 nd *= nd; // nd^4
418 nd = max(1.0 - nd, 0.0);
419 nd *= nd; // nd^2
420 return nd * pow(max(distance, 0.0001), -decay);
421 }
422
423 #if !defined(DISABLE_LIGHT_OMNI) || (defined(ADDITIVE_OMNI) && defined(USE_ADDITIVE_LIGHTING))
424 void light_process_omni(uint idx, vec3 vertex, vec3 eye_vec, vec3 normal, float roughness,
425 inout vec3 diffuse_light, inout vec3 specular_light) {
426 vec3 light_rel_vec = omni_lights[idx].position - vertex;
427 float light_length = length(light_rel_vec);
428 float omni_attenuation = get_omni_spot_attenuation(light_length, omni_lights[idx].inv_radius, omni_lights[idx].attenuation);
429 vec3 color = omni_lights[idx].color * omni_attenuation; // No light shaders here, so combine.
430
431 light_compute(normal, normalize(light_rel_vec), eye_vec, color, false, roughness,
432 diffuse_light,
433 specular_light);
434 }
435 #endif // !defined(DISABLE_LIGHT_OMNI) || (defined(ADDITIVE_OMNI) && defined(USE_ADDITIVE_LIGHTING))
436
437 #if !defined(DISABLE_LIGHT_SPOT) || (defined(ADDITIVE_SPOT) && defined(USE_ADDITIVE_LIGHTING))
438 void light_process_spot(uint idx, vec3 vertex, vec3 eye_vec, vec3 normal, float roughness,
439 inout vec3 diffuse_light,
440 inout vec3 specular_light) {
441 vec3 light_rel_vec = spot_lights[idx].position - vertex;
442 float light_length = length(light_rel_vec);
443 float spot_attenuation = get_omni_spot_attenuation(light_length, spot_lights[idx].inv_radius, spot_lights[idx].attenuation);
444 vec3 spot_dir = spot_lights[idx].direction;
445 float scos = max(dot(-normalize(light_rel_vec), spot_dir), spot_lights[idx].cone_angle);
446 float spot_rim = max(0.0001, (1.0 - scos) / (1.0 - spot_lights[idx].cone_angle));
447
448 mediump float cone_attenuation = spot_lights[idx].cone_attenuation;
449 spot_attenuation *= 1.0 - pow(spot_rim, cone_attenuation);
450
451 vec3 color = spot_lights[idx].color * spot_attenuation;
452
453 light_compute(normal, normalize(light_rel_vec), eye_vec, color, false, roughness,
454 diffuse_light, specular_light);
455 }
456 #endif // !defined(DISABLE_LIGHT_SPOT) || (defined(ADDITIVE_SPOT) && defined(USE_ADDITIVE_LIGHTING))
457
458 #endif // !defined(MODE_RENDER_DEPTH) && !defined(MODE_UNSHADED) && defined(USE_VERTEX_LIGHTING)
459
460 #endif // USE_VERTEX_LIGHTING
461 #endif // RENDER_MOTION_VECTORS
462
463 #ifdef USE_MULTIVIEW
464 struct MultiviewData {
465 highp mat4 projection_matrix_view[MAX_VIEWS];
466 highp mat4 inv_projection_matrix_view[MAX_VIEWS];
467 highp vec4 eye_offset[MAX_VIEWS];
468 };
469
470 layout(std140) uniform MultiviewDataBlock { // ubo:8
471 MultiviewData data;
472 }
473 multiview_data_block;
474
475 #ifdef RENDER_MOTION_VECTORS
476 layout(std140) uniform PrevMultiviewDataBlock { // ubo:13
477 MultiviewData data;
478 }
479 prev_multiview_data_block;
480 #endif // RENDER_MOTION_VECTORS
481
482 #endif // USE_MULTIVIEW
483
484 uniform highp mat4 world_transform;
485 uniform highp vec3 compressed_aabb_position;
486 uniform highp vec3 compressed_aabb_size;
487 uniform highp vec4 uv_scale;
488 uniform highp uint instance_offset;
489 uniform highp uint layer_mask;
490
491 #if defined(RENDER_MOTION_VECTORS)
492 uniform highp mat4 prev_world_transform;
493 #endif
494
495 uniform highp uint model_flags;
496
497 #ifdef RENDER_MATERIAL
498 uniform mediump vec2 uv_offset;
499 #endif
500
501 /* Varyings */
502
503 out highp vec3 vertex_interp;
504 #ifdef NORMAL_USED
505 out vec3 normal_interp;
506 #endif
507
508 #if defined(COLOR_USED)
509 out vec4 color_interp;
510 #endif
511
512 #if defined(UV_USED)
513 out vec2 uv_interp;
514 #endif
515
516 #if defined(UV2_USED) || defined(USE_LIGHTMAP)
517 out vec2 uv2_interp;
518 #endif
519
520 #if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED)
521 out vec3 tangent_interp;
522 out vec3 binormal_interp;
523 #endif
524
525 #ifndef RENDER_MOTION_VECTORS
526 #ifdef USE_ADDITIVE_LIGHTING
527 out highp vec4 shadow_coord;
528
529 #if defined(LIGHT_USE_PSSM2) || defined(LIGHT_USE_PSSM4)
530 out highp vec4 shadow_coord2;
531 #endif
532
533 #ifdef LIGHT_USE_PSSM4
534 out highp vec4 shadow_coord3;
535 out highp vec4 shadow_coord4;
536 #endif // LIGHT_USE_PSSM4
537 #endif // USE_ADDITIVE_LIGHTING
538 #endif // RENDER_MOTION_VECTORS
539
540 #ifdef MATERIAL_UNIFORMS_USED
541
542 /* clang-format off */
543 layout(std140) uniform MaterialUniforms { // ubo:3
544
545 #MATERIAL_UNIFORMS
546
547 };
548 /* clang-format on */
549
550 #endif
551
552 /* clang-format off */
553
554 #if defined(RENDER_MOTION_VECTORS)
555 out highp vec4 clip_position;
556 out highp vec4 prev_clip_position;
557 #endif
558
559 #GLOBALS
560
561 /* clang-format on */
562 invariant gl_Position;
563
564 void vertex_shader(vec4 vertex_angle_attrib_input,
565 vec3 compressed_aabb_size_input,
566 vec3 compressed_aabb_position_input,
567 mat4 world_transform_input,
568 uint model_flags_input,
569 SceneData scene_data_input,
570 #ifdef USE_INSTANCING
571 vec4 instance_xform0_input, vec4 instance_xform1_input, vec4 instance_xform2_input,
572 uvec4 instance_color_custom_data_input,
573 #endif
574 #ifdef NORMAL_USED
575 vec4 axis_tangent_attrib_input,
576 #endif
577 #if defined(COLOR_USED)
578 vec4 color_attrib_input,
579 #endif
580 #if defined(UV_USED)
581 vec2 uv_attrib_input,
582 #endif
583 #if defined(UV2_USED) || defined(USE_LIGHTMAP) || defined(RENDER_MATERIAL)
584 vec2 uv2_attrib_input,
585 #endif
586 #ifdef USE_MULTIVIEW
587 mat4 projection_matrix,
588 mat4 inv_projection_matrix,
589 vec3 eye_offset,
590 #endif
591 vec4 uv_scale_input,
592 out vec4 clip_position_output) {
593 highp vec3 vertex = vertex_angle_attrib_input.xyz * compressed_aabb_size_input + compressed_aabb_position_input;
594
595 highp mat4 model_matrix = world_transform_input;
596 #ifdef USE_INSTANCING
597 highp mat4 m = mat4(instance_xform0_input, instance_xform1_input, instance_xform2_input, vec4(0.0, 0.0, 0.0, 1.0));
598 model_matrix = model_matrix * transpose(m);
599 #endif
600
601 #ifdef NORMAL_USED
602 vec3 normal = oct_to_vec3(axis_tangent_attrib_input.xy * 2.0 - 1.0);
603 #endif
604
605 highp mat3 model_normal_matrix;
606
607 if (bool(model_flags_input & uint(FLAGS_NON_UNIFORM_SCALE))) {
608 model_normal_matrix = transpose(inverse(mat3(model_matrix)));
609 } else {
610 model_normal_matrix = mat3(model_matrix);
611 }
612
613 #if defined(NORMAL_USED) || defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED)
614
615 vec3 binormal;
616 float binormal_sign;
617 vec3 tangent;
618 if (axis_tangent_attrib.z > 0.0 || axis_tangent_attrib.w < 1.0) {
619 // Uncompressed format.
620 vec2 signed_tangent_attrib = axis_tangent_attrib.zw * 2.0 - 1.0;
621 tangent = oct_to_vec3(vec2(signed_tangent_attrib.x, abs(signed_tangent_attrib.y) * 2.0 - 1.0));
622 binormal_sign = sign(signed_tangent_attrib.y);
623 binormal = normalize(cross(normal, tangent) * binormal_sign);
624 } else {
625 // Compressed format.
626 float angle = vertex_angle_attrib_input.w;
627 binormal_sign = angle > 0.5 ? 1.0 : -1.0; // 0.5 does not exist in UNORM16, so values are either greater or smaller.
628 angle = abs(angle * 2.0 - 1.0) * M_PI; // 0.5 is basically zero, allowing to encode both signs reliably.
629 vec3 axis = normal;
630 axis_angle_to_tbn(axis, angle, tangent, binormal, normal);
631 binormal *= binormal_sign;
632 }
633 #endif
634
635 #if defined(COLOR_USED)
636 color_interp = color_attrib_input;
637 #ifdef USE_INSTANCING
638 vec4 instance_color;
639 instance_color.xy = unpackHalf2x16(instance_color_custom_data_input.x);
640 instance_color.zw = unpackHalf2x16(instance_color_custom_data_input.y);
641 color_interp *= instance_color;
642 #endif
643 #endif
644
645 #if defined(UV_USED)
646 uv_interp = uv_attrib_input;
647 #endif
648
649 #if defined(UV2_USED) || defined(USE_LIGHTMAP)
650 uv2_interp = uv2_attrib_input;
651 #endif
652
653 if (uv_scale_input != vec4(0.0)) { // Compression enabled
654 #ifdef UV_USED
655 uv_interp = (uv_interp - 0.5) * uv_scale_input.xy;
656 #endif
657 #if defined(UV2_USED) || defined(USE_LIGHTMAP)
658 uv2_interp = (uv2_interp - 0.5) * uv_scale_input.zw;
659 #endif
660 }
661
662 #if defined(OVERRIDE_POSITION)
663 highp vec4 position;
664 #endif
665
666 #ifndef USE_MULTIVIEW
667 mat4 projection_matrix = scene_data_input.projection_matrix;
668 mat4 inv_projection_matrix = scene_data_input.inv_projection_matrix;
669 #endif //!USE_MULTIVIEW
670
671 #ifdef USE_INSTANCING
672 vec4 instance_custom;
673 instance_custom.xy = unpackHalf2x16(instance_color_custom_data_input.z);
674 instance_custom.zw = unpackHalf2x16(instance_color_custom_data_input.w);
675 #else
676 vec4 instance_custom = vec4(0.0);
677 #endif
678
679 // Using world coordinates
680 #if !defined(SKIP_TRANSFORM_USED) && defined(VERTEX_WORLD_COORDS_USED)
681
682 vertex = (model_matrix * vec4(vertex, 1.0)).xyz;
683
684 #ifdef NORMAL_USED
685 normal = model_normal_matrix * normal;
686 #endif
687
688 #if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED)
689
690 tangent = model_normal_matrix * tangent;
691 binormal = model_normal_matrix * binormal;
692
693 #endif
694 #endif
695
696 #ifdef Z_CLIP_SCALE_USED
697 float z_clip_scale = 1.0;
698 #endif
699
700 float roughness = 1.0;
701
702 highp mat4 modelview = scene_data_input.view_matrix * model_matrix;
703 highp mat3 modelview_normal = mat3(scene_data_input.view_matrix) * model_normal_matrix;
704
705 float point_size = 1.0;
706
707 {
708 #CODE : VERTEX
709 }
710
711 gl_PointSize = point_size;
712
713 // Using local coordinates (default)
714 #if !defined(SKIP_TRANSFORM_USED) && !defined(VERTEX_WORLD_COORDS_USED)
715
716 vertex = (modelview * vec4(vertex, 1.0)).xyz;
717 #ifdef NORMAL_USED
718 normal = modelview_normal * normal;
719 #endif
720
721 #if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED)
722
723 binormal = modelview_normal * binormal;
724 tangent = modelview_normal * tangent;
725 #endif
726 #endif // !defined(SKIP_TRANSFORM_USED) && !defined(VERTEX_WORLD_COORDS_USED)
727
728 // Using world coordinates
729 #if !defined(SKIP_TRANSFORM_USED) && defined(VERTEX_WORLD_COORDS_USED)
730
731 vertex = (scene_data_input.view_matrix * vec4(vertex, 1.0)).xyz;
732 #ifdef NORMAL_USED
733 normal = (scene_data_input.view_matrix * vec4(normal, 0.0)).xyz;
734 #endif
735
736 #if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED)
737 binormal = (scene_data_input.view_matrix * vec4(binormal, 0.0)).xyz;
738 tangent = (scene_data_input.view_matrix * vec4(tangent, 0.0)).xyz;
739 #endif
740 #endif
741
742 vertex_interp = vertex;
743
744 // Normalize TBN vectors before interpolation, per MikkTSpace.
745 // See: http://www.mikktspace.com/
746 #ifdef NORMAL_USED
747 normal_interp = normalize(normal);
748 #endif
749
750 #if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED)
751 tangent_interp = normalize(tangent);
752 binormal_interp = normalize(binormal);
753 #endif
754
755 #ifndef RENDER_MOTION_VECTORS
756 // Calculate shadows.
757 #ifdef USE_ADDITIVE_LIGHTING
758 #if defined(ADDITIVE_OMNI) || defined(ADDITIVE_SPOT)
759 // Apply normal bias at draw time to avoid issues with scaling non-fused geometry.
760 vec3 light_rel_vec = positional_shadows[positional_shadow_index].light_position - vertex_interp;
761 float light_length = length(light_rel_vec);
762 float aNdotL = abs(dot(normalize(normal_interp), normalize(light_rel_vec)));
763 vec3 normal_offset = (1.0 - aNdotL) * positional_shadows[positional_shadow_index].shadow_normal_bias * light_length * normal_interp;
764
765 #ifdef ADDITIVE_SPOT
766 // Calculate coord here so we can take advantage of prefetch.
767 shadow_coord = positional_shadows[positional_shadow_index].shadow_matrix * vec4(vertex_interp + normal_offset, 1.0);
768 #endif
769
770 #ifdef ADDITIVE_OMNI
771 // Can't interpolate unit direction nicely, so forget about prefetch.
772 shadow_coord = vec4(vertex_interp + normal_offset, 1.0);
773 #endif
774 #else // ADDITIVE_DIRECTIONAL
775 vec3 base_normal_bias = normalize(normal_interp) * (1.0 - max(0.0, dot(directional_shadows[directional_shadow_index].direction, -normalize(normal_interp))));
776 vec3 normal_offset = base_normal_bias * directional_shadows[directional_shadow_index].shadow_normal_bias.x;
777 shadow_coord = directional_shadows[directional_shadow_index].shadow_matrix1 * vec4(vertex_interp + normal_offset, 1.0);
778
779 #if defined(LIGHT_USE_PSSM2) || defined(LIGHT_USE_PSSM4)
780 normal_offset = base_normal_bias * directional_shadows[directional_shadow_index].shadow_normal_bias.y;
781 shadow_coord2 = directional_shadows[directional_shadow_index].shadow_matrix2 * vec4(vertex_interp + normal_offset, 1.0);
782 #endif
783
784 #ifdef LIGHT_USE_PSSM4
785 normal_offset = base_normal_bias * directional_shadows[directional_shadow_index].shadow_normal_bias.z;
786 shadow_coord3 = directional_shadows[directional_shadow_index].shadow_matrix3 * vec4(vertex_interp + normal_offset, 1.0);
787 normal_offset = base_normal_bias * directional_shadows[directional_shadow_index].shadow_normal_bias.w;
788 shadow_coord4 = directional_shadows[directional_shadow_index].shadow_matrix4 * vec4(vertex_interp + normal_offset, 1.0);
789 #endif //LIGHT_USE_PSSM4
790
791 #endif // !(defined(ADDITIVE_OMNI) || defined(ADDITIVE_SPOT))
792 #endif // USE_ADDITIVE_LIGHTING
793
794 #if defined(RENDER_SHADOWS) && !defined(RENDER_SHADOWS_LINEAR)
795 // This is an optimized version of normalize(vertex_interp) * scene_data_input.shadow_bias / length(vertex_interp).
796 float light_length_sq = dot(vertex_interp, vertex_interp);
797 vertex_interp += vertex_interp * scene_data_input.shadow_bias / light_length_sq;
798 #endif
799 #endif // RENDER_MOTION_VECTORS
800
801 #if defined(OVERRIDE_POSITION)
802 clip_position_output = position;
803 #else
804 clip_position_output = projection_matrix * vec4(vertex_interp, 1.0);
805 #endif
806
807 #if !defined(RENDER_SHADOWS) && !defined(RENDER_SHADOWS_LINEAR)
808 #ifdef Z_CLIP_SCALE_USED
809 clip_position_output.z = mix(clip_position_output.w, clip_position_output.z, z_clip_scale);
810 #endif
811 #endif
812
813 #ifdef RENDER_MATERIAL
814 vec2 uv_dest_attrib;
815 if (uv_scale_input != vec4(0.0)) {
816 uv_dest_attrib = (uv2_attrib_input.xy - 0.5) * uv_scale_input.zw;
817 } else {
818 uv_dest_attrib = uv2_attrib_input.xy;
819 }
820
821 clip_position_output.xy = (uv_dest_attrib + uv_offset) * 2.0 - 1.0;
822 clip_position_output.z = 0.00001;
823 clip_position_output.w = 1.0;
824 #endif
825
826 #ifndef RENDER_MOTION_VECTORS
827 #ifdef USE_VERTEX_LIGHTING
828 #if !defined(MODE_RENDER_DEPTH) && !defined(MODE_UNSHADED)
829 #ifdef USE_MULTIVIEW
830 vec3 view = -normalize(vertex_interp - eye_offset);
831 #else
832 vec3 view = -normalize(vertex_interp);
833 #endif
834 diffuse_light_interp = vec3(0.0);
835 specular_light_interp = vec3(0.0);
836 #ifdef BASE_PASS
837 #ifndef DISABLE_LIGHT_DIRECTIONAL
838 for (uint i = uint(0); i < scene_data_input.directional_light_count; i++) {
839 if (!bool(directional_lights[i].mask & layer_mask)) {
840 continue;
841 }
842 #if defined(USE_LIGHTMAP) && !defined(DISABLE_LIGHTMAP)
843 if (bool(directional_lights[i].enabled_bake_mode & DIRECTIONAL_LIGHT_BAKE_STATIC)) {
844 continue;
845 }
846 #endif
847 light_compute(normal_interp, normalize(directional_lights[i].direction), normalize(view), directional_lights[i].color * directional_lights[i].energy, true, roughness,
848 diffuse_light_interp.rgb,
849 specular_light_interp.rgb);
850 }
851 #endif // !DISABLE_LIGHT_DIRECTIONAL
852
853 #ifndef DISABLE_LIGHT_OMNI
854 for (uint i = 0u; i < omni_light_count; i++) {
855 light_process_omni(omni_light_indices[i], vertex_interp, view, normal_interp, roughness,
856 diffuse_light_interp.rgb, specular_light_interp.rgb);
857 }
858 #endif // !DISABLE_LIGHT_OMNI
859
860 #ifndef DISABLE_LIGHT_SPOT
861 for (uint i = 0u; i < spot_light_count; i++) {
862 light_process_spot(spot_light_indices[i], vertex_interp, view, normal_interp, roughness,
863 diffuse_light_interp.rgb, specular_light_interp.rgb);
864 }
865 #endif // !DISABLE_LIGHT_SPOT
866 #endif // BASE_PASS
867
868 /* ADDITIVE LIGHTING PASS */
869 #ifdef USE_ADDITIVE_LIGHTING
870 additive_diffuse_light_interp = vec3(0.0);
871 additive_specular_light_interp = vec3(0.0);
872 #if !defined(ADDITIVE_OMNI) && !defined(ADDITIVE_SPOT)
873
874 if (bool(directional_lights[directional_shadow_index].mask & layer_mask)) {
875 light_compute(normal_interp, normalize(directional_lights[directional_shadow_index].direction), normalize(view), directional_lights[directional_shadow_index].color * directional_lights[directional_shadow_index].energy, true, roughness,
876 additive_diffuse_light_interp.rgb,
877 additive_specular_light_interp.rgb);
878 }
879 #endif // !defined(ADDITIVE_OMNI) && !defined(ADDITIVE_SPOT)
880
881 #ifdef ADDITIVE_OMNI
882 light_process_omni(omni_light_index, vertex_interp, view, normal_interp, roughness,
883 additive_diffuse_light_interp.rgb, additive_specular_light_interp.rgb);
884 #endif // ADDITIVE_OMNI
885
886 #ifdef ADDITIVE_SPOT
887 light_process_spot(spot_light_index, vertex_interp, view, normal_interp, roughness,
888 additive_diffuse_light_interp.rgb, additive_specular_light_interp.rgb);
889 #endif // ADDITIVE_SPOT
890
891 #endif // USE_ADDITIVE_LIGHTING
892 #endif // !defined(MODE_RENDER_DEPTH) && !defined(MODE_UNSHADED)
893 #endif // USE_VERTEX_LIGHTING
894 #endif // RENDER_MOTION_VECTORS
895 }
896
897 void main() {
898 #if defined(RENDER_MOTION_VECTORS)
899
900 #ifdef USE_INSTANCING
901 // Check for inactive particle instances.
902 highp vec4 input_instance_xform0;
903 highp vec4 input_instance_xform1;
904 highp vec4 input_instance_xform2;
905 highp uvec4 input_instance_color_custom_data;
906 if (prev_instance_xform0.xyz == vec3(0.0, 0.0, 0.0)) {
907 input_instance_xform0 = instance_xform0;
908 input_instance_xform1 = instance_xform1;
909 input_instance_xform2 = instance_xform2;
910 input_instance_color_custom_data = instance_color_custom_data;
911 } else {
912 input_instance_xform0 = prev_instance_xform0;
913 input_instance_xform1 = prev_instance_xform1;
914 input_instance_xform2 = prev_instance_xform2;
915 input_instance_color_custom_data = prev_instance_color_custom_data;
916 }
917 #endif
918
919 vertex_shader(prev_vertex_attrib,
920 compressed_aabb_size,
921 compressed_aabb_position,
922 prev_world_transform,
923 model_flags,
924 prev_scene_data_block.data,
925 #ifdef USE_INSTANCING
926 input_instance_xform0, input_instance_xform1, input_instance_xform2,
927 input_instance_color_custom_data,
928 #endif
929 #ifdef NORMAL_USED
930 prev_normal_attrib,
931 #endif
932 #if defined(COLOR_USED)
933 color_attrib,
934 #endif
935 #if defined(UV_USED)
936 uv_attrib,
937 #endif
938 #if defined(UV2_USED) || defined(USE_LIGHTMAP) || defined(RENDER_MATERIAL)
939 uv2_attrib,
940 #endif
941 #ifdef USE_MULTIVIEW
942 prev_multiview_data_block.data.projection_matrix_view[ViewIndex],
943 prev_multiview_data_block.data.inv_projection_matrix_view[ViewIndex],
944 prev_multiview_data_block.data.eye_offset[ViewIndex].xyz,
945 #endif
946 uv_scale,
947 prev_clip_position);
948 #else
949 vec4 clip_position;
950 #endif // defined(RENDER_MOTION_VECTORS)
951
952 vertex_shader(vertex_angle_attrib,
953 compressed_aabb_size,
954 compressed_aabb_position,
955 world_transform,
956 model_flags,
957 scene_data_block.data,
958 #ifdef USE_INSTANCING
959 instance_xform0, instance_xform1, instance_xform2,
960 instance_color_custom_data,
961 #endif
962 #ifdef NORMAL_USED
963 axis_tangent_attrib,
964 #endif
965 #if defined(COLOR_USED)
966 color_attrib,
967 #endif
968 #if defined(UV_USED)
969 uv_attrib,
970 #endif
971 #if defined(UV2_USED) || defined(USE_LIGHTMAP) || defined(RENDER_MATERIAL)
972 uv2_attrib,
973 #endif
974 #ifdef USE_MULTIVIEW
975 multiview_data_block.data.projection_matrix_view[ViewIndex],
976 multiview_data_block.data.inv_projection_matrix_view[ViewIndex],
977 multiview_data_block.data.eye_offset[ViewIndex].xyz,
978 #endif
979 uv_scale,
980 clip_position);
981
982 gl_Position = clip_position;
983 }
984
985 /* clang-format off */
986 #[fragment]
987
988 // Default to SPECULAR_SCHLICK_GGX.
989 #if !defined(SPECULAR_DISABLED) && !defined(SPECULAR_SCHLICK_GGX) && !defined(SPECULAR_TOON)
990 #define SPECULAR_SCHLICK_GGX
991 #endif
992
993 #if !defined(MODE_RENDER_DEPTH) || defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED) ||defined(LIGHT_CLEARCOAT_USED)
994 #ifndef NORMAL_USED
995 #define NORMAL_USED
996 #endif
997 #endif
998
999 #ifdef MODE_UNSHADED
1000 #ifdef USE_ADDITIVE_LIGHTING
1001 #undef USE_ADDITIVE_LIGHTING
1002 #endif
1003 #endif // MODE_UNSHADED
1004
1005 #ifndef MODE_RENDER_DEPTH
1006 #include "tonemap_inc.glsl"
1007 #endif
1008 #include "stdlib_inc.glsl"
1009
1010 /* texture unit usage, N is max_texture_unit-N
1011
1012 1-color correction // In tonemap_inc.glsl
1013 2-radiance
1014 3-shadow
1015 4-lightmap textures
1016 5-shadowmask textures
1017 6-screen
1018 7-depth
1019 8-reflection probe 1
1020 9-reflection probe 2
1021
1022 */
1023
1024 #define M_PI 3.14159265359
1025 /* clang-format on */
1026
1027 #define SHADER_IS_SRGB true
1028 #define SHADER_SPACE_FAR -1.0
1029
1030 #ifdef USE_MULTIVIEW
1031 #define OUTPUT_IS_MULTIVIEW true
1032 #else
1033 #define OUTPUT_IS_MULTIVIEW false
1034 #endif
1035
1036 #if defined(RENDER_SHADOWS) || defined(RENDER_SHADOWS_LINEAR)
1037 #define IN_SHADOW_PASS true
1038 #else
1039 #define IN_SHADOW_PASS false
1040 #endif
1041
1042 #define FLAGS_NON_UNIFORM_SCALE (1 << 4)
1043
1044 /* Varyings */
1045
1046 #if defined(RENDER_MOTION_VECTORS)
1047 in highp vec4 clip_position;
1048 in highp vec4 prev_clip_position;
1049 #endif
1050
1051 #ifndef RENDER_MOTION_VECTORS
1052 #if defined(COLOR_USED)
1053 in vec4 color_interp;
1054 #endif
1055
1056 #if defined(UV_USED)
1057 in vec2 uv_interp;
1058 #endif
1059
1060 #if defined(UV2_USED)
1061 in vec2 uv2_interp;
1062 #else
1063 #ifdef USE_LIGHTMAP
1064 in vec2 uv2_interp;
1065 #endif
1066 #endif
1067
1068 #if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED)
1069 in vec3 tangent_interp;
1070 in vec3 binormal_interp;
1071 #endif
1072
1073 #ifdef NORMAL_USED
1074 in vec3 normal_interp;
1075 #endif
1076
1077 in highp vec3 vertex_interp;
1078
1079 #ifdef USE_ADDITIVE_LIGHTING
1080 in highp vec4 shadow_coord;
1081
1082 #if defined(LIGHT_USE_PSSM2) || defined(LIGHT_USE_PSSM4)
1083 in highp vec4 shadow_coord2;
1084 #endif
1085
1086 #ifdef LIGHT_USE_PSSM4
1087 in highp vec4 shadow_coord3;
1088 in highp vec4 shadow_coord4;
1089 #endif //LIGHT_USE_PSSM4
1090 #endif
1091
1092 #ifdef USE_RADIANCE_MAP
1093
1094 #define RADIANCE_MAX_LOD 5.0
1095
1096 uniform samplerCube radiance_map; // texunit:-2
1097
1098 #endif // USE_RADIANCE_MAP
1099
1100 #ifndef DISABLE_REFLECTION_PROBE
1101
1102 #define REFLECTION_PROBE_MAX_LOD 8.0
1103
1104 uniform bool refprobe1_use_box_project;
1105 uniform highp vec3 refprobe1_box_extents;
1106 uniform vec3 refprobe1_box_offset;
1107 uniform highp mat4 refprobe1_local_matrix;
1108 uniform bool refprobe1_exterior;
1109 uniform float refprobe1_intensity;
1110 uniform float refprobe1_blend_distance;
1111 uniform int refprobe1_ambient_mode;
1112 uniform vec4 refprobe1_ambient_color;
1113
1114 uniform samplerCube refprobe1_texture; // texunit:-8
1115
1116 #ifdef SECOND_REFLECTION_PROBE
1117
1118 uniform bool refprobe2_use_box_project;
1119 uniform highp vec3 refprobe2_box_extents;
1120 uniform vec3 refprobe2_box_offset;
1121 uniform highp mat4 refprobe2_local_matrix;
1122 uniform bool refprobe2_exterior;
1123 uniform float refprobe2_intensity;
1124 uniform float refprobe2_blend_distance;
1125 uniform int refprobe2_ambient_mode;
1126 uniform vec4 refprobe2_ambient_color;
1127
1128 uniform samplerCube refprobe2_texture; // texunit:-9
1129
1130 #endif // SECOND_REFLECTION_PROBE
1131
1132 #endif // DISABLE_REFLECTION_PROBE
1133
1134 layout(std140) uniform GlobalShaderUniformData { //ubo:1
1135 vec4 global_shader_uniforms[MAX_GLOBAL_SHADER_UNIFORMS];
1136 };
1137
1138 /* Material Uniforms */
1139 #ifdef MATERIAL_UNIFORMS_USED
1140
1141 /* clang-format off */
1142 layout(std140) uniform MaterialUniforms { // ubo:3
1143
1144 #MATERIAL_UNIFORMS
1145
1146 };
1147 /* clang-format on */
1148
1149 #endif
1150
1151 struct SceneData {
1152 highp mat4 projection_matrix;
1153 highp mat4 inv_projection_matrix;
1154 highp mat4 inv_view_matrix;
1155 highp mat4 view_matrix;
1156
1157 // Used for billboards to cast correct shadows.
1158 highp mat4 main_cam_inv_view_matrix;
1159
1160 vec2 viewport_size;
1161 vec2 screen_pixel_size;
1162
1163 mediump vec4 ambient_light_color_energy;
1164
1165 mediump float ambient_color_sky_mix;
1166 uint directional_shadow_count;
1167 float emissive_exposure_normalization;
1168 bool use_ambient_light;
1169
1170 bool use_ambient_cubemap;
1171 bool use_reflection_cubemap;
1172 float fog_aerial_perspective;
1173 float time;
1174
1175 mat3x4 radiance_inverse_xform;
1176
1177 uint directional_light_count;
1178 float z_far;
1179 float z_near;
1180 float IBL_exposure_normalization;
1181
1182 bool fog_enabled;
1183 uint fog_mode;
1184 float fog_density;
1185 float fog_height;
1186
1187 float fog_height_density;
1188 float fog_depth_curve;
1189 float fog_sun_scatter;
1190 float fog_depth_begin;
1191
1192 vec3 fog_light_color;
1193 float fog_depth_end;
1194
1195 float shadow_bias;
1196 float luminance_multiplier;
1197 uint camera_visible_layers;
1198 bool pancake_shadows;
1199 };
1200
1201 layout(std140) uniform SceneDataBlock { // ubo:2
1202 SceneData data;
1203 }
1204 scene_data_block;
1205
1206 #ifdef USE_MULTIVIEW
1207 struct MultiviewData {
1208 highp mat4 projection_matrix_view[MAX_VIEWS];
1209 highp mat4 inv_projection_matrix_view[MAX_VIEWS];
1210 highp vec4 eye_offset[MAX_VIEWS];
1211 };
1212
1213 layout(std140) uniform MultiviewDataBlock { // ubo:8
1214 MultiviewData data;
1215 }
1216 multiview_data_block;
1217 #endif
1218
1219 uniform highp mat4 world_transform;
1220 uniform highp uint instance_offset;
1221 uniform highp uint layer_mask;
1222 uniform highp uint model_flags;
1223
1224 #define DIRECTIONAL_LIGHT_ENABLED uint(1 << 0)
1225 #define DIRECTIONAL_LIGHT_BAKE_STATIC uint(1 << 1)
1226 #define DIRECTIONAL_LIGHT_BAKE_DYNAMIC uint(1 << 2)
1227
1228 #ifndef MODE_RENDER_DEPTH
1229 #ifdef USE_VERTEX_LIGHTING
1230 in vec3 diffuse_light_interp;
1231 in vec3 specular_light_interp;
1232
1233 #ifdef USE_ADDITIVE_LIGHTING
1234 in vec3 additive_diffuse_light_interp;
1235 in vec3 additive_specular_light_interp;
1236 #endif // USE_ADDITIVE_LIGHTING
1237 #endif // USE_VERTEX_LIGHTING
1238
1239 // Directional light data.
1240 #if !defined(DISABLE_LIGHT_DIRECTIONAL) || (!defined(ADDITIVE_OMNI) && !defined(ADDITIVE_SPOT)) || defined(USE_SUN_SCATTER)
1241
1242 struct DirectionalLightData {
1243 mediump vec3 direction;
1244 mediump float energy;
1245 mediump vec3 color;
1246 mediump float size;
1247 lowp uint enabled_bake_mode;
1248 mediump float shadow_opacity;
1249 mediump float specular;
1250 highp uint mask;
1251 };
1252
1253 layout(std140) uniform DirectionalLights { // ubo:7
1254 DirectionalLightData directional_lights[MAX_DIRECTIONAL_LIGHT_DATA_STRUCTS];
1255 };
1256
1257 #if defined(USE_ADDITIVE_LIGHTING) && (!defined(ADDITIVE_OMNI) && !defined(ADDITIVE_SPOT))
1258 // Directional shadows can be in the base pass or in the additive passes
1259 uniform highp sampler2DShadow directional_shadow_atlas; // texunit:-3
1260 #endif // defined(USE_ADDITIVE_LIGHTING) && (!defined(ADDITIVE_OMNI) && !defined(ADDITIVE_SPOT))
1261
1262 #endif // !DISABLE_LIGHT_DIRECTIONAL || USE_SUN_SCATTER
1263
1264 // Omni and spot light data.
1265 #if !defined(DISABLE_LIGHT_OMNI) || !defined(DISABLE_LIGHT_SPOT) || defined(ADDITIVE_OMNI) || defined(ADDITIVE_SPOT)
1266
1267 struct LightData { // This structure needs to be as packed as possible.
1268 highp vec3 position;
1269 highp float inv_radius;
1270
1271 mediump vec3 direction;
1272 highp float size;
1273
1274 mediump vec3 color;
1275 mediump float attenuation;
1276
1277 mediump float cone_attenuation;
1278 mediump float cone_angle;
1279 mediump float specular_amount;
1280 mediump float shadow_opacity;
1281
1282 lowp vec3 pad;
1283 lowp uint bake_mode;
1284 };
1285
1286 #if !defined(DISABLE_LIGHT_OMNI) || defined(ADDITIVE_OMNI)
1287 layout(std140) uniform OmniLightData { // ubo:5
1288 LightData omni_lights[MAX_LIGHT_DATA_STRUCTS];
1289 };
1290 #if defined(BASE_PASS) && !defined(USE_VERTEX_LIGHTING)
1291 uniform uint omni_light_indices[MAX_FORWARD_LIGHTS];
1292 uniform uint omni_light_count;
1293 #endif // defined(BASE_PASS) && !defined(USE_VERTEX_LIGHTING)
1294 #endif // !defined(DISABLE_LIGHT_OMNI) || defined(ADDITIVE_OMNI)
1295
1296 #if !defined(DISABLE_LIGHT_SPOT) || defined(ADDITIVE_SPOT)
1297 layout(std140) uniform SpotLightData { // ubo:6
1298 LightData spot_lights[MAX_LIGHT_DATA_STRUCTS];
1299 };
1300 #if defined(BASE_PASS) && !defined(USE_VERTEX_LIGHTING)
1301 uniform uint spot_light_indices[MAX_FORWARD_LIGHTS];
1302 uniform uint spot_light_count;
1303 #endif // defined(BASE_PASS) && !defined(USE_VERTEX_LIGHTING)
1304 #endif // !defined(DISABLE_LIGHT_SPOT) || defined(ADDITIVE_SPOT)
1305 #endif // !defined(DISABLE_LIGHT_OMNI) || !defined(DISABLE_LIGHT_SPOT) || defined(ADDITIVE_OMNI) || defined(ADDITIVE_SPOT)
1306
1307 #ifdef USE_ADDITIVE_LIGHTING
1308 #ifdef ADDITIVE_OMNI
1309 uniform highp samplerCubeShadow omni_shadow_texture; // texunit:-3
1310 uniform lowp uint omni_light_index;
1311 #endif
1312 #ifdef ADDITIVE_SPOT
1313 uniform highp sampler2DShadow spot_shadow_texture; // texunit:-3
1314 uniform lowp uint spot_light_index;
1315 #endif
1316
1317 #if defined(ADDITIVE_OMNI) || defined(ADDITIVE_SPOT)
1318 struct PositionalShadowData {
1319 highp mat4 shadow_matrix;
1320 highp vec3 light_position;
1321 highp float shadow_normal_bias;
1322 vec3 pad;
1323 highp float shadow_atlas_pixel_size;
1324 };
1325
1326 layout(std140) uniform PositionalShadows { // ubo:9
1327 PositionalShadowData positional_shadows[MAX_LIGHT_DATA_STRUCTS];
1328 };
1329
1330 uniform lowp uint positional_shadow_index;
1331 #else // ADDITIVE_DIRECTIONAL
1332 struct DirectionalShadowData {
1333 highp vec3 direction;
1334 highp float shadow_atlas_pixel_size;
1335 highp vec4 shadow_normal_bias;
1336 highp vec4 shadow_split_offsets;
1337 highp mat4 shadow_matrix1;
1338 highp mat4 shadow_matrix2;
1339 highp mat4 shadow_matrix3;
1340 highp mat4 shadow_matrix4;
1341 mediump float fade_from;
1342 mediump float fade_to;
1343 mediump vec2 pad;
1344 };
1345
1346 layout(std140) uniform DirectionalShadows { // ubo:10
1347 DirectionalShadowData directional_shadows[MAX_DIRECTIONAL_LIGHT_DATA_STRUCTS];
1348 };
1349
1350 uniform lowp uint directional_shadow_index;
1351 #endif // !(defined(ADDITIVE_OMNI) || defined(ADDITIVE_SPOT))
1352
1353 #if !defined(ADDITIVE_OMNI)
1354 float sample_shadow(highp sampler2DShadow shadow, float shadow_pixel_size, vec4 pos) {
1355 // Use textureProjLod with LOD set to 0.0 over textureProj, as textureProj not working correctly on ANGLE with Metal backend.
1356 // https://github.com/godotengine/godot/issues/93537
1357 float avg = textureProjLod(shadow, pos, 0.0);
1358 #ifdef SHADOW_MODE_PCF_13
1359 pos /= pos.w;
1360 avg += textureProjLod(shadow, vec4(pos.xy + vec2(shadow_pixel_size * 2.0, 0.0), pos.zw), 0.0);
1361 avg += textureProjLod(shadow, vec4(pos.xy + vec2(-shadow_pixel_size * 2.0, 0.0), pos.zw), 0.0);
1362 avg += textureProjLod(shadow, vec4(pos.xy + vec2(0.0, shadow_pixel_size * 2.0), pos.zw), 0.0);
1363 avg += textureProjLod(shadow, vec4(pos.xy + vec2(0.0, -shadow_pixel_size * 2.0), pos.zw), 0.0);
1364
1365 // Early bail if distant samples are fully shaded (or none are shaded) to improve performance.
1366 if (avg <= 0.000001) {
1367 // None shaded at all.
1368 return 0.0;
1369 } else if (avg >= 4.999999) {
1370 // All fully shaded.
1371 return 1.0;
1372 }
1373
1374 avg += textureProjLod(shadow, vec4(pos.xy + vec2(shadow_pixel_size, 0.0), pos.zw), 0.0);
1375 avg += textureProjLod(shadow, vec4(pos.xy + vec2(-shadow_pixel_size, 0.0), pos.zw), 0.0);
1376 avg += textureProjLod(shadow, vec4(pos.xy + vec2(0.0, shadow_pixel_size), pos.zw), 0.0);
1377 avg += textureProjLod(shadow, vec4(pos.xy + vec2(0.0, -shadow_pixel_size), pos.zw), 0.0);
1378 avg += textureProjLod(shadow, vec4(pos.xy + vec2(shadow_pixel_size, shadow_pixel_size), pos.zw), 0.0);
1379 avg += textureProjLod(shadow, vec4(pos.xy + vec2(-shadow_pixel_size, shadow_pixel_size), pos.zw), 0.0);
1380 avg += textureProjLod(shadow, vec4(pos.xy + vec2(shadow_pixel_size, -shadow_pixel_size), pos.zw), 0.0);
1381 avg += textureProjLod(shadow, vec4(pos.xy + vec2(-shadow_pixel_size, -shadow_pixel_size), pos.zw), 0.0);
1382 return avg * (1.0 / 13.0);
1383 #endif
1384
1385 #ifdef SHADOW_MODE_PCF_5
1386 pos /= pos.w;
1387 avg += textureProjLod(shadow, vec4(pos.xy + vec2(shadow_pixel_size, 0.0), pos.zw), 0.0);
1388 avg += textureProjLod(shadow, vec4(pos.xy + vec2(-shadow_pixel_size, 0.0), pos.zw), 0.0);
1389 avg += textureProjLod(shadow, vec4(pos.xy + vec2(0.0, shadow_pixel_size), pos.zw), 0.0);
1390 avg += textureProjLod(shadow, vec4(pos.xy + vec2(0.0, -shadow_pixel_size), pos.zw), 0.0);
1391 return avg * (1.0 / 5.0);
1392
1393 #endif
1394
1395 return avg;
1396 }
1397 #endif //!defined(ADDITIVE_OMNI)
1398 #endif // USE_ADDITIVE_LIGHTING
1399
1400 #endif // !MODE_RENDER_DEPTH
1401
1402 #ifndef DISABLE_LIGHTMAP
1403 #ifdef USE_LIGHTMAP
1404 uniform mediump sampler2DArray lightmap_textures; //texunit:-4
1405 uniform lowp sampler2DArray shadowmask_textures; //texunit:-5
1406 uniform lowp uint lightmap_slice;
1407 uniform highp vec4 lightmap_uv_scale;
1408 uniform float lightmap_exposure_normalization;
1409 uniform uint lightmap_shadowmask_mode;
1410
1411 #define SHADOWMASK_MODE_NONE uint(0)
1412 #define SHADOWMASK_MODE_REPLACE uint(1)
1413 #define SHADOWMASK_MODE_OVERLAY uint(2)
1414 #define SHADOWMASK_MODE_ONLY uint(3)
1415
1416 #ifdef LIGHTMAP_BICUBIC_FILTER
1417 uniform highp vec2 lightmap_texture_size;
1418 #endif
1419
1420 #ifdef USE_SH_LIGHTMAP
1421 uniform mediump mat3 lightmap_normal_xform;
1422 #endif // USE_SH_LIGHTMAP
1423 #endif // USE_LIGHTMAP
1424
1425 #ifdef USE_LIGHTMAP_CAPTURE
1426 uniform mediump vec4[9] lightmap_captures;
1427 #endif // USE_LIGHTMAP_CAPTURE
1428 #endif // !DISABLE_LIGHTMAP
1429
1430 #ifdef USE_MULTIVIEW
1431 uniform highp sampler2DArray depth_buffer; // texunit:-7
1432 uniform highp sampler2DArray color_buffer; // texunit:-6
1433 vec3 multiview_uv(vec2 uv) {
1434 return vec3(uv, ViewIndex);
1435 }
1436 ivec3 multiview_uv(ivec2 uv) {
1437 return ivec3(uv, int(ViewIndex));
1438 }
1439 #else
1440 uniform highp sampler2D depth_buffer; // texunit:-7
1441 uniform highp sampler2D color_buffer; // texunit:-6
1442 vec2 multiview_uv(vec2 uv) {
1443 return uv;
1444 }
1445 ivec2 multiview_uv(ivec2 uv) {
1446 return uv;
1447 }
1448 #endif
1449
1450 uniform mediump float opaque_prepass_threshold;
1451 #endif // !RENDER_MOTION_VECTORS
1452
1453 #if defined(RENDER_MATERIAL)
1454 layout(location = 0) out vec4 albedo_output_buffer;
1455 layout(location = 1) out vec4 normal_output_buffer;
1456 layout(location = 2) out vec4 orm_output_buffer;
1457 layout(location = 3) out vec4 emission_output_buffer;
1458
1459 #else // !RENDER_MATERIAL
1460
1461 #ifndef RENDER_MOTION_VECTORS
1462 // Normal color rendering.
1463 layout(location = 0) out vec4 frag_color;
1464 #else
1465 layout(location = 0) out vec4 motion_vectors;
1466 #endif // !RENDER_MOTION_VECTORS
1467
1468 #endif // !RENDER_MATERIAL
1469
1470 /* clang-format off */
1471
1472 #GLOBALS
1473
1474 /* clang-format on */
1475
1476 #ifndef RENDER_MOTION_VECTORS
1477 vec3 F0(float metallic, float specular, vec3 albedo) {
1478 float dielectric = 0.16 * specular * specular;
1479 // use albedo * metallic as colored specular reflectance at 0 angle for metallic materials;
1480 // see https://google.github.io/filament/Filament.md.html
1481 return mix(vec3(dielectric), albedo, vec3(metallic));
1482 }
1483 #ifndef MODE_RENDER_DEPTH
1484
1485 #ifndef USE_VERTEX_LIGHTING
1486 #if !defined(DISABLE_LIGHT_DIRECTIONAL) || !defined(DISABLE_LIGHT_OMNI) || !defined(DISABLE_LIGHT_SPOT) || defined(USE_ADDITIVE_LIGHTING)
1487
1488 float D_GGX(float cos_theta_m, float alpha) {
1489 float a = cos_theta_m * alpha;
1490 float k = alpha / (1.0 - cos_theta_m * cos_theta_m + a * a);
1491 return k * k * (1.0 / M_PI);
1492 }
1493
1494 // From Earl Hammon, Jr. "PBR Diffuse Lighting for GGX+Smith Microsurfaces" https://www.gdcvault.com/play/1024478/PBR-Diffuse-Lighting-for-GGX
1495 float V_GGX(float NdotL, float NdotV, float alpha) {
1496 return 0.5 / mix(2.0 * NdotL * NdotV, NdotL + NdotV, alpha);
1497 }
1498
1499 float D_GGX_anisotropic(float cos_theta_m, float alpha_x, float alpha_y, float cos_phi, float sin_phi) {
1500 float alpha2 = alpha_x * alpha_y;
1501 highp vec3 v = vec3(alpha_y * cos_phi, alpha_x * sin_phi, alpha2 * cos_theta_m);
1502 highp float v2 = dot(v, v);
1503 float w2 = alpha2 / v2;
1504 float D = alpha2 * w2 * w2 * (1.0 / M_PI);
1505 return D;
1506 }
1507
1508 float V_GGX_anisotropic(float alpha_x, float alpha_y, float TdotV, float TdotL, float BdotV, float BdotL, float NdotV, float NdotL) {
1509 float Lambda_V = NdotL * length(vec3(alpha_x * TdotV, alpha_y * BdotV, NdotV));
1510 float Lambda_L = NdotV * length(vec3(alpha_x * TdotL, alpha_y * BdotL, NdotL));
1511 return 0.5 / (Lambda_V + Lambda_L);
1512 }
1513
1514 float SchlickFresnel(float u) {
1515 float m = 1.0 - u;
1516 float m2 = m * m;
1517 return m2 * m2 * m; // pow(m,5)
1518 }
1519
1520 void light_compute(vec3 N, vec3 L, vec3 V, float A, vec3 light_color, bool is_directional, float attenuation, vec3 f0, float roughness, float metallic, float specular_amount, vec3 albedo, inout float alpha, vec2 screen_uv,
1521 #ifdef LIGHT_BACKLIGHT_USED
1522 vec3 backlight,
1523 #endif
1524 #ifdef LIGHT_RIM_USED
1525 float rim, float rim_tint,
1526 #endif
1527 #ifdef LIGHT_CLEARCOAT_USED
1528 float clearcoat, float clearcoat_roughness, vec3 vertex_normal,
1529 #endif
1530 #ifdef LIGHT_ANISOTROPY_USED
1531 vec3 B, vec3 T, float anisotropy,
1532 #endif
1533 inout vec3 diffuse_light, inout vec3 specular_light) {
1534
1535 #if defined(LIGHT_CODE_USED)
1536 // light is written by the light shader
1537
1538 highp mat4 model_matrix = world_transform;
1539 mat4 projection_matrix = scene_data_block.data.projection_matrix;
1540 mat4 inv_projection_matrix = scene_data_block.data.inv_projection_matrix;
1541
1542 vec3 normal = N;
1543 vec3 light = L;
1544 vec3 view = V;
1545
1546 /* clang-format off */
1547
1548 #CODE : LIGHT
1549
1550 /* clang-format on */
1551
1552 #else
1553 float NdotL = min(A + dot(N, L), 1.0);
1554 float cNdotL = max(NdotL, 0.0); // clamped NdotL
1555 float NdotV = dot(N, V);
1556 float cNdotV = max(NdotV, 1e-4);
1557
1558 #if defined(DIFFUSE_BURLEY) || defined(SPECULAR_SCHLICK_GGX) || defined(LIGHT_CLEARCOAT_USED)
1559 vec3 H = normalize(V + L);
1560 #endif
1561
1562 #if defined(SPECULAR_SCHLICK_GGX)
1563 float cNdotH = clamp(A + dot(N, H), 0.0, 1.0);
1564 #endif
1565
1566 #if defined(DIFFUSE_BURLEY) || defined(SPECULAR_SCHLICK_GGX) || defined(LIGHT_CLEARCOAT_USED)
1567 float cLdotH = clamp(A + dot(L, H), 0.0, 1.0);
1568 #endif
1569
1570 if (metallic < 1.0) {
1571 float diffuse_brdf_NL; // BRDF times N.L for calculating diffuse radiance
1572
1573 #if defined(DIFFUSE_LAMBERT_WRAP)
1574 // Energy conserving lambert wrap shader.
1575 // https://web.archive.org/web/20210228210901/http://blog.stevemcauley.com/2011/12/03/energy-conserving-wrapped-diffuse/
1576 diffuse_brdf_NL = max(0.0, (NdotL + roughness) / ((1.0 + roughness) * (1.0 + roughness))) * (1.0 / M_PI);
1577 #elif defined(DIFFUSE_TOON)
1578 diffuse_brdf_NL = smoothstep(-roughness, max(roughness, 0.01), NdotL) * (1.0 / M_PI);
1579 #elif defined(DIFFUSE_BURLEY)
1580 {
1581 float FD90_minus_1 = 2.0 * cLdotH * cLdotH * roughness - 0.5;
1582 float FdV = 1.0 + FD90_minus_1 * SchlickFresnel(cNdotV);
1583 float FdL = 1.0 + FD90_minus_1 * SchlickFresnel(cNdotL);
1584 diffuse_brdf_NL = (1.0 / M_PI) * FdV * FdL * cNdotL;
1585 }
1586 #else
1587 // Lambert
1588 diffuse_brdf_NL = cNdotL * (1.0 / M_PI);
1589 #endif
1590
1591 diffuse_light += light_color * diffuse_brdf_NL * attenuation;
1592
1593 #if defined(LIGHT_BACKLIGHT_USED)
1594 diffuse_light += light_color * (vec3(1.0 / M_PI) - diffuse_brdf_NL) * backlight * attenuation;
1595 #endif
1596
1597 #if defined(LIGHT_RIM_USED)
1598 // Epsilon min to prevent pow(0, 0) singularity which results in undefined behavior.
1599 float rim_light = pow(max(1e-4, 1.0 - cNdotV), max(0.0, (1.0 - roughness) * 16.0));
1600 diffuse_light += rim_light * rim * mix(vec3(1.0), albedo, rim_tint) * light_color;
1601 #endif
1602 }
1603
1604 if (roughness > 0.0) { // FIXME: roughness == 0 should not disable specular light entirely
1605
1606 // D
1607
1608 #if defined(SPECULAR_TOON)
1609
1610 vec3 R = normalize(-reflect(L, N));
1611 float RdotV = dot(R, V);
1612 float mid = 1.0 - roughness;
1613 mid *= mid;
1614 float intensity = smoothstep(mid - roughness * 0.5, mid + roughness * 0.5, RdotV) * mid;
1615 diffuse_light += light_color * intensity * attenuation * specular_amount; // write to diffuse_light, as in toon shading you generally want no reflection
1616
1617 #elif defined(SPECULAR_DISABLED)
1618 // none..
1619
1620 #elif defined(SPECULAR_SCHLICK_GGX)
1621 // shlick+ggx as default
1622 float alpha_ggx = roughness * roughness;
1623 #if defined(LIGHT_ANISOTROPY_USED)
1624 float aspect = sqrt(1.0 - anisotropy * 0.9);
1625 float ax = alpha_ggx / aspect;
1626 float ay = alpha_ggx * aspect;
1627 float XdotH = dot(T, H);
1628 float YdotH = dot(B, H);
1629 float D = D_GGX_anisotropic(cNdotH, ax, ay, XdotH, YdotH);
1630 float G = V_GGX_anisotropic(ax, ay, dot(T, V), dot(T, L), dot(B, V), dot(B, L), cNdotV, cNdotL);
1631 #else
1632 float D = D_GGX(cNdotH, alpha_ggx);
1633 float G = V_GGX(cNdotL, cNdotV, alpha_ggx);
1634 #endif // LIGHT_ANISOTROPY_USED
1635 // F
1636 float cLdotH5 = SchlickFresnel(cLdotH);
1637 // Calculate Fresnel using cheap approximate specular occlusion term from Filament:
1638 // https://google.github.io/filament/Filament.html#lighting/occlusion/specularocclusion
1639 float f90 = clamp(50.0 * f0.g, 0.0, 1.0);
1640 vec3 F = f0 + (f90 - f0) * cLdotH5;
1641
1642 vec3 specular_brdf_NL = cNdotL * D * F * G;
1643
1644 specular_light += specular_brdf_NL * light_color * attenuation * specular_amount;
1645 #endif
1646
1647 #if defined(LIGHT_CLEARCOAT_USED)
1648 // Clearcoat ignores normal_map, use vertex normal instead
1649 float ccNdotL = max(min(A + dot(vertex_normal, L), 1.0), 0.0);
1650 float ccNdotH = clamp(A + dot(vertex_normal, H), 0.0, 1.0);
1651 float ccNdotV = max(dot(vertex_normal, V), 1e-4);
1652
1653 #if !defined(SPECULAR_SCHLICK_GGX)
1654 float cLdotH5 = SchlickFresnel(cLdotH);
1655 #endif
1656 float Dr = D_GGX(ccNdotH, mix(0.001, 0.1, clearcoat_roughness));
1657 float Gr = 0.25 / (cLdotH * cLdotH + 1e-4);
1658 float Fr = mix(.04, 1.0, cLdotH5);
1659 float clearcoat_specular_brdf_NL = clearcoat * Gr * Fr * Dr * cNdotL;
1660
1661 specular_light += clearcoat_specular_brdf_NL * light_color * attenuation * specular_amount;
1662 // TODO: Clearcoat adds light to the scene right now (it is non-energy conserving), both diffuse and specular need to be scaled by (1.0 - FR)
1663 // but to do so we need to rearrange this entire function
1664 #endif // LIGHT_CLEARCOAT_USED
1665 }
1666
1667 #ifdef USE_SHADOW_TO_OPACITY
1668 alpha = min(alpha, clamp(1.0 - attenuation, 0.0, 1.0));
1669 #endif
1670
1671 #endif // LIGHT_CODE_USED
1672 }
1673
1674 float get_omni_spot_attenuation(float distance, float inv_range, float decay) {
1675 float nd = distance * inv_range;
1676 nd *= nd;
1677 nd *= nd; // nd^4
1678 nd = max(1.0 - nd, 0.0);
1679 nd *= nd; // nd^2
1680 return nd * pow(max(distance, 0.0001), -decay);
1681 }
1682
1683 #if !defined(DISABLE_LIGHT_OMNI) || defined(ADDITIVE_OMNI)
1684 void light_process_omni(uint idx, vec3 vertex, vec3 eye_vec, vec3 normal, vec3 f0, float roughness, float metallic, float shadow, vec3 albedo, inout float alpha, vec2 screen_uv,
1685 #ifdef LIGHT_BACKLIGHT_USED
1686 vec3 backlight,
1687 #endif
1688 #ifdef LIGHT_RIM_USED
1689 float rim, float rim_tint,
1690 #endif
1691 #ifdef LIGHT_CLEARCOAT_USED
1692 float clearcoat, float clearcoat_roughness, vec3 vertex_normal,
1693 #endif
1694 #ifdef LIGHT_ANISOTROPY_USED
1695 vec3 binormal, vec3 tangent, float anisotropy,
1696 #endif
1697 inout vec3 diffuse_light, inout vec3 specular_light) {
1698 vec3 light_rel_vec = omni_lights[idx].position - vertex;
1699 float light_length = length(light_rel_vec);
1700 float omni_attenuation = get_omni_spot_attenuation(light_length, omni_lights[idx].inv_radius, omni_lights[idx].attenuation);
1701 vec3 color = omni_lights[idx].color;
1702 float size_A = 0.0;
1703
1704 if (omni_lights[idx].size > 0.0) {
1705 float t = omni_lights[idx].size / max(0.001, light_length);
1706 size_A = max(0.0, 1.0 - 1.0 / sqrt(1.0 + t * t));
1707 }
1708
1709 omni_attenuation *= shadow;
1710
1711 light_compute(normal, normalize(light_rel_vec), eye_vec, size_A, color, false, omni_attenuation, f0, roughness, metallic, omni_lights[idx].specular_amount, albedo, alpha, screen_uv,
1712 #ifdef LIGHT_BACKLIGHT_USED
1713 backlight,
1714 #endif
1715 #ifdef LIGHT_RIM_USED
1716 rim * omni_attenuation, rim_tint,
1717 #endif
1718 #ifdef LIGHT_CLEARCOAT_USED
1719 clearcoat, clearcoat_roughness, vertex_normal,
1720 #endif
1721 #ifdef LIGHT_ANISOTROPY_USED
1722 binormal, tangent, anisotropy,
1723 #endif
1724 diffuse_light,
1725 specular_light);
1726 }
1727 #endif // !DISABLE_LIGHT_OMNI
1728
1729 #if !defined(DISABLE_LIGHT_SPOT) || defined(ADDITIVE_SPOT)
1730 void light_process_spot(uint idx, vec3 vertex, vec3 eye_vec, vec3 normal, vec3 f0, float roughness, float metallic, float shadow, vec3 albedo, inout float alpha, vec2 screen_uv,
1731 #ifdef LIGHT_BACKLIGHT_USED
1732 vec3 backlight,
1733 #endif
1734 #ifdef LIGHT_RIM_USED
1735 float rim, float rim_tint,
1736 #endif
1737 #ifdef LIGHT_CLEARCOAT_USED
1738 float clearcoat, float clearcoat_roughness, vec3 vertex_normal,
1739 #endif
1740 #ifdef LIGHT_ANISOTROPY_USED
1741 vec3 binormal, vec3 tangent, float anisotropy,
1742 #endif
1743 inout vec3 diffuse_light,
1744 inout vec3 specular_light) {
1745
1746 vec3 light_rel_vec = spot_lights[idx].position - vertex;
1747 float light_length = length(light_rel_vec);
1748 float spot_attenuation = get_omni_spot_attenuation(light_length, spot_lights[idx].inv_radius, spot_lights[idx].attenuation);
1749 vec3 spot_dir = spot_lights[idx].direction;
1750 float scos = max(dot(-normalize(light_rel_vec), spot_dir), spot_lights[idx].cone_angle);
1751 float spot_rim = max(0.0001, (1.0 - scos) / (1.0 - spot_lights[idx].cone_angle));
1752
1753 mediump float cone_attenuation = spot_lights[idx].cone_attenuation;
1754 spot_attenuation *= 1.0 - pow(spot_rim, cone_attenuation);
1755
1756 vec3 color = spot_lights[idx].color;
1757
1758 float size_A = 0.0;
1759
1760 if (spot_lights[idx].size > 0.0) {
1761 float t = spot_lights[idx].size / max(0.001, light_length);
1762 size_A = max(0.0, 1.0 - 1.0 / sqrt(1.0 + t * t));
1763 }
1764
1765 spot_attenuation *= shadow;
1766
1767 light_compute(normal, normalize(light_rel_vec), eye_vec, size_A, color, false, spot_attenuation, f0, roughness, metallic, spot_lights[idx].specular_amount, albedo, alpha, screen_uv,
1768 #ifdef LIGHT_BACKLIGHT_USED
1769 backlight,
1770 #endif
1771 #ifdef LIGHT_RIM_USED
1772 rim * spot_attenuation, rim_tint,
1773 #endif
1774 #ifdef LIGHT_CLEARCOAT_USED
1775 clearcoat, clearcoat_roughness, vertex_normal,
1776 #endif
1777 #ifdef LIGHT_ANISOTROPY_USED
1778 binormal, tangent, anisotropy,
1779 #endif
1780 diffuse_light, specular_light);
1781 }
1782 #endif // !defined(DISABLE_LIGHT_SPOT) || defined(ADDITIVE_SPOT)
1783
1784 #endif // !defined(DISABLE_LIGHT_DIRECTIONAL) || !defined(DISABLE_LIGHT_OMNI) || !defined(DISABLE_LIGHT_SPOT)
1785 #endif // !USE_VERTEX_LIGHTING
1786
1787 vec4 fog_process(vec3 vertex) {
1788 vec3 fog_color = scene_data_block.data.fog_light_color;
1789
1790 #ifdef USE_RADIANCE_MAP
1791 /*
1792 if (scene_data_block.data.fog_aerial_perspective > 0.0) {
1793 vec3 sky_fog_color = vec3(0.0);
1794 vec3 cube_view = scene_data_block.data.radiance_inverse_xform * vertex;
1795 // mip_level always reads from the second mipmap and higher so the fog is always slightly blurred
1796 float mip_level = mix(1.0 / MAX_ROUGHNESS_LOD, 1.0, 1.0 - (abs(vertex.z) - scene_data_block.data.z_near) / (scene_data_block.data.z_far - scene_data_block.data.z_near));
1797
1798 sky_fog_color = textureLod(radiance_map, cube_view, mip_level * RADIANCE_MAX_LOD).rgb;
1799
1800 fog_color = mix(fog_color, sky_fog_color, scene_data_block.data.fog_aerial_perspective);
1801 }
1802 */
1803 #endif
1804
1805 #ifdef USE_SUN_SCATTER
1806 vec4 sun_scatter = vec4(0.0);
1807 float sun_total = 0.0;
1808 vec3 view = normalize(vertex);
1809 for (uint i = uint(0); i < scene_data_block.data.directional_light_count; i++) {
1810 vec3 light_color = directional_lights[i].color * directional_lights[i].energy;
1811 float light_amount = pow(max(dot(view, directional_lights[i].direction), 0.0), 8.0);
1812 fog_color += light_color * light_amount * scene_data_block.data.fog_sun_scatter;
1813 }
1814 for (uint i = uint(MAX_DIRECTIONAL_LIGHT_DATA_STRUCTS) - uint(scene_data_block.data.directional_shadow_count); i < uint(MAX_DIRECTIONAL_LIGHT_DATA_STRUCTS); i++) {
1815 vec3 light_color = directional_lights[i].color * directional_lights[i].energy;
1816 float light_amount = pow(max(dot(view, directional_lights[i].direction), 0.0), 8.0);
1817 fog_color += light_color * light_amount * scene_data_block.data.fog_sun_scatter;
1818 }
1819 #endif // USE_SUN_SCATTER
1820
1821 float fog_amount = 0.0;
1822
1823 #ifdef USE_DEPTH_FOG
1824 float fog_z = smoothstep(scene_data_block.data.fog_depth_begin, scene_data_block.data.fog_depth_end, length(vertex));
1825 fog_amount = pow(fog_z, scene_data_block.data.fog_depth_curve) * scene_data_block.data.fog_density;
1826 #else
1827 fog_amount = 1.0 - exp(min(0.0, -length(vertex) * scene_data_block.data.fog_density));
1828 #endif // USE_DEPTH_FOG
1829
1830 if (abs(scene_data_block.data.fog_height_density) >= 0.0001) {
1831 float y = (scene_data_block.data.inv_view_matrix * vec4(vertex, 1.0)).y;
1832
1833 float y_dist = y - scene_data_block.data.fog_height;
1834
1835 float vfog_amount = 1.0 - exp(min(0.0, y_dist * scene_data_block.data.fog_height_density));
1836
1837 fog_amount = max(vfog_amount, fog_amount);
1838 }
1839
1840 return vec4(fog_color, fog_amount);
1841 }
1842
1843 #ifndef DISABLE_REFLECTION_PROBE
1844
1845 #define REFLECTION_AMBIENT_DISABLED 0
1846 #define REFLECTION_AMBIENT_ENVIRONMENT 1
1847 #define REFLECTION_AMBIENT_COLOR 2
1848
1849 void reflection_process(samplerCube reflection_map,
1850 vec3 normal, vec3 vertex,
1851 mat4 local_matrix,
1852 bool use_box_project, vec3 box_extents, vec3 box_offset,
1853 bool exterior, float intensity, float blend_distance, int ref_ambient_mode, vec4 ref_ambient_color,
1854 float roughness, vec3 ambient, vec3 skybox,
1855 inout highp vec4 reflection_accum, inout highp vec4 ambient_accum) {
1856 vec4 reflection;
1857
1858 vec3 local_pos = (local_matrix * vec4(vertex, 1.0)).xyz;
1859
1860 if (any(greaterThan(abs(local_pos), box_extents))) { //out of the reflection box
1861 return;
1862 }
1863
1864 float blend = 1.0;
1865 if (blend_distance != 0.0) {
1866 vec3 axis_blend_distance = min(vec3(blend_distance), box_extents);
1867 vec3 blend_axes = abs(local_pos) - box_extents + axis_blend_distance;
1868 blend_axes /= axis_blend_distance;
1869 blend_axes = clamp(1.0 - blend_axes, vec3(0.0), vec3(1.0));
1870
1871 blend = pow(blend_axes.x * blend_axes.y * blend_axes.z, 2.0);
1872 }
1873
1874 //reflect and make local
1875 vec3 ref_normal = normalize(reflect(vertex, normal));
1876 ref_normal = (local_matrix * vec4(ref_normal, 0.0)).xyz;
1877
1878 if (use_box_project) { //box project
1879
1880 vec3 nrdir = normalize(ref_normal);
1881 vec3 rbmax = (box_extents - local_pos) / nrdir;
1882 vec3 rbmin = (-box_extents - local_pos) / nrdir;
1883
1884 vec3 rbminmax = mix(rbmin, rbmax, vec3(greaterThan(nrdir, vec3(0.0, 0.0, 0.0))));
1885
1886 float fa = min(min(rbminmax.x, rbminmax.y), rbminmax.z);
1887 vec3 posonbox = local_pos + nrdir * fa;
1888 ref_normal = posonbox - box_offset.xyz;
1889 }
1890
1891 reflection.rgb = srgb_to_linear(textureLod(reflection_map, ref_normal, roughness * MAX_ROUGHNESS_LOD).rgb);
1892
1893 if (exterior) {
1894 reflection.rgb = mix(skybox, reflection.rgb, blend);
1895 }
1896 reflection.rgb *= intensity;
1897 reflection.a = blend;
1898 reflection.rgb *= blend;
1899
1900 reflection_accum += reflection;
1901
1902 #ifndef USE_LIGHTMAP
1903 if (ref_ambient_mode == REFLECTION_AMBIENT_ENVIRONMENT) {
1904 vec4 ambient_out;
1905 vec3 amb_normal = (local_matrix * vec4(normal, 0.0)).xyz;
1906
1907 ambient_out.rgb = srgb_to_linear(textureLod(reflection_map, amb_normal, MAX_ROUGHNESS_LOD).rgb);
1908 if (exterior) {
1909 ambient_out.rgb = mix(ambient, ambient_out.rgb, blend);
1910 }
1911
1912 ambient_out.a = blend;
1913 ambient_out.rgb *= blend;
1914 ambient_accum += ambient_out;
1915 } else if (ref_ambient_mode == REFLECTION_AMBIENT_COLOR) {
1916 vec4 ambient_out;
1917 ambient_out.rgb = ref_ambient_color.rgb;
1918 if (exterior) {
1919 ambient_out.rgb = mix(ambient, ambient_out.rgb, blend);
1920 }
1921
1922 ambient_out.a = blend;
1923 ambient_out.rgb *= blend;
1924 ambient_accum += ambient_out;
1925 }
1926 #endif // USE_LIGHTMAP
1927 }
1928
1929 #endif // DISABLE_REFLECTION_PROBE
1930
1931 #endif // !MODE_RENDER_DEPTH
1932
1933 #ifdef LIGHTMAP_BICUBIC_FILTER
1934 // w0, w1, w2, and w3 are the four cubic B-spline basis functions
1935 float w0(float a) {
1936 return (1.0 / 6.0) * (a * (a * (-a + 3.0) - 3.0) + 1.0);
1937 }
1938
1939 float w1(float a) {
1940 return (1.0 / 6.0) * (a * a * (3.0 * a - 6.0) + 4.0);
1941 }
1942
1943 float w2(float a) {
1944 return (1.0 / 6.0) * (a * (a * (-3.0 * a + 3.0) + 3.0) + 1.0);
1945 }
1946
1947 float w3(float a) {
1948 return (1.0 / 6.0) * (a * a * a);
1949 }
1950
1951 // g0 and g1 are the two amplitude functions
1952 float g0(float a) {
1953 return w0(a) + w1(a);
1954 }
1955
1956 float g1(float a) {
1957 return w2(a) + w3(a);
1958 }
1959
1960 // h0 and h1 are the two offset functions
1961 float h0(float a) {
1962 return -1.0 + w1(a) / (w0(a) + w1(a));
1963 }
1964
1965 float h1(float a) {
1966 return 1.0 + w3(a) / (w2(a) + w3(a));
1967 }
1968
1969 vec4 textureArray_bicubic(sampler2DArray tex, vec3 uv, vec2 texture_size) {
1970 vec2 texel_size = vec2(1.0) / texture_size;
1971
1972 uv.xy = uv.xy * texture_size + vec2(0.5);
1973
1974 vec2 iuv = floor(uv.xy);
1975 vec2 fuv = fract(uv.xy);
1976
1977 float g0x = g0(fuv.x);
1978 float g1x = g1(fuv.x);
1979 float h0x = h0(fuv.x);
1980 float h1x = h1(fuv.x);
1981 float h0y = h0(fuv.y);
1982 float h1y = h1(fuv.y);
1983
1984 vec2 p0 = (vec2(iuv.x + h0x, iuv.y + h0y) - vec2(0.5)) * texel_size;
1985 vec2 p1 = (vec2(iuv.x + h1x, iuv.y + h0y) - vec2(0.5)) * texel_size;
1986 vec2 p2 = (vec2(iuv.x + h0x, iuv.y + h1y) - vec2(0.5)) * texel_size;
1987 vec2 p3 = (vec2(iuv.x + h1x, iuv.y + h1y) - vec2(0.5)) * texel_size;
1988
1989 return (g0(fuv.y) * (g0x * texture(tex, vec3(p0, uv.z)) + g1x * texture(tex, vec3(p1, uv.z)))) +
1990 (g1(fuv.y) * (g0x * texture(tex, vec3(p2, uv.z)) + g1x * texture(tex, vec3(p3, uv.z))));
1991 }
1992 #endif //LIGHTMAP_BICUBIC_FILTER
1993 #endif // RENDER_MOTION_VECTORS
1994
1995 void main() {
1996 #ifndef RENDER_MOTION_VECTORS
1997 //lay out everything, whatever is unused is optimized away anyway
1998 vec3 vertex = vertex_interp;
1999 #ifdef USE_MULTIVIEW
2000 vec3 eye_offset = multiview_data_block.data.eye_offset[ViewIndex].xyz;
2001 vec3 view = -normalize(vertex_interp - eye_offset);
2002 mat4 projection_matrix = multiview_data_block.data.projection_matrix_view[ViewIndex];
2003 mat4 inv_projection_matrix = multiview_data_block.data.inv_projection_matrix_view[ViewIndex];
2004 #else
2005 vec3 eye_offset = vec3(0.0, 0.0, 0.0);
2006 vec3 view = -normalize(vertex_interp);
2007 mat4 projection_matrix = scene_data_block.data.projection_matrix;
2008 mat4 inv_projection_matrix = scene_data_block.data.inv_projection_matrix;
2009 #endif
2010 highp mat4 model_matrix = world_transform;
2011 vec3 albedo = vec3(1.0);
2012 vec3 backlight = vec3(0.0);
2013 vec4 transmittance_color = vec4(0.0, 0.0, 0.0, 1.0);
2014 float transmittance_depth = 0.0;
2015 float transmittance_boost = 0.0;
2016 float metallic = 0.0;
2017 float specular = 0.5;
2018 vec3 emission = vec3(0.0);
2019 float roughness = 1.0;
2020 float rim = 0.0;
2021 float rim_tint = 0.0;
2022 float clearcoat = 0.0;
2023 float clearcoat_roughness = 0.0;
2024 float anisotropy = 0.0;
2025 vec2 anisotropy_flow = vec2(1.0, 0.0);
2026 #ifdef PREMUL_ALPHA_USED
2027 float premul_alpha = 1.0;
2028 #endif // PREMUL_ALPHA_USED
2029 vec4 fog = vec4(0.0);
2030 #if defined(CUSTOM_RADIANCE_USED)
2031 vec4 custom_radiance = vec4(0.0);
2032 #endif
2033 #if defined(CUSTOM_IRRADIANCE_USED)
2034 vec4 custom_irradiance = vec4(0.0);
2035 #endif
2036
2037 float ao = 1.0;
2038 float ao_light_affect = 0.0;
2039
2040 float alpha = 1.0;
2041
2042 #if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED)
2043 vec3 binormal = binormal_interp;
2044 vec3 tangent = tangent_interp;
2045 #else
2046 vec3 binormal = vec3(0.0);
2047 vec3 tangent = vec3(0.0);
2048 #endif
2049
2050 #ifdef NORMAL_USED
2051 vec3 normal = normal_interp;
2052 #if defined(DO_SIDE_CHECK)
2053 if (!gl_FrontFacing) {
2054 normal = -normal;
2055 }
2056 #endif // DO_SIDE_CHECK
2057 #endif // NORMAL_USED
2058
2059 #ifdef UV_USED
2060 vec2 uv = uv_interp;
2061 #endif
2062
2063 #if defined(UV2_USED) || defined(USE_LIGHTMAP)
2064 vec2 uv2 = uv2_interp;
2065 #endif
2066
2067 #if defined(COLOR_USED)
2068 vec4 color = color_interp;
2069 #endif
2070
2071 #if defined(NORMAL_MAP_USED)
2072
2073 vec3 normal_map = vec3(0.5);
2074 #endif
2075
2076 #if defined(BENT_NORMAL_MAP_USED)
2077 vec3 bent_normal_map = vec3(0.5);
2078 #endif
2079
2080 float normal_map_depth = 1.0;
2081
2082 vec2 screen_uv = gl_FragCoord.xy * scene_data_block.data.screen_pixel_size;
2083
2084 float sss_strength = 0.0;
2085
2086 #ifdef ALPHA_SCISSOR_USED
2087 float alpha_scissor_threshold = 1.0;
2088 #endif // ALPHA_SCISSOR_USED
2089
2090 #ifdef ALPHA_HASH_USED
2091 float alpha_hash_scale = 1.0;
2092 #endif // ALPHA_HASH_USED
2093
2094 #ifdef ALPHA_ANTIALIASING_EDGE_USED
2095 float alpha_antialiasing_edge = 0.0;
2096 vec2 alpha_texture_coordinate = vec2(0.0, 0.0);
2097 #endif // ALPHA_ANTIALIASING_EDGE_USED
2098
2099 #ifdef LIGHT_VERTEX_USED
2100 vec3 light_vertex = vertex;
2101 #endif //LIGHT_VERTEX_USED
2102
2103 highp mat3 model_normal_matrix;
2104 if (bool(model_flags & uint(FLAGS_NON_UNIFORM_SCALE))) {
2105 model_normal_matrix = transpose(inverse(mat3(model_matrix)));
2106 } else {
2107 model_normal_matrix = mat3(model_matrix);
2108 }
2109
2110 {
2111 #CODE : FRAGMENT
2112 }
2113
2114 // Keep albedo values in positive number range as negative values "wraparound" into positive numbers resulting in wrong colors
2115 albedo = max(albedo, vec3(0.0));
2116
2117 #ifdef LIGHT_VERTEX_USED
2118 vertex = light_vertex;
2119 #ifdef USE_MULTIVIEW
2120 view = -normalize(vertex - eye_offset);
2121 #else
2122 view = -normalize(vertex);
2123 #endif //USE_MULTIVIEW
2124 #endif //LIGHT_VERTEX_USED
2125
2126 #ifdef NORMAL_USED
2127 vec3 geo_normal = normalize(normal);
2128 #endif // NORMAL_USED
2129
2130 #ifndef USE_SHADOW_TO_OPACITY
2131
2132 #if defined(ALPHA_SCISSOR_USED)
2133 #ifdef RENDER_MATERIAL
2134 if (alpha < alpha_scissor_threshold) {
2135 alpha = 0.0;
2136 } else {
2137 alpha = 1.0;
2138 }
2139 #else
2140 if (alpha < alpha_scissor_threshold) {
2141 discard;
2142 }
2143 alpha = 1.0;
2144 #endif // RENDER_MATERIAL
2145 #else
2146 #ifdef MODE_RENDER_DEPTH
2147 #ifdef USE_OPAQUE_PREPASS
2148
2149 if (alpha < opaque_prepass_threshold) {
2150 discard;
2151 }
2152 #endif // USE_OPAQUE_PREPASS
2153 #endif // MODE_RENDER_DEPTH
2154 #endif // !ALPHA_SCISSOR_USED
2155
2156 #endif // !USE_SHADOW_TO_OPACITY
2157
2158 #if defined(NORMAL_MAP_USED)
2159 normal_map.xy = normal_map.xy * 2.0 - 1.0;
2160 normal_map.z = sqrt(max(0.0, 1.0 - dot(normal_map.xy, normal_map.xy))); //always ignore Z, as it can be RG packed, Z may be pos/neg, etc.
2161
2162 // Tangent-space transformation is performed using unnormalized TBN vectors, per MikkTSpace.
2163 // See: http://www.mikktspace.com/
2164 normal = normalize(mix(normal, tangent * normal_map.x + binormal * normal_map.y + normal * normal_map.z, normal_map_depth));
2165 #elif defined(NORMAL_USED)
2166 normal = geo_normal;
2167 #endif // NORMAL_MAP_USED
2168
2169 #ifdef LIGHT_ANISOTROPY_USED
2170
2171 if (anisotropy > 0.01) {
2172 mat3 rot = mat3(normalize(tangent), normalize(binormal), normal);
2173 // Make local to space.
2174 tangent = normalize(rot * vec3(anisotropy_flow.x, anisotropy_flow.y, 0.0));
2175 binormal = normalize(rot * vec3(-anisotropy_flow.y, anisotropy_flow.x, 0.0));
2176 }
2177
2178 #endif
2179
2180 #ifndef MODE_RENDER_DEPTH
2181
2182 #ifndef FOG_DISABLED
2183 #ifndef CUSTOM_FOG_USED
2184 #ifndef DISABLE_FOG
2185 // fog must be processed as early as possible and then packed.
2186 // to maximize VGPR usage
2187
2188 if (scene_data_block.data.fog_enabled) {
2189 fog = fog_process(vertex);
2190 }
2191 #endif // !DISABLE_FOG
2192 #endif // !CUSTOM_FOG_USED
2193
2194 uint fog_rg = packHalf2x16(fog.rg);
2195 uint fog_ba = packHalf2x16(fog.ba);
2196 #endif // !FOG_DISABLED
2197
2198 // Convert colors to linear
2199 albedo = srgb_to_linear(albedo);
2200 emission = srgb_to_linear(emission);
2201 // TODO Backlight and transmittance when used
2202 #ifndef MODE_UNSHADED
2203 vec3 f0 = F0(metallic, specular, albedo);
2204 vec3 specular_light = vec3(0.0, 0.0, 0.0);
2205 vec3 diffuse_light = vec3(0.0, 0.0, 0.0);
2206 vec3 ambient_light = vec3(0.0, 0.0, 0.0);
2207
2208 #ifdef BASE_PASS
2209 /////////////////////// LIGHTING //////////////////////////////
2210
2211 #ifndef AMBIENT_LIGHT_DISABLED
2212 // IBL precalculations
2213 float ndotv = clamp(dot(normal, view), 0.0, 1.0);
2214 vec3 F = f0 + (max(vec3(1.0 - roughness), f0) - f0) * pow(1.0 - ndotv, 5.0);
2215
2216 #ifdef USE_RADIANCE_MAP
2217 if (scene_data_block.data.use_reflection_cubemap) {
2218 #ifdef LIGHT_ANISOTROPY_USED
2219 // https://google.github.io/filament/Filament.html#lighting/imagebasedlights/anisotropy
2220 vec3 anisotropic_direction = anisotropy >= 0.0 ? binormal : tangent;
2221 vec3 anisotropic_tangent = cross(anisotropic_direction, view);
2222 vec3 anisotropic_normal = cross(anisotropic_tangent, anisotropic_direction);
2223 vec3 bent_normal = normalize(mix(normal, anisotropic_normal, abs(anisotropy) * clamp(5.0 * roughness, 0.0, 1.0)));
2224 vec3 ref_vec = reflect(-view, bent_normal);
2225 #else
2226 vec3 ref_vec = reflect(-view, normal);
2227 #endif
2228 ref_vec = mix(ref_vec, normal, roughness * roughness);
2229 float horizon = min(1.0 + dot(ref_vec, normal), 1.0);
2230 ref_vec = mat3(scene_data_block.data.radiance_inverse_xform) * ref_vec;
2231 specular_light = textureLod(radiance_map, ref_vec, sqrt(roughness) * RADIANCE_MAX_LOD).rgb;
2232 specular_light = srgb_to_linear(specular_light);
2233 specular_light *= horizon * horizon;
2234 specular_light *= scene_data_block.data.ambient_light_color_energy.a;
2235 }
2236 #endif // USE_RADIANCE_MAP
2237
2238 // Calculate Reflection probes
2239 #ifndef DISABLE_REFLECTION_PROBE
2240 vec4 ambient_accum = vec4(0.0);
2241 {
2242 vec4 reflection_accum = vec4(0.0);
2243
2244 reflection_process(refprobe1_texture, normal, vertex_interp, refprobe1_local_matrix,
2245 refprobe1_use_box_project, refprobe1_box_extents, refprobe1_box_offset,
2246 refprobe1_exterior, refprobe1_intensity, refprobe1_blend_distance, refprobe1_ambient_mode, refprobe1_ambient_color,
2247 roughness, ambient_light, specular_light, reflection_accum, ambient_accum);
2248
2249 #ifdef SECOND_REFLECTION_PROBE
2250
2251 reflection_process(refprobe2_texture, normal, vertex_interp, refprobe2_local_matrix,
2252 refprobe2_use_box_project, refprobe2_box_extents, refprobe2_box_offset,
2253 refprobe2_exterior, refprobe2_intensity, refprobe2_blend_distance, refprobe2_ambient_mode, refprobe2_ambient_color,
2254 roughness, ambient_light, specular_light, reflection_accum, ambient_accum);
2255
2256 #endif // SECOND_REFLECTION_PROBE
2257
2258 if (reflection_accum.a > 0.0) {
2259 specular_light = reflection_accum.rgb / reflection_accum.a;
2260 }
2261 }
2262 #endif // DISABLE_REFLECTION_PROBE
2263
2264 #if defined(CUSTOM_RADIANCE_USED)
2265 specular_light = mix(specular_light, custom_radiance.rgb, custom_radiance.a);
2266 #endif // CUSTOM_RADIANCE_USED
2267
2268 #if !defined(USE_LIGHTMAP) && !defined(USE_LIGHTMAP_CAPTURE)
2269 //lightmap overrides everything
2270 if (scene_data_block.data.use_ambient_light) {
2271 ambient_light = scene_data_block.data.ambient_light_color_energy.rgb;
2272
2273 #ifdef USE_RADIANCE_MAP
2274 if (scene_data_block.data.use_ambient_cubemap) {
2275 vec3 ambient_dir = mat3(scene_data_block.data.radiance_inverse_xform) * normal;
2276 vec3 cubemap_ambient = textureLod(radiance_map, ambient_dir, RADIANCE_MAX_LOD).rgb;
2277 cubemap_ambient = srgb_to_linear(cubemap_ambient);
2278 ambient_light = mix(ambient_light, cubemap_ambient * scene_data_block.data.ambient_light_color_energy.a, scene_data_block.data.ambient_color_sky_mix);
2279 }
2280 #endif // USE_RADIANCE_MAP
2281
2282 #ifndef DISABLE_REFLECTION_PROBE
2283 if (ambient_accum.a > 0.0) {
2284 ambient_light = mix(ambient_light, (ambient_accum.rgb / ambient_accum.a) * scene_data_block.data.ambient_light_color_energy.a, scene_data_block.data.ambient_color_sky_mix);
2285 }
2286 #endif // DISABLE_REFLECTION_PROBE
2287 }
2288 #endif // !USE_LIGHTMAP && !USE_LIGHTMAP_CAPTURE
2289
2290 #if defined(CUSTOM_IRRADIANCE_USED)
2291 ambient_light = mix(ambient_light, custom_irradiance.rgb, custom_irradiance.a);
2292 #endif // CUSTOM_IRRADIANCE_USED
2293
2294 #ifndef DISABLE_LIGHTMAP
2295 #ifdef USE_LIGHTMAP_CAPTURE
2296 {
2297 // The world normal.
2298 vec3 wnormal = mat3(scene_data_block.data.inv_view_matrix) * normal;
2299
2300 // The SH coefficients used for evaluating diffuse data from SH probes.
2301 const float c0 = 0.886227; // l0 sqrt(1.0/(4.0*PI)) * PI
2302 const float c1 = 1.023327; // l1 sqrt(3.0/(4.0*PI)) * PI*2.0/3.0
2303 const float c2 = 0.858086; // l2n2, l2n1, l2p1 sqrt(15.0/(4.0*PI)) * PI*1.0/4.0
2304 const float c3 = 0.247708; // l20 sqrt(5.0/(16.0*PI)) * PI*1.0/4.0
2305 const float c4 = 0.429043; // l2p2 sqrt(15.0/(16.0*PI))* PI*1.0/4.0
2306
2307 ambient_light += (c0 * lightmap_captures[0].rgb +
2308 c1 * lightmap_captures[1].rgb * wnormal.y +
2309 c1 * lightmap_captures[2].rgb * wnormal.z +
2310 c1 * lightmap_captures[3].rgb * wnormal.x +
2311 c2 * lightmap_captures[4].rgb * wnormal.x * wnormal.y +
2312 c2 * lightmap_captures[5].rgb * wnormal.y * wnormal.z +
2313 c3 * lightmap_captures[6].rgb * (3.0 * wnormal.z * wnormal.z - 1.0) +
2314 c2 * lightmap_captures[7].rgb * wnormal.x * wnormal.z +
2315 c4 * lightmap_captures[8].rgb * (wnormal.x * wnormal.x - wnormal.y * wnormal.y)) *
2316 scene_data_block.data.IBL_exposure_normalization;
2317 }
2318 #else
2319 #ifdef USE_LIGHTMAP
2320 {
2321 vec3 uvw;
2322 uvw.xy = uv2 * lightmap_uv_scale.zw + lightmap_uv_scale.xy;
2323 uvw.z = float(lightmap_slice);
2324
2325 #ifdef USE_SH_LIGHTMAP
2326 uvw.z *= 4.0; // SH textures use 4 times more data.
2327
2328 #ifdef LIGHTMAP_BICUBIC_FILTER
2329 vec3 lm_light_l0 = textureArray_bicubic(lightmap_textures, uvw + vec3(0.0, 0.0, 0.0), lightmap_texture_size).rgb;
2330 vec3 lm_light_l1n1 = (textureArray_bicubic(lightmap_textures, uvw + vec3(0.0, 0.0, 1.0), lightmap_texture_size).rgb - vec3(0.5)) * 2.0;
2331 vec3 lm_light_l1_0 = (textureArray_bicubic(lightmap_textures, uvw + vec3(0.0, 0.0, 2.0), lightmap_texture_size).rgb - vec3(0.5)) * 2.0;
2332 vec3 lm_light_l1p1 = (textureArray_bicubic(lightmap_textures, uvw + vec3(0.0, 0.0, 3.0), lightmap_texture_size).rgb - vec3(0.5)) * 2.0;
2333 #else
2334 vec3 lm_light_l0 = textureLod(lightmap_textures, uvw + vec3(0.0, 0.0, 0.0), 0.0).rgb;
2335 vec3 lm_light_l1n1 = (textureLod(lightmap_textures, uvw + vec3(0.0, 0.0, 1.0), 0.0).rgb - vec3(0.5)) * 2.0;
2336 vec3 lm_light_l1_0 = (textureLod(lightmap_textures, uvw + vec3(0.0, 0.0, 2.0), 0.0).rgb - vec3(0.5)) * 2.0;
2337 vec3 lm_light_l1p1 = (textureLod(lightmap_textures, uvw + vec3(0.0, 0.0, 3.0), 0.0).rgb - vec3(0.5)) * 2.0;
2338 #endif
2339
2340 vec3 n = normalize(lightmap_normal_xform * normal);
2341
2342 ambient_light += lm_light_l0 * lightmap_exposure_normalization;
2343 ambient_light += lm_light_l1n1 * n.y * (lm_light_l0 * lightmap_exposure_normalization * 4.0);
2344 ambient_light += lm_light_l1_0 * n.z * (lm_light_l0 * lightmap_exposure_normalization * 4.0);
2345 ambient_light += lm_light_l1p1 * n.x * (lm_light_l0 * lightmap_exposure_normalization * 4.0);
2346 #else
2347 #ifdef LIGHTMAP_BICUBIC_FILTER
2348 ambient_light += textureArray_bicubic(lightmap_textures, uvw, lightmap_texture_size).rgb * lightmap_exposure_normalization;
2349 #else
2350 ambient_light += textureLod(lightmap_textures, uvw, 0.0).rgb * lightmap_exposure_normalization;
2351 #endif
2352 #endif
2353 }
2354 #endif // USE_LIGHTMAP
2355 #endif // USE_LIGHTMAP_CAPTURE
2356 #endif // !DISABLE_LIGHTMAP
2357
2358 ambient_light *= ao;
2359 #ifndef SPECULAR_OCCLUSION_DISABLED
2360 float specular_occlusion = (ambient_light.r * 0.3 + ambient_light.g * 0.59 + ambient_light.b * 0.11) * 2.0; // Luminance of ambient light.
2361 specular_occlusion = min(specular_occlusion * 4.0, 1.0); // This multiplication preserves speculars on bright areas.
2362
2363 float reflective_f = (1.0 - roughness) * metallic;
2364 // 10.0 is a magic number, it gives the intended effect in most scenarios.
2365 // Low enough for occlusion, high enough for reaction to lights and shadows.
2366 specular_occlusion = max(min(reflective_f * specular_occlusion * 10.0, 1.0), specular_occlusion);
2367 specular_light *= specular_occlusion;
2368 #endif // !SPECULAR_OCCLUSION_DISABLED
2369 ambient_light *= albedo.rgb;
2370
2371 #endif // !AMBIENT_LIGHT_DISABLED
2372
2373 // convert ao to direct light ao
2374 ao = mix(1.0, ao, ao_light_affect);
2375 #ifndef AMBIENT_LIGHT_DISABLED
2376 {
2377 #if defined(DIFFUSE_TOON)
2378 //simplify for toon, as
2379 specular_light *= specular * metallic * albedo * 2.0;
2380 #else
2381
2382 // scales the specular reflections, needs to be be computed before lighting happens,
2383 // but after environment, GI, and reflection probes are added
2384 // Environment brdf approximation (Lazarov 2013)
2385 // see https://www.unrealengine.com/en-US/blog/physically-based-shading-on-mobile
2386 const vec4 c0 = vec4(-1.0, -0.0275, -0.572, 0.022);
2387 const vec4 c1 = vec4(1.0, 0.0425, 1.04, -0.04);
2388 vec4 r = roughness * c0 + c1;
2389 float ndotv = clamp(dot(normal, view), 0.0, 1.0);
2390
2391 float a004 = min(r.x * r.x, exp2(-9.28 * ndotv)) * r.x + r.y;
2392 vec2 env = vec2(-1.04, 1.04) * a004 + r.zw;
2393 specular_light *= env.x * f0 + env.y * clamp(50.0 * f0.g, metallic, 1.0);
2394 #endif
2395 }
2396 #endif // !AMBIENT_LIGHT_DISABLED
2397
2398 #ifdef USE_VERTEX_LIGHTING
2399 specular_light += specular_light_interp * f0;
2400 diffuse_light += diffuse_light_interp;
2401 #else
2402
2403 #ifndef DISABLE_LIGHT_DIRECTIONAL
2404 for (uint i = uint(0); i < scene_data_block.data.directional_light_count; i++) {
2405 if (!bool(directional_lights[i].mask & layer_mask)) {
2406 continue;
2407 }
2408 #if defined(USE_LIGHTMAP) && !defined(DISABLE_LIGHTMAP)
2409 if (bool(directional_lights[i].enabled_bake_mode & DIRECTIONAL_LIGHT_BAKE_STATIC)) {
2410 continue;
2411 }
2412 #endif
2413 light_compute(normal, normalize(directional_lights[i].direction), normalize(view), directional_lights[i].size, directional_lights[i].color * directional_lights[i].energy, true, 1.0, f0, roughness, metallic, directional_lights[i].specular, albedo, alpha, screen_uv,
2414 #ifdef LIGHT_BACKLIGHT_USED
2415 backlight,
2416 #endif
2417 #ifdef LIGHT_RIM_USED
2418 rim, rim_tint,
2419 #endif
2420 #ifdef LIGHT_CLEARCOAT_USED
2421 clearcoat, clearcoat_roughness, geo_normal,
2422 #endif // LIGHT_CLEARCOAT_USED
2423 #ifdef LIGHT_ANISOTROPY_USED
2424 binormal,
2425 tangent, anisotropy,
2426 #endif
2427 diffuse_light,
2428 specular_light);
2429 }
2430 #endif // !DISABLE_LIGHT_DIRECTIONAL
2431
2432 #ifndef DISABLE_LIGHT_OMNI
2433 for (uint i = 0u; i < MAX_FORWARD_LIGHTS; i++) {
2434 if (i >= omni_light_count) {
2435 break;
2436 }
2437
2438 light_process_omni(omni_light_indices[i], vertex, view, normal, f0, roughness, metallic, 1.0, albedo, alpha, screen_uv,
2439 #ifdef LIGHT_BACKLIGHT_USED
2440 backlight,
2441 #endif
2442 #ifdef LIGHT_RIM_USED
2443 rim,
2444 rim_tint,
2445 #endif
2446 #ifdef LIGHT_CLEARCOAT_USED
2447 clearcoat, clearcoat_roughness, geo_normal,
2448 #endif // LIGHT_CLEARCOAT_USED
2449 #ifdef LIGHT_ANISOTROPY_USED
2450 binormal, tangent, anisotropy,
2451 #endif
2452 diffuse_light, specular_light);
2453 }
2454 #endif // !DISABLE_LIGHT_OMNI
2455
2456 #ifndef DISABLE_LIGHT_SPOT
2457 for (uint i = 0u; i < MAX_FORWARD_LIGHTS; i++) {
2458 if (i >= spot_light_count) {
2459 break;
2460 }
2461
2462 light_process_spot(spot_light_indices[i], vertex, view, normal, f0, roughness, metallic, 1.0, albedo, alpha, screen_uv,
2463 #ifdef LIGHT_BACKLIGHT_USED
2464 backlight,
2465 #endif
2466 #ifdef LIGHT_RIM_USED
2467 rim,
2468 rim_tint,
2469 #endif
2470 #ifdef LIGHT_CLEARCOAT_USED
2471 clearcoat, clearcoat_roughness, geo_normal,
2472 #endif // LIGHT_CLEARCOAT_USED
2473 #ifdef LIGHT_ANISOTROPY_USED
2474 tangent,
2475 binormal, anisotropy,
2476 #endif
2477 diffuse_light, specular_light);
2478 }
2479 #endif // !DISABLE_LIGHT_SPOT
2480 #endif // !USE_VERTEX_LIGHTING
2481 #endif // BASE_PASS
2482 #endif // !MODE_UNSHADED
2483
2484 #endif // !MODE_RENDER_DEPTH
2485
2486 #if defined(USE_SHADOW_TO_OPACITY)
2487 #ifndef MODE_RENDER_DEPTH
2488 #ifndef MODE_UNSHADED
2489 alpha = min(alpha, clamp(length(ambient_light), 0.0, 1.0));
2490 #else
2491 alpha = 0.0;
2492 #endif
2493
2494 #if defined(ALPHA_SCISSOR_USED)
2495 #ifdef RENDER_MATERIAL
2496 if (alpha < alpha_scissor_threshold) {
2497 alpha = 0.0;
2498 } else {
2499 alpha = 1.0;
2500 }
2501 #else
2502 if (alpha < alpha_scissor_threshold) {
2503 discard;
2504 }
2505 #endif // RENDER_MATERIAL
2506 #endif // !ALPHA_SCISSOR_USED
2507
2508 #endif // !MODE_RENDER_DEPTH
2509 #endif // USE_SHADOW_TO_OPACITY
2510
2511 #ifdef MODE_RENDER_DEPTH
2512 #ifdef RENDER_SHADOWS_LINEAR
2513 // Linearize the depth buffer if rendering cubemap shadows.
2514 gl_FragDepth = (scene_data_block.data.z_far - (length(vertex) + scene_data_block.data.shadow_bias)) / scene_data_block.data.z_far;
2515 #endif
2516
2517 // Nothing happens, so a tree-ssa optimizer will result in no fragment shader :)
2518 #else // !MODE_RENDER_DEPTH
2519
2520 #ifdef RENDER_MATERIAL
2521
2522 albedo_output_buffer.rgb = albedo;
2523 albedo_output_buffer.a = alpha;
2524
2525 normal_output_buffer.rgb = normal * 0.5 + 0.5;
2526 normal_output_buffer.a = 0.0;
2527
2528 orm_output_buffer.r = ao;
2529 orm_output_buffer.g = roughness;
2530 orm_output_buffer.b = metallic;
2531 orm_output_buffer.a = 1.0;
2532
2533 emission_output_buffer.rgb = emission;
2534 emission_output_buffer.a = 0.0;
2535 #else // !RENDER_MATERIAL
2536 #ifdef BASE_PASS
2537 #ifdef MODE_UNSHADED
2538 frag_color = vec4(albedo, alpha);
2539 #else
2540
2541 diffuse_light *= albedo;
2542 diffuse_light *= 1.0 - metallic;
2543 ambient_light *= 1.0 - metallic;
2544
2545 frag_color = vec4(diffuse_light + specular_light, alpha);
2546 frag_color.rgb += emission + ambient_light;
2547 #endif //!MODE_UNSHADED
2548
2549 #ifndef FOG_DISABLED
2550 fog.xy = unpackHalf2x16(fog_rg);
2551 fog.zw = unpackHalf2x16(fog_ba);
2552
2553 frag_color.rgb = mix(frag_color.rgb, fog.rgb, fog.a);
2554 #endif // !FOG_DISABLED
2555
2556 // Tonemap before writing as we are writing to an sRGB framebuffer
2557 frag_color.rgb *= exposure;
2558 #ifdef APPLY_TONEMAPPING
2559 frag_color.rgb = apply_tonemapping(frag_color.rgb);
2560 #endif
2561 frag_color.rgb = linear_to_srgb(frag_color.rgb);
2562
2563 #else // !BASE_PASS
2564 frag_color = vec4(0.0, 0.0, 0.0, alpha);
2565 #endif // !BASE_PASS
2566
2567 /* ADDITIVE LIGHTING PASS */
2568 #ifdef USE_ADDITIVE_LIGHTING
2569 diffuse_light = vec3(0.0);
2570 specular_light = vec3(0.0);
2571
2572 #ifdef USE_VERTEX_LIGHTING
2573 diffuse_light = additive_diffuse_light_interp;
2574 specular_light = additive_specular_light_interp * f0;
2575 #endif // USE_VERTEX_LIGHTING
2576
2577 #if !defined(ADDITIVE_OMNI) && !defined(ADDITIVE_SPOT)
2578
2579 #ifndef SHADOWS_DISABLED
2580 // Baked shadowmasks
2581 #ifdef USE_LIGHTMAP
2582 float shadowmask = 1.0f;
2583
2584 if (lightmap_shadowmask_mode != SHADOWMASK_MODE_NONE) {
2585 vec3 uvw;
2586 uvw.xy = uv2 * lightmap_uv_scale.zw + lightmap_uv_scale.xy;
2587 uvw.z = float(lightmap_slice);
2588
2589 #ifdef LIGHTMAP_BICUBIC_FILTER
2590 shadowmask = textureArray_bicubic(shadowmask_textures, uvw, lightmap_texture_size).x;
2591 #else
2592 shadowmask = textureLod(shadowmask_textures, uvw, 0.0).x;
2593 #endif
2594 }
2595 #endif //USE_LIGHTMAP
2596
2597 float directional_shadow = 1.0;
2598
2599 #ifdef USE_LIGHTMAP
2600 if (lightmap_shadowmask_mode != SHADOWMASK_MODE_ONLY) {
2601 #endif
2602
2603 // Orthogonal shadows
2604 #if !defined(LIGHT_USE_PSSM2) && !defined(LIGHT_USE_PSSM4)
2605 directional_shadow = sample_shadow(directional_shadow_atlas, directional_shadows[directional_shadow_index].shadow_atlas_pixel_size, shadow_coord);
2606 #endif // !defined(LIGHT_USE_PSSM2) && !defined(LIGHT_USE_PSSM4)
2607
2608 // PSSM2 shadows
2609 #ifdef LIGHT_USE_PSSM2
2610 float depth_z = -vertex.z;
2611 vec4 light_split_offsets = directional_shadows[directional_shadow_index].shadow_split_offsets;
2612 //take advantage of prefetch
2613 float shadow1 = sample_shadow(directional_shadow_atlas, directional_shadows[directional_shadow_index].shadow_atlas_pixel_size, shadow_coord);
2614 float shadow2 = sample_shadow(directional_shadow_atlas, directional_shadows[directional_shadow_index].shadow_atlas_pixel_size, shadow_coord2);
2615
2616 if (depth_z < light_split_offsets.y) {
2617
2618 #ifdef LIGHT_USE_PSSM_BLEND
2619 float directional_shadow2 = 1.0;
2620 float pssm_blend = 0.0;
2621 bool use_blend = true;
2622 #endif
2623 if (depth_z < light_split_offsets.x) {
2624 directional_shadow = shadow1;
2625
2626 #ifdef LIGHT_USE_PSSM_BLEND
2627 directional_shadow2 = shadow2;
2628 pssm_blend = smoothstep(0.0, light_split_offsets.x, depth_z);
2629 #endif
2630 } else {
2631 directional_shadow = shadow2;
2632 #ifdef LIGHT_USE_PSSM_BLEND
2633 use_blend = false;
2634 #endif
2635 }
2636 #ifdef LIGHT_USE_PSSM_BLEND
2637 if (use_blend) {
2638 directional_shadow = mix(directional_shadow, directional_shadow2, pssm_blend);
2639 }
2640 #endif
2641 }
2642
2643 #endif //LIGHT_USE_PSSM2
2644 // PSSM4 shadows
2645 #ifdef LIGHT_USE_PSSM4
2646 float depth_z = -vertex.z;
2647 vec4 light_split_offsets = directional_shadows[directional_shadow_index].shadow_split_offsets;
2648
2649 float shadow1 = sample_shadow(directional_shadow_atlas, directional_shadows[directional_shadow_index].shadow_atlas_pixel_size, shadow_coord);
2650 float shadow2 = sample_shadow(directional_shadow_atlas, directional_shadows[directional_shadow_index].shadow_atlas_pixel_size, shadow_coord2);
2651 float shadow3 = sample_shadow(directional_shadow_atlas, directional_shadows[directional_shadow_index].shadow_atlas_pixel_size, shadow_coord3);
2652 float shadow4 = sample_shadow(directional_shadow_atlas, directional_shadows[directional_shadow_index].shadow_atlas_pixel_size, shadow_coord4);
2653
2654 if (depth_z < light_split_offsets.w) {
2655 #ifdef LIGHT_USE_PSSM_BLEND
2656 float directional_shadow2 = 1.0;
2657 float pssm_blend = 0.0;
2658 bool use_blend = true;
2659 #endif
2660 if (depth_z < light_split_offsets.y) {
2661 if (depth_z < light_split_offsets.x) {
2662 directional_shadow = shadow1;
2663
2664 #ifdef LIGHT_USE_PSSM_BLEND
2665 directional_shadow2 = shadow2;
2666
2667 pssm_blend = smoothstep(0.0, light_split_offsets.x, depth_z);
2668 #endif
2669 } else {
2670 directional_shadow = shadow2;
2671
2672 #ifdef LIGHT_USE_PSSM_BLEND
2673 directional_shadow2 = shadow3;
2674
2675 pssm_blend = smoothstep(light_split_offsets.x, light_split_offsets.y, depth_z);
2676 #endif
2677 }
2678 } else {
2679 if (depth_z < light_split_offsets.z) {
2680 directional_shadow = shadow3;
2681
2682 #if defined(LIGHT_USE_PSSM_BLEND)
2683 directional_shadow2 = shadow4;
2684 pssm_blend = smoothstep(light_split_offsets.y, light_split_offsets.z, depth_z);
2685 #endif
2686
2687 } else {
2688 directional_shadow = shadow4;
2689
2690 #if defined(LIGHT_USE_PSSM_BLEND)
2691 use_blend = false;
2692 #endif
2693 }
2694 }
2695 #if defined(LIGHT_USE_PSSM_BLEND)
2696 if (use_blend) {
2697 directional_shadow = mix(directional_shadow, directional_shadow2, pssm_blend);
2698 }
2699 #endif
2700 }
2701
2702 #endif //LIGHT_USE_PSSM4
2703
2704 #ifdef USE_LIGHTMAP
2705 if (lightmap_shadowmask_mode == SHADOWMASK_MODE_REPLACE) {
2706 directional_shadow = mix(directional_shadow, shadowmask, smoothstep(directional_shadows[directional_shadow_index].fade_from, directional_shadows[directional_shadow_index].fade_to, vertex.z));
2707 } else if (lightmap_shadowmask_mode == SHADOWMASK_MODE_OVERLAY) {
2708 directional_shadow = shadowmask * mix(directional_shadow, 1.0, smoothstep(directional_shadows[directional_shadow_index].fade_from, directional_shadows[directional_shadow_index].fade_to, vertex.z));
2709 } else {
2710 #endif
2711 directional_shadow = mix(directional_shadow, 1.0, smoothstep(directional_shadows[directional_shadow_index].fade_from, directional_shadows[directional_shadow_index].fade_to, vertex.z));
2712 #ifdef USE_LIGHTMAP
2713 }
2714
2715 } else { // lightmap_shadowmask_mode == SHADOWMASK_MODE_ONLY
2716 directional_shadow = shadowmask;
2717 }
2718 #endif
2719
2720 directional_shadow = mix(1.0, directional_shadow, directional_lights[directional_shadow_index].shadow_opacity);
2721
2722 #else
2723 float directional_shadow = 1.0f;
2724 #endif // SHADOWS_DISABLED
2725
2726 #ifndef USE_VERTEX_LIGHTING
2727 if (bool(directional_lights[directional_shadow_index].mask & layer_mask)) {
2728 light_compute(normal, normalize(directional_lights[directional_shadow_index].direction), normalize(view), directional_lights[directional_shadow_index].size, directional_lights[directional_shadow_index].color * directional_lights[directional_shadow_index].energy, true, directional_shadow, f0, roughness, metallic, directional_lights[directional_shadow_index].specular, albedo, alpha, screen_uv,
2729 #ifdef LIGHT_BACKLIGHT_USED
2730 backlight,
2731 #endif
2732 #ifdef LIGHT_RIM_USED
2733 rim, rim_tint,
2734 #endif
2735 #ifdef LIGHT_CLEARCOAT_USED
2736 clearcoat, clearcoat_roughness, geo_normal,
2737 #endif // LIGHT_CLEARCOAT_USED
2738 #ifdef LIGHT_ANISOTROPY_USED
2739 binormal,
2740 tangent, anisotropy,
2741 #endif
2742 diffuse_light,
2743 specular_light);
2744 } else {
2745 #endif // !USE_VERTEX_LIGHTING
2746 // Just apply shadows to vertex lighting.
2747 diffuse_light *= directional_shadow;
2748 specular_light *= directional_shadow;
2749 #ifndef USE_VERTEX_LIGHTING
2750 }
2751 #endif // !USE_VERTEX_LIGHTING
2752 #endif // !defined(ADDITIVE_OMNI) && !defined(ADDITIVE_SPOT)
2753
2754 #ifdef ADDITIVE_OMNI
2755 float omni_shadow = 1.0f;
2756 #ifndef SHADOWS_DISABLED
2757 vec3 light_ray = ((positional_shadows[positional_shadow_index].shadow_matrix * vec4(shadow_coord.xyz, 1.0))).xyz;
2758 omni_shadow = texture(omni_shadow_texture, vec4(light_ray, 1.0 - length(light_ray) * omni_lights[omni_light_index].inv_radius));
2759 omni_shadow = mix(1.0, omni_shadow, omni_lights[omni_light_index].shadow_opacity);
2760 #endif // SHADOWS_DISABLED
2761
2762 #ifndef USE_VERTEX_LIGHTING
2763 light_process_omni(omni_light_index, vertex, view, normal, f0, roughness, metallic, omni_shadow, albedo, alpha, screen_uv,
2764 #ifdef LIGHT_BACKLIGHT_USED
2765 backlight,
2766 #endif
2767 #ifdef LIGHT_RIM_USED
2768 rim,
2769 rim_tint,
2770 #endif
2771 #ifdef LIGHT_CLEARCOAT_USED
2772 clearcoat, clearcoat_roughness, geo_normal,
2773 #endif // LIGHT_CLEARCOAT_USED
2774 #ifdef LIGHT_ANISOTROPY_USED
2775 binormal, tangent, anisotropy,
2776 #endif
2777 diffuse_light, specular_light);
2778 #else
2779 // Just apply shadows to vertex lighting.
2780 diffuse_light *= omni_shadow;
2781 specular_light *= omni_shadow;
2782 #endif // !USE_VERTEX_LIGHTING
2783 #endif // ADDITIVE_OMNI
2784
2785 #ifdef ADDITIVE_SPOT
2786 float spot_shadow = 1.0f;
2787 #ifndef SHADOWS_DISABLED
2788 spot_shadow = sample_shadow(spot_shadow_texture, positional_shadows[positional_shadow_index].shadow_atlas_pixel_size, shadow_coord);
2789 spot_shadow = mix(1.0, spot_shadow, spot_lights[spot_light_index].shadow_opacity);
2790 #endif // SHADOWS_DISABLED
2791
2792 #ifndef USE_VERTEX_LIGHTING
2793 light_process_spot(spot_light_index, vertex, view, normal, f0, roughness, metallic, spot_shadow, albedo, alpha, screen_uv,
2794 #ifdef LIGHT_BACKLIGHT_USED
2795 backlight,
2796 #endif
2797 #ifdef LIGHT_RIM_USED
2798 rim,
2799 rim_tint,
2800 #endif
2801 #ifdef LIGHT_CLEARCOAT_USED
2802 clearcoat, clearcoat_roughness, geo_normal,
2803 #endif // LIGHT_RIM_USED
2804 #ifdef LIGHT_ANISOTROPY_USED
2805 tangent,
2806 binormal, anisotropy,
2807 #endif
2808 diffuse_light, specular_light);
2809 #else
2810 // Just apply shadows to vertex lighting.
2811 diffuse_light *= spot_shadow;
2812 specular_light *= spot_shadow;
2813 #endif // !USE_VERTEX_LIGHTING
2814
2815 #endif // ADDITIVE_SPOT
2816
2817 diffuse_light *= albedo;
2818 diffuse_light *= 1.0 - metallic;
2819 vec3 additive_light_color = diffuse_light + specular_light;
2820
2821 #ifndef FOG_DISABLED
2822 fog.xy = unpackHalf2x16(fog_rg);
2823 fog.zw = unpackHalf2x16(fog_ba);
2824
2825 additive_light_color *= (1.0 - fog.a);
2826 #endif // !FOG_DISABLED
2827
2828 // Tonemap before writing as we are writing to an sRGB framebuffer
2829 additive_light_color *= exposure;
2830 #ifdef APPLY_TONEMAPPING
2831 additive_light_color = apply_tonemapping(additive_light_color);
2832 #endif
2833 additive_light_color = linear_to_srgb(additive_light_color);
2834
2835 frag_color.rgb += additive_light_color;
2836 #endif // USE_ADDITIVE_LIGHTING
2837 frag_color.rgb *= scene_data_block.data.luminance_multiplier;
2838
2839 #endif // !RENDER_MATERIAL
2840 #endif // !MODE_RENDER_DEPTH
2841
2842 #ifdef PREMUL_ALPHA_USED
2843 frag_color.rgb *= premul_alpha;
2844 #endif // PREMUL_ALPHA_USED
2845 #endif // !RENDER_MOTION_VECTORS
2846
2847 #if defined(RENDER_MOTION_VECTORS)
2848 // These motion vectors are in NDC space (as opposed to screen space) to fit the OpenXR XR_FB_space_warp specification.
2849 // https://registry.khronos.org/OpenXR/specs/1.0/html/xrspec.html#XR_FB_space_warp
2850
2851 vec3 ndc = clip_position.xyz / clip_position.w;
2852 vec3 prev_ndc = prev_clip_position.xyz / prev_clip_position.w;
2853 motion_vectors = vec4(ndc - prev_ndc, 0.0);
2854 #endif // RENDER_MOTION_VECTORS
2855 }