Skip to content

Commit

Permalink
add(wip): multicam example + documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
ActuallyHappening committed Jun 9, 2024
1 parent 949874a commit b8ff982
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 1 deletion.
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ homepage = "https://github.com/urholaukkarinen/transform-gizmo"
repository = "https://github.com/urholaukkarinen/transform-gizmo"
authors = ["Urho Laukkarinen <[email protected]>"]

[workspace.metadata.scripts]
cranky = "cargo cranky --all-targets --all-features --release -- -D warnings"
fmt = "cargo fmt --all -- --check"

[workspace.dependencies]
transform-gizmo = { version = "0.1.0", path = "crates/transform-gizmo" }
transform-gizmo-egui = { version = "0.1.0", path = "crates/transform-gizmo-egui" }
Expand Down
114 changes: 114 additions & 0 deletions crates/transform-gizmo-bevy/examples/wip-bevy_multicam.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
// TODO: Does not currently work!

//! An example with two cameras in split screen
//! Adapted from the official bevy example:
//! <https://bevyengine.org/examples/3D%20Rendering/split-screen/>
//! See the project root's `examples` directory for more examples
//!
//! NOTE: DOES NOT WORK!
use bevy::prelude::*;
use bevy_render::camera::Viewport;
use bevy_window::WindowResized;
use transform_gizmo_bevy::*;

fn main() {
App::new()
.add_plugins((DefaultPlugins, TransformGizmoPlugin))
.add_systems(Startup, setup)
.add_systems(Update, set_camera_viewports)
.run();
}

#[derive(Component)]
struct LeftCamera;

#[derive(Component)]
struct RightCamera;

fn setup(
mut commands: Commands,
mut materials: ResMut<Assets<StandardMaterial>>,
mut meshes: ResMut<Assets<Mesh>>,
) {
// camera left
commands.spawn((
Camera3dBundle {
transform: Transform::from_translation(Vec3::new(1.0, 3.0, -5.0))
.looking_at(Vec3::ZERO, Vec3::Y),
..default()
},
GizmoCamera,
LeftCamera,
));

// camera right
commands.spawn((
Camera3dBundle {
transform: Transform::from_translation(Vec3::new(-3.0, 2.0, -4.0))
.looking_at(Vec3::ZERO, Vec3::Y),
camera: Camera {
order: 1,
clear_color: ClearColorConfig::None,
..default()
},
..default()
},
GizmoCamera,
RightCamera,
));

// cube
commands.spawn((
PbrBundle {
mesh: meshes.add(Cuboid::new(1.0, 1.0, 1.0)),
material: materials.add(Color::GREEN),
transform: Transform::from_translation(Vec3::new(0.0, 0.0, 0.0)),
..default()
},
GizmoTarget::default(),
));

// light
commands.spawn(PointLightBundle {
point_light: PointLight {
shadows_enabled: true,
..default()
},
transform: Transform::from_xyz(4.0, 8.0, 4.0),
..default()
});
}

fn set_camera_viewports(
windows: Query<&Window>,
mut resize_events: EventReader<WindowResized>,
mut left_camera: Query<&mut Camera, (With<LeftCamera>, Without<RightCamera>)>,
mut right_camera: Query<&mut Camera, With<RightCamera>>,
) {
// We need to dynamically resize the camera's viewports whenever the window size changes
// so then each camera always takes up half the screen.
// A resize_event is sent when the window is first created, allowing us to reuse this system for initial setup.
for resize_event in resize_events.read() {
let window = windows.get(resize_event.window).unwrap();
let mut left_camera = left_camera.single_mut();
left_camera.viewport = Some(Viewport {
physical_position: UVec2::new(0, 0),
physical_size: UVec2::new(
window.resolution.physical_width() / 2,
window.resolution.physical_height(),
),
..default()
});

let mut right_camera = right_camera.single_mut();
right_camera.viewport = Some(Viewport {
physical_position: UVec2::new(window.resolution.physical_width() / 2, 0),
physical_size: UVec2::new(
window.resolution.physical_width() / 2,
window.resolution.physical_height(),
),
..default()
});
}
}
4 changes: 3 additions & 1 deletion crates/transform-gizmo-bevy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
//! .run();
//! ```
//!
//! Add [`GizmoCamera`] component to your Camera entity.
//! Add [`GizmoCamera`] component to your Camera entity (currently only one supported).
//!
//! Add [`GizmoTarget`] component to any of your entities that you would like to manipulate the [`Transform`] of.
//!
Expand Down Expand Up @@ -217,6 +217,8 @@ impl GizmoTarget {
}

/// Marker used to specify which camera to use for gizmos.
/// Currently only allows one camera to have this component, else will
/// generate a warning and skip.
#[derive(Component)]
pub struct GizmoCamera;

Expand Down

0 comments on commit b8ff982

Please sign in to comment.