 9bad607df9
			
		
	
	
		9bad607df9
		
			
		
	
	
	
	
		
			
			# 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:    The `2d_shapes` example now includes the capsule, so we also get one more color of the rainbow 🌈 
		
			
				
	
	
		
			73 lines
		
	
	
		
			2.2 KiB
		
	
	
	
		
			Rust
		
	
	
	
	
	
			
		
		
	
	
			73 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()
 | |
|     });
 | |
| }
 |