diff --git a/crates/bevy_openxr/examples/transform_utils.rs b/crates/bevy_openxr/examples/transform_utils.rs new file mode 100644 index 0000000..cb140f0 --- /dev/null +++ b/crates/bevy_openxr/examples/transform_utils.rs @@ -0,0 +1,130 @@ +//! A simple 3D scene with light shining over a cube sitting on a plane. + +use bevy::prelude::*; +use bevy_openxr::add_xr_plugins; +use bevy_xr_utils::xr_utils_actions::{ActiveSet, XRUtilsAction, XRUtilsActionSet, XRUtilsActionState, XRUtilsActionSystemSet, XRUtilsActionsPlugin, XRUtilsBinding}; +use bevy_xr_utils::transform_utils; + +fn main() { + App::new() + .add_plugins(add_xr_plugins(DefaultPlugins)) + .add_plugins(bevy_xr_utils::hand_gizmos::HandGizmosPlugin) + .add_plugins(transform_utils::TransformUtilitiesPlugin) + .add_systems(Startup, setup) + .add_plugins(XRUtilsActionsPlugin) + .add_systems( + Startup, + create_action_entities.before(XRUtilsActionSystemSet::CreateEvents), + ) + .add_systems( + Update, + read_action_with_marker_component.after(XRUtilsActionSystemSet::SyncActionStates), + ) + .run(); +} + +/// set up a simple 3D scene +fn setup( + mut commands: Commands, + mut meshes: ResMut>, + mut materials: ResMut>, +) { + // circular base + commands.spawn(PbrBundle { + mesh: meshes.add(Circle::new(4.0)), + material: materials.add(Color::WHITE), + transform: Transform::from_rotation(Quat::from_rotation_x(-std::f32::consts::FRAC_PI_2)), + ..default() + }); + // red cube + commands.spawn(PbrBundle { + mesh: meshes.add(Cuboid::new(1.0, 1.0, 1.0)), + material: materials.add(Color::rgb_u8(252, 44, 3)), + transform: Transform::from_xyz(4.0, 0.5, 0.0).with_scale(Vec3::splat(0.5)), + ..default() + }); + // red cube + commands.spawn(PbrBundle { + mesh: meshes.add(Cuboid::new(1.0, 1.0, 1.0)), + material: materials.add(Color::rgb_u8(3, 28, 252)), + transform: Transform::from_xyz(-4.0, 0.5, 0.0).with_scale(Vec3::splat(0.5)), + ..default() + }); + // green cube + commands.spawn(PbrBundle { + mesh: meshes.add(Cuboid::new(1.0, 1.0, 1.0)), + material: materials.add(Color::rgb_u8(3, 252, 32)), + transform: Transform::from_xyz(0.0, 0.5, 4.0).with_scale(Vec3::splat(0.5)), + ..default() + }); + // white cube + commands.spawn(PbrBundle { + mesh: meshes.add(Cuboid::new(1.0, 1.0, 1.0)), + material: materials.add(Color::rgb_u8(250, 250, 250)), + transform: Transform::from_xyz(0.0, 0.5, -4.0).with_scale(Vec3::splat(0.5)), + ..default() + }); + // light + commands.spawn(PointLightBundle { + point_light: PointLight { + shadows_enabled: true, + ..default() + }, + transform: Transform::from_xyz(4.0, 8.0, 4.0), + ..default() + }); + commands.spawn(Camera3dBundle { + transform: Transform::from_xyz(-2.5, 4.5, 9.0).looking_at(Vec3::ZERO, Vec3::Y), + ..default() + }); +} + +#[derive(Component)] +pub struct FaceRedAction; + +fn create_action_entities(mut commands: Commands) { + //create a set + let set = commands + .spawn(( + XRUtilsActionSet { + name: "locomotion".into(), + pretty_name: "locomotion set".into(), + priority: u32::MIN, + }, + ActiveSet, //marker to indicate we want this synced + )) + .id(); + //create an action + let action = commands + .spawn(( + XRUtilsAction { + action_name: "face_red".into(), + localized_name: "face_red_localized".into(), + action_type: bevy_xr::actions::ActionType::Bool, + }, + FaceRedAction, //lets try a marker component + )) + .id(); + + //create a binding + let binding = commands + .spawn(XRUtilsBinding { + profile: "/interaction_profiles/valve/index_controller".into(), + binding: "/user/hand/left//input/a/click".into(), + }) + .id(); + + //add action to set, this isnt the best + //TODO look into a better system + commands.entity(action).add_child(binding); + commands.entity(set).add_child(action); +} + +fn read_action_with_marker_component( + mut action_query: Query<&XRUtilsActionState, With>, +) { + //now for the actual checking + for state in action_query.iter_mut() { + info!("action state is: {:?}", state); + } +} diff --git a/crates/bevy_xr_utils/src/lib.rs b/crates/bevy_xr_utils/src/lib.rs index 6a1a780..6db8e46 100644 --- a/crates/bevy_xr_utils/src/lib.rs +++ b/crates/bevy_xr_utils/src/lib.rs @@ -1,3 +1,4 @@ // use bevy::prelude::*; pub mod hand_gizmos; pub mod xr_utils_actions; +pub mod transform_utils; diff --git a/crates/bevy_xr_utils/src/transform_utils.rs b/crates/bevy_xr_utils/src/transform_utils.rs new file mode 100644 index 0000000..1b71e09 --- /dev/null +++ b/crates/bevy_xr_utils/src/transform_utils.rs @@ -0,0 +1,59 @@ +use bevy::prelude::*; +use bevy_openxr::{helper_traits::{ToQuat, ToVec3}, init::OxrTrackingRoot, resources::OxrViews}; + +pub struct TransformUtilitiesPlugin; + +impl Plugin for TransformUtilitiesPlugin { + fn build(&self, app: &mut App) { + app.add_event::(); + app.add_event::(); + app.add_systems(PostUpdate, handle_transform_events); + } +} + +//events +#[derive(Event, Debug)] +pub struct SnapToRotation(pub Quat); + +#[derive(Event, Debug)] +pub struct SnapToPosition(pub Vec3); + +pub fn handle_transform_events( + mut root_query: Query<&mut Transform, With>, + views: ResMut, + mut position_reader: EventReader, + mut rotation_reader: EventReader, +) { + let result = root_query.get_single_mut(); + match result { + Ok(mut root_transform) => { + //rotation first + let view = views.first(); + match view { + Some(view) => { + //get our parameters together + let mut view_translation = view.pose.position.to_vec3(); + let view_quat = view.pose.orientation.to_quat(); + let view_yaw = view_quat.to_euler(EulerRot::XYZ).1; + let science = Quat::from_axis_angle(*root_transform.up(), view_yaw); + let invert_science = science.inverse(); + view_translation.y = 0.0; //we want to do rotations around the same height as the root + let root_local = root_transform.translation; + let view_global = root_transform.rotation.mul_vec3(view_translation) + root_local; + //now set our rotation for every event, this does mean only the last event matters + for snap in rotation_reader.read() { + let rotation_quaternion = snap.0.mul_quat(invert_science); + root_transform.rotate_around(view_global, rotation_quaternion); + } + //position second + for position in position_reader.read() { + root_transform.translation = position.0; + } + + }, + None => debug!("error getting first view"), + } + } + Err(_) => debug!("error getting root transform"), + } +}