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

Commit

Permalink
bevy 0.9 surport + example
Browse files Browse the repository at this point in the history
  • Loading branch information
LiamGallagher737 committed Dec 4, 2022
1 parent 252a993 commit 7359847
Show file tree
Hide file tree
Showing 7 changed files with 166 additions and 66 deletions.
9 changes: 1 addition & 8 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
# Generated by Cargo
# will have compiled files and executables
/target/

# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
/target
Cargo.lock

# These are backup files generated by rustfmt
**/*.rs.bk
13 changes: 8 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ description = "Visual gizmos to aid with development and debugging in Bevy"
license = "MIT OR Apache-2.0"
repository = "https://github.com/LiamGallagher737/bevy_gizmos"
readme = "README.md"
version = "0.1.1"
version = "0.2.0"
edition = "2021"

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

[dependencies]
bevy = { version = "0.8", default-features = false, features = [ "render" ]}
bevy_mod_picking = "0.7"
lazy_static = "1.4.0"
bevy = "0.9"
bevy_mod_picking = "0.10"
lazy_static = "1.4"
26 changes: 18 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Visual gizmos to aid with development and debugging in [Bevy](https://bevyengine

Add the following to your `Cargo.toml`
```toml
bevy_mod_gizmos = "0.1"
bevy_mod_gizmos = "0.2.0"
```

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

For interactive gizmos add the following when creating your camera
For interactive gizmos add the following bundle when spawning your camera
```rs
.insert_bundle(GizmoInteractionCamera::default())
GizmoInteractionCamera::default()
```

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



<!--
# Demo
```console
cargo run --example CommingSoon

This exampels showcases all built-in gizmmo types and interactions. Click on a gizmo and it will print to the console its name.

```
cargo run --example demo
```
-->



Expand Down Expand Up @@ -91,7 +92,16 @@ Gizmo::torus(position, size, color)
Gizmo::new(position, scale, color, mesh_handle)
```

[More Info](https://docs.rs/bevy_mod_gizmos/0.1.0/bevy_mod_gizmos/gizmo/struct.Gizmo.html)
[More Info](https://docs.rs/bevy_mod_gizmos/latest/bevy_mod_gizmos/gizmo/struct.Gizmo.html)



# Bevy Tracking

|Bevy|bevy_mod_gizmos|
|---|---|
|0.9|0.2.0|
|0.7|0.1.1|



Expand Down
59 changes: 59 additions & 0 deletions examples/demo.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
use bevy::prelude::*;
use bevy_mod_gizmos::*;

#[rustfmt::skip]
fn main() {
App::new()

.add_plugins(DefaultPlugins)
.add_plugin(GizmosPlugin)

.add_startup_system(setup)
.add_system(move_entities)
.add_system(gizmos)

.run();
}

fn gizmos(query: Query<&Transform, Without<Camera>>) {
let positions: Vec<Vec3> = query.iter().map(|t| t.translation).collect();
draw_gizmos(
vec![
Gizmo::sphere(positions[0], 1.0, Color::GREEN).on_click(|_| println!("Clicked Sphere")),
Gizmo::cube(positions[1], 1.0, Color::RED).on_click(|_| println!("Clicked Cube")),
Gizmo::cubiod(positions[2], Vec3::new(1.0, 0.5, 1.5), Color::BLUE)
.on_click(|_| println!("Clicked Cubiod")),
Gizmo::capsule(positions[3], 1.0, 1.5, Color::ORANGE)
.on_click(|_| println!("Clicked Capsule")),
Gizmo::torus(positions[4], 1.0, Color::YELLOW).on_click(|_| println!("Clicked Torus")),
],
false, // Draw a line?
);
}

fn setup(mut commands: Commands) {
// Spawn camera
let cam_transform = Transform::from_xyz(4.0, 5.0, 8.0);
commands.spawn((
Camera3dBundle {
transform: cam_transform.looking_at([4.0, 0.0, 0.0].into(), Vec3::Y),
..Default::default()
},
GizmoInteractionCamera::default(),
));

// Create one entity for each gizmo type
for i in 0..5 {
commands.spawn(TransformBundle::from_transform(Transform::from_xyz(
i as f32 * 2.0,
0.0,
0.0,
)));
}
}

fn move_entities(mut query: Query<&mut Transform, Without<Camera>>, time: Res<Time>) {
for (i, mut transform) in query.iter_mut().enumerate() {
transform.translation.y = (time.elapsed_seconds() + i as f32).sin();
}
}
1 change: 1 addition & 0 deletions src/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ pub fn draw_gizmos(mut gizmos: Vec<Gizmo>, line: bool) {
/// * `color` - The color of the line
/// # Example
/// ```
/// # use bevy::prelude::*;
/// use bevy_mod_gizmos::*;
/// 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);
/// ```
Expand Down
36 changes: 18 additions & 18 deletions src/gizmo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,26 +167,26 @@ impl Gizmo {
}
}

/// Change the gizmos positon
/// Change the gizmos translation
/// # Example
/// ```
/// use bevy::prelude::*;
/// use bevy_mod_gizmos::*;
///
/// let gizmo = Gizmo::default().with_position(Vec3::new(8.0, 2.0, 5.0));
/// # use bevy::prelude::*;
/// # use bevy_mod_gizmos::*;
/// let gizmo = Gizmo::default().with_translation(Vec3::new(8.0, 2.0, 5.0));
/// assert_eq!(gizmo.transform.translation, Vec3::new(8.0, 2.0, 5.0));
/// ```
pub fn with_position(mut self, translation: Vec3) -> Self {
pub fn with_translation(mut self, translation: Vec3) -> Self {
self.transform.translation = translation;
self
}

/// Change the gizmos scale
/// # Example
/// ```
/// use bevy::prelude::*;
/// use bevy_mod_gizmos::*;
///
/// # use bevy::prelude::*;
/// # use bevy_mod_gizmos::*;
/// let gizmo = Gizmo::default().with_scale(Vec3::new(3.0, 6.0, 9.0));
/// assert_eq!(gizmo.transform.scale, Vec3::new(3.0, 6.0, 9.0));
/// ```
pub fn with_scale(mut self, scale: Vec3) -> Self {
self.transform.scale = scale;
Expand All @@ -196,10 +196,10 @@ impl Gizmo {
/// Change the gizmos rotation
/// # Example
/// ```
/// use bevy::prelude::*;
/// use bevy_mod_gizmos::*;
///
/// # use bevy::prelude::*;
/// # use bevy_mod_gizmos::*;
/// let gizmo = Gizmo::default().with_rotation(Quat::from_xyzw(0.0, 0.7, 0.7, 0.0));
/// assert_eq!(gizmo.transform.rotation, Quat::from_xyzw(0.0, 0.7, 0.7, 0.0));
/// ```
pub fn with_rotation(mut self, rotation: Quat) -> Self {
self.transform.rotation = rotation;
Expand All @@ -209,10 +209,10 @@ impl Gizmo {
/// Change the gizmos color
/// # Example
/// ```
/// use bevy::prelude::*;
/// use bevy_mod_gizmos::*;
///
/// # use bevy::prelude::*;
/// # use bevy_mod_gizmos::*;
/// let gizmo = Gizmo::default().with_color(Color::GREEN);
/// assert_eq!(gizmo.color, Color::GREEN);
/// ```
pub fn with_color(mut self, color: Color) -> Self {
self.color = color;
Expand All @@ -222,10 +222,10 @@ impl Gizmo {
/// Change the gizmos mesh
/// # Example
/// ```
/// use bevy::prelude::*;
/// use bevy_mod_gizmos::*;
///
/// # use bevy::prelude::*;
/// # use bevy_mod_gizmos::*;
/// let gizmo = Gizmo::default().with_mesh(Handle::<Mesh>::default());
/// assert_eq!(gizmo.mesh_handle, Handle::<Mesh>::default());
/// ```
pub fn with_mesh(mut self, mesh_handle: Handle<Mesh>) -> Self {
self.mesh_handle = mesh_handle;
Expand Down
88 changes: 61 additions & 27 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,51 @@
//! Visual gizmos to aid with development and debugging in [Bevy](https://bevyengine.org/)
//!
//! # Examples
//!
//! Draw a single gizmo
//! ```
//! # use bevy::prelude::*;
//! # use bevy_mod_gizmos::*;
//! draw_gizmo(Gizmo::sphere(Vec3::new(12.0, 0.0, 8.0), 1.0, Color::BLUE));
//! ```
//!
//! Draw multiple gizmos
//! ```
//! # use bevy::prelude::*;
//! # use bevy_mod_gizmos::*;
//! draw_gizmos(
//! vec![
//! Gizmo::sphere(Vec3::new(12.0, 0.0, 8.0), 1.0, Color::BLUE),
//! Gizmo::cube(Vec3::new(12.0, 0.0, 8.0), 1.0, Color::BLUE),
//! ],
//! true, // True if you want to draw a line between the gizmos
//! );
//! ```
//!
//! Draw a line
//! ```
//! # use bevy::prelude::*;
//! # use bevy_mod_gizmos::*;
//! draw_line(
//! vec![
//! Vec3::new(0.0, 0.0, 0.0),
//! Vec3::new(1.0, 0.0, 0.0),
//! Vec3::new(1.0, 0.0, 1.0),
//! ],
//! Color::BLUE,
//! );
//! ```

use bevy::{
math::Vec3,
pbr::{NotShadowCaster, NotShadowReceiver, PbrBundle, StandardMaterial},
prelude::{
App, Assets, Color, Commands, CoreStage, Entity, ExclusiveSystemDescriptorCoercion, Handle,
IntoExclusiveSystem, Mesh, ParallelSystemDescriptorCoercion, Plugin, ResMut,
App, Assets, Color, Commands, CoreStage, Entity, Handle, Mesh, Plugin, ResMut, Resource,
},
render::mesh::{Indices, PrimitiveTopology},
utils::hashbrown::HashMap,
};
use bevy_mod_picking::{DefaultPickingPlugins, PickableBundle, PickingSystem};
use bevy_mod_picking::{DefaultPickingPlugins, PickableBundle};
use interactions::{interaction_system, INTERACTIONS};
use lazy_static::lazy_static;
use std::sync::RwLock;
Expand All @@ -18,29 +55,20 @@ pub mod gizmo;
pub mod interactions;

pub use basic::*;
pub use interactions::GizmoInteractionCamera;

/// Add this to your bevy [`App`] for gizmos to function
pub struct GizmosPlugin;
impl Plugin for GizmosPlugin {
fn build(&self, app: &mut App) {
app.add_plugins(DefaultPickingPlugins);
app.add_plugin(bevy_mod_picking::DebugCursorPickingPlugin);
app.init_resource::<GizmoEntities>();
app.init_resource::<MaterialHandles>();
app.add_startup_system(gizmo::setup);
app.add_system_to_stage(
CoreStage::First,
interaction_system
.exclusive_system()
.after(PickingSystem::Events),
);
app.add_system_to_stage(
CoreStage::PreUpdate,
cleanup_system.before(PickingSystem::Selection),
);
app.add_system_to_stage(CoreStage::First, interaction_system);
app.add_system_to_stage(CoreStage::PreUpdate, cleanup_system);
app.add_system_to_stage(CoreStage::Update, gizmos_system);
app.add_system_to_stage(CoreStage::Update, lines_system);
// app.add_system(interaction_system.exclusive_system());
}
}

Expand All @@ -66,21 +94,27 @@ fn gizmos_system(
mut gizmo_entities: ResMut<GizmoEntities>,
mut material_handles: ResMut<MaterialHandles>,
) {
// Stops flickering for some off reason
// TODO: Needs a proper solution
std::thread::sleep(std::time::Duration::from_micros(1));

if let Ok(mut gizmo_buffer) = GIZMO_BUFFER.write() {
while let Some(gizmo) = gizmo_buffer.pop() {
let material_handle =
get_material_handle(gizmo.color, &mut material_handles, &mut materials);

let entity = commands
.spawn_bundle(PbrBundle {
transform: gizmo.transform,
mesh: gizmo.mesh_handle,
material: material_handle,
..Default::default()
})
.insert(NotShadowCaster)
.insert(NotShadowReceiver)
.insert_bundle(PickableBundle::default())
.spawn((
PbrBundle {
transform: gizmo.transform,
mesh: gizmo.mesh_handle,
material: material_handle,
..Default::default()
},
NotShadowCaster,
NotShadowReceiver,
PickableBundle::default(),
))
.id();

gizmo_entities.0.push(entity);
Expand Down Expand Up @@ -133,7 +167,7 @@ fn lines_system(
get_material_handle(line.color, &mut material_handles, &mut materials);
gizmo_entities.0.push(
commands
.spawn_bundle(PbrBundle {
.spawn(PbrBundle {
mesh: mesh_handle,
material: material_handle,
..Default::default()
Expand Down Expand Up @@ -197,7 +231,7 @@ fn get_material_handle(
}
}

#[derive(Default)]
#[derive(Default, Resource)]
struct GizmoEntities(Vec<Entity>);
#[derive(Default)]
#[derive(Default, Resource)]
struct MaterialHandles(HashMap<u32, Handle<StandardMaterial>>);

0 comments on commit 7359847

Please sign in to comment.