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

Mesh creation from primitive shapes #11007

Closed
wants to merge 38 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
fb73e5d
`From<Mesh>` for `Cuboid`, `Rectangle`, `RegularPolygon` and `Circle`
Jondolf Dec 2, 2023
9eb2642
Implement `From<Triangle2d>` for `Mesh`
Jondolf Dec 3, 2023
5fc1dd9
Split meshes into many modules, use builder pattern and add spheres
Jondolf Dec 5, 2023
f47cea1
Add torus mesh
Jondolf Dec 6, 2023
d51d769
Add WIP cone mesh
Jondolf Dec 6, 2023
7e39698
Impl `Default` for some primitives
Jondolf Dec 6, 2023
c9ed807
Use primitives in `3d_shapes` example
Jondolf Dec 6, 2023
b4ab575
Fix cone UVs
Jondolf Dec 6, 2023
68591a0
Normalize vertex normals in fragment shader instead of vertex shader
Jondolf Dec 6, 2023
5a1da1d
Add cylinder mesh
Jondolf Dec 6, 2023
b979555
Add capsule mesh
Jondolf Dec 6, 2023
361c95f
Add conical frustum mesh
Jondolf Dec 7, 2023
fd66b2f
Clean up cone code
Jondolf Dec 7, 2023
c321329
Change `Builder` names to `Mesh`
Jondolf Dec 7, 2023
4e3d60b
Support regular polygons and circles facing different directions
Jondolf Dec 7, 2023
46b5f50
Fix camera position in 3d_shapes example
Jondolf Dec 7, 2023
e1d9d06
Refactor cone, conical frustum and cylinder to use circle for base
Jondolf Dec 7, 2023
4a1d4fc
Refactor and support 2D shape orientation and fix UVs
Jondolf Dec 8, 2023
0d6f225
Make cone and torus default sizes more consistent
Jondolf Dec 8, 2023
cd45c31
Merge branch 'main' into mesh-from-primitives
Jondolf Dec 8, 2023
37f0f24
Add `CuboidMesh`
Jondolf Dec 8, 2023
86c15e5
Add more derives and add `MeshFacingExtension`
Jondolf Dec 8, 2023
2f6708f
Add `EllipseMesh`
Jondolf Dec 8, 2023
041e381
Reuse ellipse mesh code for regular polygons
Jondolf Dec 9, 2023
c54bf5d
Add plane mesh
Jondolf Dec 16, 2023
60b00c2
Clean up cone mesh code
Jondolf Dec 16, 2023
7a1a81f
Fix capsule rings
Jondolf Dec 16, 2023
9e4dca7
Warn missing docs
Jondolf Dec 16, 2023
842b546
Document and clean up mesh code
Jondolf Dec 17, 2023
45d204c
Inline methods
Jondolf Dec 17, 2023
92dc3e9
Add re-exports
Jondolf Dec 17, 2023
ceef5d5
Merge branch 'main' into mesh-from-primitives
Jondolf Dec 17, 2023
9bf725e
Revert shader change
Jondolf Dec 17, 2023
4a9371b
Fix comments
Jondolf Dec 17, 2023
cd85d9b
Remove old shapes and update examples
Jondolf Dec 17, 2023
6ebcc9b
Fix import in doc example
Jondolf Dec 17, 2023
ff949a9
Re-export more
Jondolf Dec 17, 2023
a0c42ce
Add missing docs
Jondolf Dec 17, 2023
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
63 changes: 56 additions & 7 deletions crates/bevy_math/src/primitives/dim2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ pub struct Circle {
}
impl Primitive2d for Circle {}

impl Default for Circle {
fn default() -> Self {
Self { radius: 0.5 }
}
}

/// An ellipse primitive
#[derive(Clone, Copy, Debug)]
pub struct Ellipse {
Expand All @@ -76,6 +82,15 @@ pub struct Ellipse {
}
impl Primitive2d for Ellipse {}

impl Default for Ellipse {
fn default() -> Self {
Self {
half_width: 1.0,
half_height: 0.5,
}
}
}

impl Ellipse {
/// Create a new `Ellipse` from a "width" and a "height"
pub fn new(width: f32, height: f32) -> Self {
Expand All @@ -95,6 +110,14 @@ pub struct Plane2d {
}
impl Primitive2d for Plane2d {}

impl Default for Plane2d {
fn default() -> Self {
Self {
normal: Direction2d::from_normalized(Vec2::Y),
}
}
}

impl Plane2d {
/// Create a new `Plane2d` from a normal
///
Expand Down Expand Up @@ -220,16 +243,24 @@ impl BoxedPolyline2d {
}

/// A triangle in 2D space
#[derive(Clone, Debug, PartialEq)]
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Triangle2d {
/// The vertices of the triangle
pub vertices: [Vec2; 3],
}
impl Primitive2d for Triangle2d {}

impl Default for Triangle2d {
fn default() -> Self {
Self {
vertices: [Vec2::Y * 0.5, Vec2::new(-0.5, -0.5), Vec2::new(0.5, -0.5)],
}
}
}

impl Triangle2d {
/// Create a new `Triangle2d` from points `a`, `b`, and `c`
pub fn new(a: Vec2, b: Vec2, c: Vec2) -> Self {
pub const fn new(a: Vec2, b: Vec2, c: Vec2) -> Self {
Self {
vertices: [a, b, c],
}
Expand Down Expand Up @@ -267,6 +298,15 @@ pub struct Rectangle {
}
impl Primitive2d for Rectangle {}

impl Default for Rectangle {
fn default() -> Self {
Self {
half_width: 0.5,
half_height: 0.5,
}
}
}

impl Rectangle {
/// Create a rectangle from a full width and height
pub fn new(width: f32, height: f32) -> Self {
Expand Down Expand Up @@ -347,18 +387,27 @@ pub struct RegularPolygon {
}
impl Primitive2d for RegularPolygon {}

impl Default for RegularPolygon {
fn default() -> Self {
Self {
circumcircle: Circle { radius: 0.5 },
sides: 6,
}
}
}

impl RegularPolygon {
/// Create a new `RegularPolygon`
/// from the radius of the circumcircle and number of sides
/// from the radius of the circumcircle and a number of sides
///
/// # Panics
///
/// Panics if `circumcircle_radius` is non-positive
pub fn new(circumcircle_radius: f32, sides: usize) -> Self {
assert!(circumcircle_radius > 0.0);
/// Panics if `circumradius` is non-positive
pub fn new(circumradius: f32, sides: usize) -> Self {
assert!(circumradius > 0.0);
Self {
circumcircle: Circle {
radius: circumcircle_radius,
radius: circumradius,
},
sides,
}
Expand Down
70 changes: 70 additions & 0 deletions crates/bevy_math/src/primitives/dim3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ pub struct Sphere {
}
impl Primitive3d for Sphere {}

impl Default for Sphere {
fn default() -> Self {
Self { radius: 0.5 }
}
}

/// An unbounded plane in 3D space. It forms a separating surface through the origin,
/// stretching infinitely far
#[derive(Clone, Copy, Debug)]
Expand All @@ -75,6 +81,14 @@ pub struct Plane3d {
}
impl Primitive3d for Plane3d {}

impl Default for Plane3d {
fn default() -> Self {
Self {
normal: Direction3d::from_normalized(Vec3::Y),
}
}
}

impl Plane3d {
/// Create a new `Plane3d` from a normal
///
Expand Down Expand Up @@ -206,6 +220,14 @@ pub struct Cuboid {
}
impl Primitive3d for Cuboid {}

impl Default for Cuboid {
fn default() -> Self {
Self {
half_extents: Vec3::splat(0.5),
}
}
}

impl Cuboid {
/// Create a cuboid from a full x, y, and z length
pub fn new(x_length: f32, y_length: f32, z_length: f32) -> Self {
Expand All @@ -230,6 +252,15 @@ pub struct Cylinder {
}
impl Primitive3d for Cylinder {}

impl Default for Cylinder {
fn default() -> Self {
Self {
radius: 0.5,
half_height: 0.5,
}
}
}

impl Cylinder {
/// Create a cylinder from a radius and full height
pub fn new(radius: f32, height: f32) -> Self {
Expand All @@ -252,6 +283,15 @@ pub struct Capsule {
impl super::Primitive2d for Capsule {}
impl Primitive3d for Capsule {}

impl Default for Capsule {
fn default() -> Self {
Self {
radius: 0.5,
half_length: 0.5,
}
}
}

impl Capsule {
/// Create a new `Capsule` from a radius and length
pub fn new(radius: f32, length: f32) -> Self {
Expand All @@ -272,6 +312,15 @@ pub struct Cone {
}
impl Primitive3d for Cone {}

impl Default for Cone {
fn default() -> Self {
Self {
radius: 0.5,
height: 1.0,
}
}
}

/// A conical frustum primitive.
/// A conical frustum can be created
/// by slicing off a section of a cone.
Expand All @@ -286,6 +335,18 @@ pub struct ConicalFrustum {
}
impl Primitive3d for ConicalFrustum {}

impl Default for ConicalFrustum {
fn default() -> Self {
Self {
// This produces the same shape as the default Cone,
// but trancated to half the height.
radius_top: 0.25,
radius_bottom: 0.5,
height: 0.5,
}
}
}

/// The type of torus determined by the minor and major radii
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum TorusKind {
Expand Down Expand Up @@ -320,6 +381,15 @@ pub struct Torus {
}
impl Primitive3d for Torus {}

impl Default for Torus {
fn default() -> Self {
Self {
minor_radius: 0.25,
major_radius: 0.75,
}
}
}

impl Torus {
/// Create a new `Torus` from an inner and outer radius.
///
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_render/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub mod prelude {
pub use crate::{
camera::{Camera, OrthographicProjection, PerspectiveProjection, Projection},
color::Color,
mesh::{morph::MorphWeights, shape, Mesh},
mesh::{morph::MorphWeights, shape, shape::*, Mesh},
render_resource::Shader,
spatial_bundle::SpatialBundle,
texture::{Image, ImagePlugin},
Expand Down
1 change: 0 additions & 1 deletion crates/bevy_render/src/mesh/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#[allow(clippy::module_inception)]
mod mesh;
pub mod morph;
/// Generation for some primitive shape meshes.
pub mod shape;

pub use mesh::*;
Expand Down
Loading