Skip to content

Commit

Permalink
Wireframe example now toggles global setting on and off.
Browse files Browse the repository at this point in the history
  • Loading branch information
Wcubed committed Jan 31, 2023
1 parent 3d223b6 commit 2b2584a
Showing 1 changed file with 50 additions and 16 deletions.
66 changes: 50 additions & 16 deletions examples/3d/wireframe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,39 +15,54 @@ fn main() {
},
}))
.add_plugin(WireframePlugin)
.insert_resource(WireFrameToggleTimer(Timer::from_seconds(
1.0,
TimerMode::Repeating,
)))
.add_startup_system(setup)
.add_system(toggle_global_wireframe_setting)
.run();
}

/// set up a simple 3D scene
fn setup(
mut commands: Commands,
mut wireframe_config: ResMut<WireframeConfig>,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
// This enables drawing of wireframes on every mesh, except those with
// `WireframeOverride::NeverRender`.
// To instead enable it on individual meshes, make this `false`, and add a
// `WireframeOverride::AlwaysRender` component for the meshes that should have wireframes.
wireframe_config.global = true;
// plane
commands.spawn((
PbrBundle {
mesh: meshes.add(Mesh::from(shape::Plane { size: 5.0 })),
material: materials.add(Color::rgb(0.3, 0.5, 0.3).into()),
commands.spawn(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Plane { size: 5.0 })),
material: materials.add(Color::rgb(0.3, 0.3, 0.5).into()),
..default()
});

// Red cube: Never renders a wireframe
commands
.spawn(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Cube { size: 1.0 })),
material: materials.add(Color::rgb(0.8, 0.1, 0.1).into()),
transform: Transform::from_xyz(-1.0, 0.5, -1.0),
..default()
},
// This disables wireframe rendering for this entity.
WireframeOverride::NeverRender,
));
// cube
})
.insert(WireframeOverride::NeverRender);
// Orange cube: Follows global wireframe setting
commands.spawn(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Cube { size: 1.0 })),
material: materials.add(Color::rgb(0.8, 0.7, 0.6).into()),
material: materials.add(Color::rgb(0.8, 0.8, 0.1).into()),
transform: Transform::from_xyz(0.0, 0.5, 0.0),
..default()
});
// Green cube: Always renders a wireframe
commands
.spawn(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Cube { size: 1.0 })),
material: materials.add(Color::rgb(0.1, 0.8, 0.1).into()),
transform: Transform::from_xyz(1.0, 0.5, 1.0),
..default()
})
.insert(WireframeOverride::AlwaysRender);

// light
commands.spawn(PointLightBundle {
transform: Transform::from_xyz(4.0, 8.0, 4.0),
Expand All @@ -59,3 +74,22 @@ fn setup(
..default()
});
}

/// This timer is used to periodically toggle the wireframe rendering.
#[derive(Resource)]
struct WireFrameToggleTimer(Timer);

/// Periodically turns the global wireframe setting on and off, to show the differences between
/// [`WireframeOverride::AlwaysRender`], [`WireframeOverride::NeverRender`], and no override.
fn toggle_global_wireframe_setting(
time: Res<Time>,
mut timer: ResMut<WireFrameToggleTimer>,
mut wireframe_config: ResMut<WireframeConfig>,
) {
if timer.0.tick(time.delta()).just_finished() {
// The global wireframe config enables drawing of wireframes on every mesh, except those with
// `WireframeOverride::NeverRender`. Meshes with `WireframeOverride::AlwaysRender` will
// always have a wireframe, regardless of the global configuration.
wireframe_config.global = !wireframe_config.global;
}
}

0 comments on commit 2b2584a

Please sign in to comment.