Skip to content

Commit

Permalink
extract reusable prepass_alpha_discard
Browse files Browse the repository at this point in the history
This renames the old prepase_alpha_discard to
prepass_sample_color_and_alpha_discard to indicate
that it accessing PBR-specific texture bindings.
  • Loading branch information
bonsairobo committed Aug 21, 2023
1 parent 4b1f023 commit 966f536
Showing 1 changed file with 24 additions and 16 deletions.
40 changes: 24 additions & 16 deletions crates/bevy_pbr/src/render/pbr_prepass.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -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<f32> = 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<f32>) {
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) {
Expand All @@ -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<f32> = 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
}
Expand All @@ -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;

Expand Down Expand Up @@ -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

0 comments on commit 966f536

Please sign in to comment.