From 33e8016048e2055b633541bfb43bd6881376e4c1 Mon Sep 17 00:00:00 2001 From: Guillaume Meunier Date: Sat, 15 Nov 2025 21:56:12 +0100 Subject: [PATCH] Add fading between hand interaction profile and hand tracking --- client/constants.h | 3 +- client/render/imgui_impl.cpp | 187 ++++++++++++++++++----------------- client/render/imgui_impl.h | 9 +- client/scenes/lobby.cpp | 46 +++++---- client/scenes/stream.cpp | 2 - 5 files changed, 126 insertions(+), 121 deletions(-) diff --git a/client/constants.h b/client/constants.h index 6d3b93aa..1d39f73d 100644 --- a/client/constants.h +++ b/client/constants.h @@ -43,7 +43,8 @@ constexpr float scroll_ratio = 10; constexpr float trigger_click_thd = 0.8; // Thresholds on the distance between the fingertip and GUI layers (positive: in front of the GUI, negative: behind the GUI) -constexpr float palm_distance_close_thd = 0.20; // Distance to switch between touch and aim interaction +constexpr float palm_distance_close_thd_lo = 0.18; // Distance to switch between touch and aim interaction +constexpr float palm_distance_close_thd_hi = 0.22; // Distance to switch between touch and aim interaction constexpr float fingertip_distance_touching_thd_hi = -0.01; // Distance to register a click constexpr float fingertip_distance_touching_thd_lo = -0.15; // Distance to register a click constexpr float fingertip_distance_stick_thd = -0.02; // Max distance where the fingertip is moved to be on the GUI diff --git a/client/render/imgui_impl.cpp b/client/render/imgui_impl.cpp index bf41d9f9..8cf23016 100644 --- a/client/render/imgui_impl.cpp +++ b/client/render/imgui_impl.cpp @@ -556,107 +556,125 @@ std::vector imgui_context::read_controllers_sta if (not controllers_enabled) return new_states; - aim_interaction = {true, true}; + aim_interaction = {1, 1}; // Get the hand/controller state from OpenXR - - // First hands, so we can set aim_interaction - for (const auto & [controller, state]: std::ranges::zip_view(controllers, new_states)) + for (const auto & [controller, state, current_aim_interaction]: std::ranges::zip_view(controllers, new_states, aim_interaction)) { const auto & ctrl = controller.first; - if (not ctrl.hand) - continue; - state.source = ImGuiMouseSource_VRHandTracking; + std::pair, float> index_tip_position{{}, std::numeric_limits::infinity()}; + std::pair, float> palm_position{{}, std::numeric_limits::infinity()}; + std::pair, float> controller_position{{}, std::numeric_limits::infinity()}; - if (auto joints = ctrl.hand->locate(world, display_time)) - { - XrHandJointLocationEXT & index_tip = (*joints)[XR_HAND_JOINT_INDEX_TIP_EXT].first; - if (index_tip.locationFlags & XR_SPACE_LOCATION_POSITION_TRACKED_BIT) - { - state.aim_position = { - index_tip.pose.position.x, - index_tip.pose.position.y, - index_tip.pose.position.z}; - // aim_orientation is ignored by ray_plane_intersection() for hands - } - compute_pointer_position(state); - - XrHandJointLocationEXT & palm = (*joints)[XR_HAND_JOINT_PALM_EXT].first; - if (palm.locationFlags & XR_SPACE_LOCATION_POSITION_VALID_BIT) - { - controller_state palm_state{ - .aim_position = { - palm.pose.position.x, - palm.pose.position.y, - palm.pose.position.z, - }, - .source = ImGuiMouseSource_VRHandTracking, - }; - compute_pointer_position(palm_state); - if (palm_state.hover_distance < constants::gui::palm_distance_close_thd) - aim_interaction[ctrl.index] = false; - else - state.pointer_position.reset(); - } - - if (state.hover_distance < constants::gui::fingertip_distance_touching_thd_hi and state.hover_distance > constants::gui::fingertip_distance_touching_thd_lo) - state.fingertip_touching = true; - } - } - - // Then controllers - for (const auto & [controller, state]: std::ranges::zip_view(controllers, new_states)) - { - const auto & ctrl = controller.first; + // First hands, so we can set aim_interaction + current_aim_interaction = 1; if (ctrl.hand) - continue; + { + if (auto joints = ctrl.hand->locate(world, display_time)) + { + XrHandJointLocationEXT & index_tip = (*joints)[XR_HAND_JOINT_INDEX_TIP_EXT].first; + XrHandJointLocationEXT & palm = (*joints)[XR_HAND_JOINT_PALM_EXT].first; - state.source = ImGuiMouseSource_VRController; - if (not aim_interaction[ctrl.index]) - continue; + if (index_tip.locationFlags & XR_SPACE_LOCATION_POSITION_VALID_BIT) + { + index_tip_position = compute_pointer_position(controller_state{ + .aim_position = { + index_tip.pose.position.x, + index_tip.pose.position.y, + index_tip.pose.position.z, + }, + // aim_orientation is ignored by ray_plane_intersection() for hands + .source = ImGuiMouseSource_VRHandTracking, + }); + } + if (palm.locationFlags & XR_SPACE_LOCATION_POSITION_VALID_BIT) + { + palm_position = compute_pointer_position(controller_state{ + .aim_position = { + palm.pose.position.x, + palm.pose.position.y, + palm.pose.position.z, + }, + .source = ImGuiMouseSource_VRHandTracking, + }); + } + + if (index_tip_position.second < constants::gui::fingertip_distance_touching_thd_hi and + index_tip_position.second > constants::gui::fingertip_distance_touching_thd_lo) + state.fingertip_touching = true; + + if (palm_position.second < constants::gui::palm_distance_close_thd_lo) + current_aim_interaction = 0; + else if (palm_position.second > constants::gui::palm_distance_close_thd_hi) + current_aim_interaction = 1; + else + current_aim_interaction = + (palm_position.second - constants::gui::palm_distance_close_thd_lo) / + (constants::gui::palm_distance_close_thd_hi - constants::gui::palm_distance_close_thd_lo); + } + } + + // Then controllers if (auto location = application::locate_controller(ctrl.aim, world, display_time)) { - state.aim_position = location->first + glm::mat3_cast(location->second * ctrl.offset.second) * ctrl.offset.first; - state.aim_orientation = location->second * ctrl.offset.second; - compute_pointer_position(state); + controller_position = compute_pointer_position(controller_state{ + .aim_position = location->first + glm::mat3_cast(location->second * ctrl.offset.second) * ctrl.offset.first, + .aim_orientation = location->second * ctrl.offset.second, + .source = ImGuiMouseSource_VRController, + }); - if (ctrl.trigger) + if (current_aim_interaction == 1) { - auto trigger = application::read_action_float(ctrl.trigger).value_or(std::pair{0, 0}); - state.trigger_value = trigger.second; + if (ctrl.trigger) + { + auto trigger = application::read_action_float(ctrl.trigger).value_or(std::pair{0, 0}); + state.trigger_value = trigger.second; - // TODO tunable - /*if (new_state.trigger_value < 0.5) - new_state.trigger_clicked = false; - else */ - if (state.trigger_value > constants::gui::trigger_click_thd) - state.trigger_clicked = true; - } + // TODO tunable + /*if (new_state.trigger_value < 0.5) + new_state.trigger_clicked = false; + else */ + if (state.trigger_value > constants::gui::trigger_click_thd) + state.trigger_clicked = true; + } - if (ctrl.scroll) - { - if (auto act = application::read_action_vec2(ctrl.scroll); act) - state.scroll_value = {-act->second.x * scroll_scale, act->second.y * scroll_scale}; - else - state.scroll_value = {0, 0}; + if (ctrl.scroll) + { + if (auto act = application::read_action_vec2(ctrl.scroll); act) + state.scroll_value = {-act->second.x * scroll_scale, act->second.y * scroll_scale}; + else + state.scroll_value = {0, 0}; + } } } + + if (current_aim_interaction == 0) + state.pointer_position = index_tip_position.first; + else if (current_aim_interaction == 1) + state.pointer_position = controller_position.first; + else + { + assert(index_tip_position.first); + assert(controller_position.first); + state.pointer_position = *index_tip_position.first + (*controller_position.first - *index_tip_position.first) * current_aim_interaction; + } + + if (current_aim_interaction < 1) + state.source = ImGuiMouseSource_VRHandTracking; + else + state.source = ImGuiMouseSource_VRController; } return new_states; } -void imgui_context::compute_pointer_position(imgui_context::controller_state & state) const +std::pair, float> imgui_context::compute_pointer_position(const imgui_context::controller_state & state) const { auto intersections = ray_plane_intersection(state); if (intersections.empty()) - { - state.hover_distance = std::numeric_limits::infinity(); - state.pointer_position = std::nullopt; - return; - } + return {std::nullopt, std::numeric_limits::infinity()}; if (ImGuiWindow * modal_popup = ImGui::GetTopMostAndVisiblePopupModal()) { @@ -671,17 +689,12 @@ void imgui_context::compute_pointer_position(imgui_context::controller_state & s for (auto [position, distance]: intersections) { if (in_viewport(i, position)) - { - state.hover_distance = distance; - state.pointer_position = position; - return; - } + return {position, distance}; } } } - state.hover_distance = std::numeric_limits::infinity(); - state.pointer_position = std::nullopt; + return {std::nullopt, std::numeric_limits::infinity()}; } else { @@ -694,16 +707,12 @@ void imgui_context::compute_pointer_position(imgui_context::controller_state & s continue; if (in_window(window, position)) - { - state.hover_distance = distance; - state.pointer_position = position; - return; - } + return {position, distance}; } } // If the pointer isn't in any window, take the farthest one - std::tie(state.pointer_position, state.hover_distance) = intersections.back(); + return intersections.back(); } } diff --git a/client/render/imgui_impl.h b/client/render/imgui_impl.h index 98086d70..0f96b780 100644 --- a/client/render/imgui_impl.h +++ b/client/render/imgui_impl.h @@ -93,7 +93,6 @@ public: struct controller { - uint8_t index; // left = 0, right = 1 XrSpace aim; std::pair offset; @@ -116,7 +115,7 @@ public: glm::vec2 scroll_value = {0, 0}; std::optional pointer_position; - float hover_distance = 1e10; + // float hover_distance = 1e10; bool squeeze_clicked = false; bool trigger_clicked = false; @@ -184,7 +183,7 @@ private: bool button_pressed = false; bool fingertip_touching = false; - std::array aim_interaction = {true, true}; // left, right + std::array aim_interaction = {1, 1}; // left, right, floating point to fade the cursor position between poking and hand interaction ImGuiID hovered_item = 0; // Hovered item in the current frame, reset at the beginning of the frame ImGuiID hovered_item_prev = 0; // Hovered item at the previous frame @@ -229,7 +228,7 @@ public: } std::vector> ray_plane_intersection(const controller_state & in) const; - void compute_pointer_position(controller_state & state) const; + [[nodiscard]] std::pair, float> compute_pointer_position(const controller_state & state) const; // Convert position from viewport coordinates to real-world glm::vec3 rw_from_vp(const ImVec2 & position); @@ -244,7 +243,7 @@ public: void tooltip(std::string_view text); std::array is_aim_interaction() const { - return aim_interaction; + return {aim_interaction[0] == 1, aim_interaction[1] == 1}; } }; diff --git a/client/scenes/lobby.cpp b/client/scenes/lobby.cpp index 0ced83fe..14148549 100644 --- a/client/scenes/lobby.cpp +++ b/client/scenes/lobby.cpp @@ -546,7 +546,7 @@ std::optional scenes::lobby::check_recenter_action(XrTime predicted_d .aim_orientation = aim->second, }; - imgui_ctx->compute_pointer_position(state); + state.pointer_position = imgui_ctx->compute_pointer_position(state).first; if (state.pointer_position) // TODO: check that the pointer is inside an imgui window { @@ -1202,37 +1202,35 @@ void scenes::lobby::on_focused() recenter_left_action = get_action("recenter_left").first; recenter_right_action = get_action("recenter_right").first; - std::vector imgui_inputs{ - imgui_context::controller{ - .index = 0, - .aim = get_action_space("left_aim"), - .offset = input->offset[xr::spaces::aim_left], - .trigger = get_action("left_trigger").first, - .squeeze = get_action("left_squeeze").first, - .scroll = get_action("left_scroll").first, - .haptic_output = get_action("left_haptic").first, - }, - imgui_context::controller{ - .index = 1, - .aim = get_action_space("right_aim"), - .offset = input->offset[xr::spaces::aim_right], - .trigger = get_action("right_trigger").first, - .squeeze = get_action("right_squeeze").first, - .scroll = get_action("right_scroll").first, - .haptic_output = get_action("right_haptic").first, - }, - }; - if (system.hand_tracking_supported()) { left_hand = session.create_hand_tracker(XR_HAND_LEFT_EXT); right_hand = session.create_hand_tracker(XR_HAND_RIGHT_EXT); hand_model::add_hand(*this, XR_HAND_LEFT_EXT, "assets://left-hand.glb", layer_controllers); hand_model::add_hand(*this, XR_HAND_RIGHT_EXT, "assets://right-hand.glb", layer_controllers); - imgui_inputs.push_back({.index = 0, .hand = &*left_hand}); - imgui_inputs.push_back({.index = 1, .hand = &*right_hand}); } + std::vector imgui_inputs{ + imgui_context::controller{ + .aim = get_action_space("left_aim"), + .offset = input->offset[xr::spaces::aim_left], + .trigger = get_action("left_trigger").first, + .squeeze = get_action("left_squeeze").first, + .scroll = get_action("left_scroll").first, + .haptic_output = get_action("left_haptic").first, + .hand = left_hand ? &*left_hand : nullptr, + }, + imgui_context::controller{ + .aim = get_action_space("right_aim"), + .offset = input->offset[xr::spaces::aim_right], + .trigger = get_action("right_trigger").first, + .squeeze = get_action("right_squeeze").first, + .scroll = get_action("right_scroll").first, + .haptic_output = get_action("right_haptic").first, + .hand = right_hand ? &*right_hand : nullptr, + }, + }; + face_tracker = xr::make_face_tracker(instance, system, session); // 0.4mm / pixel diff --git a/client/scenes/stream.cpp b/client/scenes/stream.cpp index bf9569b9..ccc6be29 100644 --- a/client/scenes/stream.cpp +++ b/client/scenes/stream.cpp @@ -429,7 +429,6 @@ void scenes::stream::on_focused() std::array imgui_inputs{ imgui_context::controller{ - .index = 0, .aim = get_action_space("left_aim"), .offset = input->offset[xr::spaces::aim_left], .trigger = get_action("left_trigger").first, @@ -438,7 +437,6 @@ void scenes::stream::on_focused() .haptic_output = get_action("left_haptic").first, }, imgui_context::controller{ - .index = 1, .aim = get_action_space("right_aim"), .offset = input->offset[xr::spaces::aim_right], .trigger = get_action("right_trigger").first,