diff --git a/assets/ray-htc.gltf b/assets/ray-htc.gltf new file mode 100644 index 00000000..a54121d0 --- /dev/null +++ b/assets/ray-htc.gltf @@ -0,0 +1,147 @@ +{ + "asset":{ + "generator":"Khronos glTF Blender I/O v4.2.70", + "version":"2.0" + }, + "scene":0, + "scenes":[ + { + "name":"Scene", + "nodes":[ + 0 + ] + } + ], + "nodes":[ + { + "mesh":0, + "name":"Cylinder", + "rotation":[ + 0.7071068286895752, + 0, + 0, + 0.7071068286895752 + ], + "translation":[ + 0, + 0, + -0.5 + ] + } + ], + "materials":[ + { + "name":"Material.001", + "pbrMetallicRoughness":{ + "baseColorFactor":[0,0,0,0.5] + }, + "emissiveTexture":{ + "index":0 + }, + "emissiveFactor": [ 2.0, 2.0, 2.0 ] + } + ], + "meshes":[ + { + "name":"Cylinder.001", + "primitives":[ + { + "attributes":{ + "POSITION":0, + "NORMAL":1, + "TEXCOORD_0":2 + }, + "indices":3, + "material":0 + } + ] + } + ], + "textures":[ + { + "sampler":0, + "source":0 + } + ], + "images":[ + { + "mimeType":"image/png", + "name":"Untitled", + "uri":"ray.png" + } + ], + "accessors":[ + { + "bufferView":0, + "componentType":5126, + "count":18, + "max":[ + 0.004000000189989805, + 0.5, + 0.004000000189989805 + ], + "min":[ + -0.004000000189989805, + -0.5, + -0.004000000189989805 + ], + "type":"VEC3" + }, + { + "bufferView":1, + "componentType":5126, + "count":18, + "type":"VEC3" + }, + { + "bufferView":2, + "componentType":5126, + "count":18, + "type":"VEC2" + }, + { + "bufferView":3, + "componentType":5123, + "count":48, + "type":"SCALAR" + } + ], + "bufferViews":[ + { + "buffer":0, + "byteLength":216, + "byteOffset":0, + "target":34962 + }, + { + "buffer":0, + "byteLength":216, + "byteOffset":216, + "target":34962 + }, + { + "buffer":0, + "byteLength":144, + "byteOffset":432, + "target":34962 + }, + { + "buffer":0, + "byteLength":96, + "byteOffset":576, + "target":34963 + } + ], + "samplers":[ + { + "magFilter":9729, + "minFilter":9987 + } + ], + "buffers":[ + { + "byteLength":672, + "uri":"ray.bin" + } + ] +} diff --git a/assets/ray.bin b/assets/ray.bin new file mode 100644 index 00000000..d01d9678 Binary files /dev/null and b/assets/ray.bin differ diff --git a/assets/ray.gltf b/assets/ray.gltf new file mode 100644 index 00000000..26e5272b --- /dev/null +++ b/assets/ray.gltf @@ -0,0 +1,147 @@ +{ + "asset":{ + "generator":"Khronos glTF Blender I/O v4.2.70", + "version":"2.0" + }, + "scene":0, + "scenes":[ + { + "name":"Scene", + "nodes":[ + 0 + ] + } + ], + "nodes":[ + { + "mesh":0, + "name":"Cylinder", + "rotation":[ + 0.7071068286895752, + 0, + 0, + 0.7071068286895752 + ], + "translation":[ + 0, + 0, + -0.5 + ] + } + ], + "materials":[ + { + "name":"Material.001", + "pbrMetallicRoughness":{ + "baseColorFactor":[0,0,0,0] + }, + "emissiveTexture":{ + "index":0 + }, + "emissiveFactor": [ 1.0, 1.0, 1.0 ] + } + ], + "meshes":[ + { + "name":"Cylinder.001", + "primitives":[ + { + "attributes":{ + "POSITION":0, + "NORMAL":1, + "TEXCOORD_0":2 + }, + "indices":3, + "material":0 + } + ] + } + ], + "textures":[ + { + "sampler":0, + "source":0 + } + ], + "images":[ + { + "mimeType":"image/png", + "name":"Untitled", + "uri":"ray.png" + } + ], + "accessors":[ + { + "bufferView":0, + "componentType":5126, + "count":18, + "max":[ + 0.004000000189989805, + 0.5, + 0.004000000189989805 + ], + "min":[ + -0.004000000189989805, + -0.5, + -0.004000000189989805 + ], + "type":"VEC3" + }, + { + "bufferView":1, + "componentType":5126, + "count":18, + "type":"VEC3" + }, + { + "bufferView":2, + "componentType":5126, + "count":18, + "type":"VEC2" + }, + { + "bufferView":3, + "componentType":5123, + "count":48, + "type":"SCALAR" + } + ], + "bufferViews":[ + { + "buffer":0, + "byteLength":216, + "byteOffset":0, + "target":34962 + }, + { + "buffer":0, + "byteLength":216, + "byteOffset":216, + "target":34962 + }, + { + "buffer":0, + "byteLength":144, + "byteOffset":432, + "target":34962 + }, + { + "buffer":0, + "byteLength":96, + "byteOffset":576, + "target":34963 + } + ], + "samplers":[ + { + "magFilter":9729, + "minFilter":9987 + } + ], + "buffers":[ + { + "byteLength":672, + "uri":"ray.bin" + } + ] +} diff --git a/assets/ray.png b/assets/ray.png new file mode 100644 index 00000000..7af5eee2 Binary files /dev/null and b/assets/ray.png differ diff --git a/client/hardware.cpp b/client/hardware.cpp index b798ddf6..219c112f 100644 --- a/client/hardware.cpp +++ b/client/hardware.cpp @@ -199,3 +199,51 @@ const char * permission_name(feature f) } __builtin_unreachable(); } + +std::string controller_name() +{ +#ifndef __ANDROID__ + const char * controller = std::getenv("WIVRN_CONTROLLER"); + if (controller && strcmp(controller, "")) + return controller; +#endif + + switch (guess_model()) + { + case model::oculus_quest: + return "oculus-touch-v2"; + case model::oculus_quest_2: + return "oculus-touch-v3"; + case model::meta_quest_pro: + return "meta-quest-touch-pro"; + case model::meta_quest_3: + return "meta-quest-touch-plus"; + case model::pico_neo_3: + return "pico-neo3"; + case model::pico_4: + return "pico-4"; + case model::htc_vive_focus_3: + case model::htc_vive_xr_elite: + return "htc-vive-focus-3"; + case model::lynx_r1: + case model::unknown: + return "generic-trigger-squeeze"; + } + + __builtin_unreachable(); +} + +std::string controller_ray_model_name() +{ + switch (guess_model()) + { + case model::htc_vive_focus_3: + case model::htc_vive_xr_elite: + // XR Elite's runtime always assume alpha is unpremultiplied in the composition layers + // Assume it's the same for all HTC headsets + return "ray-htc.gltf"; + + default: + return "ray.gltf"; + } +} diff --git a/client/hardware.h b/client/hardware.h index 66061c8a..200f395c 100644 --- a/client/hardware.h +++ b/client/hardware.h @@ -19,6 +19,7 @@ #pragma once #include +#include enum class model { @@ -50,3 +51,6 @@ bool need_srgb_conversion(model); // Return nullptr if no permission is required const char * permission_name(feature f); + +std::string controller_name(); +std::string controller_ray_model_name(); diff --git a/client/render/scene_data.h b/client/render/scene_data.h index 73871b88..6e7dd729 100644 --- a/client/render/scene_data.h +++ b/client/render/scene_data.h @@ -113,7 +113,7 @@ struct scene_data std::string name; std::string shader_name = "lit"; - bool blend_enable = false; + bool blend_enable = true; }; struct vertex diff --git a/client/render/scene_renderer.cpp b/client/render/scene_renderer.cpp index c580a705..87f53166 100644 --- a/client/render/scene_renderer.cpp +++ b/client/render/scene_renderer.cpp @@ -160,7 +160,31 @@ static std::array layout_bindings_1{ vk::DescriptorSetLayoutBinding{ .binding = 0, .descriptorType = vk::DescriptorType::eCombinedImageSampler, - .descriptorCount = 5, + .descriptorCount = 1, + .stageFlags = vk::ShaderStageFlagBits::eFragment, + }, + vk::DescriptorSetLayoutBinding{ + .binding = 1, + .descriptorType = vk::DescriptorType::eCombinedImageSampler, + .descriptorCount = 1, + .stageFlags = vk::ShaderStageFlagBits::eFragment, + }, + vk::DescriptorSetLayoutBinding{ + .binding = 2, + .descriptorType = vk::DescriptorType::eCombinedImageSampler, + .descriptorCount = 1, + .stageFlags = vk::ShaderStageFlagBits::eFragment, + }, + vk::DescriptorSetLayoutBinding{ + .binding = 3, + .descriptorType = vk::DescriptorType::eCombinedImageSampler, + .descriptorCount = 1, + .stageFlags = vk::ShaderStageFlagBits::eFragment, + }, + vk::DescriptorSetLayoutBinding{ + .binding = 4, + .descriptorType = vk::DescriptorType::eCombinedImageSampler, + .descriptorCount = 1, .stageFlags = vk::ShaderStageFlagBits::eFragment, }, vk::DescriptorSetLayoutBinding{ @@ -583,8 +607,8 @@ vk::raii::Pipeline scene_renderer::create_pipeline(const pipeline_info & info) .rasterizationSamples = MSAA_SAMPLES, }}, .DepthStencilState = {vk::PipelineDepthStencilStateCreateInfo{ - .depthTestEnable = true, - .depthWriteEnable = true, + .depthTestEnable = info.depth_test_enable, + .depthWriteEnable = info.depth_write_enable, .depthCompareOp = vk::CompareOp::eGreater, .depthBoundsTestEnable = false, .minDepthBounds = 0.0f, @@ -593,10 +617,10 @@ vk::raii::Pipeline scene_renderer::create_pipeline(const pipeline_info & info) .ColorBlendState = {vk::PipelineColorBlendStateCreateInfo{}}, .ColorBlendAttachments = {vk::PipelineColorBlendAttachmentState{ .blendEnable = info.blend_enable, - .srcColorBlendFactor = vk::BlendFactor::eSrcAlpha, + .srcColorBlendFactor = vk::BlendFactor::eOne, .dstColorBlendFactor = vk::BlendFactor::eOneMinusSrcAlpha, .colorBlendOp = vk::BlendOp::eAdd, - .srcAlphaBlendFactor = vk::BlendFactor::eSrcAlpha, + .srcAlphaBlendFactor = vk::BlendFactor::eOne, .dstAlphaBlendFactor = vk::BlendFactor::eOneMinusSrcAlpha, .alphaBlendOp = vk::BlendOp::eAdd, .colorWriteMask = vk::ColorComponentFlagBits::eR | vk::ColorComponentFlagBits::eG | vk::ColorComponentFlagBits::eB | vk::ColorComponentFlagBits::eA}}, @@ -726,14 +750,43 @@ void scene_renderer::update_material_descriptor_set(scene_data::material & mater .range = sizeof(scene_data::material::gpu_data), }; + // Write each descriptor separately because the HTC XR Elite needs it for some reason std::array write_ds{ vk::WriteDescriptorSet{ .dstSet = ds, .dstBinding = 0, - .descriptorCount = (uint32_t)write_ds_image.size(), + .descriptorCount = 1, .descriptorType = vk::DescriptorType::eCombinedImageSampler, .pImageInfo = write_ds_image.data(), }, + vk::WriteDescriptorSet{ + .dstSet = ds, + .dstBinding = 1, + .descriptorCount = 1, + .descriptorType = vk::DescriptorType::eCombinedImageSampler, + .pImageInfo = write_ds_image.data() + 1, + }, + vk::WriteDescriptorSet{ + .dstSet = ds, + .dstBinding = 2, + .descriptorCount = 1, + .descriptorType = vk::DescriptorType::eCombinedImageSampler, + .pImageInfo = write_ds_image.data() + 2, + }, + vk::WriteDescriptorSet{ + .dstSet = ds, + .dstBinding = 3, + .descriptorCount = 1, + .descriptorType = vk::DescriptorType::eCombinedImageSampler, + .pImageInfo = write_ds_image.data() + 3, + }, + vk::WriteDescriptorSet{ + .dstSet = ds, + .dstBinding = 4, + .descriptorCount = 1, + .descriptorType = vk::DescriptorType::eCombinedImageSampler, + .pImageInfo = write_ds_image.data() + 4, + }, vk::WriteDescriptorSet{ .dstSet = ds, .dstBinding = (uint32_t)write_ds_image.size(), diff --git a/client/render/scene_renderer.h b/client/render/scene_renderer.h index aea28fa1..91e83a0b 100644 --- a/client/render/scene_renderer.h +++ b/client/render/scene_renderer.h @@ -44,6 +44,8 @@ struct pipeline_info vk::FrontFace front_face = vk::FrontFace::eClockwise; vk::PrimitiveTopology topology = vk::PrimitiveTopology::eTriangleList; bool blend_enable = false; + bool depth_test_enable = true; + bool depth_write_enable = true; // Specialization constants data int32_t nb_texcoords = 2; diff --git a/client/scenes/input_profile.cpp b/client/scenes/input_profile.cpp index 30b4121b..271c063f 100644 --- a/client/scenes/input_profile.cpp +++ b/client/scenes/input_profile.cpp @@ -20,7 +20,10 @@ #include "application.h" #include "asset.h" +#include "hardware.h" +#include #include +#include #include #include #include @@ -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::infinity(); + + return -dot2 / dot1; +} + +void input_profile::apply(XrSpace world_space, XrTime predicted_display_time, bool hide_left, bool hide_right, std::span 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); diff --git a/client/scenes/input_profile.h b/client/scenes/input_profile.h index a528504d..6dd14276 100644 --- a/client/scenes/input_profile.h +++ b/client/scenes/input_profile.h @@ -21,6 +21,7 @@ #include #include #include +#include #include #include @@ -59,9 +60,11 @@ struct input_profile std::vector responses; std::vector> 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 pointer_limits); }; diff --git a/client/scenes/lobby.cpp b/client/scenes/lobby.cpp index 21fe1a61..4f34c8a1 100644 --- a/client/scenes/lobby.cpp +++ b/client/scenes/lobby.cpp @@ -623,6 +623,27 @@ template overloaded(Ts...) -> overloaded; } // 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> imgui_layers = draw_gui(frame_state.predictedDisplayTime); + // Get the planes that limit the ray size from the composition layers + std::vector 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()) diff --git a/client/shaders/lit.glsl b/client/shaders/lit.glsl index 88941aa5..a34dff45 100644 --- a/client/shaders/lit.glsl +++ b/client/shaders/lit.glsl @@ -171,13 +171,13 @@ void main() vec3 light = ambient + diffuse /*+ specular*/; - vec4 c = texture(base_color, fract(texcoord[0])) * vec4(light, 1.0); + vec4 bc = material.base_color_factor * texture(base_color, fract(texcoord[0])) * vec4(light, 1.0); + + vec4 ec = material.base_emissive_factor * texture(emissive, fract(texcoord[0])); float fog = clamp((length(frag_pos) - fog_min_dist) / (fog_max_dist - fog_min_dist), 0.0, 1.0); - c = mix(c, fog_color, fog); - -// vec4 c = vec4(1,1,1,1); + bc = mix(bc + ec, fog_color, fog); // if (alpha_cutout && c.a <= 0.5) // discard; @@ -187,12 +187,12 @@ void main() ivec2 tmp = ivec2(gl_FragCoord.xy) % 4; float dither_thd = dither_pattern[tmp.x][tmp.y]; - vec4 color = c * 255.0f; + vec4 color = bc * 255.0f; bvec4 tmp2 = greaterThan(fract(color), vec4(dither_thd, dither_thd, dither_thd, dither_thd)); out_color = (ceil(color) + vec4(tmp2)) / 255.0; } else - out_color = c; + out_color = bc; } #endif