Skip to content

Commit 2f22f5c

Browse files
superdumpcart
andcommitted
MSAA example (#3049)
Add an example that demonstrates the difference between no MSAA and MSAA 4x. This is also useful for testing panics when resizing the window using MSAA. This is on top of #3042 . Co-authored-by: Carter Anderson <mcanders1@gmail.com>
1 parent 95032a3 commit 2f22f5c

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed

Cargo.toml

+4
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,10 @@ path = "examples/3d/load_gltf_pipelined.rs"
180180
name = "msaa"
181181
path = "examples/3d/msaa.rs"
182182

183+
[[example]]
184+
name = "msaa_pipelined"
185+
path = "examples/3d/msaa_pipelined.rs"
186+
183187
[[example]]
184188
name = "orthographic"
185189
path = "examples/3d/orthographic.rs"

examples/3d/msaa_pipelined.rs

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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+
}

examples/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ Example | File | Description
103103
`load_gltf_pipelined` | [`3d/load_gltf_pipelined.rs`](./3d/load_gltf_pipelined.rs) | Loads and renders a gltf file as a scene
104104
`many_cubes_pipelined` | [`3d/many_cubes_pipelined.rs`](./3d/many_cubes_pipelined.rs) | Simple benchmark to test per-entity draw overhead
105105
`msaa` | [`3d/msaa.rs`](./3d/msaa.rs) | Configures MSAA (Multi-Sample Anti-Aliasing) for smoother edges
106+
`msaa_pipelined` | [`3d/msaa_pipelined.rs`](./3d/msaa_pipelined.rs) | Configures MSAA (Multi-Sample Anti-Aliasing) for smoother edges
106107
`orthographic` | [`3d/orthographic.rs`](./3d/orthographic.rs) | Shows how to create a 3D orthographic view (for isometric-look games or CAD applications)
107108
`orthographic_pipelined` | [`3d/orthographic_pipelined.rs`](./3d/orthographic_pipelined.rs) | Shows how to create a 3D orthographic view (for isometric-look games or CAD applications)
108109
`parenting` | [`3d/parenting.rs`](./3d/parenting.rs) | Demonstrates parent->child relationships and relative transformations

0 commit comments

Comments
 (0)