Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions crates/bevy_gizmos/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ bevy_reflect = { path = "../bevy_reflect", version = "0.18.0-dev" }
bevy_transform = { path = "../bevy_transform", version = "0.18.0-dev" }
bevy_gizmos_macros = { path = "macros", version = "0.18.0-dev" }
bevy_time = { path = "../bevy_time", version = "0.18.0-dev" }
bevy_text = { path = "../bevy_text", version = "0.18.0-dev" }
bevy_image = { path = "../bevy_image", version = "0.18.0-dev" }

[lints]
workspace = true
Expand Down
124 changes: 123 additions & 1 deletion crates/bevy_gizmos/src/gizmos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ use core::{
ops::{Deref, DerefMut},
};

use bevy_asset::Assets;
use bevy_color::{Color, LinearRgba};
use bevy_ecs::system::ResMut;
use bevy_ecs::{
change_detection::Tick,
query::FilteredAccessSet,
Expand All @@ -18,14 +20,18 @@ use bevy_ecs::{
},
world::{unsafe_world_cell::UnsafeWorldCell, World},
};
use bevy_image::{Image, TextureAtlasLayout};
use bevy_math::{bounding::Aabb3d, Isometry2d, Isometry3d, Vec2, Vec3};
use bevy_reflect::{std_traits::ReflectDefault, Reflect};

use bevy_text::{CosmicFontSystem, Font, FontAtlasSet, SwashCache, TextLayoutInfo};
use bevy_transform::TransformPoint;
use bevy_utils::default;

use crate::{
config::{DefaultGizmoConfigGroup, GizmoConfigGroup, GizmoConfigStore},
prelude::GizmoConfig,
text::{GizmoText, GizmoTextBuffer},
};

/// Storage of gizmo primitives.
Expand All @@ -35,6 +41,9 @@ pub struct GizmoStorage<Config, Clear> {
pub(crate) list_colors: Vec<LinearRgba>,
pub(crate) strip_positions: Vec<Vec3>,
pub(crate) strip_colors: Vec<LinearRgba>,
pub(crate) glyph_vertices: Vec<Vec3>,
pub(crate) glyph_uvs: Vec<Vec2>,
pub(crate) glyph_colors: Vec<LinearRgba>,
marker: PhantomData<(Config, Clear)>,
}

Expand All @@ -45,6 +54,9 @@ impl<Config, Clear> Default for GizmoStorage<Config, Clear> {
list_colors: default(),
strip_positions: default(),
strip_colors: default(),
glyph_vertices: default(),
glyph_uvs: default(),
glyph_colors: default(),
marker: PhantomData,
}
}
Expand All @@ -64,6 +76,12 @@ where
self.list_colors.extend(other.list_colors.iter());
self.strip_positions.extend(other.strip_positions.iter());
self.strip_colors.extend(other.strip_colors.iter());

{
self.glyph_vertices.extend(other.glyph_vertices.iter());
self.glyph_uvs.extend(other.glyph_uvs.iter());
self.glyph_colors.extend(other.glyph_colors.iter());
}
}

pub(crate) fn swap<OtherConfig, OtherClear>(
Expand All @@ -74,6 +92,12 @@ where
mem::swap(&mut self.list_colors, &mut other.list_colors);
mem::swap(&mut self.strip_positions, &mut other.strip_positions);
mem::swap(&mut self.strip_colors, &mut other.strip_colors);

{
mem::swap(&mut self.glyph_vertices, &mut other.glyph_vertices);
mem::swap(&mut self.glyph_uvs, &mut other.glyph_uvs);
mem::swap(&mut self.glyph_colors, &mut other.glyph_colors);
}
}

/// Clear this gizmo storage of any requested gizmos.
Expand All @@ -82,6 +106,12 @@ where
self.list_colors.clear();
self.strip_positions.clear();
self.strip_colors.clear();

{
self.glyph_vertices.clear();
self.glyph_uvs.clear();
self.glyph_colors.clear()
}
}
}

Expand Down Expand Up @@ -150,6 +180,30 @@ where
pub config: &'w GizmoConfig,
/// The currently used [`GizmoConfigGroup`]
pub config_ext: &'w Config,
/// text buffer
pub text_buffer: ResMut<'w, GizmoTextBuffer<Config, Clear>>,
}

impl<'w, 's, Config, Clear> Gizmos<'w, 's, Config, Clear>
where
Config: GizmoConfigGroup,
Clear: 'static + Send + Sync,
{
/// Draw text
pub fn text_2d(
&mut self,
position: Vec2,
text: impl Into<String>,
size: f32,
color: impl Into<Color>,
) {
self.text_buffer.text.push(GizmoText {
position,
text: text.into(),
size,
color: color.into(),
});
}
}

impl<'w, 's, Config, Clear> Deref for Gizmos<'w, 's, Config, Clear>
Expand Down Expand Up @@ -177,7 +231,9 @@ where
type GizmosState<Config, Clear> = (
Deferred<'static, GizmoBuffer<Config, Clear>>,
Res<'static, GizmoConfigStore>,
ResMut<'static, GizmoTextBuffer<Config, Clear>>,
);

#[doc(hidden)]
pub struct GizmosFetchState<Config, Clear>
where
Expand Down Expand Up @@ -237,14 +293,15 @@ where
}

#[inline]

unsafe fn get_param<'w, 's>(
state: &'s mut Self::State,
system_meta: &SystemMeta,
world: UnsafeWorldCell<'w>,
change_tick: Tick,
) -> Self::Item<'w, 's> {
// SAFETY: Delegated to existing `SystemParam` implementations.
let (mut f0, f1) = unsafe {
let (mut f0, f1, text_buffer) = unsafe {
GizmosState::<Config, Clear>::get_param(
&mut state.state,
system_meta,
Expand All @@ -263,6 +320,7 @@ where
buffer: f0,
config,
config_ext,
text_buffer,
}
}
}
Expand Down Expand Up @@ -298,6 +356,12 @@ where
pub strip_positions: Vec<Vec3>,
/// The colors of line strip vertices.
pub strip_colors: Vec<LinearRgba>,
/// The positions the glyph vertices.
pub glyph_vertices: Vec<Vec3>,
/// The UV coords for the glyph vertices.
pub glyph_uvs: Vec<Vec2>,
/// The colors for each glyph vertex.
pub glyph_colors: Vec<LinearRgba>,
#[reflect(ignore, clone)]
pub(crate) marker: PhantomData<(Config, Clear)>,
}
Expand All @@ -314,6 +378,12 @@ where
list_colors: Vec::new(),
strip_positions: Vec::new(),
strip_colors: Vec::new(),

glyph_vertices: Vec::new(),

glyph_uvs: Vec::new(),

glyph_colors: Vec::new(),
marker: PhantomData,
}
}
Expand All @@ -329,6 +399,15 @@ pub struct GizmoBufferView<'a> {
pub strip_positions: &'a Vec<Vec3>,
/// Vertex colors for line-strip topology.
pub strip_colors: &'a Vec<LinearRgba>,

/// Vertex positions for glyphs.
pub glyph_vertices: &'a Vec<Vec3>,

/// Vertex UVs for glyphs.
pub glyph_uvs: &'a Vec<Vec2>,

/// Vertex colors for glyphs.
pub glyph_colors: &'a Vec<LinearRgba>,
}

impl<Config, Clear> SystemBuffer for GizmoBuffer<Config, Clear>
Expand Down Expand Up @@ -365,13 +444,25 @@ where
list_colors,
strip_positions,
strip_colors,

glyph_vertices,

glyph_uvs,

glyph_colors,
..
} = self;
GizmoBufferView {
list_positions,
list_colors,
strip_positions,
strip_colors,

glyph_vertices,

glyph_uvs,

glyph_colors,
}
}
/// Draw a line in 3D from `start` to `end`.
Expand Down Expand Up @@ -804,6 +895,37 @@ where
self.line_gradient_2d(start, start + vector, start_color, end_color);
}

/// Draw a glyph
#[inline]
pub fn draw_glyph_2d(
&mut self,
min: Vec2,
max: Vec2,
uv_min: Vec2,
uv_max: Vec2,
color: impl Into<Color>,
) {
let linear_color = LinearRgba::from(color.into());

self.glyph_vertices.extend([
min.extend(0.),
max.extend(0.),
Vec3::new(min.x, max.y, 0.),
min.extend(0.),
Vec3::new(max.x, min.y, 0.),
max.extend(0.),
]);
self.glyph_uvs.extend([
uv_min,
uv_max,
Vec2::new(uv_min.x, uv_max.y),
uv_min,
Vec2::new(uv_max.x, uv_min.y),
uv_max,
]);
self.glyph_colors.extend(iter::repeat(linear_color).take(6));
}

/// Draw a wireframe rectangle in 2D with the given `isometry` applied.
///
/// If `isometry == Isometry2d::IDENTITY` then
Expand Down
14 changes: 12 additions & 2 deletions crates/bevy_gizmos/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ pub mod grid;
pub mod primitives;
pub mod retained;
pub mod rounded_box;
pub mod text;

#[cfg(feature = "bevy_light")]
pub mod light;
Expand Down Expand Up @@ -62,7 +63,7 @@ pub mod prelude {
pub use crate::light::{LightGizmoColor, LightGizmoConfigGroup, ShowLightGizmo};
}

use bevy_app::{App, FixedFirst, FixedLast, Last, Plugin, RunFixedMainLoop};
use bevy_app::{App, FixedFirst, FixedLast, Last, Plugin, PostUpdate, RunFixedMainLoop};
use bevy_asset::{Asset, AssetApp, Assets, Handle};
use bevy_ecs::{
resource::Resource,
Expand All @@ -71,7 +72,11 @@ use bevy_ecs::{
};
use bevy_reflect::TypePath;

use crate::{config::ErasedGizmoConfigGroup, gizmos::GizmoBuffer};
use crate::{
config::ErasedGizmoConfigGroup,
gizmos::GizmoBuffer,
text::{gizmo_text_system, GizmoText, GizmoTextBuffer},
};

use bevy_time::Fixed;
use bevy_utils::TypeIdMap;
Expand Down Expand Up @@ -136,6 +141,7 @@ impl AppGizmoBuilder for App {
self.init_resource::<GizmoStorage<Config, ()>>()
.init_resource::<GizmoStorage<Config, Fixed>>()
.init_resource::<GizmoStorage<Config, Swap<Fixed>>>()
.init_resource::<GizmoTextBuffer<Config, ()>>()
.add_systems(
RunFixedMainLoop,
start_gizmo_context::<Config, Fixed>
Expand All @@ -148,6 +154,7 @@ impl AppGizmoBuilder for App {
end_gizmo_context::<Config, Fixed>
.in_set(bevy_app::RunFixedMainLoopSystems::AfterFixedMainLoop),
)
.add_systems(PostUpdate, gizmo_text_system::<Config, ()>)
.add_systems(
Last,
(
Expand Down Expand Up @@ -287,6 +294,9 @@ fn update_gizmo_meshes<Config: GizmoConfigGroup>(
list_colors: mem::take(&mut storage.list_colors),
strip_positions: mem::take(&mut storage.strip_positions),
strip_colors: mem::take(&mut storage.strip_colors),
glyph_vertices: mem::take(&mut storage.glyph_vertices),
glyph_uvs: mem::take(&mut storage.glyph_uvs),
glyph_colors: mem::take(&mut storage.glyph_colors),
marker: PhantomData,
},
};
Expand Down
Loading
Loading