1#define_import_path bevy_pbr::pbr_functions 6 mesh_view_bindings as view_bindings, 9 lighting::{LAYER_BASE, LAYER_CLEARCOAT}, 11 clustered_forward as clustering, 15 mesh_types::{MESH_FLAGS_SHADOW_RECEIVER_BIT, MESH_FLAGS_TRANSMITTED_SHADOW_RECEIVER_BIT}, 17#import bevy_render::maths::{E, powsafe} 19#ifdef MESHLET_MESH_MATERIAL_PASS 20#import bevy_pbr::meshlet_visibility_buffer_resolve::VertexOutput 21#else ifdef PREPASS_PIPELINE 22#import bevy_pbr::prepass_io::VertexOutput 23#else // PREPASS_PIPELINE 24#import bevy_pbr::forward_io::VertexOutput 25#endif // PREPASS_PIPELINE 28#import bevy_pbr::environment_map 31#ifdef TONEMAP_IN_SHADER 32#import bevy_core_pipeline::tonemapping::{tone_mapping, screen_space_dither} 40#ifdef MESHLET_MESH_MATERIAL_PASS 43#else // MESHLET_MESH_MATERIAL_PASS 45#endif // MESHLET_MESH_MATERIAL_PASS 55const DITHER_THRESHOLD_MAP: vec4<u32> = vec4( 76#ifdef VISIBILITY_RANGE_DITHER 77fn visibility_range_dither(frag_coord: vec4<f32>, dither: i32) { 84 if (dither <= -16 || dither >= 16) { 89 let coords = vec2<u32>(floor(frag_coord.xy)) % 4u; 90 let threshold = i32((DITHER_THRESHOLD_MAP[coords.y] >> (coords.x * 8)) & 0xff); 91 if ((dither >= 0 && dither + threshold >= 16) || (dither < 0 && 1 + dither + threshold <= 0)) { 97fn alpha_discard(material: pbr_types::StandardMaterial, output_color: vec4<f32>) -> vec4<f32> { 98 var color = output_color; 99 let alpha_mode = material.flags & pbr_types::STANDARD_MATERIAL_FLAGS_ALPHA_MODE_RESERVED_BITS; 100 if alpha_mode == pbr_types::STANDARD_MATERIAL_FLAGS_ALPHA_MODE_OPAQUE { 109 else if alpha_mode == pbr_types::STANDARD_MATERIAL_FLAGS_ALPHA_MODE_MASK || 110 alpha_mode == pbr_types::STANDARD_MATERIAL_FLAGS_ALPHA_MODE_ALPHA_TO_COVERAGE { 111 if color.a >= material.alpha_cutoff { 127 texture: texture_2d<f32>, 132#ifdef MESHLET_MESH_MATERIAL_PASS 133 return textureSampleGrad(texture, samp, uv, bias.ddx_uv, bias.ddy_uv); 135 return textureSampleBias(texture, samp, uv, bias.mip_bias); 139fn prepare_world_normal( 140 world_normal: vec3<f32>, 144 var output: vec3<f32> = world_normal; 145#ifndef VERTEX_TANGENTS 146#ifndef STANDARD_MATERIAL_NORMAL_MAP 149 output = (f32(!double_sided || is_front) * 2.0 - 1.0) * output; 159fn calculate_tbn_mikktspace(world_normal: vec3<f32>, world_tangent: vec4<f32>) -> mat3x3<f32> { 166 var N: vec3<f32> = world_normal; 172 var T: vec3<f32> = world_tangent.xyz; 173 var B: vec3<f32> = world_tangent.w * cross(N, T); 175#ifdef MESHLET_MESH_MATERIAL_PASS 177 let inverse_length_n = 1.0 / length(N); 178 T *= inverse_length_n; 179 B *= inverse_length_n; 180 N *= inverse_length_n; 183 return mat3x3(T, B, N); 186fn apply_normal_mapping( 187 standard_material_flags: u32, 200 if (standard_material_flags & pbr_types::STANDARD_MATERIAL_FLAGS_TWO_COMPONENT_NORMAL_MAP) != 0u { 202 Nt = vec3<f32>(Nt.rg * 2.0 - 1.0, 0.0); 203 Nt.z = sqrt(1.0 - Nt.x * Nt.x - Nt.y * Nt.y); 208 if (standard_material_flags & pbr_types::STANDARD_MATERIAL_FLAGS_FLIP_NORMAL_MAP_Y) != 0u { 212 if double_sided && !is_front { 221 N = Nt.x * T + Nt.y * B + Nt.z * N; 226#ifdef STANDARD_MATERIAL_ANISOTROPY 233fn bend_normal_for_anisotropy(lighting_input: ptr<function, lighting::LightingInput>) { 235 let N = (*lighting_input).layers[LAYER_BASE].N; 236 let roughness = (*lighting_input).layers[LAYER_BASE].roughness; 237 let V = (*lighting_input).V; 238 let anisotropy = (*lighting_input).anisotropy; 239 let Ba = (*lighting_input).Ba; 241 var bent_normal = normalize(cross(cross(Ba, V), Ba)); 246 let a = pow(2.0, pow(2.0, 1.0 - anisotropy * (1.0 - roughness))); 247 bent_normal = normalize(mix(bent_normal, N, a)); 254 let R = normalize(mix(reflect(-V, bent_normal), bent_normal, roughness * roughness)); 256 (*lighting_input).layers[LAYER_BASE].N = bent_normal; 257 (*lighting_input).layers[LAYER_BASE].R = R; 260#endif // STANDARD_MATERIAL_ANISTROPY 265 world_position: vec4<f32>, 266 is_orthographic: bool, 271 V = normalize(vec3<f32>(view_bindings::view.clip_from_world[0].z, view_bindings::view.clip_from_world[1].z, view_bindings::view.clip_from_world[2].z)); 274 V = normalize(view_bindings::view.world_position.xyz - world_position.xyz); 280fn calculate_diffuse_color( 281 base_color: vec3<f32>, 283 specular_transmission: f32, 284 diffuse_transmission: f32 286 return base_color * (1.0 - metallic) * (1.0 - specular_transmission) * 287 (1.0 - diffuse_transmission); 292fn calculate_F0(base_color: vec3<f32>, metallic: f32, reflectance: f32) -> vec3<f32> { 293 return 0.16 * reflectance * reflectance * (1.0 - metallic) + base_color * metallic; 296#ifndef PREPASS_FRAGMENT 297fn apply_pbr_lighting( 298 in: pbr_types::PbrInput, 300 var output_color: vec4<f32> = in.material.base_color; 302 let emissive = in.material.emissive; 305 let metallic = in.material.metallic; 306 let perceptual_roughness = in.material.perceptual_roughness; 307 let roughness = lighting::perceptualRoughnessToRoughness(perceptual_roughness); 308 let ior = in.material.ior; 309 let thickness = in.material.thickness; 310 let reflectance = in.material.reflectance; 311 let diffuse_transmission = in.material.diffuse_transmission; 312 let specular_transmission = in.material.specular_transmission; 314 let specular_transmissive_color = specular_transmission * in.material.base_color.rgb; 316 let diffuse_occlusion = in.diffuse_occlusion; 317 let specular_occlusion = in.specular_occlusion; 320 let NdotV = max(dot(in.N, in.V), 0.0001); 321 let R = reflect(-in.V, in.N); 323#ifdef STANDARD_MATERIAL_CLEARCOAT 326 let clearcoat = in.material.clearcoat; 327 let clearcoat_perceptual_roughness = in.material.clearcoat_perceptual_roughness; 328 let clearcoat_roughness = lighting::perceptualRoughnessToRoughness(clearcoat_perceptual_roughness); 329 let clearcoat_N = in.clearcoat_N; 330 let clearcoat_NdotV = max(dot(clearcoat_N, in.V), 0.0001); 331 let clearcoat_R = reflect(-in.V, clearcoat_N); 332#endif // STANDARD_MATERIAL_CLEARCOAT 334 let diffuse_color = calculate_diffuse_color( 337 specular_transmission, 342 let diffuse_transmissive_color = output_color.rgb * (1.0 - metallic) * (1.0 - specular_transmission) * diffuse_transmission; 345 let diffuse_transmissive_lobe_world_position = in.world_position - vec4<f32>(in.world_normal, 0.0) * thickness; 347 let F0 = calculate_F0(output_color.rgb, metallic, reflectance); 348 let F_ab = lighting::F_AB(perceptual_roughness, NdotV); 350 var direct_light: vec3<f32> = vec3<f32>(0.0); 353 var transmitted_light: vec3<f32> = vec3<f32>(0.0); 356 var lighting_input: lighting::LightingInput; 357 lighting_input.layers[LAYER_BASE].NdotV = NdotV; 358 lighting_input.layers[LAYER_BASE].N = in.N; 359 lighting_input.layers[LAYER_BASE].R = R; 360 lighting_input.layers[LAYER_BASE].perceptual_roughness = perceptual_roughness; 361 lighting_input.layers[LAYER_BASE].roughness = roughness; 362 lighting_input.P = in.world_position.xyz; 363 lighting_input.V = in.V; 364 lighting_input.diffuse_color = diffuse_color; 365 lighting_input.F0_ = F0; 366 lighting_input.F_ab = F_ab; 367#ifdef STANDARD_MATERIAL_CLEARCOAT 368 lighting_input.layers[LAYER_CLEARCOAT].NdotV = clearcoat_NdotV; 369 lighting_input.layers[LAYER_CLEARCOAT].N = clearcoat_N; 370 lighting_input.layers[LAYER_CLEARCOAT].R = clearcoat_R; 371 lighting_input.layers[LAYER_CLEARCOAT].perceptual_roughness = clearcoat_perceptual_roughness; 372 lighting_input.layers[LAYER_CLEARCOAT].roughness = clearcoat_roughness; 373 lighting_input.clearcoat_strength = clearcoat; 374#endif // STANDARD_MATERIAL_CLEARCOAT 375#ifdef STANDARD_MATERIAL_ANISOTROPY 376 lighting_input.anisotropy = in.anisotropy_strength; 377 lighting_input.Ta = in.anisotropy_T; 378 lighting_input.Ba = in.anisotropy_B; 379#endif // STANDARD_MATERIAL_ANISOTROPY 382#ifdef STANDARD_MATERIAL_DIFFUSE_TRANSMISSION 383 var transmissive_lighting_input: lighting::LightingInput; 384 transmissive_lighting_input.layers[LAYER_BASE].NdotV = 1.0; 385 transmissive_lighting_input.layers[LAYER_BASE].N = -in.N; 386 transmissive_lighting_input.layers[LAYER_BASE].R = vec3(0.0); 387 transmissive_lighting_input.layers[LAYER_BASE].perceptual_roughness = 1.0; 388 transmissive_lighting_input.layers[LAYER_BASE].roughness = 1.0; 389 transmissive_lighting_input.P = diffuse_transmissive_lobe_world_position.xyz; 390 transmissive_lighting_input.V = -in.V; 391 transmissive_lighting_input.diffuse_color = diffuse_transmissive_color; 392 transmissive_lighting_input.F0_ = vec3(0.0); 393 transmissive_lighting_input.F_ab = vec2(0.1); 394#ifdef STANDARD_MATERIAL_CLEARCOAT 395 transmissive_lighting_input.layers[LAYER_CLEARCOAT].NdotV = 0.0; 396 transmissive_lighting_input.layers[LAYER_CLEARCOAT].N = vec3(0.0); 397 transmissive_lighting_input.layers[LAYER_CLEARCOAT].R = vec3(0.0); 398 transmissive_lighting_input.layers[LAYER_CLEARCOAT].perceptual_roughness = 0.0; 399 transmissive_lighting_input.layers[LAYER_CLEARCOAT].roughness = 0.0; 400 transmissive_lighting_input.clearcoat_strength = 0.0; 401#endif // STANDARD_MATERIAL_CLEARCOAT 402#ifdef STANDARD_MATERIAL_ANISOTROPY 403 lighting_input.anisotropy = in.anisotropy_strength; 404 lighting_input.Ta = in.anisotropy_T; 405 lighting_input.Ba = in.anisotropy_B; 406#endif // STANDARD_MATERIAL_ANISOTROPY 407#endif // STANDARD_MATERIAL_DIFFUSE_TRANSMISSION 409 let view_z = dot(vec4<f32>( 410 view_bindings::view.view_from_world[0].z, 411 view_bindings::view.view_from_world[1].z, 412 view_bindings::view.view_from_world[2].z, 413 view_bindings::view.view_from_world[3].z 414 ), in.world_position); 415 let cluster_index = clustering::fragment_cluster_index(in.frag_coord.xy, view_z, in.is_orthographic); 416 let offset_and_counts = clustering::unpack_offset_and_counts(cluster_index); 419 for (var i: u32 = offset_and_counts[0]; i < offset_and_counts[0] + offset_and_counts[1]; i = i + 1u) { 420 let light_id = clustering::get_clusterable_object_id(i); 421 var shadow: f32 = 1.0; 422 if ((in.flags & MESH_FLAGS_SHADOW_RECEIVER_BIT) != 0u 423 && (view_bindings::clusterable_objects.data[light_id].flags & mesh_view_types::POINT_LIGHT_FLAGS_SHADOWS_ENABLED_BIT) != 0u) { 424 shadow = shadows::fetch_point_shadow(light_id, in.world_position, in.world_normal); 427 let light_contrib = lighting::point_light(light_id, &lighting_input); 428 direct_light += light_contrib * shadow; 430#ifdef STANDARD_MATERIAL_DIFFUSE_TRANSMISSION 440 var transmitted_shadow: f32 = 1.0; 441 if ((in.flags & (MESH_FLAGS_SHADOW_RECEIVER_BIT | MESH_FLAGS_TRANSMITTED_SHADOW_RECEIVER_BIT)) == (MESH_FLAGS_SHADOW_RECEIVER_BIT | MESH_FLAGS_TRANSMITTED_SHADOW_RECEIVER_BIT) 442 && (view_bindings::clusterable_objects.data[light_id].flags & mesh_view_types::POINT_LIGHT_FLAGS_SHADOWS_ENABLED_BIT) != 0u) { 443 transmitted_shadow = shadows::fetch_point_shadow(light_id, diffuse_transmissive_lobe_world_position, -in.world_normal); 446 let transmitted_light_contrib = 447 lighting::point_light(light_id, &transmissive_lighting_input); 448 transmitted_light += transmitted_light_contrib * transmitted_shadow; 453 for (var i: u32 = offset_and_counts[0] + offset_and_counts[1]; i < offset_and_counts[0] + offset_and_counts[1] + offset_and_counts[2]; i = i + 1u) { 454 let light_id = clustering::get_clusterable_object_id(i); 456 var shadow: f32 = 1.0; 457 if ((in.flags & MESH_FLAGS_SHADOW_RECEIVER_BIT) != 0u 458 && (view_bindings::clusterable_objects.data[light_id].flags & 459 mesh_view_types::POINT_LIGHT_FLAGS_SHADOWS_ENABLED_BIT) != 0u) { 460 shadow = shadows::fetch_spot_shadow( 464 view_bindings::clusterable_objects.data[light_id].shadow_map_near_z, 468 let light_contrib = lighting::spot_light(light_id, &lighting_input); 469 direct_light += light_contrib * shadow; 471#ifdef STANDARD_MATERIAL_DIFFUSE_TRANSMISSION 481 var transmitted_shadow: f32 = 1.0; 482 if ((in.flags & (MESH_FLAGS_SHADOW_RECEIVER_BIT | MESH_FLAGS_TRANSMITTED_SHADOW_RECEIVER_BIT)) == (MESH_FLAGS_SHADOW_RECEIVER_BIT | MESH_FLAGS_TRANSMITTED_SHADOW_RECEIVER_BIT) 483 && (view_bindings::clusterable_objects.data[light_id].flags & mesh_view_types::POINT_LIGHT_FLAGS_SHADOWS_ENABLED_BIT) != 0u) { 484 transmitted_shadow = shadows::fetch_spot_shadow( 486 diffuse_transmissive_lobe_world_position, 488 view_bindings::clusterable_objects.data[light_id].shadow_map_near_z, 492 let transmitted_light_contrib = 493 lighting::spot_light(light_id, &transmissive_lighting_input); 494 transmitted_light += transmitted_light_contrib * transmitted_shadow; 499 let n_directional_lights = view_bindings::lights.n_directional_lights; 500 for (var i: u32 = 0u; i < n_directional_lights; i = i + 1u) { 503 let light = &view_bindings::lights.directional_lights[i]; 504 if (*light).skip != 0u { 508 var shadow: f32 = 1.0; 509 if ((in.flags & MESH_FLAGS_SHADOW_RECEIVER_BIT) != 0u 510 && (view_bindings::lights.directional_lights[i].flags & mesh_view_types::DIRECTIONAL_LIGHT_FLAGS_SHADOWS_ENABLED_BIT) != 0u) { 511 shadow = shadows::fetch_directional_shadow(i, in.world_position, in.world_normal, view_z); 514 var light_contrib = lighting::directional_light(i, &lighting_input); 516#ifdef DIRECTIONAL_LIGHT_SHADOW_MAP_DEBUG_CASCADES 517 light_contrib = shadows::cascade_debug_visualization(light_contrib, i, view_z); 519 direct_light += light_contrib * shadow; 521#ifdef STANDARD_MATERIAL_DIFFUSE_TRANSMISSION 531 var transmitted_shadow: f32 = 1.0; 532 if ((in.flags & (MESH_FLAGS_SHADOW_RECEIVER_BIT | MESH_FLAGS_TRANSMITTED_SHADOW_RECEIVER_BIT)) == (MESH_FLAGS_SHADOW_RECEIVER_BIT | MESH_FLAGS_TRANSMITTED_SHADOW_RECEIVER_BIT) 533 && (view_bindings::lights.directional_lights[i].flags & mesh_view_types::DIRECTIONAL_LIGHT_FLAGS_SHADOWS_ENABLED_BIT) != 0u) { 534 transmitted_shadow = shadows::fetch_directional_shadow(i, diffuse_transmissive_lobe_world_position, -in.world_normal, view_z); 537 let transmitted_light_contrib = 538 lighting::directional_light(i, &transmissive_lighting_input); 539 transmitted_light += transmitted_light_contrib * transmitted_shadow; 543#ifdef STANDARD_MATERIAL_DIFFUSE_TRANSMISSION 552 transmitted_light += ambient::ambient_light(diffuse_transmissive_lobe_world_position, -in.N, -in.V, 1.0, diffuse_transmissive_color, vec3<f32>(0.0), 1.0, vec3<f32>(1.0)); 566 var indirect_light = vec3(0.0f); 567 var found_diffuse_indirect = false; 570 indirect_light += in.lightmap_light * diffuse_color; 571 found_diffuse_indirect = true; 574#ifdef IRRADIANCE_VOLUME 576 if (!found_diffuse_indirect) { 577 let irradiance_volume_light = irradiance_volume::irradiance_volume_light( 578 in.world_position.xyz, in.N); 579 indirect_light += irradiance_volume_light * diffuse_color * diffuse_occlusion; 580 found_diffuse_indirect = true; 585#ifdef ENVIRONMENT_MAP 587#ifdef STANDARD_MATERIAL_ANISOTROPY 588 var bent_normal_lighting_input = lighting_input; 589 bend_normal_for_anisotropy(&bent_normal_lighting_input); 590 let environment_map_lighting_input = &bent_normal_lighting_input; 591#else // STANDARD_MATERIAL_ANISOTROPY 592 let environment_map_lighting_input = &lighting_input; 593#endif // STANDARD_MATERIAL_ANISOTROPY 595 let environment_light = environment_map::environment_map_light( 596 environment_map_lighting_input, 597 found_diffuse_indirect 602#ifdef SCREEN_SPACE_REFLECTIONS 603 let use_ssr = perceptual_roughness <= 604 view_bindings::ssr_settings.perceptual_roughness_threshold; 605#else // SCREEN_SPACE_REFLECTIONS 607#endif // SCREEN_SPACE_REFLECTIONS 610 let environment_light = environment_map::environment_map_light( 612 found_diffuse_indirect 615 indirect_light += environment_light.diffuse * diffuse_occlusion + 616 environment_light.specular * specular_occlusion; 619#endif // ENVIRONMENT_MAP 622 indirect_light += ambient::ambient_light(in.world_position, in.N, in.V, NdotV, diffuse_color, F0, perceptual_roughness, diffuse_occlusion); 626 var specular_transmitted_environment_light = vec3<f32>(0.0); 628#ifdef ENVIRONMENT_MAP 630#ifdef STANDARD_MATERIAL_DIFFUSE_OR_SPECULAR_TRANSMISSION 646 refract(in.V, -in.N, 1.0 / ior) * thickness 649 var transmissive_environment_light_input: lighting::LightingInput; 650 transmissive_environment_light_input.diffuse_color = vec3(1.0); 651 transmissive_environment_light_input.layers[LAYER_BASE].NdotV = 1.0; 652 transmissive_environment_light_input.P = in.world_position.xyz; 653 transmissive_environment_light_input.layers[LAYER_BASE].N = -in.N; 654 transmissive_environment_light_input.V = in.V; 655 transmissive_environment_light_input.layers[LAYER_BASE].R = T; 656 transmissive_environment_light_input.layers[LAYER_BASE].perceptual_roughness = perceptual_roughness; 657 transmissive_environment_light_input.layers[LAYER_BASE].roughness = roughness; 658 transmissive_environment_light_input.F0_ = vec3<f32>(1.0); 659 transmissive_environment_light_input.F_ab = vec2(0.1); 660#ifdef STANDARD_MATERIAL_CLEARCOAT 662 transmissive_environment_light_input.clearcoat_strength = 0.0; 663 transmissive_environment_light_input.layers[LAYER_CLEARCOAT].NdotV = 0.0; 664 transmissive_environment_light_input.layers[LAYER_CLEARCOAT].N = in.N; 665 transmissive_environment_light_input.layers[LAYER_CLEARCOAT].R = vec3(0.0); 666 transmissive_environment_light_input.layers[LAYER_CLEARCOAT].perceptual_roughness = 0.0; 667 transmissive_environment_light_input.layers[LAYER_CLEARCOAT].roughness = 0.0; 668#endif // STANDARD_MATERIAL_CLEARCOAT 670 let transmitted_environment_light = 671 environment_map::environment_map_light(&transmissive_environment_light_input, false); 673#ifdef STANDARD_MATERIAL_DIFFUSE_TRANSMISSION 674 transmitted_light += transmitted_environment_light.diffuse * diffuse_transmissive_color; 675#endif // STANDARD_MATERIAL_DIFFUSE_TRANSMISSION 676#ifdef STANDARD_MATERIAL_SPECULAR_TRANSMISSION 677 specular_transmitted_environment_light = transmitted_environment_light.specular * specular_transmissive_color; 678#endif // STANDARD_MATERIAL_SPECULAR_TRANSMISSION 680#endif // STANDARD_MATERIAL_SPECULAR_OR_DIFFUSE_TRANSMISSION 682#endif // ENVIRONMENT_MAP 684 var emissive_light = emissive.rgb * output_color.a; 690#ifdef STANDARD_MATERIAL_CLEARCOAT 691 emissive_light = emissive_light * (0.04 + (1.0 - 0.04) * pow(1.0 - clearcoat_NdotV, 5.0)); 694 emissive_light = emissive_light * mix(1.0, view_bindings::view.exposure, emissive.a); 696#ifdef STANDARD_MATERIAL_SPECULAR_TRANSMISSION 697 transmitted_light += transmission::specular_transmissive_light(in.world_position, in.frag_coord.xyz, view_z, in.N, in.V, F0, ior, thickness, perceptual_roughness, specular_transmissive_color, specular_transmitted_environment_light).rgb; 699 if (in.material.flags & pbr_types::STANDARD_MATERIAL_FLAGS_ATTENUATION_ENABLED_BIT) != 0u { 703 var attenuation_fog: mesh_view_types::Fog; 704 attenuation_fog.base_color.a = 1.0; 705 attenuation_fog.be = pow(1.0 - in.material.attenuation_color.rgb, vec3<f32>(E)) / in.material.attenuation_distance; 708 transmitted_light = bevy_pbr::fog::atmospheric_fog( 709 attenuation_fog, vec4<f32>(transmitted_light, 1.0), thickness, 716 output_color = vec4<f32>( 717 (view_bindings::view.exposure * (transmitted_light + direct_light + indirect_light)) + emissive_light, 721 output_color = clustering::cluster_debug_visualization( 731#endif // PREPASS_FRAGMENT 733fn apply_fog(fog_params: mesh_view_types::Fog, input_color: vec4<f32>, fragment_world_position: vec3<f32>, view_world_position: vec3<f32>) -> vec4<f32> { 734 let view_to_world = fragment_world_position.xyz - view_world_position.xyz; 740 let distance = length(view_to_world); 742 var scattering = vec3<f32>(0.0); 743 if fog_params.directional_light_color.a > 0.0 { 744 let view_to_world_normalized = view_to_world / distance; 745 let n_directional_lights = view_bindings::lights.n_directional_lights; 746 for (var i: u32 = 0u; i < n_directional_lights; i = i + 1u) { 747 let light = view_bindings::lights.directional_lights[i]; 750 dot(view_to_world_normalized, light.direction_to_light), 753 fog_params.directional_light_exponent 754 ) * light.color.rgb * view_bindings::view.exposure; 758 if fog_params.mode == mesh_view_types::FOG_MODE_LINEAR { 759 return bevy_pbr::fog::linear_fog(fog_params, input_color, distance, scattering); 760 } else if fog_params.mode == mesh_view_types::FOG_MODE_EXPONENTIAL { 761 return bevy_pbr::fog::exponential_fog(fog_params, input_color, distance, scattering); 762 } else if fog_params.mode == mesh_view_types::FOG_MODE_EXPONENTIAL_SQUARED { 763 return bevy_pbr::fog::exponential_squared_fog(fog_params, input_color, distance, scattering); 764 } else if fog_params.mode == mesh_view_types::FOG_MODE_ATMOSPHERIC { 765 return bevy_pbr::fog::atmospheric_fog(fog_params, input_color, distance, scattering); 771#ifdef PREMULTIPLY_ALPHA 772fn premultiply_alpha(standard_material_flags: u32, color: vec4<f32>) -> vec4<f32> { 777#ifdef BLEND_PREMULTIPLIED_ALPHA 781 let alpha_mode = standard_material_flags & pbr_types::STANDARD_MATERIAL_FLAGS_ALPHA_MODE_RESERVED_BITS; 782 if alpha_mode == pbr_types::STANDARD_MATERIAL_FLAGS_ALPHA_MODE_ADD { 794 return vec4<f32>(color.rgb * color.a, 0.0); 818 return vec4<f32>(color.rgb * color.a, color.a); 825fn main_pass_post_lighting_processing( 826 pbr_input: pbr_types::PbrInput, 827 input_color: vec4<f32>, 829 var output_color = input_color; 832 if (view_bindings::fog.mode != mesh_view_types::FOG_MODE_OFF && (pbr_input.material.flags & pbr_types::STANDARD_MATERIAL_FLAGS_FOG_ENABLED_BIT) != 0u) { 833 output_color = apply_fog(view_bindings::fog, output_color, pbr_input.world_position.xyz, view_bindings::view.world_position.xyz); 836#ifdef TONEMAP_IN_SHADER 837 output_color = tone_mapping(output_color, view_bindings::view.color_grading); 839 var output_rgb = output_color.rgb; 840 output_rgb = powsafe(output_rgb, 1.0 / 2.2); 841 output_rgb += screen_space_dither(pbr_input.frag_coord.xy); 844 output_rgb = powsafe(output_rgb, 2.2); 845 output_color = vec4(output_rgb, output_color.a); 848#ifdef PREMULTIPLY_ALPHA 849 output_color = premultiply_alpha(pbr_input.material.flags, output_color);