diff --git a/crates/bevy_pbr/src/render/pbr_prepass.wgsl b/crates/bevy_pbr/src/render/pbr_prepass.wgsl index 542e540309a5c3..2c1fbd760fada9 100644 --- a/crates/bevy_pbr/src/render/pbr_prepass.wgsl +++ b/crates/bevy_pbr/src/render/pbr_prepass.wgsl @@ -32,21 +32,10 @@ struct FragmentInput { // Cutoff used for the premultiplied alpha modes BLEND and ADD. const PREMULTIPLIED_ALPHA_CUTOFF = 0.05; -// We can use a simplified version of alpha_discard() here since we only need to handle the alpha_cutoff -fn prepass_alpha_discard(in: FragmentInput) { - -#ifdef MAY_DISCARD - var output_color: vec4 = bevy_pbr::pbr_bindings::material.base_color; - -#ifdef VERTEX_UVS - if (bevy_pbr::pbr_bindings::material.flags & bevy_pbr::pbr_types::STANDARD_MATERIAL_FLAGS_BASE_COLOR_TEXTURE_BIT) != 0u { - output_color = output_color * textureSampleBias(bevy_pbr::pbr_bindings::base_color_texture, bevy_pbr::pbr_bindings::base_color_sampler, in.uv, bevy_pbr::prepass_bindings::view.mip_bias); - } -#endif // VERTEX_UVS - - let alpha_mode = bevy_pbr::pbr_bindings::material.flags & bevy_pbr::pbr_types::STANDARD_MATERIAL_FLAGS_ALPHA_MODE_RESERVED_BITS; +fn prepass_alpha_discard(material_flags: u32, alpha_cutoff: f32, output_color: vec4) { + let alpha_mode = material_flags & bevy_pbr::pbr_types::STANDARD_MATERIAL_FLAGS_ALPHA_MODE_RESERVED_BITS; if alpha_mode == bevy_pbr::pbr_types::STANDARD_MATERIAL_FLAGS_ALPHA_MODE_MASK { - if output_color.a < bevy_pbr::pbr_bindings::material.alpha_cutoff { + if output_color.a < alpha_cutoff { discard; } } else if (alpha_mode == bevy_pbr::pbr_types::STANDARD_MATERIAL_FLAGS_ALPHA_MODE_BLEND || alpha_mode == bevy_pbr::pbr_types::STANDARD_MATERIAL_FLAGS_ALPHA_MODE_ADD) { @@ -58,6 +47,25 @@ fn prepass_alpha_discard(in: FragmentInput) { discard; } } +} + +// We can use a simplified version of alpha_discard() here since we only need to handle the alpha_cutoff +fn prepass_sample_color_and_alpha_discard(in: FragmentInput) { + +#ifdef MAY_DISCARD + var output_color: vec4 = bevy_pbr::pbr_bindings::material.base_color; + +#ifdef VERTEX_UVS + if (bevy_pbr::pbr_bindings::material.flags & bevy_pbr::pbr_types::STANDARD_MATERIAL_FLAGS_BASE_COLOR_TEXTURE_BIT) != 0u { + output_color = output_color * textureSampleBias(bevy_pbr::pbr_bindings::base_color_texture, bevy_pbr::pbr_bindings::base_color_sampler, in.uv, bevy_pbr::prepass_bindings::view.mip_bias); + } +#endif // VERTEX_UVS + + prepass_alpha_discard( + bevy_pbr::pbr_bindings::material.flags, + bevy_pbr::pbr_bindings::material.alpha_cutoff, + output_color + ); #endif // MAY_DISCARD } @@ -79,7 +87,7 @@ struct FragmentOutput { @fragment fn fragment(in: FragmentInput) -> FragmentOutput { - prepass_alpha_discard(in); + prepass_sample_color_and_alpha_discard(in); var out: FragmentOutput; @@ -135,6 +143,6 @@ fn fragment(in: FragmentInput) -> FragmentOutput { #else @fragment fn fragment(in: FragmentInput) { - prepass_alpha_discard(in); + prepass_sample_color_and_alpha_discard(in); } #endif // PREPASS_FRAGMENT