Skip to content

Commit

Permalink
Add MSAA to shader_prepass example
Browse files Browse the repository at this point in the history
  • Loading branch information
trolleyman committed Jul 1, 2023
1 parent cc16bc6 commit aae82bd
Showing 1 changed file with 21 additions and 10 deletions.
31 changes: 21 additions & 10 deletions examples/shader/shader_prepass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ fn main() {
},
))
.add_systems(Startup, setup)
.add_systems(Update, (rotate, toggle_prepass_view))
.add_systems(Update, (rotate, toggle_prepass_view, toggle_multisampling))
// Disabling MSAA for maximum compatibility. Shader prepass with MSAA needs GPU capability MULTISAMPLED_SHADING
.insert_resource(Msaa::Off)
.run();
Expand Down Expand Up @@ -142,10 +142,12 @@ fn setup(
commands.spawn(
TextBundle::from_sections(vec![
TextSection::new("Prepass Output: transparent\n", style.clone()),
TextSection::new("MSAA: Off\n", style.clone()),
TextSection::new("\n\n", style.clone()),
TextSection::new("Controls\n", style.clone()),
TextSection::new("---------------\n", style.clone()),
TextSection::new("Space - Change output\n", style),
TextSection::new("Space - Change output\n", style.clone()),
TextSection::new("M - Change MSAA\n", style),
])
.with_style(Style {
position_type: PositionType::Absolute,
Expand Down Expand Up @@ -242,17 +244,9 @@ fn toggle_prepass_view(
3 => "motion vectors",
_ => unreachable!(),
};
let text_color = if *prepass_view == 3 {
Color::BLACK
} else {
Color::WHITE
};

let mut text = text.single_mut();
text.sections[0].value = format!("Prepass Output: {label}\n");
for section in &mut text.sections {
section.style.color = text_color;
}

let handle = material_handle.single();
let mat = materials.get_mut(handle).unwrap();
Expand All @@ -261,3 +255,20 @@ fn toggle_prepass_view(
mat.settings.show_motion_vectors = (*prepass_view == 3) as u32;
}
}

fn toggle_multisampling(
keycode: Res<Input<KeyCode>>,
mut text: Query<&mut Text>,
mut msaa: ResMut<Msaa>,
) {
if keycode.just_pressed(KeyCode::M) {
*msaa = match *msaa {
Msaa::Off => Msaa::Sample2,
Msaa::Sample2 => Msaa::Sample4,
Msaa::Sample4 => Msaa::Sample8,
Msaa::Sample8 => Msaa::Off,
};
let mut text = text.single_mut();
text.sections[1].value = format!("MSAA: {:?}\n", *msaa);
}
}

0 comments on commit aae82bd

Please sign in to comment.