bevy_gizmos/
cross.rs

1//! Additional [`GizmoBuffer`] Functions -- Crosses
2//!
3//! Includes the implementation of [`GizmoBuffer::cross`] and [`GizmoBuffer::cross_2d`],
4//! and assorted support items.
5
6use crate::{gizmos::GizmoBuffer, prelude::GizmoConfigGroup};
7use bevy_color::Color;
8use bevy_math::{Isometry2d, Isometry3d, Vec2, Vec3};
9
10impl<Config, Clear> GizmoBuffer<Config, Clear>
11where
12    Config: GizmoConfigGroup,
13    Clear: 'static + Send + Sync,
14{
15    /// Draw a cross in 3D with the given `isometry` applied.
16    ///
17    /// If `isometry == Isometry3d::IDENTITY` then
18    ///
19    /// - the center is at `Vec3::ZERO`
20    /// - the `half_size`s are aligned with the `Vec3::X`, `Vec3::Y` and `Vec3::Z` axes.
21    ///
22    /// This should be called for each frame the cross needs to be rendered.
23    ///
24    /// # Example
25    /// ```
26    /// # use bevy_gizmos::prelude::*;
27    /// # use bevy_math::prelude::*;
28    /// # use bevy_color::palettes::basic::WHITE;
29    /// fn system(mut gizmos: Gizmos) {
30    ///     gizmos.cross(Isometry3d::IDENTITY, 0.5, WHITE);
31    /// }
32    /// # bevy_ecs::system::assert_is_system(system);
33    /// ```
34    pub fn cross(
35        &mut self,
36        isometry: impl Into<Isometry3d>,
37        half_size: f32,
38        color: impl Into<Color>,
39    ) {
40        let isometry = isometry.into();
41        let color: Color = color.into();
42        [Vec3::X, Vec3::Y, Vec3::Z]
43            .map(|axis| axis * half_size)
44            .into_iter()
45            .for_each(|axis| {
46                self.line(isometry * axis, isometry * (-axis), color);
47            });
48    }
49
50    /// Draw a cross in 2D with the given `isometry` applied.
51    ///
52    /// If `isometry == Isometry2d::IDENTITY` then
53    ///
54    /// - the center is at `Vec3::ZERO`
55    /// - the `half_size`s are aligned with the `Vec3::X` and `Vec3::Y` axes.
56    ///
57    /// This should be called for each frame the cross needs to be rendered.
58    ///
59    /// # Example
60    /// ```
61    /// # use bevy_gizmos::prelude::*;
62    /// # use bevy_math::prelude::*;
63    /// # use bevy_color::palettes::basic::WHITE;
64    /// fn system(mut gizmos: Gizmos) {
65    ///     gizmos.cross_2d(Isometry2d::IDENTITY, 0.5, WHITE);
66    /// }
67    /// # bevy_ecs::system::assert_is_system(system);
68    /// ```
69    pub fn cross_2d(
70        &mut self,
71        isometry: impl Into<Isometry2d>,
72        half_size: f32,
73        color: impl Into<Color>,
74    ) {
75        let isometry = isometry.into();
76        let color: Color = color.into();
77        [Vec2::X, Vec2::Y]
78            .map(|axis| axis * half_size)
79            .into_iter()
80            .for_each(|axis| {
81                self.line_2d(isometry * axis, isometry * (-axis), color);
82            });
83    }
84}