mirror of
https://github.com/bevyengine/bevy
synced 2024-11-14 00:47:32 +00:00
599e5e4e76
# Objective - As part of the migration process we need to a) see the end effect of the migration on user ergonomics b) check for serious perf regressions c) actually migrate the code - To accomplish this, I'm going to attempt to migrate all of the remaining user-facing usages of `LegacyColor` in one PR, being careful to keep a clean commit history. - Fixes #12056. ## Solution I've chosen to use the polymorphic `Color` type as our standard user-facing API. - [x] Migrate `bevy_gizmos`. - [x] Take `impl Into<Color>` in all `bevy_gizmos` APIs - [x] Migrate sprites - [x] Migrate UI - [x] Migrate `ColorMaterial` - [x] Migrate `MaterialMesh2D` - [x] Migrate fog - [x] Migrate lights - [x] Migrate StandardMaterial - [x] Migrate wireframes - [x] Migrate clear color - [x] Migrate text - [x] Migrate gltf loader - [x] Register color types for reflection - [x] Remove `LegacyColor` - [x] Make sure CI passes Incidental improvements to ease migration: - added `Color::srgba_u8`, `Color::srgba_from_array` and friends - added `set_alpha`, `is_fully_transparent` and `is_fully_opaque` to the `Alpha` trait - add and immediately deprecate (lol) `Color::rgb` and friends in favor of more explicit and consistent `Color::srgb` - standardized on white and black for most example text colors - added vector field traits to `LinearRgba`: ~~`Add`, `Sub`, `AddAssign`, `SubAssign`,~~ `Mul<f32>` and `Div<f32>`. Multiplications and divisions do not scale alpha. `Add` and `Sub` have been cut from this PR. - added `LinearRgba` and `Srgba` `RED/GREEN/BLUE` - added `LinearRgba_to_f32_array` and `LinearRgba::to_u32` ## Migration Guide Bevy's color types have changed! Wherever you used a `bevy::render::Color`, a `bevy::color::Color` is used instead. These are quite similar! Both are enums storing a color in a specific color space (or to be more precise, using a specific color model). However, each of the different color models now has its own type. TODO... - `Color::rgba`, `Color::rgb`, `Color::rbga_u8`, `Color::rgb_u8`, `Color::rgb_from_array` are now `Color::srgba`, `Color::srgb`, `Color::srgba_u8`, `Color::srgb_u8` and `Color::srgb_from_array`. - `Color::set_a` and `Color::a` is now `Color::set_alpha` and `Color::alpha`. These are part of the `Alpha` trait in `bevy_color`. - `Color::is_fully_transparent` is now part of the `Alpha` trait in `bevy_color` - `Color::r`, `Color::set_r`, `Color::with_r` and the equivalents for `g`, `b` `h`, `s` and `l` have been removed due to causing silent relatively expensive conversions. Convert your `Color` into the desired color space, perform your operations there, and then convert it back into a polymorphic `Color` enum. - `Color::hex` is now `Srgba::hex`. Call `.into` or construct a `Color::Srgba` variant manually to convert it. - `WireframeMaterial`, `ExtractedUiNode`, `ExtractedDirectionalLight`, `ExtractedPointLight`, `ExtractedSpotLight` and `ExtractedSprite` now store a `LinearRgba`, rather than a polymorphic `Color` - `Color::rgb_linear` and `Color::rgba_linear` are now `Color::linear_rgb` and `Color::linear_rgba` - The various CSS color constants are no longer stored directly on `Color`. Instead, they're defined in the `Srgba` color space, and accessed via `bevy::color::palettes::css`. Call `.into()` on them to convert them into a `Color` for quick debugging use, and consider using the much prettier `tailwind` palette for prototyping. - The `LIME_GREEN` color has been renamed to `LIMEGREEN` to comply with the standard naming. - Vector field arithmetic operations on `Color` (add, subtract, multiply and divide by a f32) have been removed. Instead, convert your colors into `LinearRgba` space, and perform your operations explicitly there. This is particularly relevant when working with emissive or HDR colors, whose color channel values are routinely outside of the ordinary 0 to 1 range. - `Color::as_linear_rgba_f32` has been removed. Call `LinearRgba::to_f32_array` instead, converting if needed. - `Color::as_linear_rgba_u32` has been removed. Call `LinearRgba::to_u32` instead, converting if needed. - Several other color conversion methods to transform LCH or HSL colors into float arrays or `Vec` types have been removed. Please reimplement these externally or open a PR to re-add them if you found them particularly useful. - Various methods on `Color` such as `rgb` or `hsl` to convert the color into a specific color space have been removed. Convert into `LinearRgba`, then to the color space of your choice. - Various implicitly-converting color value methods on `Color` such as `r`, `g`, `b` or `h` have been removed. Please convert it into the color space of your choice, then check these properties. - `Color` no longer implements `AsBindGroup`. Store a `LinearRgba` internally instead to avoid conversion costs. --------- Co-authored-by: Alice Cecile <alice.i.cecil@gmail.com> Co-authored-by: Afonso Lage <lage.afonso@gmail.com> Co-authored-by: Rob Parrett <robparrett@gmail.com> Co-authored-by: Zachary Harrold <zac@harrold.com.au>
275 lines
8.3 KiB
Rust
275 lines
8.3 KiB
Rust
//! General UI benchmark that stress tests layouting, text, interaction and rendering
|
|
|
|
use argh::FromArgs;
|
|
use bevy::{
|
|
color::palettes::css::ORANGE_RED,
|
|
diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin},
|
|
prelude::*,
|
|
window::{PresentMode, WindowPlugin, WindowResolution},
|
|
winit::{UpdateMode, WinitSettings},
|
|
};
|
|
|
|
const FONT_SIZE: f32 = 7.0;
|
|
|
|
#[derive(FromArgs, Resource)]
|
|
/// `many_buttons` general UI benchmark that stress tests layouting, text, interaction and rendering
|
|
struct Args {
|
|
/// whether to add text to each button
|
|
#[argh(switch)]
|
|
no_text: bool,
|
|
|
|
/// whether to add borders to each button
|
|
#[argh(switch)]
|
|
no_borders: bool,
|
|
|
|
/// whether to perform a full relayout each frame
|
|
#[argh(switch)]
|
|
relayout: bool,
|
|
|
|
/// whether to recompute all text each frame
|
|
#[argh(switch)]
|
|
recompute_text: bool,
|
|
|
|
/// how many buttons per row and column of the grid.
|
|
#[argh(option, default = "110")]
|
|
buttons: usize,
|
|
|
|
/// give every nth button an image
|
|
#[argh(option, default = "4")]
|
|
image_freq: usize,
|
|
|
|
/// use the grid layout model
|
|
#[argh(switch)]
|
|
grid: bool,
|
|
}
|
|
|
|
/// This example shows what happens when there is a lot of buttons on screen.
|
|
fn main() {
|
|
// `from_env` panics on the web
|
|
#[cfg(not(target_arch = "wasm32"))]
|
|
let args: Args = argh::from_env();
|
|
#[cfg(target_arch = "wasm32")]
|
|
let args = Args::from_args(&[], &[]).unwrap();
|
|
|
|
let mut app = App::new();
|
|
|
|
app.add_plugins((
|
|
DefaultPlugins.set(WindowPlugin {
|
|
primary_window: Some(Window {
|
|
present_mode: PresentMode::AutoNoVsync,
|
|
resolution: WindowResolution::new(1920.0, 1080.0).with_scale_factor_override(1.0),
|
|
..default()
|
|
}),
|
|
..default()
|
|
}),
|
|
FrameTimeDiagnosticsPlugin,
|
|
LogDiagnosticsPlugin::default(),
|
|
))
|
|
.insert_resource(WinitSettings {
|
|
focused_mode: UpdateMode::Continuous,
|
|
unfocused_mode: UpdateMode::Continuous,
|
|
})
|
|
.add_systems(Update, button_system);
|
|
|
|
if args.grid {
|
|
app.add_systems(Startup, setup_grid);
|
|
} else {
|
|
app.add_systems(Startup, setup_flex);
|
|
}
|
|
|
|
if args.relayout {
|
|
app.add_systems(Update, |mut style_query: Query<&mut Style>| {
|
|
style_query
|
|
.iter_mut()
|
|
.for_each(|mut style| style.set_changed());
|
|
});
|
|
}
|
|
|
|
if args.recompute_text {
|
|
app.add_systems(Update, |mut text_query: Query<&mut Text>| {
|
|
text_query
|
|
.iter_mut()
|
|
.for_each(|mut text| text.set_changed());
|
|
});
|
|
}
|
|
|
|
app.insert_resource(args).run();
|
|
}
|
|
|
|
#[derive(Component)]
|
|
struct IdleColor(BackgroundColor);
|
|
|
|
fn button_system(
|
|
mut interaction_query: Query<
|
|
(&Interaction, &mut BackgroundColor, &IdleColor),
|
|
Changed<Interaction>,
|
|
>,
|
|
) {
|
|
for (interaction, mut button_color, IdleColor(idle_color)) in interaction_query.iter_mut() {
|
|
*button_color = match interaction {
|
|
Interaction::Hovered => ORANGE_RED.into(),
|
|
_ => *idle_color,
|
|
};
|
|
}
|
|
}
|
|
|
|
fn setup_flex(mut commands: Commands, asset_server: Res<AssetServer>, args: Res<Args>) {
|
|
warn!(include_str!("warning_string.txt"));
|
|
let image = if 0 < args.image_freq {
|
|
Some(asset_server.load("branding/icon.png"))
|
|
} else {
|
|
None
|
|
};
|
|
|
|
let buttons_f = args.buttons as f32;
|
|
let border = if args.no_borders {
|
|
UiRect::ZERO
|
|
} else {
|
|
UiRect::all(Val::VMin(0.05 * 90. / buttons_f))
|
|
};
|
|
|
|
let as_rainbow = |i: usize| Color::hsl((i as f32 / buttons_f) * 360.0, 0.9, 0.8);
|
|
commands.spawn(Camera2dBundle::default());
|
|
commands
|
|
.spawn(NodeBundle {
|
|
style: Style {
|
|
flex_direction: FlexDirection::Column,
|
|
justify_content: JustifyContent::Center,
|
|
align_items: AlignItems::Center,
|
|
width: Val::Percent(100.),
|
|
height: Val::Percent(100.),
|
|
..default()
|
|
},
|
|
..default()
|
|
})
|
|
.with_children(|commands| {
|
|
for column in 0..args.buttons {
|
|
commands
|
|
.spawn(NodeBundle::default())
|
|
.with_children(|commands| {
|
|
for row in 0..args.buttons {
|
|
let color = as_rainbow(row % column.max(1)).into();
|
|
let border_color = Color::WHITE.with_alpha(0.5).into();
|
|
spawn_button(
|
|
commands,
|
|
color,
|
|
buttons_f,
|
|
column,
|
|
row,
|
|
!args.no_text,
|
|
border,
|
|
border_color,
|
|
image
|
|
.as_ref()
|
|
.filter(|_| (column + row) % args.image_freq == 0)
|
|
.cloned(),
|
|
);
|
|
}
|
|
});
|
|
}
|
|
});
|
|
}
|
|
|
|
fn setup_grid(mut commands: Commands, asset_server: Res<AssetServer>, args: Res<Args>) {
|
|
warn!(include_str!("warning_string.txt"));
|
|
let image = if 0 < args.image_freq {
|
|
Some(asset_server.load("branding/icon.png"))
|
|
} else {
|
|
None
|
|
};
|
|
|
|
let buttons_f = args.buttons as f32;
|
|
let border = if args.no_borders {
|
|
UiRect::ZERO
|
|
} else {
|
|
UiRect::all(Val::VMin(0.05 * 90. / buttons_f))
|
|
};
|
|
|
|
let as_rainbow = |i: usize| Color::hsl((i as f32 / buttons_f) * 360.0, 0.9, 0.8);
|
|
commands.spawn(Camera2dBundle::default());
|
|
commands
|
|
.spawn(NodeBundle {
|
|
style: Style {
|
|
display: Display::Grid,
|
|
width: Val::Percent(100.),
|
|
height: Val::Percent(100.0),
|
|
grid_template_columns: RepeatedGridTrack::flex(args.buttons as u16, 1.0),
|
|
grid_template_rows: RepeatedGridTrack::flex(args.buttons as u16, 1.0),
|
|
..default()
|
|
},
|
|
..default()
|
|
})
|
|
.with_children(|commands| {
|
|
for column in 0..args.buttons {
|
|
for row in 0..args.buttons {
|
|
let color = as_rainbow(row % column.max(1)).into();
|
|
let border_color = Color::WHITE.with_alpha(0.5).into();
|
|
spawn_button(
|
|
commands,
|
|
color,
|
|
buttons_f,
|
|
column,
|
|
row,
|
|
!args.no_text,
|
|
border,
|
|
border_color,
|
|
image
|
|
.as_ref()
|
|
.filter(|_| (column + row) % args.image_freq == 0)
|
|
.cloned(),
|
|
);
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
#[allow(clippy::too_many_arguments)]
|
|
fn spawn_button(
|
|
commands: &mut ChildBuilder,
|
|
background_color: BackgroundColor,
|
|
buttons: f32,
|
|
column: usize,
|
|
row: usize,
|
|
spawn_text: bool,
|
|
border: UiRect,
|
|
border_color: BorderColor,
|
|
image: Option<Handle<Image>>,
|
|
) {
|
|
let width = Val::Vw(90.0 / buttons);
|
|
let height = Val::Vh(90.0 / buttons);
|
|
let margin = UiRect::axes(width * 0.05, height * 0.05);
|
|
let mut builder = commands.spawn((
|
|
ButtonBundle {
|
|
style: Style {
|
|
width,
|
|
height,
|
|
margin,
|
|
align_items: AlignItems::Center,
|
|
justify_content: JustifyContent::Center,
|
|
border,
|
|
..default()
|
|
},
|
|
background_color,
|
|
border_color,
|
|
..default()
|
|
},
|
|
IdleColor(background_color),
|
|
));
|
|
|
|
if let Some(image) = image {
|
|
builder.insert(UiImage::new(image));
|
|
}
|
|
|
|
if spawn_text {
|
|
builder.with_children(|parent| {
|
|
parent.spawn(TextBundle::from_section(
|
|
format!("{column}, {row}"),
|
|
TextStyle {
|
|
font_size: FONT_SIZE,
|
|
color: Color::srgb(0.2, 0.2, 0.2),
|
|
..default()
|
|
},
|
|
));
|
|
});
|
|
}
|
|
}
|