Clip the rays instead of shrinking them
This commit is contained in:
@@ -722,6 +722,7 @@ public:
|
||||
unsorted_objects[index].scale = glm::make_vec3(TRS.scale.data());
|
||||
unsorted_objects[index].visible = true;
|
||||
unsorted_objects[index].name = gltf_node.name;
|
||||
std::ranges::fill(unsorted_objects[index].clipping_planes, glm::vec4{0, 0, 0, 1});
|
||||
}
|
||||
|
||||
return unsorted_objects;
|
||||
@@ -940,6 +941,35 @@ node_handle scene_data::find_node(node_handle root, std::string_view name)
|
||||
throw std::runtime_error("Node " + std::string(name) + " not found");
|
||||
}
|
||||
|
||||
std::vector<node_handle> scene_data::find_children(node_handle root)
|
||||
{
|
||||
assert(root.id < scene_nodes.size());
|
||||
assert(root.scene == this);
|
||||
|
||||
std::vector<bool> flag(scene_nodes.size(), false);
|
||||
|
||||
flag[root.id] = true;
|
||||
|
||||
std::vector<node_handle> nodes;
|
||||
|
||||
for (size_t index = root.id; index < scene_nodes.size(); index++)
|
||||
{
|
||||
size_t parent = scene_nodes[index].parent_id;
|
||||
|
||||
if (parent == node::root_id)
|
||||
continue;
|
||||
|
||||
if (!flag[parent])
|
||||
continue;
|
||||
|
||||
nodes.emplace_back(index, this);
|
||||
|
||||
flag[index] = true;
|
||||
}
|
||||
|
||||
return nodes;
|
||||
}
|
||||
|
||||
std::shared_ptr<scene_data::material> scene_data::find_material(std::string_view name)
|
||||
{
|
||||
for (auto & mesh: meshes)
|
||||
|
||||
@@ -175,6 +175,8 @@ struct scene_data
|
||||
std::string name;
|
||||
bool visible;
|
||||
|
||||
std::array<glm::vec4, 4> clipping_planes;
|
||||
|
||||
uint32_t layer_mask = 1;
|
||||
|
||||
std::vector<std::pair<size_t, glm::mat4>> joints; // Node index, inverse bind matrix of each joint
|
||||
@@ -280,6 +282,13 @@ public:
|
||||
return {scene->scene_nodes[id].parent_id, scene};
|
||||
}
|
||||
|
||||
std::vector<node_handle> children()
|
||||
{
|
||||
assert(scene != nullptr);
|
||||
|
||||
return scene->find_children(*this);
|
||||
}
|
||||
|
||||
operator bool() const
|
||||
{
|
||||
return id != scene_data::node::root_id;
|
||||
|
||||
@@ -533,21 +533,16 @@ vk::raii::Pipeline scene_renderer::create_pipeline(const pipeline_info & info)
|
||||
},
|
||||
vk::SpecializationMapEntry{
|
||||
.constantID = 1,
|
||||
.offset = offsetof(pipeline_info, nb_clipping),
|
||||
.size = sizeof(int32_t),
|
||||
},
|
||||
vk::SpecializationMapEntry{
|
||||
.constantID = 2,
|
||||
.offset = offsetof(pipeline_info, dithering),
|
||||
.size = sizeof(VkBool32),
|
||||
},
|
||||
vk::SpecializationMapEntry{
|
||||
.constantID = 3,
|
||||
.constantID = 2,
|
||||
.offset = offsetof(pipeline_info, alpha_cutout),
|
||||
.size = sizeof(VkBool32),
|
||||
},
|
||||
vk::SpecializationMapEntry{
|
||||
.constantID = 4,
|
||||
.constantID = 3,
|
||||
.offset = offsetof(pipeline_info, skinning),
|
||||
.size = sizeof(VkBool32),
|
||||
}};
|
||||
@@ -624,7 +619,6 @@ vk::raii::Pipeline scene_renderer::create_pipeline(const pipeline_info & info)
|
||||
.dstAlphaBlendFactor = vk::BlendFactor::eOneMinusSrcAlpha,
|
||||
.alphaBlendOp = vk::BlendOp::eAdd,
|
||||
.colorWriteMask = vk::ColorComponentFlagBits::eR | vk::ColorComponentFlagBits::eG | vk::ColorComponentFlagBits::eB | vk::ColorComponentFlagBits::eA}},
|
||||
.DynamicState = {},
|
||||
.layout = *pipeline_layout,
|
||||
.renderPass = *renderpass,
|
||||
.subpass = 0,
|
||||
@@ -912,7 +906,7 @@ void scene_renderer::render(scene_data & scene, const std::array<float, 4> & cle
|
||||
|
||||
vk::DeviceSize instance_ubo_offset = resources.uniform_buffer_offset;
|
||||
instance_gpu_data & object_ubo = *reinterpret_cast<instance_gpu_data *>(ubo + resources.uniform_buffer_offset);
|
||||
resources.uniform_buffer_offset += utils::align_up(buffer_alignment, sizeof(frame_gpu_data));
|
||||
resources.uniform_buffer_offset += utils::align_up(buffer_alignment, sizeof(instance_gpu_data));
|
||||
|
||||
vk::DeviceSize joints_ubo_offset = 0;
|
||||
if (!node.joints.empty())
|
||||
@@ -931,6 +925,7 @@ void scene_renderer::render(scene_data & scene, const std::array<float, 4> & cle
|
||||
object_ubo.model = transform;
|
||||
object_ubo.modelview = frame.view * transform;
|
||||
object_ubo.modelviewproj = viewproj * transform;
|
||||
object_ubo.clipping_planes = node.clipping_planes;
|
||||
|
||||
for (scene_data::primitive & primitive: mesh.primitives)
|
||||
{
|
||||
|
||||
@@ -49,7 +49,6 @@ struct pipeline_info
|
||||
|
||||
// Specialization constants data
|
||||
int32_t nb_texcoords = 2;
|
||||
int32_t nb_clipping = 1;
|
||||
VkBool32 dithering = true;
|
||||
VkBool32 alpha_cutout = false;
|
||||
VkBool32 skinning = false;
|
||||
@@ -151,6 +150,7 @@ class scene_renderer
|
||||
glm::mat4 model;
|
||||
glm::mat4 modelview;
|
||||
glm::mat4 modelviewproj;
|
||||
std::array<glm::vec4, 4> clipping_planes;
|
||||
};
|
||||
|
||||
struct per_frame_resources
|
||||
|
||||
@@ -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,
|
||||
|
||||
+16
-13
@@ -20,10 +20,11 @@
|
||||
#version 450
|
||||
|
||||
layout (constant_id = 0) const int nb_texcoords = 2;
|
||||
layout (constant_id = 1) const int nb_clipping = 1;
|
||||
layout (constant_id = 2) const bool dithering = true;
|
||||
layout (constant_id = 3) const bool alpha_cutout = false;
|
||||
layout (constant_id = 4) const bool skinning = false;
|
||||
layout (constant_id = 1) const bool dithering = true;
|
||||
layout (constant_id = 2) const bool alpha_cutout = false;
|
||||
layout (constant_id = 3) const bool skinning = false;
|
||||
|
||||
const int nb_clipping = 4;
|
||||
|
||||
const float fog_min_dist = 20.0;
|
||||
const float fog_max_dist = 35.0;
|
||||
@@ -36,7 +37,6 @@ layout(set = 0, binding = 0) uniform scene_ssbo
|
||||
vec4 light_position;
|
||||
vec4 ambient_color;
|
||||
vec4 light_color;
|
||||
// vec4 clipping_plane[8];
|
||||
} scene;
|
||||
|
||||
layout(set = 0, binding = 1) uniform mesh_ssbo
|
||||
@@ -44,6 +44,7 @@ layout(set = 0, binding = 1) uniform mesh_ssbo
|
||||
mat4 model;
|
||||
mat4 modelview;
|
||||
mat4 modelviewproj;
|
||||
vec4 clipping_plane[nb_clipping];
|
||||
} mesh;
|
||||
|
||||
layout(set = 0, binding = 2) uniform joints_ssbo
|
||||
@@ -105,12 +106,14 @@ layout(location = 0) out vec4 out_color;
|
||||
// Shader code
|
||||
#ifdef VERT_SHADER
|
||||
|
||||
// out float gl_ClipDistance[8];
|
||||
// out float gl_CullDistance[8];
|
||||
out gl_PerVertex
|
||||
{
|
||||
vec4 gl_Position;
|
||||
float gl_ClipDistance[nb_clipping];
|
||||
};
|
||||
|
||||
void main()
|
||||
{
|
||||
|
||||
for(int i = 0; i < nb_texcoords; i++)
|
||||
texcoord[i] = in_texcoord[i];
|
||||
|
||||
@@ -133,11 +136,11 @@ void main()
|
||||
frag_pos = mesh.modelview * vec4(in_position, 1.0);
|
||||
light_pos = scene.view * scene.light_position;
|
||||
|
||||
// for(int i = 0; i < nb_clipping; i++)
|
||||
// {
|
||||
// gl_ClipDistance[i] = dot(scene.clipping_plane[i], frag_pos);
|
||||
// gl_CullDistance[i] = dot(scene.clipping_plane[i], frag_pos);
|
||||
// }
|
||||
for(int i = 0; i < nb_clipping; i++)
|
||||
{
|
||||
gl_ClipDistance[i] = dot(mesh.clipping_plane[i], mesh.model * vec4(in_position, 1.0));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -33,14 +33,12 @@ public:
|
||||
|
||||
std::vector<vk::PipelineShaderStageCreateInfo> Stages;
|
||||
|
||||
vk::PipelineVertexInputStateCreateInfo VertexInputState;
|
||||
std::vector<vk::VertexInputBindingDescription> VertexBindingDescriptions;
|
||||
std::vector<vk::VertexInputAttributeDescription> VertexAttributeDescriptions;
|
||||
|
||||
std::optional<vk::PipelineInputAssemblyStateCreateInfo> InputAssemblyState;
|
||||
std::optional<vk::PipelineTessellationStateCreateInfo> TessellationState;
|
||||
|
||||
vk::PipelineViewportStateCreateInfo ViewportState; // Automatically filled
|
||||
std::vector<vk::Viewport> Viewports = {};
|
||||
std::vector<vk::Rect2D> Scissors = {};
|
||||
|
||||
@@ -51,9 +49,13 @@ public:
|
||||
vk::PipelineColorBlendStateCreateInfo ColorBlendState;
|
||||
std::vector<vk::PipelineColorBlendAttachmentState> ColorBlendAttachments;
|
||||
|
||||
vk::PipelineDynamicStateCreateInfo DynamicState;
|
||||
std::vector<vk::DynamicState> DynamicStates;
|
||||
|
||||
// Automatically filled
|
||||
vk::PipelineVertexInputStateCreateInfo VertexInputState;
|
||||
vk::PipelineViewportStateCreateInfo ViewportState;
|
||||
vk::PipelineDynamicStateCreateInfo DynamicState;
|
||||
|
||||
vk::PipelineLayout layout;
|
||||
vk::RenderPass renderPass = {};
|
||||
uint32_t subpass = {};
|
||||
|
||||
Reference in New Issue
Block a user