bevy/examples/2d/2d_shapes.rs
Joona Aalto 9bad607df9
Implement meshing for Capsule2d (#11639)
# Objective

The `Capsule2d` primitive was added in #11585. It should support meshing
like the other 2D primitives.

## Solution

Implement meshing for `Capsule2d`.

It doesn't currently support "rings" like Bevy's `Capsule` shape (not
`Capsule3d`), but it does support resolution to control the number of
vertices used for one hemicircle. The total vertex count is two times
the resolution; if we allowed setting the full vertex count, odd numbers
would lead to uneven vertex counts for the top and bottom hemicircles
and produce potentially unwanted results.

The capsule looks like this (with UV visualization and wireframe) using
resolutions of 16, 8, and 3:

![Resolution
16](https://github.com/bevyengine/bevy/assets/57632562/feae22de-bdc5-438a-861f-848284b67a52)
![Resolution
8](https://github.com/bevyengine/bevy/assets/57632562/e95aab8e-793f-45ac-8a74-8be39f7626dd)
![Resolution of
3](https://github.com/bevyengine/bevy/assets/57632562/bcf01d23-1d8b-4cdb-966a-c9022a07c287)

The `2d_shapes` example now includes the capsule, so we also get one
more color of the rainbow 🌈

![New 2D shapes
example](https://github.com/bevyengine/bevy/assets/57632562/1c45b5f5-d26a-4e8c-8e8a-e106ab14d46e)
2024-02-03 18:03:43 +00:00

72 lines
2.2 KiB
Rust

//! Shows how to render simple primitive shapes with a single color.
use bevy::{prelude::*, sprite::MaterialMesh2dBundle};
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_systems(Startup, setup)
.run();
}
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()
});
}