mesh2d_arcs/
mesh2d_arcs.rs

1//! Demonstrates UV mappings of the [`CircularSector`] and [`CircularSegment`] primitives.
2//!
3//! Also draws the bounding boxes and circles of the primitives.
4
5use std::f32::consts::FRAC_PI_2;
6
7use bevy::{
8    color::palettes::css::{BLUE, GRAY, RED},
9    math::{
10        bounding::{Bounded2d, BoundingVolume},
11        Isometry2d,
12    },
13    mesh::{CircularMeshUvMode, CircularSectorMeshBuilder, CircularSegmentMeshBuilder},
14    prelude::*,
15};
16
17fn main() {
18    App::new()
19        .add_plugins(DefaultPlugins)
20        .add_systems(Startup, setup)
21        .add_systems(
22            Update,
23            (
24                draw_bounds::<CircularSector>,
25                draw_bounds::<CircularSegment>,
26            ),
27        )
28        .run();
29}
30
31#[derive(Component, Debug)]
32struct DrawBounds<Shape: Bounded2d + Send + Sync + 'static>(Shape);
33
34fn setup(
35    mut commands: Commands,
36    asset_server: Res<AssetServer>,
37    mut meshes: ResMut<Assets<Mesh>>,
38    mut materials: ResMut<Assets<ColorMaterial>>,
39) {
40    let material = materials.add(asset_server.load("branding/icon.png"));
41
42    commands.spawn((
43        Camera2d,
44        Camera {
45            clear_color: ClearColorConfig::Custom(GRAY.into()),
46            ..default()
47        },
48    ));
49
50    const NUM_SLICES: i32 = 8;
51    const SPACING_X: f32 = 100.0;
52    const OFFSET_X: f32 = SPACING_X * (NUM_SLICES - 1) as f32 / 2.0;
53
54    // This draws NUM_SLICES copies of the Bevy logo as circular sectors and segments,
55    // with successively larger angles up to a complete circle.
56    for i in 0..NUM_SLICES {
57        let fraction = (i + 1) as f32 / NUM_SLICES as f32;
58
59        let sector = CircularSector::from_turns(40.0, fraction);
60        // We want to rotate the circular sector so that the sectors appear clockwise from north.
61        // We must rotate it both in the Transform and in the mesh's UV mappings.
62        let sector_angle = -sector.half_angle();
63        let sector_mesh =
64            CircularSectorMeshBuilder::new(sector).uv_mode(CircularMeshUvMode::Mask {
65                angle: sector_angle,
66            });
67        commands.spawn((
68            Mesh2d(meshes.add(sector_mesh)),
69            MeshMaterial2d(material.clone()),
70            Transform {
71                translation: Vec3::new(SPACING_X * i as f32 - OFFSET_X, 50.0, 0.0),
72                rotation: Quat::from_rotation_z(sector_angle),
73                ..default()
74            },
75            DrawBounds(sector),
76        ));
77
78        let segment = CircularSegment::from_turns(40.0, fraction);
79        // For the circular segment, we will draw Bevy charging forward, which requires rotating the
80        // shape and texture by 90 degrees.
81        //
82        // Note that this may be unintuitive; it may feel like we should rotate the texture by the
83        // opposite angle to preserve the orientation of Bevy. But the angle is not the angle of the
84        // texture itself, rather it is the angle at which the vertices are mapped onto the texture.
85        // so it is the negative of what you might otherwise expect.
86        let segment_angle = -FRAC_PI_2;
87        let segment_mesh =
88            CircularSegmentMeshBuilder::new(segment).uv_mode(CircularMeshUvMode::Mask {
89                angle: -segment_angle,
90            });
91        commands.spawn((
92            Mesh2d(meshes.add(segment_mesh)),
93            MeshMaterial2d(material.clone()),
94            Transform {
95                translation: Vec3::new(SPACING_X * i as f32 - OFFSET_X, -50.0, 0.0),
96                rotation: Quat::from_rotation_z(segment_angle),
97                ..default()
98            },
99            DrawBounds(segment),
100        ));
101    }
102}
103
104fn draw_bounds<Shape: Bounded2d + Send + Sync + 'static>(
105    q: Query<(&DrawBounds<Shape>, &GlobalTransform)>,
106    mut gizmos: Gizmos,
107) {
108    for (shape, transform) in &q {
109        let (_, rotation, translation) = transform.to_scale_rotation_translation();
110        let translation = translation.truncate();
111        let rotation = rotation.to_euler(EulerRot::XYZ).2;
112        let isometry = Isometry2d::new(translation, Rot2::radians(rotation));
113
114        let aabb = shape.0.aabb_2d(isometry);
115        gizmos.rect_2d(aabb.center(), aabb.half_size() * 2.0, RED);
116
117        let bounding_circle = shape.0.bounding_circle(isometry);
118        gizmos.circle_2d(bounding_circle.center, bounding_circle.radius(), BLUE);
119    }
120}