Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - Add RegularPolygon and Circle meshes #3730

Closed
wants to merge 25 commits into from
Closed
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
1e2f715
Add RegularPolygon and Circle
rparrett Jan 20, 2022
fb6f09f
Add example to readme
rparrett Jan 20, 2022
2c137d7
Use usize for sides for consistency
rparrett Jan 20, 2022
03c7cc7
Preallocate vecs
rparrett Jan 20, 2022
08e366e
Minor cleanup
rparrett Jan 20, 2022
63fa48c
Generate mesh with fewer triangles
rparrett Jan 20, 2022
c6e7923
Add missing doc string
rparrett Feb 4, 2022
10b3f0f
Consolidate examples
rparrett Feb 4, 2022
630d9c5
Use new mesh attribute api
rparrett Feb 24, 2022
8dc3c72
Use new default shorthand
rparrett Mar 1, 2022
216b769
Update crates/bevy_render/src/mesh/shape/regular_polygon.rs
rparrett Mar 2, 2022
9cc7852
Update crates/bevy_render/src/mesh/shape/regular_polygon.rs
rparrett Mar 2, 2022
ea920b8
Update crates/bevy_render/src/mesh/shape/regular_polygon.rs
rparrett Mar 2, 2022
62ed3d4
Update crates/bevy_render/src/mesh/shape/regular_polygon.rs
rparrett Mar 2, 2022
f9d62b7
Optimize by calculating sin and cos simultaneously
rparrett Mar 2, 2022
4b68753
Add constructors for RegularPolygon and Circle
rparrett Mar 2, 2022
689c074
Fix unnecessary return
rparrett Mar 2, 2022
994c252
Remove accidental newline
rparrett Mar 2, 2022
880d4a2
Fix doc comment
rparrett Mar 3, 2022
d774e70
Vertices terminology seems more correct than subdivisions
rparrett Mar 4, 2022
40b925e
Fix extra garbage triangle
rparrett Mar 4, 2022
b02089e
Minor optimization
rparrett Mar 7, 2022
1e1ba1f
Friendlier panic when sides <= 2
rparrett Mar 7, 2022
0a76d2a
Slightly less ambiguous variable names
rparrett Mar 7, 2022
e0b04a6
Fix larger allocation for indices than necessary
rparrett Mar 7, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,8 @@ name = "mesh2d_manual"
path = "examples/2d/mesh2d_manual.rs"

[[example]]
name = "rect"
path = "examples/2d/rect.rs"
name = "shapes"
path = "examples/2d/shapes.rs"

[[example]]
name = "sprite"
Expand Down
2 changes: 2 additions & 0 deletions crates/bevy_render/src/mesh/shape/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,11 +224,13 @@ impl From<Plane> for Mesh {

mod capsule;
mod icosphere;
mod regular_polygon;
mod torus;
mod uvsphere;

pub use capsule::{Capsule, CapsuleUvProfile};
pub use icosphere::Icosphere;
pub use regular_polygon::{Circle, RegularPolygon};
pub use torus::Torus;
pub use uvsphere::UVSphere;
use wgpu::PrimitiveTopology;
81 changes: 81 additions & 0 deletions crates/bevy_render/src/mesh/shape/regular_polygon.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
use crate::mesh::{Indices, Mesh};
use wgpu::PrimitiveTopology;

/// A regular polygon on the xy plane
rparrett marked this conversation as resolved.
Show resolved Hide resolved
#[derive(Debug, Copy, Clone)]
pub struct RegularPolygon {
/// Inscribed radius on the xy plane.
rparrett marked this conversation as resolved.
Show resolved Hide resolved
pub radius: f32,
/// Number of sides.
pub sides: usize,
}
impl Default for RegularPolygon {
fn default() -> Self {
Self {
radius: 0.5,
sides: 6,
}
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is a hexagon the default? Does it make sense to provide a default?


impl From<RegularPolygon> for Mesh {
fn from(polygon: RegularPolygon) -> Self {
let RegularPolygon { radius, sides } = polygon;

let mut positions = Vec::with_capacity(sides);
let mut normals = Vec::with_capacity(sides);
let mut uvs = Vec::with_capacity(sides);

for i in 0..sides {
let a = std::f32::consts::FRAC_PI_2 - i as f32 * std::f32::consts::TAU / (sides as f32);

positions.push([a.cos() * radius, a.sin() * radius, 0.0]);
normals.push([0.0, 0.0, 1.0]);
uvs.push([(a.cos() + 1.0) / 2.0, 1.0 - (a.sin() + 1.0) / 2.0]);
rparrett marked this conversation as resolved.
Show resolved Hide resolved
}

let mut indices = Vec::with_capacity((sides - 1) * 3);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this will crash for 0 sides.

Copy link
Contributor Author

@rparrett rparrett Mar 4, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think is the most appropriate way to handle that situation?

if sides < 3 {
    panic!(/* ... */)
}

Or clamping the value?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is now protected by a debug_assert. Still a panic, but it doesn't really make any sense to try to build a polygon with fewer than 3 sides, and now there's a friendly error message.

for i in 1..sides as u32 {
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 on the xy plane
rparrett marked this conversation as resolved.
Show resolved Hide resolved
pub struct Circle {
rparrett marked this conversation as resolved.
Show resolved Hide resolved
/// Inscribed radius on the xy plane.
rparrett marked this conversation as resolved.
Show resolved Hide resolved
pub radius: f32,
/// The number of subdivisions applied.
pub subdivisions: usize,
}

impl Default for Circle {
fn default() -> Self {
Self {
radius: 0.5,
subdivisions: 64,
rparrett marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

impl From<Circle> for RegularPolygon {
fn from(circle: Circle) -> Self {
Self {
radius: circle.radius,
sides: circle.subdivisions,
}
}
}

impl From<Circle> for Mesh {
fn from(circle: Circle) -> Self {
Mesh::from(RegularPolygon::from(circle))
}
}
20 changes: 0 additions & 20 deletions examples/2d/rect.rs

This file was deleted.

52 changes: 52 additions & 0 deletions examples/2d/shapes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
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::default().into()).into(),
material: materials.add(ColorMaterial::from(Color::PURPLE)),
transform: Transform::from_translation(Vec3::new(-100., 0., 0.))
.with_scale(Vec2::splat(100.0).extend(1.)),
..default()
});

// Hexagon
commands.spawn_bundle(MaterialMesh2dBundle {
mesh: meshes
.add(
shape::RegularPolygon {
sides: 6,
..default()
}
.into(),
)
.into(),
material: materials.add(ColorMaterial::from(Color::TURQUOISE)),
transform: Transform::from_translation(Vec3::new(100., 0., 0.))
.with_scale(Vec2::splat(100.0).extend(1.)),
..default()
});
}
2 changes: 1 addition & 1 deletion examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ Example | File | Description
`move_sprite` | [`2d/move_sprite.rs`](./2d/move_sprite.rs) | Changes the transform of a sprite.
`mesh2d` | [`2d/mesh2d.rs`](./2d/mesh2d.rs) | Renders a 2d mesh
`mesh2d_manual` | [`2d/mesh2d_manual.rs`](./2d/mesh2d_manual.rs) | Renders a custom mesh "manually" with "mid-level" renderer apis.
`rect` | [`2d/rect.rs`](./2d/rect.rs) | Renders a rectangle
`shapes` | [`2d/shapes.rs`](./2d/shapes.rs) | Renders a rectangle, circle, and hexagon
`sprite` | [`2d/sprite.rs`](./2d/sprite.rs) | Renders a sprite
`sprite_sheet` | [`2d/sprite_sheet.rs`](./2d/sprite_sheet.rs) | Renders an animated sprite
`text2d` | [`2d/text2d.rs`](./2d/text2d.rs) | Generates text in 2d
Expand Down