forked from bevyengine/bevy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add RegularPolygon and Circle meshes (bevyengine#3730)
# Objective Bevy users often want to create circles and other simple shapes. All the machinery is in place to accomplish this, and there are external crates that help. But when writing code for e.g. a new bevy example, it's not really possible to draw a circle without bringing in a new asset, writing a bunch of scary looking mesh code, or adding a dependency. In particular, this PR was inspired by this interaction in another PR: bevyengine#3721 (comment) ## Solution This PR adds `shape::RegularPolygon` and `shape::Circle` (which is just a `RegularPolygon` that defaults to a large number of sides) ## Discussion There's a lot of ongoing discussion about shapes in <bevyengine/rfcs#12> and at least one other lingering shape PR (although it seems incomplete). That RFC currently includes `RegularPolygon` and `Circle` shapes, so I don't think that having working mesh generation code in the engine for those shapes would add much burden to an author of an implementation. But if we'd prefer not to add additional shapes until after that's sorted out, I'm happy to close this for now. ## Alternatives for users For any users stumbling on this issue, here are some plugins that will help if you need more shapes. https://github.com/Nilirad/bevy_prototype_lyon https://github.com/johanhelsing/bevy_smud https://github.com/Weasy666/bevy_svg https://github.com/redpandamonium/bevy_more_shapes https://github.com/ForesightMiningSoftwareCorporation/bevy_polyline
- Loading branch information
Showing
6 changed files
with
150 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
use crate::mesh::{Indices, Mesh}; | ||
use wgpu::PrimitiveTopology; | ||
|
||
/// A regular polygon in the xy plane | ||
#[derive(Debug, Copy, Clone)] | ||
pub struct RegularPolygon { | ||
/// Inscribed radius in the xy plane. | ||
pub radius: f32, | ||
/// Number of sides. | ||
pub sides: usize, | ||
} | ||
|
||
impl Default for RegularPolygon { | ||
fn default() -> Self { | ||
Self { | ||
radius: 0.5, | ||
sides: 6, | ||
} | ||
} | ||
} | ||
|
||
impl RegularPolygon { | ||
/// Creates a regular polygon in the xy plane | ||
pub fn new(radius: f32, sides: usize) -> Self { | ||
Self { radius, sides } | ||
} | ||
} | ||
|
||
impl From<RegularPolygon> for Mesh { | ||
fn from(polygon: RegularPolygon) -> Self { | ||
let RegularPolygon { radius, sides } = polygon; | ||
|
||
debug_assert!(sides > 2, "RegularPolygon requires at least 3 sides."); | ||
|
||
let mut positions = Vec::with_capacity(sides); | ||
let mut normals = Vec::with_capacity(sides); | ||
let mut uvs = Vec::with_capacity(sides); | ||
|
||
let step = std::f32::consts::TAU / sides as f32; | ||
for i in 0..sides { | ||
let theta = std::f32::consts::FRAC_PI_2 - i as f32 * step; | ||
let (sin, cos) = theta.sin_cos(); | ||
|
||
positions.push([cos * radius, sin * radius, 0.0]); | ||
normals.push([0.0, 0.0, 1.0]); | ||
uvs.push([0.5 * (cos + 1.0), 1.0 - 0.5 * (sin + 1.0)]); | ||
} | ||
|
||
let mut indices = Vec::with_capacity((sides - 2) * 3); | ||
for i in 1..(sides as u32 - 1) { | ||
indices.extend_from_slice(&[0, i + 1, i]); | ||
} | ||
|
||
let mut mesh = Mesh::new(PrimitiveTopology::TriangleList); | ||
mesh.insert_attribute(Mesh::ATTRIBUTE_POSITION, positions); | ||
mesh.insert_attribute(Mesh::ATTRIBUTE_NORMAL, normals); | ||
mesh.insert_attribute(Mesh::ATTRIBUTE_UV_0, uvs); | ||
mesh.set_indices(Some(Indices::U32(indices))); | ||
mesh | ||
} | ||
} | ||
|
||
/// A circle in the xy plane | ||
pub struct Circle { | ||
/// Inscribed radius in the xy plane. | ||
pub radius: f32, | ||
/// The number of vertices used. | ||
pub vertices: usize, | ||
} | ||
|
||
impl Default for Circle { | ||
fn default() -> Self { | ||
Self { | ||
radius: 0.5, | ||
vertices: 64, | ||
} | ||
} | ||
} | ||
|
||
impl Circle { | ||
/// Creates a circle in the xy plane | ||
pub fn new(radius: f32) -> Self { | ||
Self { | ||
radius, | ||
..Default::default() | ||
} | ||
} | ||
} | ||
|
||
impl From<Circle> for RegularPolygon { | ||
fn from(circle: Circle) -> Self { | ||
Self { | ||
radius: circle.radius, | ||
sides: circle.vertices, | ||
} | ||
} | ||
} | ||
|
||
impl From<Circle> for Mesh { | ||
fn from(circle: Circle) -> Self { | ||
Mesh::from(RegularPolygon::from(circle)) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
use bevy::{prelude::*, sprite::MaterialMesh2dBundle}; | ||
|
||
fn main() { | ||
App::new() | ||
.add_plugins(DefaultPlugins) | ||
.add_startup_system(setup) | ||
.run(); | ||
} | ||
|
||
fn setup( | ||
mut commands: Commands, | ||
mut meshes: ResMut<Assets<Mesh>>, | ||
mut materials: ResMut<Assets<ColorMaterial>>, | ||
) { | ||
commands.spawn_bundle(OrthographicCameraBundle::new_2d()); | ||
|
||
// Rectangle | ||
commands.spawn_bundle(SpriteBundle { | ||
sprite: Sprite { | ||
color: Color::rgb(0.25, 0.25, 0.75), | ||
custom_size: Some(Vec2::new(50.0, 100.0)), | ||
..default() | ||
}, | ||
..default() | ||
}); | ||
|
||
// Circle | ||
commands.spawn_bundle(MaterialMesh2dBundle { | ||
mesh: meshes.add(shape::Circle::new(50.).into()).into(), | ||
material: materials.add(ColorMaterial::from(Color::PURPLE)), | ||
transform: Transform::from_translation(Vec3::new(-100., 0., 0.)), | ||
..default() | ||
}); | ||
|
||
// Hexagon | ||
commands.spawn_bundle(MaterialMesh2dBundle { | ||
mesh: meshes.add(shape::RegularPolygon::new(50., 6).into()).into(), | ||
material: materials.add(ColorMaterial::from(Color::TURQUOISE)), | ||
transform: Transform::from_translation(Vec3::new(100., 0., 0.)), | ||
..default() | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters