Add fading between hand interaction profile and hand tracking

This commit is contained in:
Guillaume Meunier
2025-11-15 23:09:23 +01:00
parent 11abd8d9fe
commit 33e8016048
5 changed files with 126 additions and 121 deletions
+98 -89
View File
@@ -556,107 +556,125 @@ std::vector<imgui_context::controller_state> 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<std::optional<ImVec2>, float> index_tip_position{{}, std::numeric_limits<float>::infinity()};
std::pair<std::optional<ImVec2>, float> palm_position{{}, std::numeric_limits<float>::infinity()};
std::pair<std::optional<ImVec2>, float> controller_position{{}, std::numeric_limits<float>::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<std::optional<ImVec2>, 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<float>::infinity();
state.pointer_position = std::nullopt;
return;
}
return {std::nullopt, std::numeric_limits<float>::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<float>::infinity();
state.pointer_position = std::nullopt;
return {std::nullopt, std::numeric_limits<float>::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();
}
}