Clip the rays instead of shrinking them
This commit is contained in:
@@ -23,7 +23,6 @@
|
||||
#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>
|
||||
@@ -319,26 +318,39 @@ static void apply_visual_response(node_handle node, input_profile::node_state_vi
|
||||
node->visible = value > 0.5;
|
||||
}
|
||||
|
||||
static float compute_ray_size(glm::vec3 ray_start, glm::quat ray_orientation, glm::vec4 limit_plane)
|
||||
static void set_clipping_planes(node_handle node, std::span<glm::vec4> clipping_planes)
|
||||
{
|
||||
glm::vec3 ray_direction = -glm::column(glm::mat3_cast(ray_orientation), 2); // The aim direction is -Z
|
||||
if (not node)
|
||||
return;
|
||||
|
||||
// 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
|
||||
// If the ray starts on the wrong side of the GUI, hide it entirely
|
||||
// This assumes the node is a child of the root node
|
||||
for (glm::vec4 & plane: clipping_planes)
|
||||
{
|
||||
if (glm::dot(plane, glm::vec4(node->position, 1)) < 0)
|
||||
{
|
||||
node->visible = false;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
float dot1 = glm::dot(limit_plane, glm::vec4(ray_direction, 0));
|
||||
float dot2 = glm::dot(limit_plane, glm::vec4(ray_start, 1));
|
||||
node->visible = true;
|
||||
|
||||
// Starting behind the plane
|
||||
if (dot2 < 0)
|
||||
return -1;
|
||||
for (auto child_node: node.children())
|
||||
{
|
||||
size_t nb_clipping_planes = std::min(child_node->clipping_planes.size(), clipping_planes.size());
|
||||
|
||||
// Pointing away from the plane
|
||||
if (dot1 > 0)
|
||||
return std::numeric_limits<float>::infinity();
|
||||
auto copy_results = std::ranges::copy_n(
|
||||
clipping_planes.begin(),
|
||||
nb_clipping_planes,
|
||||
child_node->clipping_planes.begin());
|
||||
|
||||
return -dot2 / dot1;
|
||||
// Disable the remaining clipping planes
|
||||
std::ranges::fill_n(
|
||||
copy_results.out,
|
||||
child_node->clipping_planes.size() - nb_clipping_planes,
|
||||
glm::vec4(0, 0, 0, 1));
|
||||
}
|
||||
}
|
||||
|
||||
void input_profile::apply(XrSpace world_space, XrTime predicted_display_time, bool hide_left, bool hide_right, std::span<glm::vec4> pointer_limits)
|
||||
@@ -374,29 +386,8 @@ 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;
|
||||
}
|
||||
set_clipping_planes(left_ray, pointer_limits);
|
||||
set_clipping_planes(right_ray, pointer_limits);
|
||||
|
||||
for (auto & response: responses)
|
||||
{
|
||||
|
||||
@@ -62,6 +62,7 @@ struct input_profile
|
||||
|
||||
std::vector<visual_response> responses;
|
||||
std::vector<std::pair<xr::spaces, node_handle>> model_handles;
|
||||
|
||||
node_handle left_ray;
|
||||
node_handle right_ray;
|
||||
|
||||
|
||||
@@ -770,7 +770,10 @@ void scenes::lobby::render(const XrFrameState & frame_state)
|
||||
// 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));
|
||||
{
|
||||
if (z_index != constants::lobby::zindex_recenter_tip)
|
||||
ray_limits.push_back(compute_ray_limits(layer.pose));
|
||||
}
|
||||
|
||||
input->apply(world_space, frame_state.predictedDisplayTime, hide_left_controller, hide_right_controller, ray_limits);
|
||||
|
||||
|
||||
@@ -483,7 +483,6 @@ void scenes::stream::render(const XrFrameState & frame_state)
|
||||
i.blit_pipeline_layout = vk::raii::PipelineLayout(device, pipeline_layout_info);
|
||||
|
||||
vk::pipeline_builder pipeline_info{
|
||||
.flags = {},
|
||||
.Stages = {{
|
||||
.stage = vk::ShaderStageFlagBits::eVertex,
|
||||
.module = *vertex_shader,
|
||||
@@ -496,15 +495,11 @@ void scenes::stream::render(const XrFrameState & frame_state)
|
||||
.pName = "main",
|
||||
.pSpecializationInfo = &frag_specialization_info,
|
||||
}},
|
||||
.VertexInputState = {.flags = {}},
|
||||
.VertexBindingDescriptions = {},
|
||||
.VertexAttributeDescriptions = {},
|
||||
.InputAssemblyState = {{
|
||||
.topology = vk::PrimitiveTopology::eTriangleStrip,
|
||||
}},
|
||||
.ViewportState = {.flags = {}},
|
||||
.Viewports = {{}},
|
||||
.Scissors = {{}},
|
||||
.RasterizationState = {{
|
||||
.polygonMode = vk::PolygonMode::eFill,
|
||||
.lineWidth = 1,
|
||||
@@ -512,9 +507,7 @@ void scenes::stream::render(const XrFrameState & frame_state)
|
||||
.MultisampleState = {{
|
||||
.rasterizationSamples = vk::SampleCountFlagBits::e1,
|
||||
}},
|
||||
.ColorBlendState = {.flags = {}},
|
||||
.ColorBlendAttachments = {{.colorWriteMask = vk::ColorComponentFlagBits::eR | vk::ColorComponentFlagBits::eG | vk::ColorComponentFlagBits::eB}},
|
||||
.DynamicState = {.flags = {}},
|
||||
.DynamicStates = {vk::DynamicState::eViewport, vk::DynamicState::eScissor},
|
||||
.layout = *i.blit_pipeline_layout,
|
||||
.renderPass = *blit_render_pass,
|
||||
|
||||
@@ -294,17 +294,11 @@ stream_reprojection::stream_reprojection(
|
||||
.pSpecializationInfo = &specialization_info,
|
||||
},
|
||||
},
|
||||
.VertexInputState = {
|
||||
.flags = {},
|
||||
},
|
||||
.VertexBindingDescriptions = {},
|
||||
.VertexAttributeDescriptions = {},
|
||||
.InputAssemblyState = {{
|
||||
.topology = vk::PrimitiveTopology::eTriangleList,
|
||||
}},
|
||||
.ViewportState = {
|
||||
.flags = {},
|
||||
},
|
||||
.Viewports = {{
|
||||
.x = 0,
|
||||
.y = 0,
|
||||
|
||||
Reference in New Issue
Block a user