201
crates/bevy_openxr/examples/transform_utils.rs
Normal file
201
crates/bevy_openxr/examples/transform_utils.rs
Normal file
@@ -0,0 +1,201 @@
|
|||||||
|
//! A simple example of how to use the transform utils to set the players position and orientation
|
||||||
|
|
||||||
|
use bevy::prelude::*;
|
||||||
|
use bevy_openxr::add_xr_plugins;
|
||||||
|
use bevy_xr_utils::transform_utils::{self, SnapToPosition, SnapToRotation};
|
||||||
|
use bevy_xr_utils::xr_utils_actions::{
|
||||||
|
ActiveSet, XRUtilsAction, XRUtilsActionSet, XRUtilsActionState, XRUtilsActionSystemSet,
|
||||||
|
XRUtilsActionsPlugin, XRUtilsBinding,
|
||||||
|
};
|
||||||
|
|
||||||
|
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,
|
||||||
|
send_look_at_red_cube_event.after(XRUtilsActionSystemSet::SyncActionStates),
|
||||||
|
)
|
||||||
|
.add_systems(
|
||||||
|
Update,
|
||||||
|
send_recenter.after(XRUtilsActionSystemSet::SyncActionStates),
|
||||||
|
)
|
||||||
|
.insert_resource(AmbientLight {
|
||||||
|
color: Default::default(),
|
||||||
|
brightness: 500.0,
|
||||||
|
})
|
||||||
|
.run();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// set up a simple 3D scene
|
||||||
|
fn setup(
|
||||||
|
mut commands: Commands,
|
||||||
|
mut meshes: ResMut<Assets<Mesh>>,
|
||||||
|
mut materials: ResMut<Assets<StandardMaterial>>,
|
||||||
|
) {
|
||||||
|
// 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()
|
||||||
|
});
|
||||||
|
// blue 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()
|
||||||
|
});
|
||||||
|
// black cube
|
||||||
|
commands.spawn(PbrBundle {
|
||||||
|
mesh: meshes.add(Cuboid::new(1.0, 1.0, 1.0)),
|
||||||
|
material: materials.add(Color::rgb_u8(0, 0, 0)),
|
||||||
|
transform: Transform::from_xyz(0.0, 0.1, 0.0).with_scale(Vec3::splat(0.2)),
|
||||||
|
..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);
|
||||||
|
|
||||||
|
let action = commands
|
||||||
|
.spawn((
|
||||||
|
XRUtilsAction {
|
||||||
|
action_name: "center".into(),
|
||||||
|
localized_name: "center_localized".into(),
|
||||||
|
action_type: bevy_xr::actions::ActionType::Bool,
|
||||||
|
},
|
||||||
|
Center, //lets try a marker component
|
||||||
|
))
|
||||||
|
.id();
|
||||||
|
|
||||||
|
//create a binding
|
||||||
|
let binding = commands
|
||||||
|
.spawn(XRUtilsBinding {
|
||||||
|
profile: "/interaction_profiles/valve/index_controller".into(),
|
||||||
|
binding: "/user/hand/right/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 send_look_at_red_cube_event(
|
||||||
|
mut action_query: Query<&XRUtilsActionState, With<FaceRedAction>>,
|
||||||
|
mut event_writer: EventWriter<SnapToRotation>,
|
||||||
|
) {
|
||||||
|
//now for the actual checking
|
||||||
|
for state in action_query.iter_mut() {
|
||||||
|
match state {
|
||||||
|
XRUtilsActionState::Bool(state) => {
|
||||||
|
let send = state.current_state && state.changed_since_last_sync;
|
||||||
|
if send {
|
||||||
|
info!("send facing");
|
||||||
|
let quat = Transform::default()
|
||||||
|
.looking_at(Transform::from_xyz(4.0, 0.0, 0.0).translation, Vec3::Y); //this is a transform facing the red cube from the center of the scene, you should use the HMD posision but I was lazy.
|
||||||
|
event_writer.send(SnapToRotation(quat.rotation));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
XRUtilsActionState::Float(_) => (),
|
||||||
|
XRUtilsActionState::Vector(_) => (),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Component)]
|
||||||
|
pub struct Center;
|
||||||
|
|
||||||
|
fn send_recenter(
|
||||||
|
mut action_query: Query<&XRUtilsActionState, With<Center>>,
|
||||||
|
mut event_writer: EventWriter<SnapToPosition>,
|
||||||
|
) {
|
||||||
|
//now for the actual checking
|
||||||
|
for state in action_query.iter_mut() {
|
||||||
|
match state {
|
||||||
|
XRUtilsActionState::Bool(state) => {
|
||||||
|
let send = state.current_state && state.changed_since_last_sync;
|
||||||
|
if send {
|
||||||
|
let center = Transform::default().translation;
|
||||||
|
|
||||||
|
event_writer.send(SnapToPosition(center));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
XRUtilsActionState::Float(_) => (),
|
||||||
|
XRUtilsActionState::Vector(_) => (),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -14,7 +14,7 @@ impl Plugin for OxrActionBindingPlugin {
|
|||||||
app.add_schedule(Schedule::new(OxrSendActionBindings));
|
app.add_schedule(Schedule::new(OxrSendActionBindings));
|
||||||
app.add_event::<OxrSuggestActionBinding>();
|
app.add_event::<OxrSuggestActionBinding>();
|
||||||
app.add_systems(
|
app.add_systems(
|
||||||
PostUpdate,
|
Update,
|
||||||
run_action_binding_sugestion.run_if(
|
run_action_binding_sugestion.run_if(
|
||||||
|mut session_state: EventReader<OxrSessionStatusEvent>| {
|
|mut session_state: EventReader<OxrSessionStatusEvent>| {
|
||||||
session_state
|
session_state
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
// use bevy::prelude::*;
|
// use bevy::prelude::*;
|
||||||
pub mod hand_gizmos;
|
pub mod hand_gizmos;
|
||||||
pub mod xr_utils_actions;
|
pub mod xr_utils_actions;
|
||||||
|
pub mod transform_utils;
|
||||||
|
|||||||
71
crates/bevy_xr_utils/src/transform_utils.rs
Normal file
71
crates/bevy_xr_utils/src/transform_utils.rs
Normal file
@@ -0,0 +1,71 @@
|
|||||||
|
|
||||||
|
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::<SnapToRotation>();
|
||||||
|
app.add_event::<SnapToPosition>();
|
||||||
|
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<OxrTrackingRoot>>,
|
||||||
|
views: ResMut<OxrViews>,
|
||||||
|
mut position_reader: EventReader<SnapToPosition>,
|
||||||
|
mut rotation_reader: EventReader<SnapToRotation>,
|
||||||
|
) {
|
||||||
|
let result = root_query.get_single_mut();
|
||||||
|
match result {
|
||||||
|
Ok(mut root_transform) => {
|
||||||
|
let view = views.first();
|
||||||
|
match view {
|
||||||
|
Some(view) => {
|
||||||
|
//we want the view translation with a height of zero for a few calculations
|
||||||
|
let mut view_translation = view.pose.position.to_vec3();
|
||||||
|
view_translation.y = 0.0;
|
||||||
|
|
||||||
|
//position
|
||||||
|
for position in position_reader.read() {
|
||||||
|
root_transform.translation = position.0 - root_transform.rotation.mul_vec3(view_translation);
|
||||||
|
}
|
||||||
|
|
||||||
|
//rotation
|
||||||
|
let root_local = root_transform.translation.clone();
|
||||||
|
let hmd_global = root_transform.rotation.mul_vec3(view_translation) + root_local;
|
||||||
|
let view_rot = view.pose.orientation.to_quat();
|
||||||
|
let root_rot = root_transform.rotation;
|
||||||
|
let view_global_rotation = root_rot.mul_quat(view_rot).normalize();
|
||||||
|
let (global_view_yaw, _pitch, _roll) = view_global_rotation.to_euler(bevy::math::EulerRot::YXZ);
|
||||||
|
let up = Vec3::Y;
|
||||||
|
for rotation in rotation_reader.read() {
|
||||||
|
let (target_yaw, _pitch, _roll) =
|
||||||
|
rotation.0.normalize().to_euler(bevy::math::EulerRot::YXZ);
|
||||||
|
let diff_yaw = target_yaw - global_view_yaw;
|
||||||
|
|
||||||
|
//build a rotation quat?
|
||||||
|
let rotation_quat = Quat::from_axis_angle(up, diff_yaw);
|
||||||
|
//apply rotation this works
|
||||||
|
root_transform.rotate_around(hmd_global, rotation_quat);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None => debug!("error getting first view"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(_) => debug!("error getting root transform"),
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -68,22 +68,28 @@ impl Plugin for XRUtilsActionsPlugin {
|
|||||||
Startup,
|
Startup,
|
||||||
create_openxr_events.in_set(XRUtilsActionSystemSet::CreateEvents),
|
create_openxr_events.in_set(XRUtilsActionSystemSet::CreateEvents),
|
||||||
);
|
);
|
||||||
app.add_systems(Update, sync_active_action_sets);
|
app.add_systems(
|
||||||
|
Update,
|
||||||
|
sync_active_action_sets.run_if(resource_exists::<OxrSession>),
|
||||||
|
);
|
||||||
app.add_systems(
|
app.add_systems(
|
||||||
Update,
|
Update,
|
||||||
sync_and_update_action_states_f32
|
sync_and_update_action_states_f32
|
||||||
|
.run_if(resource_exists::<OxrSession>)
|
||||||
.in_set(XRUtilsActionSystemSet::SyncActionStates)
|
.in_set(XRUtilsActionSystemSet::SyncActionStates)
|
||||||
.after(sync_active_action_sets),
|
.after(sync_active_action_sets),
|
||||||
);
|
);
|
||||||
app.add_systems(
|
app.add_systems(
|
||||||
Update,
|
Update,
|
||||||
sync_and_update_action_states_bool
|
sync_and_update_action_states_bool
|
||||||
|
.run_if(resource_exists::<OxrSession>)
|
||||||
.in_set(XRUtilsActionSystemSet::SyncActionStates)
|
.in_set(XRUtilsActionSystemSet::SyncActionStates)
|
||||||
.after(sync_active_action_sets),
|
.after(sync_active_action_sets),
|
||||||
);
|
);
|
||||||
app.add_systems(
|
app.add_systems(
|
||||||
Update,
|
Update,
|
||||||
sync_and_update_action_states_vector
|
sync_and_update_action_states_vector
|
||||||
|
.run_if(resource_exists::<OxrSession>)
|
||||||
.in_set(XRUtilsActionSystemSet::SyncActionStates)
|
.in_set(XRUtilsActionSystemSet::SyncActionStates)
|
||||||
.after(sync_active_action_sets),
|
.after(sync_active_action_sets),
|
||||||
);
|
);
|
||||||
@@ -248,7 +254,7 @@ fn sync_active_action_sets(
|
|||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
let sync = session.sync_actions(&active_sets[..]);
|
let sync = session.sync_actions(&active_sets[..]);
|
||||||
match sync {
|
match sync {
|
||||||
Ok(_) => info!("sync ok"),
|
Ok(_) => (),
|
||||||
Err(_) => error!("sync error"),
|
Err(_) => error!("sync error"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -262,7 +268,6 @@ fn sync_and_update_action_states_f32(
|
|||||||
let state = reference.action.state(&session, Path::NULL);
|
let state = reference.action.state(&session, Path::NULL);
|
||||||
match state {
|
match state {
|
||||||
Ok(s) => {
|
Ok(s) => {
|
||||||
info!("we found a state");
|
|
||||||
let new_state = XRUtilsActionState::Float(ActionStateFloat {
|
let new_state = XRUtilsActionState::Float(ActionStateFloat {
|
||||||
current_state: s.current_state,
|
current_state: s.current_state,
|
||||||
changed_since_last_sync: s.changed_since_last_sync,
|
changed_since_last_sync: s.changed_since_last_sync,
|
||||||
@@ -288,7 +293,6 @@ fn sync_and_update_action_states_bool(
|
|||||||
let state = reference.action.state(&session, Path::NULL);
|
let state = reference.action.state(&session, Path::NULL);
|
||||||
match state {
|
match state {
|
||||||
Ok(s) => {
|
Ok(s) => {
|
||||||
info!("we found a state");
|
|
||||||
let new_state = XRUtilsActionState::Bool(ActionStateBool {
|
let new_state = XRUtilsActionState::Bool(ActionStateBool {
|
||||||
current_state: s.current_state,
|
current_state: s.current_state,
|
||||||
changed_since_last_sync: s.changed_since_last_sync,
|
changed_since_last_sync: s.changed_since_last_sync,
|
||||||
@@ -314,7 +318,6 @@ fn sync_and_update_action_states_vector(
|
|||||||
let state = reference.action.state(&session, Path::NULL);
|
let state = reference.action.state(&session, Path::NULL);
|
||||||
match state {
|
match state {
|
||||||
Ok(s) => {
|
Ok(s) => {
|
||||||
info!("we found a state");
|
|
||||||
let new_state = XRUtilsActionState::Vector(ActionStateVector {
|
let new_state = XRUtilsActionState::Vector(ActionStateVector {
|
||||||
current_state: [s.current_state.x, s.current_state.y],
|
current_state: [s.current_state.x, s.current_state.y],
|
||||||
changed_since_last_sync: s.changed_since_last_sync,
|
changed_since_last_sync: s.changed_since_last_sync,
|
||||||
|
|||||||
Reference in New Issue
Block a user