Skip to content

Commit

Permalink
Use new color types
Browse files Browse the repository at this point in the history
  • Loading branch information
msvbg committed Feb 27, 2024
1 parent 175e401 commit fcf34fd
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 24 deletions.
1 change: 1 addition & 0 deletions crates/bevy_sprite/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ webgpu = []
# bevy
bevy_app = { path = "../bevy_app", version = "0.14.0-dev" }
bevy_asset = { path = "../bevy_asset", version = "0.14.0-dev" }
bevy_color = { path = "../bevy_color", version = "0.14.0-dev" }
bevy_core_pipeline = { path = "../bevy_core_pipeline", version = "0.14.0-dev" }
bevy_ecs = { path = "../bevy_ecs", version = "0.14.0-dev" }
bevy_log = { path = "../bevy_log", version = "0.14.0-dev" }
Expand Down
15 changes: 8 additions & 7 deletions crates/bevy_sprite/src/mesh2d/wireframe2d.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::{Material2d, Material2dKey, Material2dPlugin, Mesh2dHandle};
use bevy_app::{Plugin, Startup, Update};
use bevy_asset::{load_internal_asset, Asset, Assets, Handle};
use bevy_color::{LinearRgba, Srgba};
use bevy_ecs::prelude::*;
use bevy_reflect::{std_traits::ReflectDefault, Reflect, TypePath};
use bevy_render::{
Expand Down Expand Up @@ -65,7 +66,7 @@ pub struct Wireframe2d;
#[derive(Component, Debug, Clone, Default, Reflect)]
#[reflect(Component, Default)]
pub struct Wireframe2dColor {
pub color: LegacyColor,
pub color: Srgba,
}

/// Disables wireframe rendering for any entity it is attached to.
Expand All @@ -85,7 +86,7 @@ pub struct Wireframe2dConfig {
/// If [`Self::global`] is set, any [`Entity`] that does not have a [`Wireframe2d`] component attached to it will have
/// wireframes using this color. Otherwise, this will be the fallback color for any entity that has a [`Wireframe2d`],
/// but no [`Wireframe2dColor`].
pub default_color: LegacyColor,
pub default_color: Srgba,
}

#[derive(Resource)]
Expand All @@ -102,7 +103,7 @@ fn setup_global_wireframe_material(
// Create the handle used for the global material
commands.insert_resource(GlobalWireframe2dMaterial {
handle: materials.add(Wireframe2dMaterial {
color: config.default_color,
color: config.default_color.into(),
}),
});
}
Expand All @@ -114,7 +115,7 @@ fn global_color_changed(
global_material: Res<GlobalWireframe2dMaterial>,
) {
if let Some(global_material) = materials.get_mut(&global_material.handle) {
global_material.color = config.default_color;
global_material.color = config.default_color.into();
}
}

Expand All @@ -129,7 +130,7 @@ fn wireframe_color_changed(
) {
for (mut handle, wireframe_color) in &mut colors_changed {
*handle = materials.add(Wireframe2dMaterial {
color: wireframe_color.color,
color: wireframe_color.color.into(),
});
}
}
Expand Down Expand Up @@ -157,7 +158,7 @@ fn apply_wireframe_material(
for (e, wireframe_color) in &wireframes {
let material = if let Some(wireframe_color) = wireframe_color {
materials.add(Wireframe2dMaterial {
color: wireframe_color.color,
color: wireframe_color.color.into(),
})
} else {
// If there's no color specified we can use the global material since it's already set to use the default_color
Expand Down Expand Up @@ -206,7 +207,7 @@ fn apply_global_wireframe_material(
#[derive(Default, AsBindGroup, TypePath, Debug, Clone, Asset)]
pub struct Wireframe2dMaterial {
#[uniform(0)]
pub color: LegacyColor,
pub color: LinearRgba,
}

impl Material2d for Wireframe2dMaterial {
Expand Down
29 changes: 12 additions & 17 deletions examples/2d/wireframe_2d.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! Showcases wireframe rendering.
//! Showcases wireframe rendering for 2d meshes.
//!
//! Wireframes currently do not work when using webgl or webgpu.
//! Supported platforms:
Expand All @@ -9,6 +9,7 @@
//! This is a native only feature.

use bevy::{
color::palettes::basic::{GREEN, RED, WHITE},
prelude::*,
render::{
render_resource::WgpuFeatures,
Expand Down Expand Up @@ -43,14 +44,14 @@ fn main() {
global: true,
// Controls the default color of all wireframes. Used as the default color for global wireframes.
// Can be changed per mesh using the `Wireframe2dColor` component.
default_color: LegacyColor::WHITE,
default_color: WHITE,
})
.add_systems(Startup, setup)
.add_systems(Update, update_colors)
.run();
}

/// set up a simple 3D scene
/// Set up a simple 3D scene
fn setup(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
Expand Down Expand Up @@ -90,12 +91,10 @@ fn setup(
Wireframe2d,
// This lets you configure the wireframe color of this entity.
// If not set, this will use the color in `WireframeConfig`
Wireframe2dColor {
color: LegacyColor::GREEN,
},
Wireframe2dColor { color: GREEN },
));

// camera
// Camera
commands.spawn(Camera2dBundle::default());

// Text used to show controls
Expand All @@ -109,7 +108,7 @@ fn setup(
);
}

/// This system let's you toggle various wireframe settings
/// This system lets you toggle various wireframe settings
fn update_colors(
keyboard_input: Res<ButtonInput<KeyCode>>,
mut config: ResMut<Wireframe2dConfig>,
Expand Down Expand Up @@ -139,21 +138,17 @@ Color: {:?}

// Toggle the global wireframe color
if keyboard_input.just_pressed(KeyCode::KeyX) {
config.default_color = if config.default_color == LegacyColor::WHITE {
LegacyColor::PINK
config.default_color = if config.default_color == WHITE {
RED
} else {
LegacyColor::WHITE
WHITE
};
}

// Toggle the color of a wireframe using WireframeColor and not the global color
// Toggle the color of a wireframe using `Wireframe2dColor` and not the global color
if keyboard_input.just_pressed(KeyCode::KeyC) {
for mut color in &mut wireframe_colors {
color.color = if color.color == LegacyColor::GREEN {
LegacyColor::RED
} else {
LegacyColor::GREEN
};
color.color = if color.color == GREEN { RED } else { GREEN };
}
}
}

0 comments on commit fcf34fd

Please sign in to comment.