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

Add Meshable trait and implement meshing for 2D primitives #11431

Merged
merged 25 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
758f250
Derive `Default` for some 2D primitives
Jondolf Jan 20, 2024
7018094
Add `Meshable`, `Facing`, and mesh builders for 2D primitives
Jondolf Jan 20, 2024
9c55a7c
Merge branch 'main' into 2d-primitive-meshing
Jondolf Jan 20, 2024
6f53e16
Use primitives in `2d_shapes` example and tweak colors
Jondolf Jan 20, 2024
b4865ec
Fix doc comments
Jondolf Jan 20, 2024
df0e2e9
Update examples README.md
Jondolf Jan 20, 2024
c315937
Add facing in `bevy_render::prelude`
Jondolf Jan 20, 2024
38e25a7
Fix doc links
Jondolf Jan 20, 2024
41f3009
Add `Meshable` to `bevy_render::prelude`
Jondolf Jan 20, 2024
ad690e5
Improve module-level docs for primitive meshing
Jondolf Jan 20, 2024
3817408
Document default values for 2D primitives
Jondolf Jan 20, 2024
51ca697
Improve `2d_shapes` example description
Jondolf Jan 20, 2024
2532647
Merge branch 'main' into 2d-primitive-meshing
Jondolf Jan 20, 2024
f6705a1
Fix merge conflicts
Jondolf Jan 20, 2024
edf42c3
Merge branch 'main' into 2d-primitive-meshing
Jondolf Jan 20, 2024
681dca6
Remove `primitives::` prefix
Jondolf Jan 20, 2024
a4b9b5c
Remove mesh facing logic
Jondolf Jan 22, 2024
7610835
Move 2D meshes to single file and remove unnecessary builders
Jondolf Jan 22, 2024
942e892
Update docs
Jondolf Jan 22, 2024
2d5e1e5
Merge branch 'main' into 2d-primitive-meshing
Jondolf Jan 22, 2024
b2c9a98
Fix doc example
Jondolf Jan 22, 2024
faeaa60
Fix intradoc links
Jondolf Jan 22, 2024
8969134
Use `Ellipse::new`
Jondolf Jan 22, 2024
6d0078c
Merge branch 'main' into 2d-primitive-meshing
Jondolf Jan 28, 2024
beedd16
Merge branch 'main' into 2d-primitive-meshing
alice-i-cecile Jan 29, 2024
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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ doc-scrape-examples = true

[package.metadata.example.2d_shapes]
name = "2D Shapes"
description = "Renders a rectangle, circle, and hexagon"
description = "Renders simple 2D primitive shapes like circles and polygons"
category = "2D Rendering"
wasm = true

Expand Down
69 changes: 61 additions & 8 deletions crates/bevy_math/src/primitives/dim2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,13 @@ pub struct Circle {
}
impl Primitive2d for Circle {}

impl Default for Circle {
/// Returns the default [`Circle`] with a radius of `0.5`.
fn default() -> Self {
Jondolf marked this conversation as resolved.
Show resolved Hide resolved
Self { radius: 0.5 }
}
}

impl Circle {
/// Finds the point on the circle that is closest to the given `point`.
///
Expand Down Expand Up @@ -118,6 +125,15 @@ pub struct Ellipse {
}
impl Primitive2d for Ellipse {}

impl Default for Ellipse {
/// Returns the default [`Ellipse`] with a half-width of `1.0` and a half-height of `0.5`.
fn default() -> Self {
Self {
half_size: Vec2::new(1.0, 0.5),
}
}
}

impl Ellipse {
/// Create a new `Ellipse` from half of its width and height.
///
Expand Down Expand Up @@ -161,6 +177,15 @@ pub struct Plane2d {
}
impl Primitive2d for Plane2d {}

impl Default for Plane2d {
/// Returns the default [`Plane2d`] with a normal pointing in the `+Y` direction.
fn default() -> Self {
Self {
normal: Direction2d::Y,
}
}
}

impl Plane2d {
/// Create a new `Plane2d` from a normal
///
Expand Down Expand Up @@ -188,7 +213,7 @@ impl Primitive2d for Line2d {}

/// A segment of a line along a direction in 2D space.
#[doc(alias = "LineSegment2d")]
#[derive(Clone, Debug)]
#[derive(Clone, Copy, Debug)]
pub struct Segment2d {
/// The direction of the line segment
pub direction: Direction2d,
Expand Down Expand Up @@ -288,16 +313,25 @@ 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 {
/// Returns the default [`Triangle2d`] with the vertices `[0.0, 0.5]`, `[-0.5, -0.5]`, and `[0.5, -0.5]`.
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 @@ -367,6 +401,15 @@ pub struct Rectangle {
pub half_size: Vec2,
}

impl Default for Rectangle {
/// Returns the default [`Rectangle`] with a half-width and half-height of `0.5`.
fn default() -> Self {
Self {
half_size: Vec2::splat(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 @@ -456,18 +499,28 @@ pub struct RegularPolygon {
}
impl Primitive2d for RegularPolygon {}

impl Default for RegularPolygon {
/// Returns the default [`RegularPolygon`] with six sides (a hexagon) and a circumradius of `0.5`.
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
2 changes: 1 addition & 1 deletion crates/bevy_render/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub mod prelude {
Projection,
},
color::Color,
mesh::{morph::MorphWeights, shape, Mesh},
mesh::{morph::MorphWeights, primitives::Meshable, shape, Mesh},
render_resource::Shader,
spatial_bundle::SpatialBundle,
texture::{Image, ImagePlugin},
Expand Down
2 changes: 2 additions & 0 deletions crates/bevy_render/src/mesh/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#[allow(clippy::module_inception)]
mod mesh;
pub mod morph;
pub mod primitives;
/// Generation for some primitive shape meshes.
pub mod shape;

pub use mesh::*;
pub use primitives::*;

use crate::{prelude::Image, render_asset::RenderAssetPlugin};
use bevy_app::{App, Plugin};
Expand Down
Loading