Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add(wip): multicam example + documentation #64

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
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
Loading