Skip to content

Commit 9353ad0

Browse files
Review changes
1 parent 31b2be3 commit 9353ad0

File tree

4 files changed

+28
-24
lines changed

4 files changed

+28
-24
lines changed

Cargo.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -404,13 +404,13 @@ category = "2D Rendering"
404404
wasm = true
405405

406406
[[example]]
407-
name = "mesh2d_circular"
408-
path = "examples/2d/mesh2d_circular.rs"
407+
name = "mesh2d_arcs"
408+
path = "examples/2d/mesh2d_arcs.rs"
409409
doc-scrape-examples = true
410410

411411
[package.metadata.example.mesh2d_circular]
412-
name = "Circular 2D Meshes"
413-
description = "Demonstrates UV-mapping of circular primitives"
412+
name = "Arc 2D Meshes"
413+
description = "Demonstrates UV-mapping of the circular segment and sector primitives"
414414
category = "2D Rendering"
415415
wasm = true
416416

crates/bevy_math/src/bounding/bounded2d/primitive_impls.rs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
//! Contains [`Bounded2d`] implementations for [geometric primitives](crate::primitives).
22
3-
use std::f32::consts::PI;
3+
use std::f32::consts::{FRAC_PI_2, PI, TAU};
44

5-
use glam::{Mat2, Vec2};
65
use smallvec::SmallVec;
76

87
use crate::{
98
primitives::{
10-
BoxedPolygon, BoxedPolyline2d, Capsule2d, Circle, CircularSector, CircularSegment,
11-
Ellipse, Line2d, Plane2d, Polygon,
12-
Polyline2d, Rectangle, RegularPolygon, Segment2d, Triangle2d,
9+
Arc2d, BoxedPolygon, BoxedPolyline2d, Capsule2d, Circle, CircularSector, CircularSegment,
10+
Ellipse, Line2d, Plane2d, Polygon, Polyline2d, Rectangle, RegularPolygon, Segment2d,
11+
Triangle2d,
1312
},
1413
Dir2, Mat2, Vec2,
1514
};
@@ -40,11 +39,11 @@ fn arc_bounding_points(arc: Arc2d, rotation: f32) -> SmallVec<[Vec2; 7]> {
4039
// The half-angles are measured from a starting point of π/2, being the angle of Vec2::Y.
4140
// Compute the normalized angles of the endpoints with the rotation taken into account, and then
4241
// check if we are looking for an angle that is between or outside them.
43-
let left_angle = (PI / 2.0 + arc.half_angle + rotation).rem_euclid(2.0 * PI);
44-
let right_angle = (PI / 2.0 - arc.half_angle + rotation).rem_euclid(2.0 * PI);
42+
let left_angle = (FRAC_PI_2 + arc.half_angle + rotation).rem_euclid(TAU);
43+
let right_angle = (FRAC_PI_2 - arc.half_angle + rotation).rem_euclid(TAU);
4544
let inverted = left_angle < right_angle;
4645
for extremum in [Vec2::X, Vec2::Y, Vec2::NEG_X, Vec2::NEG_Y] {
47-
let angle = extremum.to_angle().rem_euclid(2.0 * PI);
46+
let angle = extremum.to_angle().rem_euclid(TAU);
4847
// If inverted = true, then right_angle > left_angle, so we are looking for an angle that is not between them.
4948
// There's a chance that this condition fails due to rounding error, if the endpoint angle is juuuust shy of the axis.
5049
// But in that case, the endpoint itself is within rounding error of the axis and will define the bounds just fine.
@@ -366,16 +365,16 @@ impl Bounded2d for Capsule2d {
366365

367366
#[cfg(test)]
368367
mod tests {
369-
use std::f32::consts::PI;
368+
use std::f32::consts::{PI, TAU};
370369

371370
use approx::assert_abs_diff_eq;
372371
use glam::Vec2;
373372

374373
use crate::{
375374
bounding::Bounded2d,
376375
primitives::{
377-
Arc2d, Capsule2d, Circle, CircularSector, CircularSegment, Direction2d, Ellipse,
378-
Line2d, Plane2d, Polygon, Polyline2d, Rectangle, RegularPolygon, Segment2d, Triangle2d,
376+
Arc2d, Capsule2d, Circle, CircularSector, CircularSegment, Ellipse, Line2d, Plane2d,
377+
Polygon, Polyline2d, Rectangle, RegularPolygon, Segment2d, Triangle2d,
379378
},
380379
Dir2,
381380
};
@@ -561,7 +560,7 @@ mod tests {
561560
// Test case: An sector whose arc is minor, but whose bounding circle is not the circumcircle of the endpoints and center
562561
TestCase {
563562
name: "1/3rd circle",
564-
arc: Arc2d::from_radians(1.0, 2.0 * PI / 3.0),
563+
arc: Arc2d::from_radians(1.0, TAU / 3.0),
565564
translation: Vec2::ZERO,
566565
rotation: 0.0,
567566
aabb_min: Vec2::new(-apothem, 0.0),
@@ -713,7 +712,6 @@ mod tests {
713712
assert_eq!(aabb3.max, Vec2::new(f32::MAX / 2.0, f32::MAX / 2.0));
714713

715714
let bounding_circle = Plane2d::new(Vec2::Y).bounding_circle(translation, 0.0);
716-
dbg!(bounding_circle);
717715
assert_eq!(bounding_circle.center, translation);
718716
assert_eq!(bounding_circle.radius(), f32::MAX / 2.0);
719717
}

crates/bevy_render/src/mesh/primitives/dim2.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,16 @@ impl From<CircleMeshBuilder> for Mesh {
8383
}
8484

8585
/// Specifies how to generate UV-mappings for the [`CircularSector`] and [`CircularSegment`] shapes.
86+
///
87+
/// Currently the only variant is `Mask`, which is good for showing a portion of a texture that includes
88+
/// the entire circle, particularly the same texture will be displayed with different fractions of a
89+
/// complete circle.
90+
///
91+
/// It's expected that more will be added in the future, such as a variant that causes the texture to be
92+
/// scaled to fit the bounding box of the shape, which would be good for packed textures only including the
93+
/// portion of the circle that is needed to display.
8694
#[derive(Copy, Clone, Debug, PartialEq)]
95+
#[non_exhaustive]
8796
pub enum CircularMeshUvMode {
8897
/// Treats the shape as a mask over a circle of equal size and radius,
8998
/// with the center of the circle at the center of the texture.

examples/2d/mesh2d_circular.rs renamed to examples/2d/mesh2d_arcs.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use std::f32::consts::PI;
55

66
use bevy::{
7+
color::palettes::css::{BLUE, DARK_SLATE_GREY, RED},
78
math::bounding::{Bounded2d, BoundingVolume},
89
prelude::*,
910
render::mesh::{CircularMeshUvMode, CircularSectorMeshBuilder, CircularSegmentMeshBuilder},
@@ -37,7 +38,7 @@ fn setup(
3738

3839
commands.spawn(Camera2dBundle {
3940
camera: Camera {
40-
clear_color: ClearColorConfig::Custom(Color::DARK_GRAY),
41+
clear_color: ClearColorConfig::Custom(DARK_SLATE_GREY.into()),
4142
..default()
4243
},
4344
..default()
@@ -115,13 +116,9 @@ fn draw_bounds<Shape: Bounded2d + Send + Sync + 'static>(
115116
let rotation = rotation.to_euler(EulerRot::XYZ).2;
116117

117118
let aabb = shape.0.aabb_2d(translation, rotation);
118-
gizmos.rect_2d(aabb.center(), 0.0, aabb.half_size() * 2.0, Color::RED);
119+
gizmos.rect_2d(aabb.center(), 0.0, aabb.half_size() * 2.0, RED);
119120

120121
let bounding_circle = shape.0.bounding_circle(translation, rotation);
121-
gizmos.circle_2d(
122-
bounding_circle.center,
123-
bounding_circle.radius(),
124-
Color::BLUE,
125-
);
122+
gizmos.circle_2d(bounding_circle.center, bounding_circle.radius(), BLUE);
126123
}
127124
}

0 commit comments

Comments
 (0)