Add FB hand mesh support with GLB fallback (#934)
Use the XR_FB_HAND_TRACKING_MESH extension to render hand models when supported, and fall back to the bundled GLB hand models otherwise. This fixes incorrect hand model rendering on PFDM MR device, Have tested with PFDM MR V4.5.0 version.
This commit is contained in:
@@ -1202,6 +1202,7 @@ void application::initialize()
|
||||
XR_EXT_EYE_GAZE_INTERACTION_EXTENSION_NAME,
|
||||
XR_EXT_HAND_INTERACTION_EXTENSION_NAME,
|
||||
XR_EXT_HAND_TRACKING_EXTENSION_NAME,
|
||||
XR_FB_HAND_TRACKING_MESH_EXTENSION_NAME,
|
||||
XR_EXT_PALM_POSE_EXTENSION_NAME,
|
||||
XR_EXT_PERFORMANCE_SETTINGS_EXTENSION_NAME,
|
||||
XR_EXT_USER_PRESENCE_EXTENSION_NAME,
|
||||
|
||||
@@ -24,6 +24,7 @@
|
||||
#include "vk/allocation.h"
|
||||
#include <fastgltf/tools.hpp>
|
||||
#include <fastgltf/types.hpp>
|
||||
#include <span>
|
||||
#include <vector>
|
||||
#include <vulkan/vulkan.hpp>
|
||||
|
||||
@@ -90,6 +91,12 @@ public:
|
||||
return add(alignment, data_to_add);
|
||||
}
|
||||
|
||||
size_t add_indices(std::span<const uint16_t> indices)
|
||||
{
|
||||
usage |= vk::BufferUsageFlagBits::eIndexBuffer;
|
||||
return add(4, indices.data(), indices.size_bytes());
|
||||
}
|
||||
|
||||
std::pair<size_t, vk::IndexType> add_indices(const fastgltf::Accessor & accessor)
|
||||
{
|
||||
usage |= vk::BufferUsageFlagBits::eIndexBuffer;
|
||||
|
||||
@@ -25,6 +25,7 @@
|
||||
#include "utils/i18n.h"
|
||||
#include "utils/overloaded.h"
|
||||
#include "utils/ranges.h"
|
||||
#include <cstring>
|
||||
#include <entt/core/fwd.hpp>
|
||||
#include <magic_enum.hpp>
|
||||
#include <spdlog/spdlog.h>
|
||||
@@ -529,6 +530,26 @@ std::pair<entt::entity, components::node &> scene::add_gltf(const std::filesyste
|
||||
return add_gltf(load_gltf(path), layer_mask);
|
||||
}
|
||||
|
||||
std::shared_ptr<renderer::material> scene::create_material(std::function<void(renderer::material &)> init) const
|
||||
{
|
||||
auto material = std::make_shared<renderer::material>(*renderer->get_default_material());
|
||||
if (init)
|
||||
init(*material);
|
||||
|
||||
material->buffer = std::make_shared<buffer_allocation>(
|
||||
device,
|
||||
vk::BufferCreateInfo{
|
||||
.size = sizeof(material->staging),
|
||||
.usage = vk::BufferUsageFlagBits::eUniformBuffer},
|
||||
VmaAllocationCreateInfo{
|
||||
.flags = VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT,
|
||||
.usage = VMA_MEMORY_USAGE_AUTO});
|
||||
material->offset = 0;
|
||||
std::memcpy(material->buffer->map(), &material->staging, sizeof(material->staging));
|
||||
material->buffer->unmap();
|
||||
return material;
|
||||
}
|
||||
|
||||
void scene::remove(entt::entity entity)
|
||||
{
|
||||
std::vector to_be_removed{entity};
|
||||
|
||||
@@ -33,6 +33,7 @@
|
||||
#include <cstdint>
|
||||
#include <entt/entity/registry.hpp>
|
||||
#include <filesystem>
|
||||
#include <functional>
|
||||
#include <span>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
@@ -218,6 +219,8 @@ public:
|
||||
|
||||
entt::registry world;
|
||||
|
||||
std::shared_ptr<renderer::material> create_material(std::function<void(renderer::material &)> init = {}) const;
|
||||
|
||||
std::shared_ptr<entt::registry> load_gltf(const std::filesystem::path & path, std::function<void(float)> progress_cb = {});
|
||||
void unload_gltf(const std::filesystem::path & path);
|
||||
std::pair<entt::entity, components::node &> add_gltf(std::shared_ptr<entt::registry> gltf, uint32_t layer_mask = -1);
|
||||
|
||||
@@ -18,10 +18,21 @@
|
||||
|
||||
#include "hand_model.h"
|
||||
#include "openxr/openxr.h"
|
||||
#include "render/gpu_buffer.h"
|
||||
#include "render/scene_components.h"
|
||||
#include "vk/shader.h"
|
||||
#include <algorithm>
|
||||
#include <cctype>
|
||||
#include <cstddef>
|
||||
#include <cstring>
|
||||
#include <entt/entt.hpp>
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
#include <glm/gtx/quaternion.hpp>
|
||||
#include <magic_enum.hpp>
|
||||
#include <spdlog/spdlog.h>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
|
||||
namespace components
|
||||
{
|
||||
@@ -30,8 +41,175 @@ struct hand_joint
|
||||
XrHandEXT hand;
|
||||
XrHandJointEXT joint;
|
||||
};
|
||||
|
||||
struct hand_mesh_fb_root
|
||||
{
|
||||
XrHandEXT hand;
|
||||
};
|
||||
} // namespace components
|
||||
|
||||
static glm::vec3 to_vec3(const XrVector3f & value)
|
||||
{
|
||||
return {value.x, value.y, value.z};
|
||||
}
|
||||
|
||||
static glm::quat to_quat(const XrQuaternionf & value)
|
||||
{
|
||||
return glm::quat(value.w, value.x, value.y, value.z);
|
||||
}
|
||||
|
||||
static glm::mat4 to_matrix(const XrPosef & pose)
|
||||
{
|
||||
return glm::translate(glm::mat4(1), to_vec3(pose.position)) * glm::mat4_cast(to_quat(pose.orientation));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
static void write_vertex_attribute(std::vector<std::byte> & buffer, size_t stride, size_t offset, size_t index, const T & value)
|
||||
{
|
||||
std::memcpy(buffer.data() + index * stride + offset, &value, sizeof(value));
|
||||
}
|
||||
|
||||
static std::string normalize_hand_mesh_fb_semantic(std::string semantic)
|
||||
{
|
||||
std::transform(semantic.begin(), semantic.end(), semantic.begin(), [](unsigned char c) {
|
||||
return static_cast<char>(std::toupper(c));
|
||||
});
|
||||
|
||||
if (semantic.starts_with("IN_"))
|
||||
semantic = semantic.substr(3);
|
||||
|
||||
return semantic;
|
||||
}
|
||||
|
||||
static renderer::vertex_layout create_hand_mesh_fb_layout(scene & scene)
|
||||
{
|
||||
renderer::vertex_layout layout;
|
||||
auto shader = load_shader(scene.device, "lit_skinned.vert");
|
||||
for (const auto & input: shader->inputs)
|
||||
{
|
||||
std::string semantic = normalize_hand_mesh_fb_semantic(input.name);
|
||||
const uint32_t binding = semantic == "POSITION" || semantic == "JOINTS" || semantic == "WEIGHTS" ? 0 : 1;
|
||||
layout.add_vertex_attribute(semantic, input.format, binding, input.location, input.array_size);
|
||||
}
|
||||
return layout;
|
||||
}
|
||||
|
||||
static const vk::VertexInputBindingDescription & find_hand_mesh_fb_binding(const renderer::vertex_layout & layout, uint32_t binding)
|
||||
{
|
||||
for (const auto & description: layout.bindings)
|
||||
if (description.binding == binding)
|
||||
return description;
|
||||
|
||||
throw std::runtime_error("Missing hand mesh FB vertex binding");
|
||||
}
|
||||
|
||||
static const vk::VertexInputAttributeDescription & find_hand_mesh_fb_attribute(const renderer::vertex_layout & layout, std::string_view semantic)
|
||||
{
|
||||
for (size_t i = 0; i < layout.attribute_names.size(); ++i)
|
||||
if (layout.attribute_names[i] == semantic)
|
||||
return layout.attributes.at(i);
|
||||
|
||||
throw std::runtime_error("Missing hand mesh FB vertex attribute");
|
||||
}
|
||||
|
||||
static std::shared_ptr<renderer::material> create_hand_mesh_fb_material(scene & scene, XrHandEXT hand)
|
||||
{
|
||||
return scene.create_material([hand](renderer::material & material) {
|
||||
material.staging.base_color_factor = {0.5f, 0.5f, 0.5f, 1.0f};
|
||||
material.staging.metallic_factor = 0.0f;
|
||||
material.staging.roughness_factor = 0.5527864f;
|
||||
material.name = hand == XR_HAND_LEFT_EXT ? "left-hand-mesh-material" : "right-hand-mesh-material";
|
||||
material.double_sided = false;
|
||||
material.blend_enable = false;
|
||||
});
|
||||
}
|
||||
|
||||
static std::shared_ptr<renderer::mesh> create_hand_mesh_fb_mesh(scene & scene, const xr::hand_tracker::mesh_data & mesh)
|
||||
{
|
||||
std::vector<uint16_t> indices;
|
||||
indices.reserve(mesh.indices.size());
|
||||
for (int16_t index: mesh.indices)
|
||||
indices.push_back(static_cast<uint16_t>(index));
|
||||
|
||||
auto layout = create_hand_mesh_fb_layout(scene);
|
||||
const auto & binding_0_layout = find_hand_mesh_fb_binding(layout, 0);
|
||||
const auto & binding_1_layout = find_hand_mesh_fb_binding(layout, 1);
|
||||
const size_t position_offset = find_hand_mesh_fb_attribute(layout, "POSITION").offset;
|
||||
const size_t joints_offset = find_hand_mesh_fb_attribute(layout, "JOINTS").offset;
|
||||
const size_t weights_offset = find_hand_mesh_fb_attribute(layout, "WEIGHTS").offset;
|
||||
const size_t normal_offset = find_hand_mesh_fb_attribute(layout, "NORMAL").offset;
|
||||
const size_t tangent_offset = find_hand_mesh_fb_attribute(layout, "TANGENT").offset;
|
||||
const size_t texcoord_0_offset = find_hand_mesh_fb_attribute(layout, "TEXCOORD_0").offset;
|
||||
const size_t texcoord_1_offset = find_hand_mesh_fb_attribute(layout, "TEXCOORD_1").offset;
|
||||
const size_t color_offset = find_hand_mesh_fb_attribute(layout, "COLOR").offset;
|
||||
std::vector<std::byte> binding_0(mesh.vertex_positions.size() * binding_0_layout.stride);
|
||||
std::vector<std::byte> binding_1(mesh.vertex_positions.size() * binding_1_layout.stride);
|
||||
|
||||
glm::vec3 obb_min;
|
||||
glm::vec3 obb_max;
|
||||
for (size_t i = 0; i < mesh.vertex_positions.size(); ++i)
|
||||
{
|
||||
glm::vec3 position = to_vec3(mesh.vertex_positions.at(i));
|
||||
glm::vec3 normal = to_vec3(mesh.vertex_normals.at(i));
|
||||
glm::vec4 tangent{};
|
||||
glm::vec2 texcoord_0{mesh.vertex_uvs.at(i).x, mesh.vertex_uvs.at(i).y};
|
||||
glm::vec2 texcoord_1{};
|
||||
glm::vec4 color{1, 1, 1, 1};
|
||||
glm::vec4 joints{
|
||||
static_cast<float>(mesh.vertex_blend_indices.at(i).x),
|
||||
static_cast<float>(mesh.vertex_blend_indices.at(i).y),
|
||||
static_cast<float>(mesh.vertex_blend_indices.at(i).z),
|
||||
static_cast<float>(mesh.vertex_blend_indices.at(i).w)};
|
||||
glm::vec4 weights{
|
||||
mesh.vertex_blend_weights.at(i).x,
|
||||
mesh.vertex_blend_weights.at(i).y,
|
||||
mesh.vertex_blend_weights.at(i).z,
|
||||
mesh.vertex_blend_weights.at(i).w};
|
||||
|
||||
if (i == 0)
|
||||
obb_min = obb_max = position;
|
||||
else
|
||||
{
|
||||
obb_min.x = std::min(obb_min.x, position.x);
|
||||
obb_min.y = std::min(obb_min.y, position.y);
|
||||
obb_min.z = std::min(obb_min.z, position.z);
|
||||
obb_max.x = std::max(obb_max.x, position.x);
|
||||
obb_max.y = std::max(obb_max.y, position.y);
|
||||
obb_max.z = std::max(obb_max.z, position.z);
|
||||
}
|
||||
|
||||
write_vertex_attribute(binding_0, binding_0_layout.stride, position_offset, i, position);
|
||||
write_vertex_attribute(binding_0, binding_0_layout.stride, joints_offset, i, joints);
|
||||
write_vertex_attribute(binding_0, binding_0_layout.stride, weights_offset, i, weights);
|
||||
write_vertex_attribute(binding_1, binding_1_layout.stride, normal_offset, i, normal);
|
||||
write_vertex_attribute(binding_1, binding_1_layout.stride, tangent_offset, i, tangent);
|
||||
write_vertex_attribute(binding_1, binding_1_layout.stride, texcoord_0_offset, i, texcoord_0);
|
||||
write_vertex_attribute(binding_1, binding_1_layout.stride, texcoord_1_offset, i, texcoord_1);
|
||||
write_vertex_attribute(binding_1, binding_1_layout.stride, color_offset, i, color);
|
||||
}
|
||||
|
||||
fastgltf::Asset asset;
|
||||
gpu_buffer staging_buffer(scene.physical_device, asset);
|
||||
auto hand_mesh_fb_mesh = std::make_shared<renderer::mesh>();
|
||||
auto & primitive = hand_mesh_fb_mesh->primitives.emplace_back();
|
||||
primitive.indexed = true;
|
||||
primitive.index_count = indices.size();
|
||||
primitive.vertex_count = mesh.vertex_positions.size();
|
||||
primitive.index_type = vk::IndexType::eUint16;
|
||||
primitive.index_offset = staging_buffer.add_indices(indices);
|
||||
primitive.vertex_offset.push_back(staging_buffer.add_vertices(binding_0));
|
||||
primitive.vertex_offset.push_back(staging_buffer.add_vertices(binding_1));
|
||||
primitive.layout = std::move(layout);
|
||||
primitive.obb_min = obb_min;
|
||||
primitive.obb_max = obb_max;
|
||||
primitive.vertex_shader = "lit_skinned.vert";
|
||||
primitive.cull_mode = vk::CullModeFlagBits::eBack;
|
||||
primitive.front_face = vk::FrontFace::eCounterClockwise;
|
||||
primitive.topology = vk::PrimitiveTopology::eTriangleList;
|
||||
hand_mesh_fb_mesh->buffer = std::make_shared<buffer_allocation>(staging_buffer.copy_to_gpu(scene.device));
|
||||
return hand_mesh_fb_mesh;
|
||||
}
|
||||
|
||||
static const std::array<std::pair<XrHandJointEXT, std::string_view>, XR_HAND_JOINT_COUNT_EXT - 1 /* No palm in gltf */> joints{{
|
||||
// clang-format off
|
||||
{XR_HAND_JOINT_WRIST_EXT, "wrist"},
|
||||
@@ -82,6 +260,34 @@ void hand_model::add_hand(scene & scene,
|
||||
scene.world.emplace<components::hand_joint>(entity, hand, (XrHandJointEXT)-1);
|
||||
}
|
||||
|
||||
void hand_model::add_hand(scene & scene,
|
||||
XrHandEXT hand,
|
||||
const xr::hand_tracker::mesh_data & mesh,
|
||||
uint32_t layer_mask)
|
||||
{
|
||||
auto material = create_hand_mesh_fb_material(scene, hand);
|
||||
|
||||
entt::entity entity = scene.world.create();
|
||||
auto & node = scene.world.emplace<components::node>(entity);
|
||||
node.name = hand == XR_HAND_LEFT_EXT ? "left-hand-mesh" : "right-hand-mesh";
|
||||
node.mesh = create_hand_mesh_fb_mesh(scene, mesh);
|
||||
for (auto & primitive: node.mesh->primitives)
|
||||
primitive.material_ = material;
|
||||
node.visible = false;
|
||||
node.layer_mask = layer_mask;
|
||||
node.joints.reserve(mesh.joint_bind_poses.size());
|
||||
scene.world.emplace<components::hand_mesh_fb_root>(entity, hand);
|
||||
|
||||
for (size_t i = 0; i < mesh.joint_bind_poses.size(); ++i)
|
||||
{
|
||||
entt::entity joint_entity = scene.world.create();
|
||||
auto & joint_node = scene.world.emplace<components::node>(joint_entity);
|
||||
joint_node.parent = entity;
|
||||
joint_node.name = std::string(magic_enum::enum_name(static_cast<XrHandJointEXT>(i)));
|
||||
node.joints.emplace_back(joint_entity, glm::inverse(to_matrix(mesh.joint_bind_poses[i])));
|
||||
}
|
||||
}
|
||||
|
||||
void hand_model::apply(entt::registry & scene,
|
||||
const std::optional<std::array<xr::hand_tracker::joint, XR_HAND_JOINT_COUNT_EXT>> & left_hand,
|
||||
const std::optional<std::array<xr::hand_tracker::joint, XR_HAND_JOINT_COUNT_EXT>> & right_hand)
|
||||
@@ -127,4 +333,41 @@ void hand_model::apply(entt::registry & scene,
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
for (auto && [entity, hand_root, node]: scene.view<components::hand_mesh_fb_root, components::node>().each())
|
||||
{
|
||||
const auto & joints = hand_root.hand == XR_HAND_LEFT_EXT ? left_hand : right_hand;
|
||||
if (!joints)
|
||||
{
|
||||
node.visible = false;
|
||||
continue;
|
||||
}
|
||||
|
||||
node.visible = true;
|
||||
|
||||
const auto & wrist_pose = (*joints)[XR_HAND_JOINT_WRIST_EXT].first.pose;
|
||||
glm::vec3 wrist_position = to_vec3(wrist_pose.position);
|
||||
glm::quat wrist_orientation = to_quat(wrist_pose.orientation);
|
||||
glm::quat wrist_orientation_inv = glm::conjugate(wrist_orientation);
|
||||
|
||||
node.position = wrist_position;
|
||||
node.orientation = wrist_orientation;
|
||||
|
||||
for (size_t i = 0; i < node.joints.size(); ++i)
|
||||
{
|
||||
auto & joint_node = scene.get<components::node>(node.joints[i].first);
|
||||
if (i == XR_HAND_JOINT_WRIST_EXT)
|
||||
{
|
||||
joint_node.position = {};
|
||||
joint_node.orientation = {1, 0, 0, 0};
|
||||
continue;
|
||||
}
|
||||
|
||||
const auto & pose = (*joints)[i].first.pose;
|
||||
glm::vec3 position = to_vec3(pose.position);
|
||||
glm::quat orientation = to_quat(pose.orientation);
|
||||
joint_node.position = wrist_orientation_inv * (position - wrist_position);
|
||||
joint_node.orientation = glm::normalize(wrist_orientation_inv * orientation);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,6 +31,11 @@ void add_hand(scene & scene,
|
||||
const std::filesystem::path & gltf_path,
|
||||
uint32_t layer_mask);
|
||||
|
||||
void add_hand(scene & scene,
|
||||
XrHandEXT hand,
|
||||
const xr::hand_tracker::mesh_data & mesh,
|
||||
uint32_t layer_mask);
|
||||
|
||||
void apply(entt::registry & scene,
|
||||
const std::optional<std::array<xr::hand_tracker::joint, XR_HAND_JOINT_COUNT_EXT>> & left_hand,
|
||||
const std::optional<std::array<xr::hand_tracker::joint, XR_HAND_JOINT_COUNT_EXT>> & right_hand);
|
||||
|
||||
+19
-2
@@ -1176,8 +1176,25 @@ void scenes::lobby::on_focused()
|
||||
{
|
||||
left_hand = session.create_hand_tracker(XR_HAND_LEFT_EXT);
|
||||
right_hand = session.create_hand_tracker(XR_HAND_RIGHT_EXT);
|
||||
hand_model::add_hand(*this, XR_HAND_LEFT_EXT, "assets://left-hand.glb", layer_controllers);
|
||||
hand_model::add_hand(*this, XR_HAND_RIGHT_EXT, "assets://right-hand.glb", layer_controllers);
|
||||
|
||||
bool using_hand_mesh_fb = false;
|
||||
if (system.hand_mesh_fb_supported())
|
||||
{
|
||||
const auto * left_hand_mesh_fb = left_hand->mesh();
|
||||
const auto * right_hand_mesh_fb = right_hand->mesh();
|
||||
if (left_hand_mesh_fb and right_hand_mesh_fb)
|
||||
{
|
||||
hand_model::add_hand(*this, XR_HAND_LEFT_EXT, *left_hand_mesh_fb, layer_controllers);
|
||||
hand_model::add_hand(*this, XR_HAND_RIGHT_EXT, *right_hand_mesh_fb, layer_controllers);
|
||||
using_hand_mesh_fb = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (!using_hand_mesh_fb)
|
||||
{
|
||||
hand_model::add_hand(*this, XR_HAND_LEFT_EXT, "assets://left-hand.glb", layer_controllers);
|
||||
hand_model::add_hand(*this, XR_HAND_RIGHT_EXT, "assets://right-hand.glb", layer_controllers);
|
||||
}
|
||||
}
|
||||
|
||||
std::vector imgui_inputs{
|
||||
|
||||
@@ -21,7 +21,10 @@
|
||||
#include "xr/check.h"
|
||||
#include "xr/instance.h"
|
||||
#include "xr/session.h"
|
||||
#include "xr/to_string.h"
|
||||
#include <algorithm>
|
||||
#include <cassert>
|
||||
#include <spdlog/spdlog.h>
|
||||
|
||||
xr::hand_tracker::hand_tracker(instance & inst, session & session, const XrHandTrackerCreateInfoEXT & info) :
|
||||
handle(inst.get_proc<PFN_xrDestroyHandTrackerEXT>("xrDestroyHandTrackerEXT"))
|
||||
@@ -29,6 +32,8 @@ xr::hand_tracker::hand_tracker(instance & inst, session & session, const XrHandT
|
||||
auto xrCreateHandTrackerEXT = inst.get_proc<PFN_xrCreateHandTrackerEXT>("xrCreateHandTrackerEXT");
|
||||
assert(xrCreateHandTrackerEXT);
|
||||
xrLocateHandJointsEXT = inst.get_proc<PFN_xrLocateHandJointsEXT>("xrLocateHandJointsEXT");
|
||||
if (inst.has_extension(XR_FB_HAND_TRACKING_MESH_EXTENSION_NAME))
|
||||
xrGetHandMeshFB = inst.get_proc<PFN_xrGetHandMeshFB>("xrGetHandMeshFB");
|
||||
CHECK_XR(xrCreateHandTrackerEXT(session, &info, &id));
|
||||
}
|
||||
|
||||
@@ -87,6 +92,121 @@ std::optional<std::array<xr::hand_tracker::joint, XR_HAND_JOINT_COUNT_EXT>> xr::
|
||||
return joints;
|
||||
}
|
||||
|
||||
const xr::hand_tracker::mesh_data * xr::hand_tracker::mesh()
|
||||
{
|
||||
if (hand_mesh_fb_fetched)
|
||||
return cached_hand_mesh_fb ? &*cached_hand_mesh_fb : nullptr;
|
||||
|
||||
hand_mesh_fb_fetched = true;
|
||||
|
||||
if (!id || !xrGetHandMeshFB)
|
||||
return nullptr;
|
||||
|
||||
XrHandTrackingMeshFB mesh{
|
||||
.type = XR_TYPE_HAND_TRACKING_MESH_FB,
|
||||
};
|
||||
|
||||
XrResult result = xrGetHandMeshFB(id, &mesh);
|
||||
if (XR_FAILED(result))
|
||||
{
|
||||
spdlog::warn("xrGetHandMeshFB count query failed: {}", xr::to_string(result));
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if (mesh.jointCountOutput != XR_HAND_JOINT_COUNT_EXT)
|
||||
{
|
||||
spdlog::warn("Unsupported hand mesh joint count {}", mesh.jointCountOutput);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if (mesh.jointCountOutput > 32)
|
||||
{
|
||||
spdlog::warn("Hand mesh joint count {} exceeds renderer limit", mesh.jointCountOutput);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if (mesh.vertexCountOutput == 0 || mesh.indexCountOutput == 0)
|
||||
{
|
||||
spdlog::warn("Hand mesh data is empty");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
mesh_data data;
|
||||
data.joint_bind_poses.resize(mesh.jointCountOutput);
|
||||
data.joint_radii.resize(mesh.jointCountOutput);
|
||||
data.joint_parents.resize(mesh.jointCountOutput);
|
||||
data.vertex_positions.resize(mesh.vertexCountOutput);
|
||||
data.vertex_normals.resize(mesh.vertexCountOutput);
|
||||
data.vertex_uvs.resize(mesh.vertexCountOutput);
|
||||
data.vertex_blend_indices.resize(mesh.vertexCountOutput);
|
||||
data.vertex_blend_weights.resize(mesh.vertexCountOutput);
|
||||
data.indices.resize(mesh.indexCountOutput);
|
||||
|
||||
mesh.jointCapacityInput = data.joint_bind_poses.size();
|
||||
mesh.jointBindPoses = data.joint_bind_poses.data();
|
||||
mesh.jointRadii = data.joint_radii.data();
|
||||
mesh.jointParents = data.joint_parents.data();
|
||||
mesh.vertexCapacityInput = data.vertex_positions.size();
|
||||
mesh.vertexPositions = data.vertex_positions.data();
|
||||
mesh.vertexNormals = data.vertex_normals.data();
|
||||
mesh.vertexUVs = data.vertex_uvs.data();
|
||||
mesh.vertexBlendIndices = data.vertex_blend_indices.data();
|
||||
mesh.vertexBlendWeights = data.vertex_blend_weights.data();
|
||||
mesh.indexCapacityInput = data.indices.size();
|
||||
mesh.indices = data.indices.data();
|
||||
|
||||
result = xrGetHandMeshFB(id, &mesh);
|
||||
if (XR_FAILED(result))
|
||||
{
|
||||
spdlog::warn("xrGetHandMeshFB data query failed: {}", xr::to_string(result));
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto count_changed = [](const char * what, uint32_t output, size_t expected) {
|
||||
if (output == expected)
|
||||
return false;
|
||||
spdlog::warn("Hand mesh {} count changed from {} to {}", what, expected, output);
|
||||
return true;
|
||||
};
|
||||
|
||||
if (count_changed("joint", mesh.jointCountOutput, data.joint_bind_poses.size()) ||
|
||||
count_changed("vertex", mesh.vertexCountOutput, data.vertex_positions.size()) ||
|
||||
count_changed("index", mesh.indexCountOutput, data.indices.size()))
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if (mesh.indexCountOutput % 3 != 0)
|
||||
{
|
||||
spdlog::warn("Hand mesh index count {} is not a triangle list", mesh.indexCountOutput);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
const int joint_count = static_cast<int>(mesh.jointCountOutput);
|
||||
if (!std::ranges::all_of(data.vertex_blend_indices, [joint_count](const XrVector4sFB & blend_indices) {
|
||||
return 0 <= blend_indices.x && blend_indices.x < joint_count &&
|
||||
0 <= blend_indices.y && blend_indices.y < joint_count &&
|
||||
0 <= blend_indices.z && blend_indices.z < joint_count &&
|
||||
0 <= blend_indices.w && blend_indices.w < joint_count;
|
||||
}))
|
||||
{
|
||||
spdlog::warn("Hand mesh has out-of-range blend indices");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
const int vertex_count = static_cast<int>(mesh.vertexCountOutput);
|
||||
if (!std::ranges::all_of(data.indices, [vertex_count](int16_t index) {
|
||||
return 0 <= index && index < vertex_count;
|
||||
}))
|
||||
{
|
||||
spdlog::warn("Hand mesh has out-of-range indices");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
cached_hand_mesh_fb = std::move(data);
|
||||
return &*cached_hand_mesh_fb;
|
||||
}
|
||||
|
||||
bool xr::hand_tracker::check_flags(const std::array<joint, XR_HAND_JOINT_COUNT_EXT> & joints, XrSpaceLocationFlags position, XrSpaceVelocityFlags velocity)
|
||||
{
|
||||
return std::ranges::all_of(joints, [position, velocity](const auto & joint) {
|
||||
|
||||
@@ -22,6 +22,7 @@
|
||||
#include "utils/handle.h"
|
||||
#include <array>
|
||||
#include <optional>
|
||||
#include <vector>
|
||||
#include <openxr/openxr.h>
|
||||
|
||||
namespace xr
|
||||
@@ -31,14 +32,33 @@ class session;
|
||||
|
||||
class hand_tracker : public utils::handle<XrHandTrackerEXT>
|
||||
{
|
||||
public:
|
||||
using joint = std::pair<XrHandJointLocationEXT, XrHandJointVelocityEXT>;
|
||||
|
||||
struct mesh_data
|
||||
{
|
||||
std::vector<XrPosef> joint_bind_poses;
|
||||
std::vector<float> joint_radii;
|
||||
std::vector<XrHandJointEXT> joint_parents;
|
||||
std::vector<XrVector3f> vertex_positions;
|
||||
std::vector<XrVector3f> vertex_normals;
|
||||
std::vector<XrVector2f> vertex_uvs;
|
||||
std::vector<XrVector4sFB> vertex_blend_indices;
|
||||
std::vector<XrVector4f> vertex_blend_weights;
|
||||
std::vector<int16_t> indices;
|
||||
};
|
||||
|
||||
private:
|
||||
PFN_xrLocateHandJointsEXT xrLocateHandJointsEXT{};
|
||||
PFN_xrGetHandMeshFB xrGetHandMeshFB{};
|
||||
std::optional<mesh_data> cached_hand_mesh_fb;
|
||||
bool hand_mesh_fb_fetched = false;
|
||||
|
||||
public:
|
||||
hand_tracker(instance & inst, session & session, const XrHandTrackerCreateInfoEXT & info);
|
||||
|
||||
using joint = std::pair<XrHandJointLocationEXT, XrHandJointVelocityEXT>;
|
||||
|
||||
std::optional<std::array<joint, XR_HAND_JOINT_COUNT_EXT>> locate(XrSpace space, XrTime time);
|
||||
const mesh_data * mesh();
|
||||
|
||||
static bool check_flags(const std::array<joint, XR_HAND_JOINT_COUNT_EXT> & joints, XrSpaceLocationFlags position, XrSpaceVelocityFlags velocity);
|
||||
};
|
||||
|
||||
@@ -47,6 +47,9 @@ xr::system::system(xr::instance & inst, XrFormFactor formfactor)
|
||||
if (inst.has_extension(XR_EXT_HAND_TRACKING_EXTENSION_NAME))
|
||||
hand_tracking_supported_ = hand_tracking_properties().supportsHandTracking;
|
||||
|
||||
if (hand_tracking_supported_ and inst.has_extension(XR_FB_HAND_TRACKING_MESH_EXTENSION_NAME))
|
||||
hand_mesh_fb_supported_ = true;
|
||||
|
||||
body_tracker = xr::body_tracker_supported(inst, *this);
|
||||
face_tracker = xr::face_tracker_supported(inst, *this);
|
||||
}
|
||||
@@ -275,6 +278,11 @@ bool xr::system::hand_tracking_supported() const
|
||||
return hand_tracking_supported_;
|
||||
}
|
||||
|
||||
bool xr::system::hand_mesh_fb_supported() const
|
||||
{
|
||||
return hand_mesh_fb_supported_;
|
||||
}
|
||||
|
||||
vk::raii::PhysicalDevice xr::system::physical_device(vk::raii::Instance & vulkan) const
|
||||
{
|
||||
auto xrGetVulkanGraphicsDevice2KHR =
|
||||
|
||||
@@ -61,6 +61,7 @@ class system
|
||||
instance * inst = nullptr;
|
||||
XrSystemId id = XR_NULL_SYSTEM_ID;
|
||||
bool hand_tracking_supported_ = false;
|
||||
bool hand_mesh_fb_supported_ = false;
|
||||
passthrough_type passthrough;
|
||||
body_tracker_type body_tracker;
|
||||
face_tracker_type face_tracker;
|
||||
@@ -91,6 +92,7 @@ public:
|
||||
XrSystemBodyTrackingPropertiesBD bd_body_tracking_properties() const;
|
||||
|
||||
bool hand_tracking_supported() const;
|
||||
bool hand_mesh_fb_supported() const;
|
||||
passthrough_type passthrough_supported() const;
|
||||
face_tracker_type face_tracker_supported() const;
|
||||
body_tracker_type body_tracker_supported() const;
|
||||
|
||||
Reference in New Issue
Block a user