From 9353ad0eb95314d6d95f88b1ccfb893f5c3f88db Mon Sep 17 00:00:00 2001 From: "Alexis \"spectria\" Horizon" Date: Sat, 9 Mar 2024 03:42:14 -0500 Subject: [PATCH] Review changes --- Cargo.toml | 8 +++---- .../src/bounding/bounded2d/primitive_impls.rs | 24 +++++++++---------- .../bevy_render/src/mesh/primitives/dim2.rs | 9 +++++++ .../2d/{mesh2d_circular.rs => mesh2d_arcs.rs} | 11 ++++----- 4 files changed, 28 insertions(+), 24 deletions(-) rename examples/2d/{mesh2d_circular.rs => mesh2d_arcs.rs} (95%) diff --git a/Cargo.toml b/Cargo.toml index 33c7a8dc3c7a0..d7ff6b4253ef3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -404,13 +404,13 @@ category = "2D Rendering" wasm = true [[example]] -name = "mesh2d_circular" -path = "examples/2d/mesh2d_circular.rs" +name = "mesh2d_arcs" +path = "examples/2d/mesh2d_arcs.rs" doc-scrape-examples = true [package.metadata.example.mesh2d_circular] -name = "Circular 2D Meshes" -description = "Demonstrates UV-mapping of circular primitives" +name = "Arc 2D Meshes" +description = "Demonstrates UV-mapping of the circular segment and sector primitives" category = "2D Rendering" wasm = true diff --git a/crates/bevy_math/src/bounding/bounded2d/primitive_impls.rs b/crates/bevy_math/src/bounding/bounded2d/primitive_impls.rs index fca5d55a39d79..926c3db48e24b 100644 --- a/crates/bevy_math/src/bounding/bounded2d/primitive_impls.rs +++ b/crates/bevy_math/src/bounding/bounded2d/primitive_impls.rs @@ -1,15 +1,14 @@ //! Contains [`Bounded2d`] implementations for [geometric primitives](crate::primitives). -use std::f32::consts::PI; +use std::f32::consts::{FRAC_PI_2, PI, TAU}; -use glam::{Mat2, Vec2}; use smallvec::SmallVec; use crate::{ primitives::{ - BoxedPolygon, BoxedPolyline2d, Capsule2d, Circle, CircularSector, CircularSegment, - Ellipse, Line2d, Plane2d, Polygon, - Polyline2d, Rectangle, RegularPolygon, Segment2d, Triangle2d, + Arc2d, BoxedPolygon, BoxedPolyline2d, Capsule2d, Circle, CircularSector, CircularSegment, + Ellipse, Line2d, Plane2d, Polygon, Polyline2d, Rectangle, RegularPolygon, Segment2d, + Triangle2d, }, Dir2, Mat2, Vec2, }; @@ -40,11 +39,11 @@ fn arc_bounding_points(arc: Arc2d, rotation: f32) -> SmallVec<[Vec2; 7]> { // The half-angles are measured from a starting point of π/2, being the angle of Vec2::Y. // Compute the normalized angles of the endpoints with the rotation taken into account, and then // check if we are looking for an angle that is between or outside them. - let left_angle = (PI / 2.0 + arc.half_angle + rotation).rem_euclid(2.0 * PI); - let right_angle = (PI / 2.0 - arc.half_angle + rotation).rem_euclid(2.0 * PI); + let left_angle = (FRAC_PI_2 + arc.half_angle + rotation).rem_euclid(TAU); + let right_angle = (FRAC_PI_2 - arc.half_angle + rotation).rem_euclid(TAU); let inverted = left_angle < right_angle; for extremum in [Vec2::X, Vec2::Y, Vec2::NEG_X, Vec2::NEG_Y] { - let angle = extremum.to_angle().rem_euclid(2.0 * PI); + let angle = extremum.to_angle().rem_euclid(TAU); // If inverted = true, then right_angle > left_angle, so we are looking for an angle that is not between them. // There's a chance that this condition fails due to rounding error, if the endpoint angle is juuuust shy of the axis. // But in that case, the endpoint itself is within rounding error of the axis and will define the bounds just fine. @@ -366,7 +365,7 @@ impl Bounded2d for Capsule2d { #[cfg(test)] mod tests { - use std::f32::consts::PI; + use std::f32::consts::{PI, TAU}; use approx::assert_abs_diff_eq; use glam::Vec2; @@ -374,8 +373,8 @@ mod tests { use crate::{ bounding::Bounded2d, primitives::{ - Arc2d, Capsule2d, Circle, CircularSector, CircularSegment, Direction2d, Ellipse, - Line2d, Plane2d, Polygon, Polyline2d, Rectangle, RegularPolygon, Segment2d, Triangle2d, + Arc2d, Capsule2d, Circle, CircularSector, CircularSegment, Ellipse, Line2d, Plane2d, + Polygon, Polyline2d, Rectangle, RegularPolygon, Segment2d, Triangle2d, }, Dir2, }; @@ -561,7 +560,7 @@ mod tests { // Test case: An sector whose arc is minor, but whose bounding circle is not the circumcircle of the endpoints and center TestCase { name: "1/3rd circle", - arc: Arc2d::from_radians(1.0, 2.0 * PI / 3.0), + arc: Arc2d::from_radians(1.0, TAU / 3.0), translation: Vec2::ZERO, rotation: 0.0, aabb_min: Vec2::new(-apothem, 0.0), @@ -713,7 +712,6 @@ mod tests { assert_eq!(aabb3.max, Vec2::new(f32::MAX / 2.0, f32::MAX / 2.0)); let bounding_circle = Plane2d::new(Vec2::Y).bounding_circle(translation, 0.0); - dbg!(bounding_circle); assert_eq!(bounding_circle.center, translation); assert_eq!(bounding_circle.radius(), f32::MAX / 2.0); } diff --git a/crates/bevy_render/src/mesh/primitives/dim2.rs b/crates/bevy_render/src/mesh/primitives/dim2.rs index 974b9ea37f988..e6e284e3790ec 100644 --- a/crates/bevy_render/src/mesh/primitives/dim2.rs +++ b/crates/bevy_render/src/mesh/primitives/dim2.rs @@ -83,7 +83,16 @@ impl From for Mesh { } /// Specifies how to generate UV-mappings for the [`CircularSector`] and [`CircularSegment`] shapes. +/// +/// Currently the only variant is `Mask`, which is good for showing a portion of a texture that includes +/// the entire circle, particularly the same texture will be displayed with different fractions of a +/// complete circle. +/// +/// It's expected that more will be added in the future, such as a variant that causes the texture to be +/// scaled to fit the bounding box of the shape, which would be good for packed textures only including the +/// portion of the circle that is needed to display. #[derive(Copy, Clone, Debug, PartialEq)] +#[non_exhaustive] pub enum CircularMeshUvMode { /// Treats the shape as a mask over a circle of equal size and radius, /// with the center of the circle at the center of the texture. diff --git a/examples/2d/mesh2d_circular.rs b/examples/2d/mesh2d_arcs.rs similarity index 95% rename from examples/2d/mesh2d_circular.rs rename to examples/2d/mesh2d_arcs.rs index af203b1e1560f..812fd51be56af 100644 --- a/examples/2d/mesh2d_circular.rs +++ b/examples/2d/mesh2d_arcs.rs @@ -4,6 +4,7 @@ use std::f32::consts::PI; use bevy::{ + color::palettes::css::{BLUE, DARK_SLATE_GREY, RED}, math::bounding::{Bounded2d, BoundingVolume}, prelude::*, render::mesh::{CircularMeshUvMode, CircularSectorMeshBuilder, CircularSegmentMeshBuilder}, @@ -37,7 +38,7 @@ fn setup( commands.spawn(Camera2dBundle { camera: Camera { - clear_color: ClearColorConfig::Custom(Color::DARK_GRAY), + clear_color: ClearColorConfig::Custom(DARK_SLATE_GREY.into()), ..default() }, ..default() @@ -115,13 +116,9 @@ fn draw_bounds( let rotation = rotation.to_euler(EulerRot::XYZ).2; let aabb = shape.0.aabb_2d(translation, rotation); - gizmos.rect_2d(aabb.center(), 0.0, aabb.half_size() * 2.0, Color::RED); + gizmos.rect_2d(aabb.center(), 0.0, aabb.half_size() * 2.0, RED); let bounding_circle = shape.0.bounding_circle(translation, rotation); - gizmos.circle_2d( - bounding_circle.center, - bounding_circle.radius(), - Color::BLUE, - ); + gizmos.circle_2d(bounding_circle.center, bounding_circle.radius(), BLUE); } }