diff --git a/client/application.cpp b/client/application.cpp index c1852cfd..6a028e33 100644 --- a/client/application.cpp +++ b/client/application.cpp @@ -1367,6 +1367,7 @@ void application::initialize() XR_EXT_EYE_GAZE_INTERACTION_EXTENSION_NAME, XR_EXT_HAND_INTERACTION_EXTENSION_NAME, XR_EXT_HAND_TRACKING_EXTENSION_NAME, + XR_EXT_HAND_TRACKING_DATA_SOURCE_EXTENSION_NAME, XR_FB_HAND_TRACKING_MESH_EXTENSION_NAME, XR_EXT_PALM_POSE_EXTENSION_NAME, XR_EXT_PERFORMANCE_SETTINGS_EXTENSION_NAME, diff --git a/client/hmd_traits.cpp b/client/hmd_traits.cpp index 449970cd..cb0d04ec 100644 --- a/client/hmd_traits.cpp +++ b/client/hmd_traits.cpp @@ -344,6 +344,7 @@ void hmd_traits::init() if (panel_width_override > 0) break; } + #endif spdlog::info("HMD traits initialized"); diff --git a/client/render/imgui_impl.cpp b/client/render/imgui_impl.cpp index a235f30e..e511c494 100644 --- a/client/render/imgui_impl.cpp +++ b/client/render/imgui_impl.cpp @@ -581,10 +581,10 @@ std::vector imgui_context::read_controllers_sta current_aim_interaction = 1; if (ctrl.hand) { - if (auto joints = ctrl.hand->locate(world, display_time)) + if (auto hand = ctrl.hand->locate(world, display_time); hand and hand->is_input_source()) { - XrHandJointLocationEXT & index_tip = (*joints)[XR_HAND_JOINT_INDEX_TIP_EXT].first; - XrHandJointLocationEXT & palm = (*joints)[XR_HAND_JOINT_PALM_EXT].first; + XrHandJointLocationEXT & index_tip = hand->joints[XR_HAND_JOINT_INDEX_TIP_EXT].first; + XrHandJointLocationEXT & palm = hand->joints[XR_HAND_JOINT_PALM_EXT].first; if (index_tip.locationFlags & XR_SPACE_LOCATION_POSITION_VALID_BIT) { diff --git a/client/scenes/hand_model.cpp b/client/scenes/hand_model.cpp index 1e257dd2..621f7634 100644 --- a/client/scenes/hand_model.cpp +++ b/client/scenes/hand_model.cpp @@ -289,13 +289,13 @@ void hand_model::add_hand(scene & scene, } void hand_model::apply(entt::registry & scene, - const std::optional> & left_hand, - const std::optional> & right_hand) + const xr::hand_tracker::joint_array * left_hand, + const xr::hand_tracker::joint_array * right_hand) { for (auto && [entity, hj, node]: scene.view().each()) { - auto f = [&](const std::optional> & joints) { - if (joints.has_value()) + auto f = [&](const xr::hand_tracker::joint_array * joints) { + if (joints) { node.visible = true; diff --git a/client/scenes/hand_model.h b/client/scenes/hand_model.h index 83824c7c..ba6e638d 100644 --- a/client/scenes/hand_model.h +++ b/client/scenes/hand_model.h @@ -37,6 +37,6 @@ void add_hand(scene & scene, uint32_t layer_mask); void apply(entt::registry & scene, - const std::optional> & left_hand, - const std::optional> & right_hand); + const xr::hand_tracker::joint_array * left_hand, + const xr::hand_tracker::joint_array * right_hand); }; // namespace hand_model diff --git a/client/scenes/lobby.cpp b/client/scenes/lobby.cpp index a2e09f79..931ad157 100644 --- a/client/scenes/lobby.cpp +++ b/client/scenes/lobby.cpp @@ -438,57 +438,54 @@ void scenes::lobby::connect(const configuration::server_data & data) std::optional scenes::lobby::check_recenter_gesture( xr::spaces space, - const std::optional> & joints, + const xr::hand_tracker::joint_array & joints, const std::pair & head_pose) { if (recentering_context and std::get<0>(*recentering_context) != space) return std::nullopt; - if (joints) + const auto & palm = joints[XR_HAND_JOINT_PALM_EXT].first; + const auto palm_y = glm::rotate(glm_cast(palm.pose.orientation), glm::vec3(0, 1, 0)); + + for (XrHandJointEXT index: { + XR_HAND_JOINT_INDEX_PROXIMAL_EXT, + XR_HAND_JOINT_INDEX_INTERMEDIATE_EXT, + XR_HAND_JOINT_INDEX_DISTAL_EXT, + + XR_HAND_JOINT_MIDDLE_PROXIMAL_EXT, + XR_HAND_JOINT_MIDDLE_INTERMEDIATE_EXT, + XR_HAND_JOINT_MIDDLE_DISTAL_EXT, + + XR_HAND_JOINT_RING_PROXIMAL_EXT, + XR_HAND_JOINT_RING_INTERMEDIATE_EXT, + XR_HAND_JOINT_RING_DISTAL_EXT, + + XR_HAND_JOINT_LITTLE_PROXIMAL_EXT, + XR_HAND_JOINT_LITTLE_INTERMEDIATE_EXT, + XR_HAND_JOINT_LITTLE_DISTAL_EXT, + }) { - const auto & palm = (*joints)[XR_HAND_JOINT_PALM_EXT].first; - const auto palm_y = glm::rotate(glm_cast(palm.pose.orientation), glm::vec3(0, 1, 0)); + const auto fingertip_y = glm::rotate(glm_cast(joints[index].first.pose.orientation), glm::vec3(0, 1, 0)); - for (XrHandJointEXT index: { - XR_HAND_JOINT_INDEX_PROXIMAL_EXT, - XR_HAND_JOINT_INDEX_INTERMEDIATE_EXT, - XR_HAND_JOINT_INDEX_DISTAL_EXT, - - XR_HAND_JOINT_MIDDLE_PROXIMAL_EXT, - XR_HAND_JOINT_MIDDLE_INTERMEDIATE_EXT, - XR_HAND_JOINT_MIDDLE_DISTAL_EXT, - - XR_HAND_JOINT_RING_PROXIMAL_EXT, - XR_HAND_JOINT_RING_INTERMEDIATE_EXT, - XR_HAND_JOINT_RING_DISTAL_EXT, - - XR_HAND_JOINT_LITTLE_PROXIMAL_EXT, - XR_HAND_JOINT_LITTLE_INTERMEDIATE_EXT, - XR_HAND_JOINT_LITTLE_DISTAL_EXT, - }) + if (glm::dot(palm_y, fingertip_y) < constants::lobby::recenter_cos_fingertip_angle_max) { - const auto fingertip_y = glm::rotate(glm_cast((*joints)[index].first.pose.orientation), glm::vec3(0, 1, 0)); - - if (glm::dot(palm_y, fingertip_y) < constants::lobby::recenter_cos_fingertip_angle_max) - { - recentering_context.reset(); - return std::nullopt; - } + recentering_context.reset(); + return std::nullopt; } + } - const auto q = glm_cast(palm.pose.orientation); - const auto x = glm_cast(palm.pose.position); + const auto q = glm_cast(palm.pose.orientation); + const auto x = glm_cast(palm.pose.position); - // Make the up vector orthogonal to forward - const glm::vec3 forward = glm::rotate(head_pose.second, glm::vec3{0, 0, -1}); - const glm::vec3 up = glm::normalize(glm::vec3{0, 1, 0} - forward * glm::dot(forward, {0, 1, 0})); + // Make the up vector orthogonal to forward + const glm::vec3 forward = glm::rotate(head_pose.second, glm::vec3{0, 0, -1}); + const glm::vec3 up = glm::normalize(glm::vec3{0, 1, 0} - forward * glm::dot(forward, {0, 1, 0})); - // Y is pointing to the back of the hand - if (glm::dot(q * glm::vec3(0, -1, 0), up) > constants::lobby::recenter_cos_palm_angle_min) - { - recentering_context.emplace(space, glm::vec3{}, 0); - return x + glm::rotate(q, glm::vec3{0, -constants::lobby::recenter_distance_up, -constants::lobby::recenter_distance_front}); - } + // Y is pointing to the back of the hand + if (glm::dot(q * glm::vec3(0, -1, 0), up) > constants::lobby::recenter_cos_palm_angle_min) + { + recentering_context.emplace(space, glm::vec3{}, 0); + return x + glm::rotate(q, glm::vec3{0, -constants::lobby::recenter_distance_up, -constants::lobby::recenter_distance_front}); } recentering_context.reset(); @@ -973,24 +970,26 @@ void scenes::lobby::render(const XrFrameState & frame_state) auto left = left_hand->locate(world_space, frame_state.predictedDisplayTime); if (left) { - stick_finger_to_gui(*left, windows); + if (left->is_input_source()) + stick_finger_to_gui(left->joints, windows); hide_left_controller = true; } auto right = right_hand->locate(world_space, frame_state.predictedDisplayTime); if (right) { - stick_finger_to_gui(*right, windows); + if (right->is_input_source()) + stick_finger_to_gui(right->joints, windows); hide_right_controller = true; } - hand_model::apply(world, left, right); + hand_model::apply(world, left ? &left->joints : nullptr, right ? &right->joints : nullptr); - if (not new_gui_position and head_position) - new_gui_position = check_recenter_gesture(xr::spaces::palm_left, left, *head_position); + if (not new_gui_position and head_position and left and left->is_input_source()) + new_gui_position = check_recenter_gesture(xr::spaces::palm_left, left->joints, *head_position); - if (not new_gui_position and head_position) - new_gui_position = check_recenter_gesture(xr::spaces::palm_right, right, *head_position); + if (not new_gui_position and head_position and right and right->is_input_source()) + new_gui_position = check_recenter_gesture(xr::spaces::palm_right, right->joints, *head_position); } if (head_position and new_gui_position) diff --git a/client/scenes/lobby.h b/client/scenes/lobby.h index 94dbd616..31e900d8 100644 --- a/client/scenes/lobby.h +++ b/client/scenes/lobby.h @@ -252,7 +252,7 @@ class lobby : public scene_impl void connect(const configuration::server_data & data); std::unique_ptr connect_to_session(wivrn_discover::service service, bool manual_connection); - std::optional check_recenter_gesture(xr::spaces space, const std::optional> & joints, const std::pair & head_pose); + std::optional check_recenter_gesture(xr::spaces space, const xr::hand_tracker::joint_array & joints, const std::pair & head_pose); std::optional check_recenter_action(XrTime predicted_display_time, glm::vec3 head_position); std::optional check_recenter_gui(glm::vec3 head_position, glm::quat head_orientation); diff --git a/client/scenes/stream_gui.cpp b/client/scenes/stream_gui.cpp index fc9d6d26..a841153b 100644 --- a/client/scenes/stream_gui.cpp +++ b/client/scenes/stream_gui.cpp @@ -1099,10 +1099,12 @@ void scenes::stream::draw_gui(XrTime predicted_display_time, XrDuration predicte auto left = left_hand->locate(world_space, predicted_display_time); auto right = right_hand->locate(world_space, predicted_display_time); - if (left and xr::hand_tracker::check_flags(*left, XR_SPACE_LOCATION_POSITION_TRACKED_BIT | XR_SPACE_LOCATION_POSITION_VALID_BIT, 0)) + if (left and left->is_input_source() and + xr::hand_tracker::check_flags(left->joints, XR_SPACE_LOCATION_POSITION_TRACKED_BIT | XR_SPACE_LOCATION_POSITION_VALID_BIT, 0)) hide_left_controller = true; - if (right and xr::hand_tracker::check_flags(*right, XR_SPACE_LOCATION_POSITION_TRACKED_BIT | XR_SPACE_LOCATION_POSITION_VALID_BIT, 0)) + if (right and right->is_input_source() and + xr::hand_tracker::check_flags(right->joints, XR_SPACE_LOCATION_POSITION_TRACKED_BIT | XR_SPACE_LOCATION_POSITION_VALID_BIT, 0)) hide_right_controller = true; } diff --git a/client/scenes/stream_tracking.cpp b/client/scenes/stream_tracking.cpp index 743060e3..b74516b4 100644 --- a/client/scenes/stream_tracking.cpp +++ b/client/scenes/stream_tracking.cpp @@ -160,14 +160,14 @@ public: static std::optional> locate_hands(xr::hand_tracker & hand, XrSpace space, XrTime time) { - auto joints = hand.locate(space, time); + auto located = hand.locate(space, time); - if (joints) + if (located and located->is_input_source()) { std::array poses; for (int i = 0; i < XR_HAND_JOINT_COUNT_EXT; i++) { - const auto & joint = (*joints)[i]; + const auto & joint = located->joints[i]; poses[i] = { .position = joint.first.pose.position, .orientation = pack(joint.first.pose.orientation), diff --git a/client/xr/hand_tracker.cpp b/client/xr/hand_tracker.cpp index 343657b4..c015b093 100644 --- a/client/xr/hand_tracker.cpp +++ b/client/xr/hand_tracker.cpp @@ -32,12 +32,36 @@ xr::hand_tracker::hand_tracker(instance & inst, session & session, const XrHandT auto xrCreateHandTrackerEXT = inst.get_proc("xrCreateHandTrackerEXT"); assert(xrCreateHandTrackerEXT); xrLocateHandJointsEXT = inst.get_proc("xrLocateHandJointsEXT"); + hand_tracking_data_source_supported = inst.has_extension(XR_EXT_HAND_TRACKING_DATA_SOURCE_EXTENSION_NAME); if (inst.has_extension(XR_FB_HAND_TRACKING_MESH_EXTENSION_NAME)) xrGetHandMeshFB = inst.get_proc("xrGetHandMeshFB"); - CHECK_XR(xrCreateHandTrackerEXT(session, &info, &id)); + + if (hand_tracking_data_source_supported) + { + // request both sources, callers of locate() decide how to use the results + std::array data_sources = { + XR_HAND_TRACKING_DATA_SOURCE_UNOBSTRUCTED_EXT, + XR_HAND_TRACKING_DATA_SOURCE_CONTROLLER_EXT, + }; + + XrHandTrackingDataSourceInfoEXT data_source_info{ + .type = XR_TYPE_HAND_TRACKING_DATA_SOURCE_INFO_EXT, + .next = info.next, + .requestedDataSourceCount = static_cast(data_sources.size()), + .requestedDataSources = data_sources.data(), + }; + + auto create_info = info; + create_info.next = &data_source_info; + CHECK_XR(xrCreateHandTrackerEXT(session, &create_info, &id)); + } + else + { + CHECK_XR(xrCreateHandTrackerEXT(session, &info, &id)); + } } -std::optional> xr::hand_tracker::locate(XrSpace space, XrTime time) +std::optional xr::hand_tracker::locate(XrSpace space, XrTime time) { if (!id || !xrLocateHandJointsEXT) return std::nullopt; @@ -67,29 +91,45 @@ std::optional> xr:: .jointVelocities = joints_vel.data(), }; + XrHandTrackingDataSourceStateEXT data_source_state{ + .type = XR_TYPE_HAND_TRACKING_DATA_SOURCE_STATE_EXT, + .next = &velocities, + }; + XrHandJointLocationsEXT locations{ .type = XR_TYPE_HAND_JOINT_LOCATIONS_EXT, .next = &velocities, .jointCount = joints_pos.size(), .jointLocations = joints_pos.data(), }; + if (hand_tracking_data_source_supported) + locations.next = &data_source_state; CHECK_XR(xrLocateHandJointsEXT(id, &info, &locations)); if (!locations.isActive) return std::nullopt; + if (hand_tracking_data_source_supported && !data_source_state.isActive) + // returned joints are not valid + return std::nullopt; + // bail if any of the joint is invalid if (std::ranges::any_of(joints_pos, [](const auto & loc) { return loc.locationFlags == 0; })) return std::nullopt; - std::array joints; + joint_array joints; for (int i = 0; i < XR_HAND_JOINT_COUNT_EXT; i++) { joints[i] = {joints_pos[i], joints_vel[i]}; } - return joints; + return located_hand{ + .joints = joints, + .data_source = hand_tracking_data_source_supported + ? std::optional{data_source_state.dataSource} + : std::nullopt, + }; } const xr::hand_tracker::mesh_data * xr::hand_tracker::mesh() @@ -207,7 +247,7 @@ const xr::hand_tracker::mesh_data * xr::hand_tracker::mesh() return &*cached_hand_mesh_fb; } -bool xr::hand_tracker::check_flags(const std::array & joints, XrSpaceLocationFlags position, XrSpaceVelocityFlags velocity) +bool xr::hand_tracker::check_flags(const joint_array & joints, XrSpaceLocationFlags position, XrSpaceVelocityFlags velocity) { return std::ranges::all_of(joints, [position, velocity](const auto & joint) { return (joint.first.locationFlags & position) == position and (joint.second.velocityFlags & velocity) == velocity; diff --git a/client/xr/hand_tracker.h b/client/xr/hand_tracker.h index d079d5c5..2e3a4f3d 100644 --- a/client/xr/hand_tracker.h +++ b/client/xr/hand_tracker.h @@ -22,6 +22,7 @@ #include "utils/handle.h" #include #include +#include #include #include @@ -34,6 +35,20 @@ class hand_tracker : public utils::handle { public: using joint = std::pair; + using joint_array = std::array; + + struct located_hand + { + joint_array joints; + std::optional data_source; + + // should these joints be used for input? + // true if UNOBSTRUCTED or XR_EXT_hand_tracking_data_source not supported + bool is_input_source() const + { + return !data_source or *data_source == XR_HAND_TRACKING_DATA_SOURCE_UNOBSTRUCTED_EXT; + } + }; struct mesh_data { @@ -51,15 +66,16 @@ public: private: PFN_xrLocateHandJointsEXT xrLocateHandJointsEXT{}; PFN_xrGetHandMeshFB xrGetHandMeshFB{}; + bool hand_tracking_data_source_supported = false; std::optional cached_hand_mesh_fb; bool hand_mesh_fb_fetched = false; public: hand_tracker(instance & inst, session & session, const XrHandTrackerCreateInfoEXT & info); - std::optional> locate(XrSpace space, XrTime time); + std::optional locate(XrSpace space, XrTime time); const mesh_data * mesh(); - static bool check_flags(const std::array & joints, XrSpaceLocationFlags position, XrSpaceVelocityFlags velocity); + static bool check_flags(const joint_array & joints, XrSpaceLocationFlags position, XrSpaceVelocityFlags velocity); }; } // namespace xr