Skip to content

Commit

Permalink
Change wireframe component to a WireframeOverride enum.
Browse files Browse the repository at this point in the history
  • Loading branch information
Wcubed committed Jan 26, 2023
1 parent 56cb515 commit 47085b0
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 14 deletions.
33 changes: 24 additions & 9 deletions crates/bevy_pbr/src/wireframe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl Plugin for WireframePlugin {
Shader::from_wgsl
);

app.register_type::<Wireframe>()
app.register_type::<WireframeOverride>()
.register_type::<WireframeConfig>()
.init_resource::<WireframeConfig>()
.add_plugin(ExtractResourcePlugin::<WireframeConfig>::default());
Expand All @@ -52,24 +52,33 @@ impl Plugin for WireframePlugin {
}
}

fn extract_wireframes(mut commands: Commands, query: Extract<Query<(Entity, &Wireframe)>>) {
fn extract_wireframes(mut commands: Commands, query: Extract<Query<(Entity, &WireframeOverride)>>) {
for (entity, wireframe) in &query {
commands.get_or_spawn(entity).insert(wireframe.clone());
}
}

/// Overrides the global [`WireframeConfig`] for a single mesh.
#[derive(Component, Debug, Clone, Default, Reflect, Eq, PartialEq)]
#[derive(Component, Debug, Clone, Copy, Reflect, Eq, PartialEq)]
#[reflect(Component, Default)]
pub struct Wireframe {
pub enable: bool,
pub enum WireframeOverride {
/// Always render the wireframe for this entity, regardless of global config.
AlwaysRender,
/// Never render the wireframe for this entity, regardless of global config.
NeverRender,
}

impl Default for WireframeOverride {
fn default() -> Self {
Self::AlwaysRender
}
}

#[derive(Resource, Debug, Clone, Default, ExtractResource, Reflect)]
#[reflect(Resource)]
pub struct WireframeConfig {
/// Whether to show wireframes for all meshes.
/// Can be overridden for individual meshes by adding a [`Wireframe`] component.
/// Can be overridden for individual meshes by adding a [`WireframeOverride`] component.
pub global: bool,
}

Expand Down Expand Up @@ -113,7 +122,12 @@ fn queue_wireframes(
mut pipelines: ResMut<SpecializedMeshPipelines<WireframePipeline>>,
pipeline_cache: Res<PipelineCache>,
msaa: Res<Msaa>,
material_meshes: Query<(Entity, &Handle<Mesh>, &MeshUniform, Option<&Wireframe>)>,
material_meshes: Query<(
Entity,
&Handle<Mesh>,
&MeshUniform,
Option<&WireframeOverride>,
)>,
mut views: Query<(&ExtractedView, &VisibleEntities, &mut RenderPhase<Opaque3d>)>,
) {
let draw_custom = opaque_3d_draw_functions.read().id::<DrawWireframes>();
Expand Down Expand Up @@ -155,8 +169,9 @@ fn queue_wireframes(
.filter_map(|visible_entity| material_meshes.get(*visible_entity).ok())
.filter(|mesh| {
let wireframe_override = mesh.3;
(wireframe_config.global || wireframe_override == Some(&Wireframe { enable: true }))
&& wireframe_override != Some(&Wireframe { enable: false })
(wireframe_config.global
|| wireframe_override == Some(&WireframeOverride::AlwaysRender))
&& wireframe_override != Some(&WireframeOverride::NeverRender)
})
.for_each(add_render_phase);
}
Expand Down
11 changes: 6 additions & 5 deletions examples/3d/wireframe.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Showcases wireframe rendering.

use bevy::{
pbr::wireframe::{Wireframe, WireframeConfig, WireframePlugin},
pbr::wireframe::{WireframeConfig, WireframeOverride, WireframePlugin},
prelude::*,
render::{render_resource::WgpuFeatures, settings::WgpuSettings, RenderPlugin},
};
Expand All @@ -26,9 +26,10 @@ fn setup(
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
// This enables drawing of wireframes on every mesh.
// To instead enable it on individual meshes, make this `false`, and add a `Wireframe` component
// with `enable: true` to the meshes that should have wireframe enabled.
// 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((
Expand All @@ -38,7 +39,7 @@ fn setup(
..default()
},
// This disables wireframe rendering for this entity.
Wireframe { enable: false },
WireframeOverride::NeverRender,
));
// cube
commands.spawn(PbrBundle {
Expand Down

0 comments on commit 47085b0

Please sign in to comment.