|
| 1 | +use bevy::{ |
| 2 | + ecs::prelude::*, |
| 3 | + input::Input, |
| 4 | + math::Vec3, |
| 5 | + pbr2::{PbrBundle, PointLightBundle, StandardMaterial}, |
| 6 | + prelude::{App, Assets, KeyCode, Transform}, |
| 7 | + render2::{ |
| 8 | + camera::PerspectiveCameraBundle, |
| 9 | + color::Color, |
| 10 | + mesh::{shape, Mesh}, |
| 11 | + view::Msaa, |
| 12 | + }, |
| 13 | + PipelinedDefaultPlugins, |
| 14 | +}; |
| 15 | + |
| 16 | +/// This example shows how to configure Multi-Sample Anti-Aliasing. Setting the sample count higher |
| 17 | +/// will result in smoother edges, but it will also increase the cost to render those edges. The |
| 18 | +/// range should generally be somewhere between 1 (no multi sampling, but cheap) to 8 (crisp but |
| 19 | +/// expensive). |
| 20 | +/// Note that WGPU currently only supports 1 or 4 samples. |
| 21 | +/// Ultimately we plan on supporting whatever is natively supported on a given device. |
| 22 | +/// Check out this issue for more info: https://github.com/gfx-rs/wgpu/issues/1832 |
| 23 | +fn main() { |
| 24 | + println!("Press 'm' to toggle MSAA"); |
| 25 | + println!("Using 4x MSAA"); |
| 26 | + App::new() |
| 27 | + .insert_resource(Msaa { samples: 4 }) |
| 28 | + .add_plugins(PipelinedDefaultPlugins) |
| 29 | + .add_startup_system(setup) |
| 30 | + .add_system(cycle_msaa) |
| 31 | + .run(); |
| 32 | +} |
| 33 | + |
| 34 | +/// set up a simple 3D scene |
| 35 | +fn setup( |
| 36 | + mut commands: Commands, |
| 37 | + mut meshes: ResMut<Assets<Mesh>>, |
| 38 | + mut materials: ResMut<Assets<StandardMaterial>>, |
| 39 | +) { |
| 40 | + // cube |
| 41 | + commands.spawn_bundle(PbrBundle { |
| 42 | + mesh: meshes.add(Mesh::from(shape::Cube { size: 2.0 })), |
| 43 | + material: materials.add(Color::rgb(0.8, 0.7, 0.6).into()), |
| 44 | + ..Default::default() |
| 45 | + }); |
| 46 | + // light |
| 47 | + commands.spawn_bundle(PointLightBundle { |
| 48 | + transform: Transform::from_xyz(4.0, 8.0, 4.0), |
| 49 | + ..Default::default() |
| 50 | + }); |
| 51 | + // camera |
| 52 | + commands.spawn_bundle(PerspectiveCameraBundle { |
| 53 | + transform: Transform::from_xyz(-3.0, 3.0, 5.0).looking_at(Vec3::ZERO, Vec3::Y), |
| 54 | + ..Default::default() |
| 55 | + }); |
| 56 | +} |
| 57 | + |
| 58 | +fn cycle_msaa(input: Res<Input<KeyCode>>, mut msaa: ResMut<Msaa>) { |
| 59 | + if input.just_pressed(KeyCode::M) { |
| 60 | + if msaa.samples == 4 { |
| 61 | + println!("Not using MSAA"); |
| 62 | + msaa.samples = 1; |
| 63 | + } else { |
| 64 | + println!("Using 4x MSAA"); |
| 65 | + msaa.samples = 4; |
| 66 | + } |
| 67 | + } |
| 68 | +} |
0 commit comments