Skip to content

Commit 94e0e1f

Browse files
chamalorizalice-i-cecilerparrett
authored
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](#17237 (comment)) 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 <[email protected]> Co-authored-by: Rob Parrett <[email protected]>
1 parent 9bc0ae3 commit 94e0e1f

22 files changed

+167
-141
lines changed

examples/2d/2d_viewport_to_world.rs

+11
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,15 @@ fn draw_cursor(
3131

3232
fn setup(mut commands: Commands) {
3333
commands.spawn(Camera2d);
34+
35+
// Create a minimal UI explaining how to interact with the example
36+
commands.spawn((
37+
Text::new("Move the mouse to see the circle follow your cursor."),
38+
Node {
39+
position_type: PositionType::Absolute,
40+
top: Val::Px(12.0),
41+
left: Val::Px(12.0),
42+
..default()
43+
},
44+
));
3445
}

examples/2d/bloom_2d.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ fn setup(
6363
Text::default(),
6464
Node {
6565
position_type: PositionType::Absolute,
66-
bottom: Val::Px(12.0),
66+
top: Val::Px(12.0),
6767
left: Val::Px(12.0),
6868
..default()
6969
},

examples/2d/bounding_2d.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ const OFFSET_Y: f32 = 75.;
201201

202202
fn setup(mut commands: Commands) {
203203
commands.spawn(Camera2d);
204+
204205
commands.spawn((
205206
Transform::from_xyz(-OFFSET_X, OFFSET_Y, 0.),
206207
Shape::Circle(Circle::new(45.)),
@@ -259,7 +260,7 @@ fn setup(mut commands: Commands) {
259260
Text::default(),
260261
Node {
261262
position_type: PositionType::Absolute,
262-
bottom: Val::Px(12.0),
263+
top: Val::Px(12.0),
263264
left: Val::Px(12.0),
264265
..default()
265266
},

examples/2d/cpu_draw.rs

+9-10
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,9 @@ struct MyProcGenImage(Handle<Image>);
3838
struct SeededRng(ChaCha8Rng);
3939

4040
fn setup(mut commands: Commands, mut images: ResMut<Assets<Image>>) {
41-
// spawn a camera
4241
commands.spawn(Camera2d);
4342

44-
// create an image that we are going to draw into
43+
// Create an image that we are going to draw into
4544
let mut image = Image::new_fill(
4645
// 2D image of size 256x256
4746
Extent3d {
@@ -57,31 +56,31 @@ fn setup(mut commands: Commands, mut images: ResMut<Assets<Image>>) {
5756
RenderAssetUsages::MAIN_WORLD | RenderAssetUsages::RENDER_WORLD,
5857
);
5958

60-
// to make it extra fancy, we can set the Alpha of each pixel
61-
// so that it fades out in a circular fashion
59+
// To make it extra fancy, we can set the Alpha of each pixel,
60+
// so that it fades out in a circular fashion.
6261
for y in 0..IMAGE_HEIGHT {
6362
for x in 0..IMAGE_WIDTH {
6463
let center = Vec2::new(IMAGE_WIDTH as f32 / 2.0, IMAGE_HEIGHT as f32 / 2.0);
6564
let max_radius = IMAGE_HEIGHT.min(IMAGE_WIDTH) as f32 / 2.0;
6665
let r = Vec2::new(x as f32, y as f32).distance(center);
6766
let a = 1.0 - (r / max_radius).clamp(0.0, 1.0);
6867

69-
// here we will set the A value by accessing the raw data bytes
68+
// Here we will set the A value by accessing the raw data bytes.
7069
// (it is the 4th byte of each pixel, as per our `TextureFormat`)
7170

72-
// find our pixel by its coordinates
71+
// Find our pixel by its coordinates
7372
let pixel_bytes = image.pixel_bytes_mut(UVec3::new(x, y, 0)).unwrap();
74-
// convert our f32 to u8
73+
// Convert our f32 to u8
7574
pixel_bytes[3] = (a * u8::MAX as f32) as u8;
7675
}
7776
}
7877

79-
// add it to Bevy's assets, so it can be used for rendering
78+
// Add it to Bevy's assets, so it can be used for rendering
8079
// this will give us a handle we can use
8180
// (to display it in a sprite, or as part of UI, etc.)
8281
let handle = images.add(image);
8382

84-
// create a sprite entity using our image
83+
// Create a sprite entity using our image
8584
commands.spawn(Sprite::from_image(handle.clone()));
8685
commands.insert_resource(MyProcGenImage(handle));
8786

@@ -95,7 +94,7 @@ fn setup(mut commands: Commands, mut images: ResMut<Assets<Image>>) {
9594
fn draw(
9695
my_handle: Res<MyProcGenImage>,
9796
mut images: ResMut<Assets<Image>>,
98-
// used to keep track of where we are
97+
// Used to keep track of where we are
9998
mut i: Local<u32>,
10099
mut draw_color: Local<Color>,
101100
mut seeded_rng: ResMut<SeededRng>,

examples/2d/custom_gltf_vertex_attribute.rs

-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ fn setup(
6363
Transform::from_scale(150.0 * Vec3::ONE),
6464
));
6565

66-
// Add a camera
6766
commands.spawn(Camera2d);
6867
}
6968

examples/2d/mesh2d.rs

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ fn setup(
1515
mut materials: ResMut<Assets<ColorMaterial>>,
1616
) {
1717
commands.spawn(Camera2d);
18+
1819
commands.spawn((
1920
Mesh2d(meshes.add(Rectangle::default())),
2021
MeshMaterial2d(materials.add(Color::from(PURPLE))),

examples/2d/mesh2d_arcs.rs

+6-8
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use std::f32::consts::FRAC_PI_2;
66

77
use bevy::{
8-
color::palettes::css::{BLUE, DARK_SLATE_GREY, RED},
8+
color::palettes::css::{BLUE, GRAY, RED},
99
math::{
1010
bounding::{Bounded2d, BoundingVolume},
1111
Isometry2d,
@@ -42,16 +42,14 @@ fn setup(
4242
commands.spawn((
4343
Camera2d,
4444
Camera {
45-
clear_color: ClearColorConfig::Custom(DARK_SLATE_GREY.into()),
45+
clear_color: ClearColorConfig::Custom(GRAY.into()),
4646
..default()
4747
},
4848
));
4949

50-
const UPPER_Y: f32 = 50.0;
51-
const LOWER_Y: f32 = -50.0;
52-
const FIRST_X: f32 = -450.0;
53-
const OFFSET: f32 = 100.0;
5450
const NUM_SLICES: i32 = 8;
51+
const SPACING_X: f32 = 100.0;
52+
const OFFSET_X: f32 = SPACING_X * (NUM_SLICES - 1) as f32 / 2.0;
5553

5654
// This draws NUM_SLICES copies of the Bevy logo as circular sectors and segments,
5755
// with successively larger angles up to a complete circle.
@@ -70,7 +68,7 @@ fn setup(
7068
Mesh2d(meshes.add(sector_mesh)),
7169
MeshMaterial2d(material.clone()),
7270
Transform {
73-
translation: Vec3::new(FIRST_X + OFFSET * i as f32, 2.0 * UPPER_Y, 0.0),
71+
translation: Vec3::new(SPACING_X * i as f32 - OFFSET_X, 50.0, 0.0),
7472
rotation: Quat::from_rotation_z(sector_angle),
7573
..default()
7674
},
@@ -94,7 +92,7 @@ fn setup(
9492
Mesh2d(meshes.add(segment_mesh)),
9593
MeshMaterial2d(material.clone()),
9694
Transform {
97-
translation: Vec3::new(FIRST_X + OFFSET * i as f32, LOWER_Y, 0.0),
95+
translation: Vec3::new(SPACING_X * i as f32 - OFFSET_X, -50.0, 0.0),
9896
rotation: Quat::from_rotation_z(segment_angle),
9997
..default()
10098
},

examples/2d/mesh2d_manual.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,6 @@ fn star(
115115
Mesh2d(meshes.add(star)),
116116
));
117117

118-
// Spawn the camera
119118
commands.spawn(Camera2d);
120119
}
121120

@@ -126,7 +125,7 @@ pub struct ColoredMesh2d;
126125
/// Custom pipeline for 2d meshes with vertex colors
127126
#[derive(Resource)]
128127
pub struct ColoredMesh2dPipeline {
129-
/// this pipeline wraps the standard [`Mesh2dPipeline`]
128+
/// This pipeline wraps the standard [`Mesh2dPipeline`]
130129
mesh2d_pipeline: Mesh2dPipeline,
131130
}
132131

examples/2d/mesh2d_vertex_color_texture.rs

-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ fn setup(
3232

3333
let mesh_handle = meshes.add(mesh);
3434

35-
// Spawn camera
3635
commands.spawn(Camera2d);
3736

3837
// Spawn the quad with vertex colors

examples/2d/move_sprite.rs

+11-10
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,17 @@ fn main() {
1212

1313
#[derive(Component)]
1414
enum Direction {
15-
Up,
16-
Down,
15+
Left,
16+
Right,
1717
}
1818

1919
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
2020
commands.spawn(Camera2d);
21+
2122
commands.spawn((
2223
Sprite::from_image(asset_server.load("branding/icon.png")),
23-
Transform::from_xyz(100., 0., 0.),
24-
Direction::Up,
24+
Transform::from_xyz(0., 0., 0.),
25+
Direction::Right,
2526
));
2627
}
2728

@@ -30,14 +31,14 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
3031
fn sprite_movement(time: Res<Time>, mut sprite_position: Query<(&mut Direction, &mut Transform)>) {
3132
for (mut logo, mut transform) in &mut sprite_position {
3233
match *logo {
33-
Direction::Up => transform.translation.y += 150. * time.delta_secs(),
34-
Direction::Down => transform.translation.y -= 150. * time.delta_secs(),
34+
Direction::Right => transform.translation.x += 150. * time.delta_secs(),
35+
Direction::Left => transform.translation.x -= 150. * time.delta_secs(),
3536
}
3637

37-
if transform.translation.y > 200. {
38-
*logo = Direction::Down;
39-
} else if transform.translation.y < -200. {
40-
*logo = Direction::Up;
38+
if transform.translation.x > 200. {
39+
*logo = Direction::Left;
40+
} else if transform.translation.x < -200. {
41+
*logo = Direction::Right;
4142
}
4243
}
4344
}

examples/2d/pixel_grid_snap.rs

+13-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Shows how to create graphics that snap to the pixel grid by rendering to a texture in 2D
22
33
use bevy::{
4+
color::palettes::css::GRAY,
45
prelude::*,
56
render::{
67
camera::RenderTarget,
@@ -50,18 +51,18 @@ struct OuterCamera;
5051
struct Rotate;
5152

5253
fn setup_sprite(mut commands: Commands, asset_server: Res<AssetServer>) {
53-
// the sample sprite that will be rendered to the pixel-perfect canvas
54+
// The sample sprite that will be rendered to the pixel-perfect canvas
5455
commands.spawn((
5556
Sprite::from_image(asset_server.load("pixel/bevy_pixel_dark.png")),
56-
Transform::from_xyz(-40., 20., 2.),
57+
Transform::from_xyz(-45., 20., 2.),
5758
Rotate,
5859
PIXEL_PERFECT_LAYERS,
5960
));
6061

61-
// the sample sprite that will be rendered to the high-res "outer world"
62+
// The sample sprite that will be rendered to the high-res "outer world"
6263
commands.spawn((
6364
Sprite::from_image(asset_server.load("pixel/bevy_pixel_light.png")),
64-
Transform::from_xyz(-40., -20., 2.),
65+
Transform::from_xyz(-45., -20., 2.),
6566
Rotate,
6667
HIGH_RES_LAYERS,
6768
));
@@ -76,7 +77,7 @@ fn setup_mesh(
7677
commands.spawn((
7778
Mesh2d(meshes.add(Capsule2d::default())),
7879
MeshMaterial2d(materials.add(Color::BLACK)),
79-
Transform::from_xyz(40., 0., 2.).with_scale(Vec3::splat(32.)),
80+
Transform::from_xyz(25., 0., 2.).with_scale(Vec3::splat(32.)),
8081
Rotate,
8182
PIXEL_PERFECT_LAYERS,
8283
));
@@ -89,7 +90,7 @@ fn setup_camera(mut commands: Commands, mut images: ResMut<Assets<Image>>) {
8990
..default()
9091
};
9192

92-
// this Image serves as a canvas representing the low-resolution game screen
93+
// This Image serves as a canvas representing the low-resolution game screen
9394
let mut canvas = Image {
9495
texture_descriptor: TextureDescriptor {
9596
label: None,
@@ -106,29 +107,30 @@ fn setup_camera(mut commands: Commands, mut images: ResMut<Assets<Image>>) {
106107
..default()
107108
};
108109

109-
// fill image.data with zeroes
110+
// Fill image.data with zeroes
110111
canvas.resize(canvas_size);
111112

112113
let image_handle = images.add(canvas);
113114

114-
// this camera renders whatever is on `PIXEL_PERFECT_LAYERS` to the canvas
115+
// This camera renders whatever is on `PIXEL_PERFECT_LAYERS` to the canvas
115116
commands.spawn((
116117
Camera2d,
117118
Camera {
118-
// render before the "main pass" camera
119+
// Render before the "main pass" camera
119120
order: -1,
120121
target: RenderTarget::Image(image_handle.clone().into()),
122+
clear_color: ClearColorConfig::Custom(GRAY.into()),
121123
..default()
122124
},
123125
Msaa::Off,
124126
InGameCamera,
125127
PIXEL_PERFECT_LAYERS,
126128
));
127129

128-
// spawn the canvas
130+
// Spawn the canvas
129131
commands.spawn((Sprite::from_image(image_handle), Canvas, HIGH_RES_LAYERS));
130132

131-
// the "outer" camera renders whatever is on `HIGH_RES_LAYERS` to the screen.
133+
// The "outer" camera renders whatever is on `HIGH_RES_LAYERS` to the screen.
132134
// here, the canvas and one of the sample sprites will be rendered by this camera
133135
commands.spawn((Camera2d, Msaa::Off, OuterCamera, HIGH_RES_LAYERS));
134136
}

0 commit comments

Comments
 (0)