 599e5e4e76
			
		
	
	
		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>
		
			
				
	
	
		
			300 lines
		
	
	
		
			8.8 KiB
		
	
	
	
		
			Rust
		
	
	
	
	
	
			
		
		
	
	
			300 lines
		
	
	
		
			8.8 KiB
		
	
	
	
		
			Rust
		
	
	
	
	
	
| //! Tests how different transforms behave when clipped with `Overflow::Hidden`
 | |
| use bevy::{
 | |
|     color::palettes::css::DARK_GRAY, input::common_conditions::input_just_pressed, prelude::*,
 | |
| };
 | |
| use std::f32::consts::{FRAC_PI_2, PI, TAU};
 | |
| 
 | |
| const CONTAINER_SIZE: f32 = 150.0;
 | |
| const HALF_CONTAINER_SIZE: f32 = CONTAINER_SIZE / 2.0;
 | |
| const LOOP_LENGTH: f32 = 4.0;
 | |
| 
 | |
| fn main() {
 | |
|     App::new()
 | |
|         .add_plugins(DefaultPlugins)
 | |
|         .init_resource::<AnimationState>()
 | |
|         .add_systems(Startup, setup)
 | |
|         .add_systems(
 | |
|             Update,
 | |
|             (
 | |
|                 toggle_overflow.run_if(input_just_pressed(KeyCode::KeyO)),
 | |
|                 next_container_size.run_if(input_just_pressed(KeyCode::KeyS)),
 | |
|                 update_transform::<Move>,
 | |
|                 update_transform::<Scale>,
 | |
|                 update_transform::<Rotate>,
 | |
|                 update_animation,
 | |
|             ),
 | |
|         )
 | |
|         .run();
 | |
| }
 | |
| 
 | |
| #[derive(Component)]
 | |
| struct Instructions;
 | |
| 
 | |
| #[derive(Resource, Default)]
 | |
| struct AnimationState {
 | |
|     playing: bool,
 | |
|     paused_at: f32,
 | |
|     paused_total: f32,
 | |
|     t: f32,
 | |
| }
 | |
| 
 | |
| #[derive(Component)]
 | |
| struct Container(u8);
 | |
| 
 | |
| trait UpdateTransform {
 | |
|     fn update(&self, t: f32, transform: &mut Transform);
 | |
| }
 | |
| 
 | |
| #[derive(Component)]
 | |
| struct Move;
 | |
| 
 | |
| impl UpdateTransform for Move {
 | |
|     fn update(&self, t: f32, transform: &mut Transform) {
 | |
|         transform.translation.x = (t * TAU - FRAC_PI_2).sin() * HALF_CONTAINER_SIZE;
 | |
|         transform.translation.y = -(t * TAU - FRAC_PI_2).cos() * HALF_CONTAINER_SIZE;
 | |
|     }
 | |
| }
 | |
| 
 | |
| #[derive(Component)]
 | |
| struct Scale;
 | |
| 
 | |
| impl UpdateTransform for Scale {
 | |
|     fn update(&self, t: f32, transform: &mut Transform) {
 | |
|         transform.scale.x = 1.0 + 0.5 * (t * TAU).cos().max(0.0);
 | |
|         transform.scale.y = 1.0 + 0.5 * (t * TAU + PI).cos().max(0.0);
 | |
|     }
 | |
| }
 | |
| 
 | |
| #[derive(Component)]
 | |
| struct Rotate;
 | |
| 
 | |
| impl UpdateTransform for Rotate {
 | |
|     fn update(&self, t: f32, transform: &mut Transform) {
 | |
|         transform.rotation = Quat::from_axis_angle(Vec3::Z, ((t * TAU).cos() * 45.0).to_radians());
 | |
|     }
 | |
| }
 | |
| 
 | |
| fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
 | |
|     // Camera
 | |
| 
 | |
|     commands.spawn(Camera2dBundle::default());
 | |
| 
 | |
|     // Instructions
 | |
| 
 | |
|     let text_style = TextStyle {
 | |
|         font_size: 20.,
 | |
|         ..default()
 | |
|     };
 | |
| 
 | |
|     commands.spawn((
 | |
|         TextBundle::from_sections([
 | |
|             TextSection::new(
 | |
|                 "Next Overflow Setting (O)\nNext Container Size (S)\nToggle Animation (space)\n\n",
 | |
|                 text_style.clone(),
 | |
|             ),
 | |
|             TextSection::new(format!("{:?}", Overflow::clip()), text_style.clone()),
 | |
|         ])
 | |
|         .with_style(Style {
 | |
|             position_type: PositionType::Absolute,
 | |
|             top: Val::Px(12.0),
 | |
|             left: Val::Px(12.0),
 | |
|             ..default()
 | |
|         }),
 | |
|         Instructions,
 | |
|     ));
 | |
| 
 | |
|     // Overflow Debug
 | |
| 
 | |
|     commands
 | |
|         .spawn(NodeBundle {
 | |
|             style: Style {
 | |
|                 width: Val::Percent(100.),
 | |
|                 height: Val::Percent(100.),
 | |
|                 justify_content: JustifyContent::Center,
 | |
|                 align_items: AlignItems::Center,
 | |
|                 ..default()
 | |
|             },
 | |
|             ..default()
 | |
|         })
 | |
|         .with_children(|parent| {
 | |
|             parent
 | |
|                 .spawn(NodeBundle {
 | |
|                     style: Style {
 | |
|                         display: Display::Grid,
 | |
|                         grid_template_columns: RepeatedGridTrack::px(3, CONTAINER_SIZE),
 | |
|                         grid_template_rows: RepeatedGridTrack::px(2, CONTAINER_SIZE),
 | |
|                         row_gap: Val::Px(80.),
 | |
|                         column_gap: Val::Px(80.),
 | |
|                         ..default()
 | |
|                     },
 | |
|                     ..default()
 | |
|                 })
 | |
|                 .with_children(|parent| {
 | |
|                     spawn_image(parent, &asset_server, Move);
 | |
|                     spawn_image(parent, &asset_server, Scale);
 | |
|                     spawn_image(parent, &asset_server, Rotate);
 | |
| 
 | |
|                     spawn_text(parent, &asset_server, Move);
 | |
|                     spawn_text(parent, &asset_server, Scale);
 | |
|                     spawn_text(parent, &asset_server, Rotate);
 | |
|                 });
 | |
|         });
 | |
| }
 | |
| 
 | |
| fn spawn_image(
 | |
|     parent: &mut ChildBuilder,
 | |
|     asset_server: &Res<AssetServer>,
 | |
|     update_transform: impl UpdateTransform + Component,
 | |
| ) {
 | |
|     spawn_container(parent, update_transform, |parent| {
 | |
|         parent.spawn(ImageBundle {
 | |
|             image: UiImage::new(asset_server.load("branding/bevy_logo_dark_big.png")),
 | |
|             style: Style {
 | |
|                 height: Val::Px(100.),
 | |
|                 position_type: PositionType::Absolute,
 | |
|                 top: Val::Px(-50.),
 | |
|                 left: Val::Px(-200.),
 | |
|                 ..default()
 | |
|             },
 | |
|             ..default()
 | |
|         });
 | |
|     });
 | |
| }
 | |
| 
 | |
| fn spawn_text(
 | |
|     parent: &mut ChildBuilder,
 | |
|     asset_server: &Res<AssetServer>,
 | |
|     update_transform: impl UpdateTransform + Component,
 | |
| ) {
 | |
|     spawn_container(parent, update_transform, |parent| {
 | |
|         parent.spawn(TextBundle::from_section(
 | |
|             "Bevy",
 | |
|             TextStyle {
 | |
|                 font: asset_server.load("fonts/FiraSans-Bold.ttf"),
 | |
|                 font_size: 120.0,
 | |
|                 ..default()
 | |
|             },
 | |
|         ));
 | |
|     });
 | |
| }
 | |
| 
 | |
| fn spawn_container(
 | |
|     parent: &mut ChildBuilder,
 | |
|     update_transform: impl UpdateTransform + Component,
 | |
|     spawn_children: impl FnOnce(&mut ChildBuilder),
 | |
| ) {
 | |
|     let mut transform = Transform::default();
 | |
| 
 | |
|     update_transform.update(0.0, &mut transform);
 | |
| 
 | |
|     parent
 | |
|         .spawn((
 | |
|             NodeBundle {
 | |
|                 style: Style {
 | |
|                     width: Val::Percent(100.),
 | |
|                     height: Val::Percent(100.),
 | |
|                     align_items: AlignItems::Center,
 | |
|                     justify_content: JustifyContent::Center,
 | |
|                     overflow: Overflow::clip(),
 | |
|                     ..default()
 | |
|                 },
 | |
|                 background_color: DARK_GRAY.into(),
 | |
|                 ..default()
 | |
|             },
 | |
|             Container(0),
 | |
|         ))
 | |
|         .with_children(|parent| {
 | |
|             parent
 | |
|                 .spawn((
 | |
|                     NodeBundle {
 | |
|                         style: Style {
 | |
|                             align_items: AlignItems::Center,
 | |
|                             justify_content: JustifyContent::Center,
 | |
|                             top: Val::Px(transform.translation.x),
 | |
|                             left: Val::Px(transform.translation.y),
 | |
|                             ..default()
 | |
|                         },
 | |
|                         transform,
 | |
|                         ..default()
 | |
|                     },
 | |
|                     update_transform,
 | |
|                 ))
 | |
|                 .with_children(spawn_children);
 | |
|         });
 | |
| }
 | |
| 
 | |
| fn update_animation(
 | |
|     mut animation: ResMut<AnimationState>,
 | |
|     time: Res<Time>,
 | |
|     keys: Res<ButtonInput<KeyCode>>,
 | |
| ) {
 | |
|     let delta = time.elapsed_seconds();
 | |
| 
 | |
|     if keys.just_pressed(KeyCode::Space) {
 | |
|         animation.playing = !animation.playing;
 | |
| 
 | |
|         if !animation.playing {
 | |
|             animation.paused_at = delta;
 | |
|         } else {
 | |
|             animation.paused_total += delta - animation.paused_at;
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     if animation.playing {
 | |
|         animation.t = (delta - animation.paused_total) % LOOP_LENGTH / LOOP_LENGTH;
 | |
|     }
 | |
| }
 | |
| 
 | |
| fn update_transform<T: UpdateTransform + Component>(
 | |
|     animation: Res<AnimationState>,
 | |
|     mut containers: Query<(&mut Transform, &mut Style, &T)>,
 | |
| ) {
 | |
|     for (mut transform, mut style, update_transform) in &mut containers {
 | |
|         update_transform.update(animation.t, &mut transform);
 | |
| 
 | |
|         style.left = Val::Px(transform.translation.x);
 | |
|         style.top = Val::Px(transform.translation.y);
 | |
|     }
 | |
| }
 | |
| 
 | |
| fn toggle_overflow(
 | |
|     mut containers: Query<&mut Style, With<Container>>,
 | |
|     mut instructions: Query<&mut Text, With<Instructions>>,
 | |
| ) {
 | |
|     for mut style in &mut containers {
 | |
|         style.overflow = match style.overflow {
 | |
|             Overflow {
 | |
|                 x: OverflowAxis::Visible,
 | |
|                 y: OverflowAxis::Visible,
 | |
|             } => Overflow::clip_y(),
 | |
|             Overflow {
 | |
|                 x: OverflowAxis::Visible,
 | |
|                 y: OverflowAxis::Clip,
 | |
|             } => Overflow::clip_x(),
 | |
|             Overflow {
 | |
|                 x: OverflowAxis::Clip,
 | |
|                 y: OverflowAxis::Visible,
 | |
|             } => Overflow::clip(),
 | |
|             _ => Overflow::visible(),
 | |
|         };
 | |
| 
 | |
|         let mut text = instructions.single_mut();
 | |
|         text.sections[1].value = format!("{:?}", style.overflow);
 | |
|     }
 | |
| }
 | |
| 
 | |
| fn next_container_size(mut containers: Query<(&mut Style, &mut Container)>) {
 | |
|     for (mut style, mut container) in &mut containers {
 | |
|         container.0 = (container.0 + 1) % 3;
 | |
| 
 | |
|         style.width = match container.0 {
 | |
|             2 => Val::Percent(30.),
 | |
|             _ => Val::Percent(100.),
 | |
|         };
 | |
|         style.height = match container.0 {
 | |
|             1 => Val::Percent(30.),
 | |
|             _ => Val::Percent(100.),
 | |
|         };
 | |
|     }
 | |
| }
 |