Skip to content

Commit 2b2584a

Browse files
committed
Wireframe example now toggles global setting on and off.
1 parent 3d223b6 commit 2b2584a

File tree

1 file changed

+50
-16
lines changed

1 file changed

+50
-16
lines changed

examples/3d/wireframe.rs

Lines changed: 50 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,39 +15,54 @@ fn main() {
1515
},
1616
}))
1717
.add_plugin(WireframePlugin)
18+
.insert_resource(WireFrameToggleTimer(Timer::from_seconds(
19+
1.0,
20+
TimerMode::Repeating,
21+
)))
1822
.add_startup_system(setup)
23+
.add_system(toggle_global_wireframe_setting)
1924
.run();
2025
}
2126

2227
/// set up a simple 3D scene
2328
fn setup(
2429
mut commands: Commands,
25-
mut wireframe_config: ResMut<WireframeConfig>,
2630
mut meshes: ResMut<Assets<Mesh>>,
2731
mut materials: ResMut<Assets<StandardMaterial>>,
2832
) {
29-
// This enables drawing of wireframes on every mesh, except those with
30-
// `WireframeOverride::NeverRender`.
31-
// To instead enable it on individual meshes, make this `false`, and add a
32-
// `WireframeOverride::AlwaysRender` component for the meshes that should have wireframes.
33-
wireframe_config.global = true;
3433
// plane
35-
commands.spawn((
36-
PbrBundle {
37-
mesh: meshes.add(Mesh::from(shape::Plane { size: 5.0 })),
38-
material: materials.add(Color::rgb(0.3, 0.5, 0.3).into()),
34+
commands.spawn(PbrBundle {
35+
mesh: meshes.add(Mesh::from(shape::Plane { size: 5.0 })),
36+
material: materials.add(Color::rgb(0.3, 0.3, 0.5).into()),
37+
..default()
38+
});
39+
40+
// Red cube: Never renders a wireframe
41+
commands
42+
.spawn(PbrBundle {
43+
mesh: meshes.add(Mesh::from(shape::Cube { size: 1.0 })),
44+
material: materials.add(Color::rgb(0.8, 0.1, 0.1).into()),
45+
transform: Transform::from_xyz(-1.0, 0.5, -1.0),
3946
..default()
40-
},
41-
// This disables wireframe rendering for this entity.
42-
WireframeOverride::NeverRender,
43-
));
44-
// cube
47+
})
48+
.insert(WireframeOverride::NeverRender);
49+
// Orange cube: Follows global wireframe setting
4550
commands.spawn(PbrBundle {
4651
mesh: meshes.add(Mesh::from(shape::Cube { size: 1.0 })),
47-
material: materials.add(Color::rgb(0.8, 0.7, 0.6).into()),
52+
material: materials.add(Color::rgb(0.8, 0.8, 0.1).into()),
4853
transform: Transform::from_xyz(0.0, 0.5, 0.0),
4954
..default()
5055
});
56+
// Green cube: Always renders a wireframe
57+
commands
58+
.spawn(PbrBundle {
59+
mesh: meshes.add(Mesh::from(shape::Cube { size: 1.0 })),
60+
material: materials.add(Color::rgb(0.1, 0.8, 0.1).into()),
61+
transform: Transform::from_xyz(1.0, 0.5, 1.0),
62+
..default()
63+
})
64+
.insert(WireframeOverride::AlwaysRender);
65+
5166
// light
5267
commands.spawn(PointLightBundle {
5368
transform: Transform::from_xyz(4.0, 8.0, 4.0),
@@ -59,3 +74,22 @@ fn setup(
5974
..default()
6075
});
6176
}
77+
78+
/// This timer is used to periodically toggle the wireframe rendering.
79+
#[derive(Resource)]
80+
struct WireFrameToggleTimer(Timer);
81+
82+
/// Periodically turns the global wireframe setting on and off, to show the differences between
83+
/// [`WireframeOverride::AlwaysRender`], [`WireframeOverride::NeverRender`], and no override.
84+
fn toggle_global_wireframe_setting(
85+
time: Res<Time>,
86+
mut timer: ResMut<WireFrameToggleTimer>,
87+
mut wireframe_config: ResMut<WireframeConfig>,
88+
) {
89+
if timer.0.tick(time.delta()).just_finished() {
90+
// The global wireframe config enables drawing of wireframes on every mesh, except those with
91+
// `WireframeOverride::NeverRender`. Meshes with `WireframeOverride::AlwaysRender` will
92+
// always have a wireframe, regardless of the global configuration.
93+
wireframe_config.global = !wireframe_config.global;
94+
}
95+
}

0 commit comments

Comments
 (0)