From 94e0e1f0318ea83ca3775cbb5fe4bf3a7da90683 Mon Sep 17 00:00:00 2001 From: Radislav Myasnikov Date: Thu, 23 Jan 2025 17:46:58 +0100 Subject: [PATCH] Updated the 2D examples to make them uniform (#17237) # Objective Make the examples look more uniform and more polished. following the issue #17167 ## Solution - [x] Added a minimal UI explaining how to interact with the examples only when needed. - [x] Used the same notation for interactions ex : "Up Arrow: Move Forward \nLeft / Right Arrow: Turn" - [x] Set the color to [GRAY](https://github.com/bevyengine/bevy/pull/17237#discussion_r1907560092) when it's not visible enough - [x] Changed some colors to be easy on the eyes - [x] removed the //camera comment - [x] Unified the use of capital letters in the examples. - [x] Simplified the mesh2d_arc offset calculations. ... --------- Co-authored-by: Alice Cecile Co-authored-by: Rob Parrett --- examples/2d/2d_viewport_to_world.rs | 11 +++ examples/2d/bloom_2d.rs | 2 +- examples/2d/bounding_2d.rs | 3 +- examples/2d/cpu_draw.rs | 19 +++-- examples/2d/custom_gltf_vertex_attribute.rs | 1 - examples/2d/mesh2d.rs | 1 + examples/2d/mesh2d_arcs.rs | 14 ++-- examples/2d/mesh2d_manual.rs | 3 +- examples/2d/mesh2d_vertex_color_texture.rs | 1 - examples/2d/move_sprite.rs | 21 +++--- examples/2d/pixel_grid_snap.rs | 24 +++--- examples/2d/rotation.rs | 84 ++++++++++++--------- examples/2d/sprite.rs | 1 + examples/2d/sprite_animation.rs | 46 +++++------ examples/2d/sprite_flipping.rs | 1 + examples/2d/sprite_sheet.rs | 2 + examples/2d/sprite_slice.rs | 5 +- examples/2d/sprite_tile.rs | 1 + examples/2d/text2d.rs | 17 ++--- examples/2d/texture_atlas.rs | 41 +++++----- examples/2d/transparency_2d.rs | 9 ++- examples/2d/wireframe_2d.rs | 1 - 22 files changed, 167 insertions(+), 141 deletions(-) diff --git a/examples/2d/2d_viewport_to_world.rs b/examples/2d/2d_viewport_to_world.rs index 44a16d99a27b1..74cb9bb7f37f8 100644 --- a/examples/2d/2d_viewport_to_world.rs +++ b/examples/2d/2d_viewport_to_world.rs @@ -31,4 +31,15 @@ fn draw_cursor( fn setup(mut commands: Commands) { commands.spawn(Camera2d); + + // Create a minimal UI explaining how to interact with the example + commands.spawn(( + Text::new("Move the mouse to see the circle follow your cursor."), + Node { + position_type: PositionType::Absolute, + top: Val::Px(12.0), + left: Val::Px(12.0), + ..default() + }, + )); } diff --git a/examples/2d/bloom_2d.rs b/examples/2d/bloom_2d.rs index ebb20dceaf541..0fc22fa13f845 100644 --- a/examples/2d/bloom_2d.rs +++ b/examples/2d/bloom_2d.rs @@ -63,7 +63,7 @@ fn setup( Text::default(), Node { position_type: PositionType::Absolute, - bottom: Val::Px(12.0), + top: Val::Px(12.0), left: Val::Px(12.0), ..default() }, diff --git a/examples/2d/bounding_2d.rs b/examples/2d/bounding_2d.rs index a90718058c214..8fbb41d784456 100644 --- a/examples/2d/bounding_2d.rs +++ b/examples/2d/bounding_2d.rs @@ -201,6 +201,7 @@ const OFFSET_Y: f32 = 75.; fn setup(mut commands: Commands) { commands.spawn(Camera2d); + commands.spawn(( Transform::from_xyz(-OFFSET_X, OFFSET_Y, 0.), Shape::Circle(Circle::new(45.)), @@ -259,7 +260,7 @@ fn setup(mut commands: Commands) { Text::default(), Node { position_type: PositionType::Absolute, - bottom: Val::Px(12.0), + top: Val::Px(12.0), left: Val::Px(12.0), ..default() }, diff --git a/examples/2d/cpu_draw.rs b/examples/2d/cpu_draw.rs index 0faea0a4ef8f9..adf7f63b90c44 100644 --- a/examples/2d/cpu_draw.rs +++ b/examples/2d/cpu_draw.rs @@ -38,10 +38,9 @@ struct MyProcGenImage(Handle); struct SeededRng(ChaCha8Rng); fn setup(mut commands: Commands, mut images: ResMut>) { - // spawn a camera commands.spawn(Camera2d); - // create an image that we are going to draw into + // Create an image that we are going to draw into let mut image = Image::new_fill( // 2D image of size 256x256 Extent3d { @@ -57,8 +56,8 @@ fn setup(mut commands: Commands, mut images: ResMut>) { RenderAssetUsages::MAIN_WORLD | RenderAssetUsages::RENDER_WORLD, ); - // to make it extra fancy, we can set the Alpha of each pixel - // so that it fades out in a circular fashion + // To make it extra fancy, we can set the Alpha of each pixel, + // so that it fades out in a circular fashion. for y in 0..IMAGE_HEIGHT { for x in 0..IMAGE_WIDTH { let center = Vec2::new(IMAGE_WIDTH as f32 / 2.0, IMAGE_HEIGHT as f32 / 2.0); @@ -66,22 +65,22 @@ fn setup(mut commands: Commands, mut images: ResMut>) { let r = Vec2::new(x as f32, y as f32).distance(center); let a = 1.0 - (r / max_radius).clamp(0.0, 1.0); - // here we will set the A value by accessing the raw data bytes + // Here we will set the A value by accessing the raw data bytes. // (it is the 4th byte of each pixel, as per our `TextureFormat`) - // find our pixel by its coordinates + // Find our pixel by its coordinates let pixel_bytes = image.pixel_bytes_mut(UVec3::new(x, y, 0)).unwrap(); - // convert our f32 to u8 + // Convert our f32 to u8 pixel_bytes[3] = (a * u8::MAX as f32) as u8; } } - // add it to Bevy's assets, so it can be used for rendering + // Add it to Bevy's assets, so it can be used for rendering // this will give us a handle we can use // (to display it in a sprite, or as part of UI, etc.) let handle = images.add(image); - // create a sprite entity using our image + // Create a sprite entity using our image commands.spawn(Sprite::from_image(handle.clone())); commands.insert_resource(MyProcGenImage(handle)); @@ -95,7 +94,7 @@ fn setup(mut commands: Commands, mut images: ResMut>) { fn draw( my_handle: Res, mut images: ResMut>, - // used to keep track of where we are + // Used to keep track of where we are mut i: Local, mut draw_color: Local, mut seeded_rng: ResMut, diff --git a/examples/2d/custom_gltf_vertex_attribute.rs b/examples/2d/custom_gltf_vertex_attribute.rs index 04011ab0e957f..0742e3616fe04 100644 --- a/examples/2d/custom_gltf_vertex_attribute.rs +++ b/examples/2d/custom_gltf_vertex_attribute.rs @@ -63,7 +63,6 @@ fn setup( Transform::from_scale(150.0 * Vec3::ONE), )); - // Add a camera commands.spawn(Camera2d); } diff --git a/examples/2d/mesh2d.rs b/examples/2d/mesh2d.rs index fbc507e2ef4a0..2608b45916d23 100644 --- a/examples/2d/mesh2d.rs +++ b/examples/2d/mesh2d.rs @@ -15,6 +15,7 @@ fn setup( mut materials: ResMut>, ) { commands.spawn(Camera2d); + commands.spawn(( Mesh2d(meshes.add(Rectangle::default())), MeshMaterial2d(materials.add(Color::from(PURPLE))), diff --git a/examples/2d/mesh2d_arcs.rs b/examples/2d/mesh2d_arcs.rs index e1b2f604359e5..db66b784acd54 100644 --- a/examples/2d/mesh2d_arcs.rs +++ b/examples/2d/mesh2d_arcs.rs @@ -5,7 +5,7 @@ use std::f32::consts::FRAC_PI_2; use bevy::{ - color::palettes::css::{BLUE, DARK_SLATE_GREY, RED}, + color::palettes::css::{BLUE, GRAY, RED}, math::{ bounding::{Bounded2d, BoundingVolume}, Isometry2d, @@ -42,16 +42,14 @@ fn setup( commands.spawn(( Camera2d, Camera { - clear_color: ClearColorConfig::Custom(DARK_SLATE_GREY.into()), + clear_color: ClearColorConfig::Custom(GRAY.into()), ..default() }, )); - const UPPER_Y: f32 = 50.0; - const LOWER_Y: f32 = -50.0; - const FIRST_X: f32 = -450.0; - const OFFSET: f32 = 100.0; const NUM_SLICES: i32 = 8; + const SPACING_X: f32 = 100.0; + const OFFSET_X: f32 = SPACING_X * (NUM_SLICES - 1) as f32 / 2.0; // This draws NUM_SLICES copies of the Bevy logo as circular sectors and segments, // with successively larger angles up to a complete circle. @@ -70,7 +68,7 @@ fn setup( Mesh2d(meshes.add(sector_mesh)), MeshMaterial2d(material.clone()), Transform { - translation: Vec3::new(FIRST_X + OFFSET * i as f32, 2.0 * UPPER_Y, 0.0), + translation: Vec3::new(SPACING_X * i as f32 - OFFSET_X, 50.0, 0.0), rotation: Quat::from_rotation_z(sector_angle), ..default() }, @@ -94,7 +92,7 @@ fn setup( Mesh2d(meshes.add(segment_mesh)), MeshMaterial2d(material.clone()), Transform { - translation: Vec3::new(FIRST_X + OFFSET * i as f32, LOWER_Y, 0.0), + translation: Vec3::new(SPACING_X * i as f32 - OFFSET_X, -50.0, 0.0), rotation: Quat::from_rotation_z(segment_angle), ..default() }, diff --git a/examples/2d/mesh2d_manual.rs b/examples/2d/mesh2d_manual.rs index 15d611ce667cf..05ed47f5c3dbc 100644 --- a/examples/2d/mesh2d_manual.rs +++ b/examples/2d/mesh2d_manual.rs @@ -115,7 +115,6 @@ fn star( Mesh2d(meshes.add(star)), )); - // Spawn the camera commands.spawn(Camera2d); } @@ -126,7 +125,7 @@ pub struct ColoredMesh2d; /// Custom pipeline for 2d meshes with vertex colors #[derive(Resource)] pub struct ColoredMesh2dPipeline { - /// this pipeline wraps the standard [`Mesh2dPipeline`] + /// This pipeline wraps the standard [`Mesh2dPipeline`] mesh2d_pipeline: Mesh2dPipeline, } diff --git a/examples/2d/mesh2d_vertex_color_texture.rs b/examples/2d/mesh2d_vertex_color_texture.rs index 24a242075e59f..bd6e8199ffb6d 100644 --- a/examples/2d/mesh2d_vertex_color_texture.rs +++ b/examples/2d/mesh2d_vertex_color_texture.rs @@ -32,7 +32,6 @@ fn setup( let mesh_handle = meshes.add(mesh); - // Spawn camera commands.spawn(Camera2d); // Spawn the quad with vertex colors diff --git a/examples/2d/move_sprite.rs b/examples/2d/move_sprite.rs index ebea86b8c85e0..d84ccadc16123 100644 --- a/examples/2d/move_sprite.rs +++ b/examples/2d/move_sprite.rs @@ -12,16 +12,17 @@ fn main() { #[derive(Component)] enum Direction { - Up, - Down, + Left, + Right, } fn setup(mut commands: Commands, asset_server: Res) { commands.spawn(Camera2d); + commands.spawn(( Sprite::from_image(asset_server.load("branding/icon.png")), - Transform::from_xyz(100., 0., 0.), - Direction::Up, + Transform::from_xyz(0., 0., 0.), + Direction::Right, )); } @@ -30,14 +31,14 @@ fn setup(mut commands: Commands, asset_server: Res) { fn sprite_movement(time: Res