Skip to content

Commit

Permalink
Un-hardcode positions and colors in 2d_shapes example (#11867)
Browse files Browse the repository at this point in the history
# Objective

We recently got some neat new 2d shapes and the shapes are no longer
centered on the screen.

The hardcoded positions and colors are a pain to deal with when a new
shape is added.

## Solution

Delete a bunch of code and position shapes evenly. Assign colors evenly
too.

## Before

<img width="1280" alt="Screenshot 2024-02-14 at 3 17 40 PM"
src="https://github.com/bevyengine/bevy/assets/200550/cc9fd9a8-4019-4907-a50e-621cb656c20a">

## After

<img width="1280" alt="Screenshot 2024-02-14 at 3 17 24 PM"
src="https://github.com/bevyengine/bevy/assets/200550/033a3f91-d3bc-4ec8-af59-42a221f8b8e7">

---------

Co-authored-by: BD103 <59022059+BD103@users.noreply.github.com>
Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com>
Co-authored-by: Joona Aalto <jondolf.dev@gmail.com>
  • Loading branch information
4 people authored Feb 14, 2024
1 parent dc9b486 commit 1d5388e
Showing 1 changed file with 36 additions and 54 deletions.
90 changes: 36 additions & 54 deletions examples/2d/2d_shapes.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
//! Shows how to render simple primitive shapes with a single color.
use bevy::{prelude::*, sprite::MaterialMesh2dBundle};
use bevy::{
prelude::*,
sprite::{MaterialMesh2dBundle, Mesh2dHandle},
};

fn main() {
App::new()
Expand All @@ -9,64 +12,43 @@ fn main() {
.run();
}

const X_EXTENT: f32 = 600.;

fn setup(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<ColorMaterial>>,
) {
commands.spawn(Camera2dBundle::default());

// Circle
commands.spawn(MaterialMesh2dBundle {
mesh: meshes.add(Circle { radius: 50.0 }).into(),
material: materials.add(Color::VIOLET),
transform: Transform::from_translation(Vec3::new(-275.0, 0.0, 0.0)),
..default()
});

// Ellipse
commands.spawn(MaterialMesh2dBundle {
mesh: meshes.add(Ellipse::new(25.0, 50.0)).into(),
material: materials.add(Color::TURQUOISE),
transform: Transform::from_translation(Vec3::new(-150.0, 0.0, 0.0)),
..default()
});

// Capsule
commands.spawn(MaterialMesh2dBundle {
mesh: meshes.add(Capsule2d::new(25.0, 50.0)).into(),
material: materials.add(Color::LIME_GREEN),
transform: Transform::from_translation(Vec3::new(-50.0, 0.0, 0.0)),
..default()
});

// Rectangle
commands.spawn(MaterialMesh2dBundle {
mesh: meshes.add(Rectangle::new(50.0, 100.0)).into(),
material: materials.add(Color::YELLOW),
transform: Transform::from_translation(Vec3::new(50.0, 0.0, 0.0)),
..default()
});

// Hexagon
commands.spawn(MaterialMesh2dBundle {
mesh: meshes.add(RegularPolygon::new(50.0, 6)).into(),
material: materials.add(Color::ORANGE),
transform: Transform::from_translation(Vec3::new(175.0, 0.0, 0.0)),
..default()
});

// Triangle
commands.spawn(MaterialMesh2dBundle {
mesh: meshes
.add(Triangle2d::new(
Vec2::Y * 50.0,
Vec2::new(-50.0, -50.0),
Vec2::new(50.0, -50.0),
))
.into(),
material: materials.add(Color::ORANGE_RED),
transform: Transform::from_translation(Vec3::new(300.0, 0.0, 0.0)),
..default()
});
let shapes = [
Mesh2dHandle(meshes.add(Circle { radius: 50.0 })),
Mesh2dHandle(meshes.add(Ellipse::new(25.0, 50.0))),
Mesh2dHandle(meshes.add(Capsule2d::new(25.0, 50.0))),
Mesh2dHandle(meshes.add(Rectangle::new(50.0, 100.0))),
Mesh2dHandle(meshes.add(RegularPolygon::new(50.0, 6))),
Mesh2dHandle(meshes.add(Triangle2d::new(
Vec2::Y * 50.0,
Vec2::new(-50.0, -50.0),
Vec2::new(50.0, -50.0),
))),
];
let num_shapes = shapes.len();

for (i, shape) in shapes.into_iter().enumerate() {
// Distribute colors evenly across the rainbow.
let color = Color::hsl(360. * i as f32 / num_shapes as f32, 0.95, 0.7);

commands.spawn(MaterialMesh2dBundle {
mesh: shape,
material: materials.add(color),
transform: Transform::from_xyz(
// Distribute shapes from -X_EXTENT to +X_EXTENT.
-X_EXTENT / 2. + i as f32 / (num_shapes - 1) as f32 * X_EXTENT,
0.0,
0.0,
),
..default()
});
}
}

0 comments on commit 1d5388e

Please sign in to comment.