Stick the index to the closest imgui window, register click only when the finger is 1cm inside the window

This commit is contained in:
Guillaume Meunier
2025-11-01 20:58:06 +01:00
parent aa3a1808f8
commit c0dc193247
4 changed files with 309 additions and 14 deletions
+5 -4
View File
@@ -26,7 +26,7 @@
namespace constants::gui
{
// Minimum distance between a GUI layer and a fingertip/controller to register a click
constexpr float min_pointer_distance = -0.1;
constexpr float min_pointer_distance = -0.02;
// Font to use (desktop only)
constexpr const char font_name[] = "Noto Sans";
@@ -41,9 +41,10 @@ constexpr float scroll_ratio = 10;
// Threshold on the trigger value to register a click
constexpr float trigger_click_thd = 0.8;
// Thresholds on the distance between the fingertip and GUI layers
constexpr float fingertip_distance_hovering_thd = 0.15;
constexpr float fingertip_distance_touching_thd = 0.0;
// Thresholds on the distance between the fingertip and GUI layers (positive: in front of the GUI, negative: behind the GUI)
constexpr float fingertip_distance_hovering_thd = 0.15; // Max distance to have the cursor hovering
constexpr float fingertip_distance_touching_thd = -0.01; // Max 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
// Minimum scroll value to enable a controller
constexpr float scroll_value_thd = 0.01;
+53 -1
View File
@@ -146,7 +146,7 @@ static bool in_viewport(const imgui_context::viewport & viewport, ImVec2 positio
return true;
}
static bool window_intersects_viewport(ImGuiWindow * window, imgui_context::viewport & viewport)
static bool window_intersects_viewport(ImGuiWindow * window, const imgui_context::viewport & viewport)
{
ImRect w{window->Pos.x, window->Pos.y, window->Pos.x + window->Size.x, window->Pos.y + window->Size.y};
ImRect v(viewport.vp_origin.x, viewport.vp_origin.y, viewport.vp_origin.x + viewport.vp_size.x, viewport.vp_origin.y + viewport.vp_size.y);
@@ -1005,6 +1005,58 @@ std::vector<std::pair<int, XrCompositionLayerQuad>> imgui_context::end_frame()
return quads;
}
std::vector<imgui_context::viewport> imgui_context::windows()
{
std::vector<imgui_context::viewport> w;
ImGuiWindow * modal_popup = ImGui::GetTopMostAndVisiblePopupModal();
for (const imgui_context::viewport & layer: layers_)
{
if (layer.space != xr::spaces::world)
continue;
ImRect v(layer.vp_origin.x, layer.vp_origin.y, layer.vp_origin.x + layer.vp_size.x, layer.vp_origin.y + layer.vp_size.y);
for (ImGuiWindow * window: context->Windows)
{
if (not window->Active or window->Hidden /*or window->ParentWindowInBeginStack != nullptr*/)
continue;
if (modal_popup != nullptr and window != modal_popup and not layer.always_show_cursor)
continue;
if (not window_intersects_viewport(window, layer))
continue;
// The window intersects the viewport: compute the window position in the real world
ImVec2 max = ImMin(v.Max, window->Pos + window->Size);
ImVec2 min = ImMax(v.Min, window->Pos);
ImVec2 center = (min + max) / 2;
w.push_back(viewport{
.space = layer.space,
.position = layer.position +
glm::mat3_cast(layer.orientation) *
glm::vec3(
((center.x - layer.vp_origin.x) / layer.vp_size.x - 0.5) * layer.size.x,
(-(center.y - layer.vp_origin.y) / layer.vp_size.y + 0.5) * layer.size.y,
0),
.orientation = layer.orientation,
.size = {
(max.x - min.x) * layer.size.x / layer.vp_size.x,
(max.y - min.y) * layer.size.y / layer.vp_size.y,
},
.vp_origin = {min.x, min.y},
.vp_size = {max.x - min.x, max.y - min.y},
.always_show_cursor = layer.always_show_cursor,
});
}
}
return w;
}
void imgui_context::vibrate_on_hover()
{
if (ImGui::IsItemHovered())
+2
View File
@@ -216,6 +216,8 @@ public:
return layers_;
}
std::vector<viewport> windows();
viewport & layer(ImVec2 position);
void new_frame(XrTime display_time);
+249 -9
View File
@@ -584,6 +584,240 @@ static glm::vec4 compute_ray_limits(const XrPosef & pose, float margin = 0)
return glm::vec4(normal, -glm::dot(p, normal) - margin);
}
static void stick_finger_to_gui(std::array<xr::hand_tracker::joint, XR_HAND_JOINT_COUNT_EXT> & hand, const std::vector<imgui_context::viewport> & layers)
{
// Move XR_HAND_JOINT_INDEX_{TIP,DISTAL,INTERMEDIATE,PROXIMAL}_EXT so that:
// - Only the X rotation changes for distal and intermediate (relative to the parent bone)
// - Only the X and Y rotation change for the proximal
// - The orientation of the tip is the same as the distal
// - The position stays constant in the frame of the parent bone
// - The position of the tip is on the closest imgui window (considering the tip radius)
// - The rotation on X for the index proximal is limited to [-90, +30]
// - The rotation on Y for the index proximal is limited to [-30, +30]
// - The rotation on X for the index intermediate are equal and limited to [-90, 0]
// Convert from OpenXR types to glm
glm::vec3 tip_x{
hand[XR_HAND_JOINT_INDEX_TIP_EXT].first.pose.position.x,
hand[XR_HAND_JOINT_INDEX_TIP_EXT].first.pose.position.y,
hand[XR_HAND_JOINT_INDEX_TIP_EXT].first.pose.position.z};
// Compute the target position of the tip
std::vector<std::pair<glm::vec3, float>> intersections; // Target position of the finger tip, distance to the GUI plane
for (const imgui_context::viewport & layer: layers)
{
// Ignore layers that are not absolutely positionned
if (layer.space != xr::spaces::world)
continue;
// Compute all vectors in the reference frame of the GUI plane
// The normal of the GUI plane is +Z, the tip points toward -Z
auto M = glm::mat3_cast(layer.orientation); // plane-to-world transform
glm::vec3 ray_start = glm::transpose(M) * (tip_x - layer.position);
// Ignore this layer if the tip is outside the limits
if (std::abs(ray_start.x) > layer.size.x / 2)
continue;
if (std::abs(ray_start.y) > layer.size.y / 2)
continue;
float distance = ray_start.z; // Positive when the point is in front of the plane
// Not near the plane: ignore
if (distance > 0 or distance < constants::gui::fingertip_distance_stick_thd)
continue;
glm::vec3 target_position = tip_x + M * glm::vec3{0, 0, -distance};
intersections.emplace_back(target_position, std::abs(distance));
}
if (intersections.empty())
return;
auto [target_position, distance] = *std::ranges::min_element(intersections, {}, &std::pair<glm::vec3, float>::second);
// Convert from OpenXR to glm
glm::vec3 distal_x{
hand[XR_HAND_JOINT_INDEX_DISTAL_EXT].first.pose.position.x,
hand[XR_HAND_JOINT_INDEX_DISTAL_EXT].first.pose.position.y,
hand[XR_HAND_JOINT_INDEX_DISTAL_EXT].first.pose.position.z};
glm::vec3 intermediate_x{
hand[XR_HAND_JOINT_INDEX_INTERMEDIATE_EXT].first.pose.position.x,
hand[XR_HAND_JOINT_INDEX_INTERMEDIATE_EXT].first.pose.position.y,
hand[XR_HAND_JOINT_INDEX_INTERMEDIATE_EXT].first.pose.position.z};
glm::vec3 proximal_x{
hand[XR_HAND_JOINT_INDEX_PROXIMAL_EXT].first.pose.position.x,
hand[XR_HAND_JOINT_INDEX_PROXIMAL_EXT].first.pose.position.y,
hand[XR_HAND_JOINT_INDEX_PROXIMAL_EXT].first.pose.position.z};
glm::vec3 metacarpal_x{
hand[XR_HAND_JOINT_INDEX_METACARPAL_EXT].first.pose.position.x,
hand[XR_HAND_JOINT_INDEX_METACARPAL_EXT].first.pose.position.y,
hand[XR_HAND_JOINT_INDEX_METACARPAL_EXT].first.pose.position.z};
glm::quat distal_q = glm::quat::wxyz(
hand[XR_HAND_JOINT_INDEX_DISTAL_EXT].first.pose.orientation.w,
hand[XR_HAND_JOINT_INDEX_DISTAL_EXT].first.pose.orientation.x,
hand[XR_HAND_JOINT_INDEX_DISTAL_EXT].first.pose.orientation.y,
hand[XR_HAND_JOINT_INDEX_DISTAL_EXT].first.pose.orientation.z);
glm::quat intermediate_q = glm::quat::wxyz(
hand[XR_HAND_JOINT_INDEX_INTERMEDIATE_EXT].first.pose.orientation.w,
hand[XR_HAND_JOINT_INDEX_INTERMEDIATE_EXT].first.pose.orientation.x,
hand[XR_HAND_JOINT_INDEX_INTERMEDIATE_EXT].first.pose.orientation.y,
hand[XR_HAND_JOINT_INDEX_INTERMEDIATE_EXT].first.pose.orientation.z);
glm::quat proximal_q = glm::quat::wxyz(
hand[XR_HAND_JOINT_INDEX_PROXIMAL_EXT].first.pose.orientation.w,
hand[XR_HAND_JOINT_INDEX_PROXIMAL_EXT].first.pose.orientation.x,
hand[XR_HAND_JOINT_INDEX_PROXIMAL_EXT].first.pose.orientation.y,
hand[XR_HAND_JOINT_INDEX_PROXIMAL_EXT].first.pose.orientation.z);
glm::quat metacarpal_q = glm::quat::wxyz(
hand[XR_HAND_JOINT_INDEX_METACARPAL_EXT].first.pose.orientation.w,
hand[XR_HAND_JOINT_INDEX_METACARPAL_EXT].first.pose.orientation.x,
hand[XR_HAND_JOINT_INDEX_METACARPAL_EXT].first.pose.orientation.y,
hand[XR_HAND_JOINT_INDEX_METACARPAL_EXT].first.pose.orientation.z);
// Get relative positions and orientations
const auto rel_tip_x = glm::rotate(glm::conjugate(distal_q), tip_x - distal_x);
// relative tip orientation is always identity
const auto rel_distal_x = glm::rotate(glm::conjugate(intermediate_q), distal_x - intermediate_x);
const auto rel_distal_q = glm::conjugate(intermediate_q) * distal_q;
const auto rel_distal_θ = glm::eulerAngles(rel_distal_q);
const auto rel_intermediate_x = glm::rotate(glm::conjugate(proximal_q), intermediate_x - proximal_x);
const auto rel_intermediate_q = glm::conjugate(proximal_q) * intermediate_q;
const auto rel_intermediate_θ = glm::eulerAngles(rel_intermediate_q);
const auto rel_proximal_x = glm::rotate(glm::conjugate(metacarpal_q), proximal_x - metacarpal_x);
const auto rel_proximal_q = glm::conjugate(metacarpal_q) * proximal_q;
const auto rel_proximal_θ = glm::eulerAngles(rel_proximal_q);
// Forward kinematics
auto f = [&](float proximal_θx, float proximal_θy, float intermediate_θx, float distal_θx) -> glm::vec3 {
// Compute the absolute tip position
const glm::quat new_rel_proximal_q = glm::quat(glm::vec3(proximal_θx, proximal_θy, rel_proximal_θ.z));
const glm::quat new_rel_intermediate_q = glm::quat(glm::vec3(intermediate_θx, rel_intermediate_θ.y, rel_intermediate_θ.z));
const glm::quat new_rel_distal_q = glm::quat(glm::vec3(distal_θx, rel_distal_θ.y, rel_distal_θ.z));
const glm::quat proximal_q = metacarpal_q * new_rel_proximal_q;
const glm::quat intermediate_q = proximal_q * new_rel_intermediate_q;
const glm::quat distal_q = intermediate_q * new_rel_distal_q;
const glm::vec3 tip_x = metacarpal_x +
glm::rotate(metacarpal_q, rel_proximal_x) +
glm::rotate(proximal_q, rel_intermediate_x) +
glm::rotate(intermediate_q, rel_distal_x) +
glm::rotate(distal_q, rel_tip_x);
return tip_x - target_position;
};
// Inverse kinematics solver for the tip position
float proximal_θx = rel_proximal_θ.x;
float proximal_θy = rel_proximal_θ.y;
float intermediate_θx = rel_intermediate_θ.x;
float distal_θx = rel_distal_θ.x;
glm::vec3 objective = f(proximal_θx, proximal_θy, intermediate_θx, distal_θx);
float eps = 0.01; // 10 milliradian ~ 0.57 deg
int n = 0;
while (glm::length(objective) > 1e-4 and n++ < 1000)
{
glm::vec3 dobj_1 = (f(proximal_θx + eps, proximal_θy, intermediate_θx, distal_θx) - objective) / eps;
glm::vec3 dobj_2 = (f(proximal_θx, proximal_θy + eps, intermediate_θx, distal_θx) - objective) / eps;
glm::vec3 dobj_3 = (f(proximal_θx, proximal_θy, intermediate_θx + eps, distal_θx) - objective) / eps;
glm::vec3 dobj_4 = (f(proximal_θx, proximal_θy, intermediate_θx, distal_θx + eps) - objective) / eps;
glm::mat4x3 jacobian(dobj_1, dobj_2, dobj_3, dobj_4);
glm::mat3x4 jacobian_transpose = glm::transpose(jacobian);
glm::vec4 direction = -glm::normalize(jacobian_transpose * objective);
glm::vec4 delta = 10 * direction * glm::length(objective);
float tmp = (delta.z + delta.w) / 2;
delta.z = delta.w = tmp;
proximal_θx = std::clamp<float>(proximal_θx + delta.x, -M_PI / 2, M_PI / 6);
proximal_θy = std::clamp<float>(proximal_θy + delta.y, -M_PI / 6, M_PI / 6);
intermediate_θx = std::clamp<float>(intermediate_θx + delta.z, -M_PI / 2, 0);
distal_θx = std::clamp<float>(distal_θx + delta.w, -M_PI / 2, 0);
objective = f(proximal_θx, proximal_θy, intermediate_θx, distal_θx);
}
// Fill in the modified bones
const glm::vec3 new_rel_proximal_θ(proximal_θx, proximal_θy, rel_proximal_θ.z);
const glm::vec3 new_rel_intermediate_θ(intermediate_θx, rel_intermediate_θ.y, rel_intermediate_θ.z);
const glm::vec3 new_rel_distal_θ(distal_θx, rel_distal_θ.y, rel_distal_θ.z);
const glm::quat new_rel_proximal_q = glm::quat(new_rel_proximal_θ);
const glm::quat new_rel_intermediate_q = glm::quat(new_rel_intermediate_θ);
const glm::quat new_rel_distal_q = glm::quat(new_rel_distal_θ);
proximal_q = metacarpal_q * new_rel_proximal_q;
intermediate_q = proximal_q * new_rel_intermediate_q;
distal_q = intermediate_q * new_rel_distal_q;
intermediate_x = proximal_x + glm::rotate(proximal_q, rel_intermediate_x);
distal_x = intermediate_x + glm::rotate(intermediate_q, rel_distal_x);
tip_x = distal_x + glm::rotate(distal_q, rel_tip_x);
hand[XR_HAND_JOINT_INDEX_PROXIMAL_EXT].first.pose.orientation = {
proximal_q.x,
proximal_q.y,
proximal_q.z,
proximal_q.w,
};
hand[XR_HAND_JOINT_INDEX_INTERMEDIATE_EXT].first.pose = {
.orientation = {
intermediate_q.x,
intermediate_q.y,
intermediate_q.z,
intermediate_q.w,
},
.position = {
intermediate_x.x,
intermediate_x.y,
intermediate_x.z,
},
};
hand[XR_HAND_JOINT_INDEX_DISTAL_EXT].first.pose = {
.orientation = {
distal_q.x,
distal_q.y,
distal_q.z,
distal_q.w,
},
.position = {
distal_x.x,
distal_x.y,
distal_x.z,
},
};
hand[XR_HAND_JOINT_INDEX_TIP_EXT].first.pose = {
.orientation = {
distal_q.x,
distal_q.y,
distal_q.z,
distal_q.w,
},
.position = {
tip_x.x,
tip_x.y,
tip_x.z,
},
};
// Translate the whole hand if we cannot find an exact solution
for (auto & hand_joint: hand)
{
hand_joint.first.pose.position.x -= objective.x;
hand_joint.first.pose.position.y -= objective.y;
hand_joint.first.pose.position.z -= objective.z;
}
}
void scenes::lobby::render(const XrFrameState & frame_state)
{
if (async_session.valid() && async_session.poll() == utils::future_status::ready)
@@ -651,6 +885,8 @@ void scenes::lobby::render(const XrFrameState & frame_state)
}
session.begin_frame();
renderer->debug_draw_clear();
#if WIVRN_FEATURE_RENDERDOC
renderdoc_begin(*vk_instance);
#endif
@@ -674,17 +910,24 @@ void scenes::lobby::render(const XrFrameState & frame_state)
if (left_hand and right_hand)
{
auto windows = imgui_ctx->windows();
auto left = left_hand->locate(world_space, frame_state.predictedDisplayTime);
if (left and xr::hand_tracker::check_flags(*left, XR_SPACE_LOCATION_POSITION_TRACKED_BIT | XR_SPACE_LOCATION_POSITION_VALID_BIT, 0))
{
stick_finger_to_gui(*left, windows);
hide_left_controller = true;
}
auto right = right_hand->locate(world_space, frame_state.predictedDisplayTime);
if (right and xr::hand_tracker::check_flags(*right, XR_SPACE_LOCATION_POSITION_TRACKED_BIT | XR_SPACE_LOCATION_POSITION_VALID_BIT, 0))
{
stick_finger_to_gui(*right, windows);
hide_right_controller = true;
}
hand_model::apply(world, left, right);
if (left and xr::hand_tracker::check_flags(*left, 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))
hide_right_controller = true;
if (not new_gui_position)
new_gui_position = check_recenter_gesture(xr::spaces::palm_left, left);
@@ -736,7 +979,6 @@ void scenes::lobby::render(const XrFrameState & frame_state)
}
#endif
renderer->debug_draw_clear();
std::vector<std::pair<int, XrCompositionLayerQuad>> imgui_layers = draw_gui(frame_state.predictedDisplayTime);
#if WIVRN_CLIENT_DEBUG_MENU
@@ -763,8 +1005,6 @@ void scenes::lobby::render(const XrFrameState & frame_state)
renderer::animate(world, frame_state.predictedDisplayPeriod * 1.0e-9);
assert(renderer);
world.get<components::node>(lobby_entity).visible = not application::get_config().passthrough_enabled;
render_start(application::get_config().passthrough_enabled, frame_state.predictedDisplayTime);