Add ray trail
This commit is contained in:
+100
-53
@@ -23,6 +23,7 @@
|
||||
#include "render/scene_components.h"
|
||||
#include "utils/mapped_file.h"
|
||||
#include "xr/space.h"
|
||||
#include <entt/entity/fwd.hpp>
|
||||
#include <entt/entt.hpp>
|
||||
#include <glm/gtc/matrix_access.hpp>
|
||||
#include <glm/gtc/quaternion.hpp>
|
||||
@@ -33,6 +34,7 @@
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <variant>
|
||||
#include <openxr/openxr.h>
|
||||
|
||||
namespace components
|
||||
{
|
||||
@@ -62,17 +64,27 @@ struct visual_response
|
||||
|
||||
details::node_state state;
|
||||
};
|
||||
} // namespace components
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
// Component added to nodes bound to an action space
|
||||
struct bound_space
|
||||
{
|
||||
xr::spaces space;
|
||||
XrDuration Δt;
|
||||
};
|
||||
|
||||
// Component added to node clipped by the GUI
|
||||
struct clipped_by_gui
|
||||
{
|
||||
};
|
||||
|
||||
struct emissive_trigger
|
||||
{
|
||||
XrAction action;
|
||||
};
|
||||
} // namespace components
|
||||
|
||||
namespace
|
||||
{
|
||||
// clang-format off
|
||||
const std::unordered_map<std::string, std::string> input_mappings = {
|
||||
{"xr-standard-squeeze" , "/input/squeeze/value" },
|
||||
@@ -171,9 +183,24 @@ struct json_visual_response
|
||||
// OpenXR component path
|
||||
std::string component_subpath;
|
||||
};
|
||||
|
||||
struct ray_extra_shader_data
|
||||
{
|
||||
float emissive_factor;
|
||||
};
|
||||
|
||||
void setup_ray_shader(entt::registry & scene, entt::entity entity, const ray_extra_shader_data & initial_data = {})
|
||||
{
|
||||
auto & node = scene.get<components::node>(entity);
|
||||
|
||||
for (auto & primitive: node.mesh->primitives)
|
||||
primitive.material_->fragment_shader_name = "ray.frag";
|
||||
|
||||
node.set_extra_shader_data(initial_data);
|
||||
}
|
||||
} // namespace
|
||||
|
||||
input_profile::input_profile(scene & scene, const std::filesystem::path & json_profile, uint32_t layer_mask_controller, uint32_t layer_mask_ray)
|
||||
input_profile::input_profile(scene & scene, const std::filesystem::path & json_profile, uint32_t layer_mask_controller, uint32_t layer_mask_ray, XrAction left_trigger, XrAction right_trigger)
|
||||
{
|
||||
utils::mapped_file json{json_profile};
|
||||
|
||||
@@ -266,7 +293,7 @@ input_profile::input_profile(scene & scene, const std::filesystem::path & json_p
|
||||
auto [e, node] = scene.add_gltf(model, layer_mask_controller);
|
||||
node.name = layout;
|
||||
entity = e;
|
||||
scene.world.emplace<bound_space>(e, space);
|
||||
scene.world.emplace<components::bound_space>(e, space, 0);
|
||||
|
||||
spdlog::debug("Created entity {}", layout);
|
||||
}
|
||||
@@ -281,16 +308,32 @@ input_profile::input_profile(scene & scene, const std::filesystem::path & json_p
|
||||
else
|
||||
continue;
|
||||
|
||||
auto && [entity, node] = scene.add_gltf(controller_ray_model_name(), layer_mask_ray);
|
||||
node.name = (std::string)layout.key + "_ray";
|
||||
spdlog::debug("Created entity {}", node.name);
|
||||
auto && [ray_entity, ray_node] = scene.add_gltf(controller_ray_model_name(), layer_mask_ray);
|
||||
|
||||
scene.world.emplace<bound_space>(entity, space);
|
||||
ray_node.name = (std::string)layout.key + "_ray";
|
||||
spdlog::debug("Created entity {}", ray_node.name);
|
||||
|
||||
if (layout.key == "left")
|
||||
left_ray = entity;
|
||||
else if (layout.key == "right")
|
||||
right_ray = entity;
|
||||
scene.world.emplace<components::bound_space>(find_node_by_name(scene.world, "Ray", ray_entity), space, 0);
|
||||
scene.world.emplace<components::clipped_by_gui>(find_node_by_name(scene.world, "Ray", ray_entity));
|
||||
scene.world.emplace<components::emissive_trigger>(find_node_by_name(scene.world, "Ray", ray_entity), layout.key == "left" ? left_trigger : right_trigger);
|
||||
setup_ray_shader(scene.world, find_node_by_name(scene.world, "Ray", ray_entity), {0.1});
|
||||
|
||||
try
|
||||
{
|
||||
// These nodes don't exist in the HTC version
|
||||
scene.world.emplace<components::bound_space>(find_node_by_name(scene.world, "Trail", ray_entity), space, 0);
|
||||
scene.world.emplace<components::bound_space>(find_node_by_name(scene.world, "Trail now", ray_entity), space, 0);
|
||||
scene.world.emplace<components::bound_space>(find_node_by_name(scene.world, "Trail -10ms", ray_entity), space, -10'000'000);
|
||||
scene.world.emplace<components::bound_space>(find_node_by_name(scene.world, "Trail -20ms", ray_entity), space, -20'000'000);
|
||||
scene.world.emplace<components::bound_space>(find_node_by_name(scene.world, "Trail -30ms", ray_entity), space, -30'000'000);
|
||||
scene.world.emplace<components::bound_space>(find_node_by_name(scene.world, "Trail -40ms", ray_entity), space, -40'000'000);
|
||||
scene.world.emplace<components::clipped_by_gui>(find_node_by_name(scene.world, "Trail", ray_entity));
|
||||
scene.world.emplace<components::emissive_trigger>(find_node_by_name(scene.world, "Trail", ray_entity), layout.key == "left" ? left_trigger : right_trigger);
|
||||
setup_ray_shader(scene.world, find_node_by_name(scene.world, "Trail", ray_entity), {0.1});
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
for (auto & json_response: json_responses)
|
||||
@@ -362,41 +405,6 @@ static void apply_visual_response(components::node & node, components::details::
|
||||
node.visible = value > 0.5;
|
||||
}
|
||||
|
||||
static void set_clipping_planes(entt::registry & scene, entt::entity entity, std::span<glm::vec4> clipping_planes)
|
||||
{
|
||||
// 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
|
||||
components::node & node = scene.get<components::node>(entity);
|
||||
for (glm::vec4 & plane: clipping_planes)
|
||||
{
|
||||
if (glm::dot(plane, glm::vec4(node.position, 1)) < 0)
|
||||
{
|
||||
scene.get<components::node>(entity).visible = false;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Apply the clipping planes on direct children of the target entity
|
||||
for (auto && [child_entity, child_node]: scene.view<components::node>().each())
|
||||
{
|
||||
if (child_node.parent == entity)
|
||||
{
|
||||
size_t nb_clipping_planes = std::min(child_node.clipping_planes.size(), clipping_planes.size());
|
||||
|
||||
auto copy_results = std::ranges::copy_n(
|
||||
clipping_planes.begin(),
|
||||
nb_clipping_planes,
|
||||
child_node.clipping_planes.begin());
|
||||
|
||||
// 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(
|
||||
entt::registry & scene,
|
||||
XrSpace world_space,
|
||||
@@ -407,7 +415,7 @@ void input_profile::apply(
|
||||
bool hide_right_ray,
|
||||
std::span<glm::vec4> pointer_limits)
|
||||
{
|
||||
for (auto && [entity, node, space]: scene.view<components::node, bound_space>().each())
|
||||
for (auto && [entity, node, space]: scene.view<components::node, components::bound_space>().each())
|
||||
{
|
||||
if ((space.space == xr::spaces::grip_left and hide_left_controller) or
|
||||
(space.space == xr::spaces::aim_left and hide_left_ray) or
|
||||
@@ -416,7 +424,7 @@ void input_profile::apply(
|
||||
{
|
||||
node.visible = false;
|
||||
}
|
||||
else if (auto location = application::locate_controller(application::space(space.space), world_space, predicted_display_time); location)
|
||||
else if (auto location = application::locate_controller(application::space(space.space), world_space, predicted_display_time + space.Δt); location)
|
||||
{
|
||||
node.visible = true;
|
||||
|
||||
@@ -495,6 +503,45 @@ void input_profile::apply(
|
||||
}
|
||||
}
|
||||
|
||||
set_clipping_planes(scene, left_ray, pointer_limits);
|
||||
set_clipping_planes(scene, right_ray, pointer_limits);
|
||||
for (auto && [entity, node, response]: scene.view<components::node, components::emissive_trigger>().each())
|
||||
{
|
||||
if (not response.action)
|
||||
continue;
|
||||
|
||||
if (auto opt_value = application::read_action_float(response.action))
|
||||
{
|
||||
float value = opt_value->second * 0.9 + 0.1;
|
||||
node.set_extra_shader_data(ray_extra_shader_data{value});
|
||||
}
|
||||
}
|
||||
|
||||
for (auto && [entity, node]: scene.view<components::node, components::clipped_by_gui>().each())
|
||||
{
|
||||
// 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
|
||||
// if (node.joints.empty())
|
||||
// {
|
||||
// for (glm::vec4 & plane: pointer_limits)
|
||||
// {
|
||||
// if (glm::dot(plane, glm::vec4(node.position, 1)) < 0)
|
||||
// {
|
||||
// scene.get<components::node>(entity).visible = false;
|
||||
// return;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
size_t nb_clipping_planes = std::min(node.clipping_planes.size(), pointer_limits.size());
|
||||
|
||||
auto copy_results = std::ranges::copy_n(
|
||||
pointer_limits.begin(),
|
||||
nb_clipping_planes,
|
||||
node.clipping_planes.begin());
|
||||
|
||||
// Disable the remaining clipping planes
|
||||
std::ranges::fill_n(
|
||||
copy_results.out,
|
||||
node.clipping_planes.size() - nb_clipping_planes,
|
||||
glm::vec4(0, 0, 0, 1));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user