Add ray from controller
This commit is contained in:
@@ -20,7 +20,10 @@
|
||||
|
||||
#include "application.h"
|
||||
#include "asset.h"
|
||||
#include "hardware.h"
|
||||
#include <glm/gtc/matrix_access.hpp>
|
||||
#include <glm/gtc/quaternion.hpp>
|
||||
#include <limits>
|
||||
#include <magic_enum.hpp>
|
||||
#include <simdjson.h>
|
||||
#include <spdlog/spdlog.h>
|
||||
@@ -130,7 +133,7 @@ struct json_visual_response
|
||||
};
|
||||
} // namespace
|
||||
|
||||
input_profile::input_profile(const std::filesystem::path & json_profile, scene_loader & loader, scene_data & scene)
|
||||
input_profile::input_profile(const std::filesystem::path & json_profile, scene_loader & loader, scene_data & scene_controllers, scene_data & scene_rays)
|
||||
{
|
||||
std::string json = asset(json_profile);
|
||||
|
||||
@@ -206,7 +209,7 @@ input_profile::input_profile(const std::filesystem::path & json_profile, scene_l
|
||||
|
||||
for (auto && [layout, model]: models)
|
||||
{
|
||||
node_handle root_node = scene.new_node();
|
||||
node_handle root_node = scene_controllers.new_node();
|
||||
root_node->name = layout;
|
||||
|
||||
XrSpace space;
|
||||
@@ -214,12 +217,38 @@ input_profile::input_profile(const std::filesystem::path & json_profile, scene_l
|
||||
space = application::space(xr::spaces::grip_left);
|
||||
else if (layout == "right")
|
||||
space = application::space(xr::spaces::grip_right);
|
||||
else if (layout == "left_aim")
|
||||
space = application::space(xr::spaces::aim_left);
|
||||
else if (layout == "right_aim")
|
||||
space = application::space(xr::spaces::aim_right);
|
||||
else
|
||||
continue;
|
||||
|
||||
model_handles.emplace_back(space, root_node);
|
||||
|
||||
scene.import(std::move(model), root_node);
|
||||
scene_controllers.import(std::move(model), root_node);
|
||||
}
|
||||
|
||||
for (simdjson::dom::key_value_pair layout: simdjson::dom::object(root["layouts"]))
|
||||
{
|
||||
XrSpace space;
|
||||
if (layout.key == "left")
|
||||
space = application::space(xr::spaces::aim_left);
|
||||
else if (layout.key == "right")
|
||||
space = application::space(xr::spaces::aim_right);
|
||||
else
|
||||
continue;
|
||||
|
||||
node_handle root_node = scene_rays.new_node();
|
||||
root_node->name = (std::string)layout.key + "_ray";
|
||||
model_handles.emplace_back(space, root_node);
|
||||
|
||||
scene_rays.import(loader(controller_ray_model_name()), root_node);
|
||||
|
||||
if (layout.key == "left")
|
||||
left_ray = root_node;
|
||||
else if (layout.key == "right")
|
||||
right_ray = root_node;
|
||||
}
|
||||
|
||||
for (auto & json_response: json_responses)
|
||||
@@ -272,7 +301,7 @@ input_profile::input_profile(const std::filesystem::path & json_profile, scene_l
|
||||
}
|
||||
}
|
||||
|
||||
response.target.node = scene.find_node(controller_root_node, json_response.target_node);
|
||||
response.target.node = scene_controllers.find_node(controller_root_node, json_response.target_node);
|
||||
response.target.state = json_response.state;
|
||||
}
|
||||
}
|
||||
@@ -288,17 +317,39 @@ static void apply_visual_response(node_handle node, input_profile::node_state_vi
|
||||
node->visible = value > 0.5;
|
||||
}
|
||||
|
||||
void input_profile::apply(XrSpace world_space, XrTime predicted_display_time, bool hide_left, bool hide_right)
|
||||
static float compute_ray_size(glm::vec3 ray_start, glm::quat ray_orientation, glm::vec4 limit_plane)
|
||||
{
|
||||
glm::vec3 ray_direction = -glm::column(glm::mat3_cast(ray_orientation), 2); // The aim direction is -Z
|
||||
|
||||
// ray_start + distance × ray_direction ∈ limit_plane
|
||||
// => dot(limit_plane, ray_start + distance × ray_direction) = 0
|
||||
// dot(limit_plane, ray_start) + distance × dot(limit_plane, ray_direction) = 0
|
||||
|
||||
float dot1 = glm::dot(limit_plane, glm::vec4(ray_direction, 0));
|
||||
float dot2 = glm::dot(limit_plane, glm::vec4(ray_start, 1));
|
||||
|
||||
// Starting behind the plane
|
||||
if (dot2 < 0)
|
||||
return -1;
|
||||
|
||||
// Pointing away from the plane
|
||||
if (dot1 > 0)
|
||||
return std::numeric_limits<float>::infinity();
|
||||
|
||||
return -dot2 / dot1;
|
||||
}
|
||||
|
||||
void input_profile::apply(XrSpace world_space, XrTime predicted_display_time, bool hide_left, bool hide_right, std::span<glm::vec4> pointer_limits)
|
||||
{
|
||||
for (auto && [space, node]: model_handles)
|
||||
{
|
||||
if (space == application::space(xr::spaces::grip_left) && hide_left)
|
||||
if ((space == application::space(xr::spaces::grip_left) or space == application::space(xr::spaces::aim_left)) and hide_left)
|
||||
{
|
||||
node->visible = false;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (space == application::space(xr::spaces::grip_right) && hide_right)
|
||||
if ((space == application::space(xr::spaces::grip_right) or space == application::space(xr::spaces::aim_right)) and hide_right)
|
||||
{
|
||||
node->visible = false;
|
||||
continue;
|
||||
@@ -316,6 +367,30 @@ void input_profile::apply(XrSpace world_space, XrTime predicted_display_time, bo
|
||||
}
|
||||
}
|
||||
|
||||
if (left_ray)
|
||||
{
|
||||
float size = 0.8; // Maximum ray length
|
||||
for (glm::vec4 plane: pointer_limits)
|
||||
size = std::min(size, compute_ray_size(left_ray->position, left_ray->orientation, plane));
|
||||
|
||||
if (size > 0)
|
||||
left_ray->scale.z = size;
|
||||
else
|
||||
left_ray->visible = false;
|
||||
}
|
||||
|
||||
if (right_ray)
|
||||
{
|
||||
float size = 0.8; // Maximum ray length
|
||||
for (glm::vec4 plane: pointer_limits)
|
||||
size = std::min(size, compute_ray_size(right_ray->position, right_ray->orientation, plane));
|
||||
|
||||
if (size > 0)
|
||||
right_ray->scale.z = size;
|
||||
else
|
||||
right_ray->visible = false;
|
||||
}
|
||||
|
||||
for (auto & response: responses)
|
||||
{
|
||||
assert(response.action);
|
||||
|
||||
@@ -21,6 +21,7 @@
|
||||
#include <filesystem>
|
||||
#include <glm/glm.hpp>
|
||||
#include <render/scene_data.h>
|
||||
#include <span>
|
||||
#include <variant>
|
||||
#include <openxr/openxr.h>
|
||||
|
||||
@@ -59,9 +60,11 @@ struct input_profile
|
||||
|
||||
std::vector<visual_response> responses;
|
||||
std::vector<std::pair<XrSpace, node_handle>> model_handles;
|
||||
node_handle left_ray;
|
||||
node_handle right_ray;
|
||||
|
||||
input_profile(const std::filesystem::path & json_profile, scene_loader & loader, scene_data & scene);
|
||||
input_profile(const std::filesystem::path & json_profile, scene_loader & loader, scene_data & scene_controllers, scene_data & scene_rays);
|
||||
|
||||
// application::poll_actions() must have been called before
|
||||
void apply(XrSpace world_space, XrTime predicted_display_time, bool hide_left, bool hide_right);
|
||||
void apply(XrSpace world_space, XrTime predicted_display_time, bool hide_left, bool hide_right, std::span<glm::vec4> pointer_limits);
|
||||
};
|
||||
|
||||
+34
-3
@@ -623,6 +623,27 @@ template <class... Ts>
|
||||
overloaded(Ts...) -> overloaded<Ts...>;
|
||||
} // namespace
|
||||
|
||||
// Return the vector v such that dot(v, x) > 0 iff x is on the side where the composition layer is visible
|
||||
static glm::vec4 compute_ray_limits(const XrPosef & pose, float margin = 0)
|
||||
{
|
||||
glm::quat q{
|
||||
pose.orientation.w,
|
||||
pose.orientation.x,
|
||||
pose.orientation.y,
|
||||
pose.orientation.z,
|
||||
};
|
||||
|
||||
glm::vec3 p{
|
||||
pose.position.x,
|
||||
pose.position.y,
|
||||
pose.position.z,
|
||||
};
|
||||
|
||||
glm::vec3 normal = glm::column(glm::mat3_cast(q), 2);
|
||||
|
||||
return glm::vec4(normal, -glm::dot(p, normal) - margin);
|
||||
}
|
||||
|
||||
void scenes::lobby::render(const XrFrameState & frame_state)
|
||||
{
|
||||
if (async_session.valid() && async_session.poll() == utils::future_status::ready)
|
||||
@@ -737,8 +758,6 @@ void scenes::lobby::render(const XrFrameState & frame_state)
|
||||
}
|
||||
}
|
||||
|
||||
input->apply(world_space, frame_state.predictedDisplayTime, hide_left_controller, hide_right_controller);
|
||||
|
||||
if (head_position && new_gui_position)
|
||||
{
|
||||
move_gui(head_position->first, *new_gui_position);
|
||||
@@ -746,6 +765,13 @@ void scenes::lobby::render(const XrFrameState & frame_state)
|
||||
|
||||
std::vector<std::pair<int, XrCompositionLayerQuad>> imgui_layers = draw_gui(frame_state.predictedDisplayTime);
|
||||
|
||||
// Get the planes that limit the ray size from the composition layers
|
||||
std::vector<glm::vec4> ray_limits;
|
||||
for (auto & [z_index, layer]: imgui_layers)
|
||||
ray_limits.push_back(compute_ray_limits(layer.pose));
|
||||
|
||||
input->apply(world_space, frame_state.predictedDisplayTime, hide_left_controller, hide_right_controller, ray_limits);
|
||||
|
||||
assert(renderer);
|
||||
renderer->start_frame();
|
||||
|
||||
@@ -970,7 +996,12 @@ void scenes::lobby::on_focused()
|
||||
controllers_scene.emplace();
|
||||
|
||||
scene_data & controllers_scene_data = composition_layer_depth_test_supported ? *lobby_scene : *controllers_scene;
|
||||
input = input_profile("controllers/" + choose_webxr_profile() + "/profile.json", loader, controllers_scene_data);
|
||||
input = input_profile(
|
||||
"controllers/" + choose_webxr_profile() + "/profile.json",
|
||||
loader,
|
||||
controllers_scene_data,
|
||||
*controllers_scene);
|
||||
|
||||
spdlog::info("Loaded input profile {}", input->id);
|
||||
|
||||
if (application::get_hand_tracking_supported())
|
||||
|
||||
Reference in New Issue
Block a user