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, DARK_SLATE_GREY, RED},
9    math::{
10        bounding::{Bounded2d, BoundingVolume},
11        Isometry2d,
12    },
13    prelude::*,
14    render::mesh::{CircularMeshUvMode, CircularSectorMeshBuilder, CircularSegmentMeshBuilder},
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(DARK_SLATE_GREY.into()),
46            ..default()
47        },
48    ));
49
50    const UPPER_Y: f32 = 50.0;
51    const LOWER_Y: f32 = -50.0;
52    const FIRST_X: f32 = -450.0;
53    const OFFSET: f32 = 100.0;
54    const NUM_SLICES: i32 = 8;
55
56    // This draws NUM_SLICES copies of the Bevy logo as circular sectors and segments,
57    // with successively larger angles up to a complete circle.
58    for i in 0..NUM_SLICES {
59        let fraction = (i + 1) as f32 / NUM_SLICES as f32;
60
61        let sector = CircularSector::from_turns(40.0, fraction);
62        // We want to rotate the circular sector so that the sectors appear clockwise from north.
63        // We must rotate it both in the Transform and in the mesh's UV mappings.
64        let sector_angle = -sector.half_angle();
65        let sector_mesh =
66            CircularSectorMeshBuilder::new(sector).uv_mode(CircularMeshUvMode::Mask {
67                angle: sector_angle,
68            });
69        commands.spawn((
70            Mesh2d(meshes.add(sector_mesh)),
71            MeshMaterial2d(material.clone()),
72            Transform {
73                translation: Vec3::new(FIRST_X + OFFSET * i as f32, 2.0 * UPPER_Y, 0.0),
74                rotation: Quat::from_rotation_z(sector_angle),
75                ..default()
76            },
77            DrawBounds(sector),
78        ));
79
80        let segment = CircularSegment::from_turns(40.0, fraction);
81        // For the circular segment, we will draw Bevy charging forward, which requires rotating the
82        // shape and texture by 90 degrees.
83        //
84        // Note that this may be unintuitive; it may feel like we should rotate the texture by the
85        // opposite angle to preserve the orientation of Bevy. But the angle is not the angle of the
86        // texture itself, rather it is the angle at which the vertices are mapped onto the texture.
87        // so it is the negative of what you might otherwise expect.
88        let segment_angle = -FRAC_PI_2;
89        let segment_mesh =
90            CircularSegmentMeshBuilder::new(segment).uv_mode(CircularMeshUvMode::Mask {
91                angle: -segment_angle,
92            });
93        commands.spawn((
94            Mesh2d(meshes.add(segment_mesh)),
95            MeshMaterial2d(material.clone()),
96            Transform {
97                translation: Vec3::new(FIRST_X + OFFSET * i as f32, LOWER_Y, 0.0),
98                rotation: Quat::from_rotation_z(segment_angle),
99                ..default()
100            },
101            DrawBounds(segment),
102        ));
103    }
104}
105
106fn draw_bounds<Shape: Bounded2d + Send + Sync + 'static>(
107    q: Query<(&DrawBounds<Shape>, &GlobalTransform)>,
108    mut gizmos: Gizmos,
109) {
110    for (shape, transform) in &q {
111        let (_, rotation, translation) = transform.to_scale_rotation_translation();
112        let translation = translation.truncate();
113        let rotation = rotation.to_euler(EulerRot::XYZ).2;
114        let isometry = Isometry2d::new(translation, Rot2::radians(rotation));
115
116        let aabb = shape.0.aabb_2d(isometry);
117        gizmos.rect_2d(aabb.center(), aabb.half_size() * 2.0, RED);
118
119        let bounding_circle = shape.0.bounding_circle(isometry);
120        gizmos.circle_2d(bounding_circle.center, bounding_circle.radius(), BLUE);
121    }
122}