Skip to content
This repository was archived by the owner on Oct 5, 2023. It is now read-only.

Commit 7359847

Browse files
bevy 0.9 surport + example
1 parent 252a993 commit 7359847

File tree

7 files changed

+166
-66
lines changed

7 files changed

+166
-66
lines changed

.gitignore

+1-8
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
1-
# Generated by Cargo
2-
# will have compiled files and executables
3-
/target/
4-
5-
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
6-
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
1+
/target
72
Cargo.lock
83

9-
# These are backup files generated by rustfmt
10-
**/*.rs.bk

Cargo.toml

+8-5
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@ description = "Visual gizmos to aid with development and debugging in Bevy"
44
license = "MIT OR Apache-2.0"
55
repository = "https://github.com/LiamGallagher737/bevy_gizmos"
66
readme = "README.md"
7-
version = "0.1.1"
7+
version = "0.2.0"
88
edition = "2021"
99

10-
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
10+
[workspace]
11+
members = [
12+
"examples/*",
13+
]
1114

1215
[dependencies]
13-
bevy = { version = "0.8", default-features = false, features = [ "render" ]}
14-
bevy_mod_picking = "0.7"
15-
lazy_static = "1.4.0"
16+
bevy = "0.9"
17+
bevy_mod_picking = "0.10"
18+
lazy_static = "1.4"

README.md

+18-8
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Visual gizmos to aid with development and debugging in [Bevy](https://bevyengine
1616

1717
Add the following to your `Cargo.toml`
1818
```toml
19-
bevy_mod_gizmos = "0.1"
19+
bevy_mod_gizmos = "0.2.0"
2020
```
2121

2222
Add this to any file you want to use gizmos in
@@ -29,9 +29,9 @@ Add the plugin to your app
2929
.add_plugin(GizmosPlugin)
3030
```
3131

32-
For interactive gizmos add the following when creating your camera
32+
For interactive gizmos add the following bundle when spawning your camera
3333
```rs
34-
.insert_bundle(GizmoInteractionCamera::default())
34+
GizmoInteractionCamera::default()
3535
```
3636

3737
To increase performance I recommend the following in your `Cargo.toml`
@@ -42,12 +42,13 @@ opt-level = 3
4242

4343

4444

45-
<!--
4645
# Demo
47-
```console
48-
cargo run --example CommingSoon
46+
47+
This exampels showcases all built-in gizmmo types and interactions. Click on a gizmo and it will print to the console its name.
48+
49+
```
50+
cargo run --example demo
4951
```
50-
-->
5152

5253

5354

@@ -91,7 +92,16 @@ Gizmo::torus(position, size, color)
9192
Gizmo::new(position, scale, color, mesh_handle)
9293
```
9394

94-
[More Info](https://docs.rs/bevy_mod_gizmos/0.1.0/bevy_mod_gizmos/gizmo/struct.Gizmo.html)
95+
[More Info](https://docs.rs/bevy_mod_gizmos/latest/bevy_mod_gizmos/gizmo/struct.Gizmo.html)
96+
97+
98+
99+
# Bevy Tracking
100+
101+
|Bevy|bevy_mod_gizmos|
102+
|---|---|
103+
|0.9|0.2.0|
104+
|0.7|0.1.1|
95105

96106

97107

examples/demo.rs

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
use bevy::prelude::*;
2+
use bevy_mod_gizmos::*;
3+
4+
#[rustfmt::skip]
5+
fn main() {
6+
App::new()
7+
8+
.add_plugins(DefaultPlugins)
9+
.add_plugin(GizmosPlugin)
10+
11+
.add_startup_system(setup)
12+
.add_system(move_entities)
13+
.add_system(gizmos)
14+
15+
.run();
16+
}
17+
18+
fn gizmos(query: Query<&Transform, Without<Camera>>) {
19+
let positions: Vec<Vec3> = query.iter().map(|t| t.translation).collect();
20+
draw_gizmos(
21+
vec![
22+
Gizmo::sphere(positions[0], 1.0, Color::GREEN).on_click(|_| println!("Clicked Sphere")),
23+
Gizmo::cube(positions[1], 1.0, Color::RED).on_click(|_| println!("Clicked Cube")),
24+
Gizmo::cubiod(positions[2], Vec3::new(1.0, 0.5, 1.5), Color::BLUE)
25+
.on_click(|_| println!("Clicked Cubiod")),
26+
Gizmo::capsule(positions[3], 1.0, 1.5, Color::ORANGE)
27+
.on_click(|_| println!("Clicked Capsule")),
28+
Gizmo::torus(positions[4], 1.0, Color::YELLOW).on_click(|_| println!("Clicked Torus")),
29+
],
30+
false, // Draw a line?
31+
);
32+
}
33+
34+
fn setup(mut commands: Commands) {
35+
// Spawn camera
36+
let cam_transform = Transform::from_xyz(4.0, 5.0, 8.0);
37+
commands.spawn((
38+
Camera3dBundle {
39+
transform: cam_transform.looking_at([4.0, 0.0, 0.0].into(), Vec3::Y),
40+
..Default::default()
41+
},
42+
GizmoInteractionCamera::default(),
43+
));
44+
45+
// Create one entity for each gizmo type
46+
for i in 0..5 {
47+
commands.spawn(TransformBundle::from_transform(Transform::from_xyz(
48+
i as f32 * 2.0,
49+
0.0,
50+
0.0,
51+
)));
52+
}
53+
}
54+
55+
fn move_entities(mut query: Query<&mut Transform, Without<Camera>>, time: Res<Time>) {
56+
for (i, mut transform) in query.iter_mut().enumerate() {
57+
transform.translation.y = (time.elapsed_seconds() + i as f32).sin();
58+
}
59+
}

src/basic.rs

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ pub fn draw_gizmos(mut gizmos: Vec<Gizmo>, line: bool) {
5252
/// * `color` - The color of the line
5353
/// # Example
5454
/// ```
55+
/// # use bevy::prelude::*;
5556
/// use bevy_mod_gizmos::*;
5657
/// draw_line(vec![Vec3::new(8.0, 2.0, 5.0), Vec3::new(9.0, 3.0, 6.0), Vec3::new(10.0, 4.0, 7.0)], Color::GREEN);
5758
/// ```

src/gizmo.rs

+18-18
Original file line numberDiff line numberDiff line change
@@ -167,26 +167,26 @@ impl Gizmo {
167167
}
168168
}
169169

170-
/// Change the gizmos positon
170+
/// Change the gizmos translation
171171
/// # Example
172172
/// ```
173-
/// use bevy::prelude::*;
174-
/// use bevy_mod_gizmos::*;
175-
///
176-
/// let gizmo = Gizmo::default().with_position(Vec3::new(8.0, 2.0, 5.0));
173+
/// # use bevy::prelude::*;
174+
/// # use bevy_mod_gizmos::*;
175+
/// let gizmo = Gizmo::default().with_translation(Vec3::new(8.0, 2.0, 5.0));
176+
/// assert_eq!(gizmo.transform.translation, Vec3::new(8.0, 2.0, 5.0));
177177
/// ```
178-
pub fn with_position(mut self, translation: Vec3) -> Self {
178+
pub fn with_translation(mut self, translation: Vec3) -> Self {
179179
self.transform.translation = translation;
180180
self
181181
}
182182

183183
/// Change the gizmos scale
184184
/// # Example
185185
/// ```
186-
/// use bevy::prelude::*;
187-
/// use bevy_mod_gizmos::*;
188-
///
186+
/// # use bevy::prelude::*;
187+
/// # use bevy_mod_gizmos::*;
189188
/// let gizmo = Gizmo::default().with_scale(Vec3::new(3.0, 6.0, 9.0));
189+
/// assert_eq!(gizmo.transform.scale, Vec3::new(3.0, 6.0, 9.0));
190190
/// ```
191191
pub fn with_scale(mut self, scale: Vec3) -> Self {
192192
self.transform.scale = scale;
@@ -196,10 +196,10 @@ impl Gizmo {
196196
/// Change the gizmos rotation
197197
/// # Example
198198
/// ```
199-
/// use bevy::prelude::*;
200-
/// use bevy_mod_gizmos::*;
201-
///
199+
/// # use bevy::prelude::*;
200+
/// # use bevy_mod_gizmos::*;
202201
/// let gizmo = Gizmo::default().with_rotation(Quat::from_xyzw(0.0, 0.7, 0.7, 0.0));
202+
/// assert_eq!(gizmo.transform.rotation, Quat::from_xyzw(0.0, 0.7, 0.7, 0.0));
203203
/// ```
204204
pub fn with_rotation(mut self, rotation: Quat) -> Self {
205205
self.transform.rotation = rotation;
@@ -209,10 +209,10 @@ impl Gizmo {
209209
/// Change the gizmos color
210210
/// # Example
211211
/// ```
212-
/// use bevy::prelude::*;
213-
/// use bevy_mod_gizmos::*;
214-
///
212+
/// # use bevy::prelude::*;
213+
/// # use bevy_mod_gizmos::*;
215214
/// let gizmo = Gizmo::default().with_color(Color::GREEN);
215+
/// assert_eq!(gizmo.color, Color::GREEN);
216216
/// ```
217217
pub fn with_color(mut self, color: Color) -> Self {
218218
self.color = color;
@@ -222,10 +222,10 @@ impl Gizmo {
222222
/// Change the gizmos mesh
223223
/// # Example
224224
/// ```
225-
/// use bevy::prelude::*;
226-
/// use bevy_mod_gizmos::*;
227-
///
225+
/// # use bevy::prelude::*;
226+
/// # use bevy_mod_gizmos::*;
228227
/// let gizmo = Gizmo::default().with_mesh(Handle::<Mesh>::default());
228+
/// assert_eq!(gizmo.mesh_handle, Handle::<Mesh>::default());
229229
/// ```
230230
pub fn with_mesh(mut self, mesh_handle: Handle<Mesh>) -> Self {
231231
self.mesh_handle = mesh_handle;

src/lib.rs

+61-27
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,51 @@
1+
//! Visual gizmos to aid with development and debugging in [Bevy](https://bevyengine.org/)
2+
//!
3+
//! # Examples
4+
//!
5+
//! Draw a single gizmo
6+
//! ```
7+
//! # use bevy::prelude::*;
8+
//! # use bevy_mod_gizmos::*;
9+
//! draw_gizmo(Gizmo::sphere(Vec3::new(12.0, 0.0, 8.0), 1.0, Color::BLUE));
10+
//! ```
11+
//!
12+
//! Draw multiple gizmos
13+
//! ```
14+
//! # use bevy::prelude::*;
15+
//! # use bevy_mod_gizmos::*;
16+
//! draw_gizmos(
17+
//! vec![
18+
//! Gizmo::sphere(Vec3::new(12.0, 0.0, 8.0), 1.0, Color::BLUE),
19+
//! Gizmo::cube(Vec3::new(12.0, 0.0, 8.0), 1.0, Color::BLUE),
20+
//! ],
21+
//! true, // True if you want to draw a line between the gizmos
22+
//! );
23+
//! ```
24+
//!
25+
//! Draw a line
26+
//! ```
27+
//! # use bevy::prelude::*;
28+
//! # use bevy_mod_gizmos::*;
29+
//! draw_line(
30+
//! vec![
31+
//! Vec3::new(0.0, 0.0, 0.0),
32+
//! Vec3::new(1.0, 0.0, 0.0),
33+
//! Vec3::new(1.0, 0.0, 1.0),
34+
//! ],
35+
//! Color::BLUE,
36+
//! );
37+
//! ```
38+
139
use bevy::{
240
math::Vec3,
341
pbr::{NotShadowCaster, NotShadowReceiver, PbrBundle, StandardMaterial},
442
prelude::{
5-
App, Assets, Color, Commands, CoreStage, Entity, ExclusiveSystemDescriptorCoercion, Handle,
6-
IntoExclusiveSystem, Mesh, ParallelSystemDescriptorCoercion, Plugin, ResMut,
43+
App, Assets, Color, Commands, CoreStage, Entity, Handle, Mesh, Plugin, ResMut, Resource,
744
},
845
render::mesh::{Indices, PrimitiveTopology},
946
utils::hashbrown::HashMap,
1047
};
11-
use bevy_mod_picking::{DefaultPickingPlugins, PickableBundle, PickingSystem};
48+
use bevy_mod_picking::{DefaultPickingPlugins, PickableBundle};
1249
use interactions::{interaction_system, INTERACTIONS};
1350
use lazy_static::lazy_static;
1451
use std::sync::RwLock;
@@ -18,29 +55,20 @@ pub mod gizmo;
1855
pub mod interactions;
1956

2057
pub use basic::*;
58+
pub use interactions::GizmoInteractionCamera;
2159

2260
/// Add this to your bevy [`App`] for gizmos to function
2361
pub struct GizmosPlugin;
2462
impl Plugin for GizmosPlugin {
2563
fn build(&self, app: &mut App) {
2664
app.add_plugins(DefaultPickingPlugins);
27-
app.add_plugin(bevy_mod_picking::DebugCursorPickingPlugin);
2865
app.init_resource::<GizmoEntities>();
2966
app.init_resource::<MaterialHandles>();
3067
app.add_startup_system(gizmo::setup);
31-
app.add_system_to_stage(
32-
CoreStage::First,
33-
interaction_system
34-
.exclusive_system()
35-
.after(PickingSystem::Events),
36-
);
37-
app.add_system_to_stage(
38-
CoreStage::PreUpdate,
39-
cleanup_system.before(PickingSystem::Selection),
40-
);
68+
app.add_system_to_stage(CoreStage::First, interaction_system);
69+
app.add_system_to_stage(CoreStage::PreUpdate, cleanup_system);
4170
app.add_system_to_stage(CoreStage::Update, gizmos_system);
4271
app.add_system_to_stage(CoreStage::Update, lines_system);
43-
// app.add_system(interaction_system.exclusive_system());
4472
}
4573
}
4674

@@ -66,21 +94,27 @@ fn gizmos_system(
6694
mut gizmo_entities: ResMut<GizmoEntities>,
6795
mut material_handles: ResMut<MaterialHandles>,
6896
) {
97+
// Stops flickering for some off reason
98+
// TODO: Needs a proper solution
99+
std::thread::sleep(std::time::Duration::from_micros(1));
100+
69101
if let Ok(mut gizmo_buffer) = GIZMO_BUFFER.write() {
70102
while let Some(gizmo) = gizmo_buffer.pop() {
71103
let material_handle =
72104
get_material_handle(gizmo.color, &mut material_handles, &mut materials);
73105

74106
let entity = commands
75-
.spawn_bundle(PbrBundle {
76-
transform: gizmo.transform,
77-
mesh: gizmo.mesh_handle,
78-
material: material_handle,
79-
..Default::default()
80-
})
81-
.insert(NotShadowCaster)
82-
.insert(NotShadowReceiver)
83-
.insert_bundle(PickableBundle::default())
107+
.spawn((
108+
PbrBundle {
109+
transform: gizmo.transform,
110+
mesh: gizmo.mesh_handle,
111+
material: material_handle,
112+
..Default::default()
113+
},
114+
NotShadowCaster,
115+
NotShadowReceiver,
116+
PickableBundle::default(),
117+
))
84118
.id();
85119

86120
gizmo_entities.0.push(entity);
@@ -133,7 +167,7 @@ fn lines_system(
133167
get_material_handle(line.color, &mut material_handles, &mut materials);
134168
gizmo_entities.0.push(
135169
commands
136-
.spawn_bundle(PbrBundle {
170+
.spawn(PbrBundle {
137171
mesh: mesh_handle,
138172
material: material_handle,
139173
..Default::default()
@@ -197,7 +231,7 @@ fn get_material_handle(
197231
}
198232
}
199233

200-
#[derive(Default)]
234+
#[derive(Default, Resource)]
201235
struct GizmoEntities(Vec<Entity>);
202-
#[derive(Default)]
236+
#[derive(Default, Resource)]
203237
struct MaterialHandles(HashMap<u32, Handle<StandardMaterial>>);

0 commit comments

Comments
 (0)