bevy_gizmos/retained.rs
1//! This module is for 'retained' alternatives to the 'immediate mode' [`Gizmos`](crate::gizmos::Gizmos) system parameter.
2
3use core::ops::{Deref, DerefMut};
4
5use bevy_asset::Handle;
6use bevy_ecs::{component::Component, reflect::ReflectComponent};
7use bevy_reflect::{std_traits::ReflectDefault, Reflect};
8use bevy_transform::components::Transform;
9
10use crate::{
11 config::{ErasedGizmoConfigGroup, GizmoLineConfig},
12 gizmos::GizmoBuffer,
13 GizmoAsset,
14};
15
16impl Deref for GizmoAsset {
17 type Target = GizmoBuffer<ErasedGizmoConfigGroup, ()>;
18
19 fn deref(&self) -> &Self::Target {
20 &self.buffer
21 }
22}
23
24impl DerefMut for GizmoAsset {
25 fn deref_mut(&mut self) -> &mut Self::Target {
26 &mut self.buffer
27 }
28}
29
30/// A component that draws the gizmos of a [`GizmoAsset`].
31///
32/// When drawing a greater number of static lines a [`Gizmo`] component can
33/// have far better performance than the [`Gizmos`] system parameter,
34/// but the system parameter will perform better for smaller lines that update often.
35///
36/// ## Example
37/// ```
38/// # use bevy_ecs::prelude::*;
39/// # use bevy_gizmos::prelude::*;
40/// # use bevy_asset::prelude::*;
41/// # use bevy_color::palettes::css::*;
42/// # use bevy_utils::default;
43/// # use bevy_math::prelude::*;
44/// fn system(
45/// mut commands: Commands,
46/// mut gizmo_assets: ResMut<Assets<GizmoAsset>>,
47/// ) {
48/// let mut gizmo = GizmoAsset::default();
49///
50/// gizmo.sphere(Vec3::ZERO, 1., RED);
51///
52/// commands.spawn(Gizmo {
53/// handle: gizmo_assets.add(gizmo),
54/// line_config: GizmoLineConfig {
55/// width: 4.,
56/// ..default()
57/// },
58/// ..default()
59/// });
60/// }
61/// ```
62///
63/// [`Gizmos`]: crate::gizmos::Gizmos
64#[derive(Component, Clone, Debug, Default, Reflect)]
65#[reflect(Component, Clone, Default)]
66#[require(Transform)]
67pub struct Gizmo {
68 /// The handle to the gizmo to draw.
69 pub handle: Handle<GizmoAsset>,
70 /// The line specific configuration for this gizmo.
71 pub line_config: GizmoLineConfig,
72 /// How closer to the camera than real geometry the gizmo should be.
73 ///
74 /// In 2D this setting has no effect and is effectively always -1.
75 ///
76 /// Value between -1 and 1 (inclusive).
77 /// * 0 means that there is no change to the gizmo position when rendering
78 /// * 1 means it is furthest away from camera as possible
79 /// * -1 means that it will always render in front of other things.
80 ///
81 /// This is typically useful if you are drawing wireframes on top of polygons
82 /// and your wireframe is z-fighting (flickering on/off) with your main model.
83 /// You would set this value to a negative number close to 0.
84 pub depth_bias: f32,
85}