From 17bae9459a3f1307bf33e3216275bf2a774acd18 Mon Sep 17 00:00:00 2001 From: Guillaume Meunier Date: Sat, 7 Feb 2026 19:54:07 +0100 Subject: [PATCH] Use custom clipping to clip controller rays --- client/render/imgui_impl.cpp | 16 +-- client/render/imgui_impl.h | 11 +- client/render/scene_components.h | 2 - client/render/scene_loader.cpp | 1 - client/render/scene_renderer.cpp | 1 - client/render/scene_renderer.h | 1 - client/scenes/input_profile.cpp | 44 ++++--- client/scenes/input_profile.h | 2 +- client/scenes/lobby.cpp | 35 ++---- client/scenes/stream_gui.cpp | 35 ++---- client/shaders/common.glsl.inc | 2 - client/shaders/debug_draw.glsl | 2 - client/shaders/lit.vert.glsl | 6 - client/shaders/lit_skinned.vert.glsl | 11 +- client/shaders/ray.frag.glsl | 67 +---------- client/shaders/ray.vert.glsl | 25 ++++ client/shaders/ray_common.glsl.inc | 171 +++++++++++++++++++++++++++ client/shaders/ray_skinned.vert.glsl | 25 ++++ 18 files changed, 277 insertions(+), 180 deletions(-) create mode 100644 client/shaders/ray.vert.glsl create mode 100644 client/shaders/ray_common.glsl.inc create mode 100644 client/shaders/ray_skinned.vert.glsl diff --git a/client/render/imgui_impl.cpp b/client/render/imgui_impl.cpp index e8f29f8e..8a2ed534 100644 --- a/client/render/imgui_impl.cpp +++ b/client/render/imgui_impl.cpp @@ -1037,11 +1037,9 @@ std::vector> imgui_context::end_frame() return quads; } -std::vector imgui_context::windows() +std::vector imgui_context::windows() { - std::vector w; - - ImGuiWindow * modal_popup = ImGui::GetTopMostAndVisiblePopupModal(); + std::vector w; for (const imgui_context::viewport & layer: layers_) { @@ -1052,10 +1050,7 @@ std::vector imgui_context::windows() for (ImGuiWindow * window: context->Windows) { - if (not window->Active or window->Hidden /*or window->ParentWindowInBeginStack != nullptr*/) - continue; - - if (modal_popup != nullptr and window != modal_popup and not layer.always_show_cursor) + if (not window->Active or window->Hidden or (window->Flags & ImGuiWindowFlags_ChildWindow)) continue; if (not window_intersects_viewport(window, layer)) @@ -1066,7 +1061,7 @@ std::vector imgui_context::windows() ImVec2 min = ImMax(v.Min, window->Pos); ImVec2 center = (min + max) / 2; - w.push_back(viewport{ + w.push_back(window_viewport{ .space = layer.space, .position = layer.position + glm::mat3_cast(layer.orientation) * @@ -1079,9 +1074,6 @@ std::vector imgui_context::windows() (max.x - min.x) * layer.size.x / layer.vp_size.x, (max.y - min.y) * layer.size.y / layer.vp_size.y, }, - .vp_origin = {min.x, min.y}, - .vp_size = {max.x - min.x, max.y - min.y}, - .always_show_cursor = layer.always_show_cursor, }); } } diff --git a/client/render/imgui_impl.h b/client/render/imgui_impl.h index 0f96b780..086392fd 100644 --- a/client/render/imgui_impl.h +++ b/client/render/imgui_impl.h @@ -145,6 +145,15 @@ public: } }; + struct window_viewport + { + // Position of this window in the world + xr::spaces space; + glm::vec3 position; + glm::quat orientation; + glm::vec2 size; + }; + private: uint32_t queue_family_index; @@ -215,7 +224,7 @@ public: return layers_; } - std::vector windows(); + std::vector windows(); viewport & layer(ImVec2 position); diff --git a/client/render/scene_components.h b/client/render/scene_components.h index 306a6e45..cd955261 100644 --- a/client/render/scene_components.h +++ b/client/render/scene_components.h @@ -156,8 +156,6 @@ struct node bool visible = true; uint32_t layer_mask = -1; - std::array clipping_planes; - // TODO: separate component? std::vector> joints; // Node index, inverse bind matrix of each joint diff --git a/client/render/scene_loader.cpp b/client/render/scene_loader.cpp index ef44967f..1b7c0331 100644 --- a/client/render/scene_loader.cpp +++ b/client/render/scene_loader.cpp @@ -1007,7 +1007,6 @@ public: node.orientation = glm::make_quat(TRS.rotation.data()); node.scale = glm::make_vec3(TRS.scale.data()); node.visible = true; - std::ranges::fill(node.clipping_planes, glm::vec4{0, 0, 0, 1}); } return entities; diff --git a/client/render/scene_renderer.cpp b/client/render/scene_renderer.cpp index 5eeb722d..fb748eb9 100644 --- a/client/render/scene_renderer.cpp +++ b/client/render/scene_renderer.cpp @@ -1135,7 +1135,6 @@ void scene_renderer::render( object_ubo.modelview[frame_index] = frame.view * transform; object_ubo.modelviewproj[frame_index] = viewproj[frame_index] * transform; } - object_ubo.clipping_planes = node.clipping_planes; std::ranges::copy(node.extra_shader_data, extra_shader_data.begin()); // Get the material diff --git a/client/render/scene_renderer.h b/client/render/scene_renderer.h index 1b9f2561..f1b9b8d3 100644 --- a/client/render/scene_renderer.h +++ b/client/render/scene_renderer.h @@ -199,7 +199,6 @@ class scene_renderer glm::mat4 model; std::array modelview; std::array modelviewproj; - std::array clipping_planes; }; struct debug_draw_vertex diff --git a/client/scenes/input_profile.cpp b/client/scenes/input_profile.cpp index ad67a33c..c1346ac5 100644 --- a/client/scenes/input_profile.cpp +++ b/client/scenes/input_profile.cpp @@ -23,6 +23,7 @@ #include "render/scene_components.h" #include "utils/mapped_file.h" #include "xr/space.h" +#include #include #include #include @@ -186,6 +187,7 @@ struct json_visual_response struct ray_extra_shader_data { + std::array window_to_world; float emissive_factor; }; @@ -194,7 +196,14 @@ void setup_ray_shader(entt::registry & scene, entt::entity entity, const ray_ext auto & node = scene.get(entity); for (auto & primitive: node.mesh->primitives) + { + if (primitive.vertex_shader == "lit.vert") + primitive.vertex_shader = "ray.vert"; + else if (primitive.vertex_shader == "lit_skinned.vert") + primitive.vertex_shader = "ray_skinned.vert"; + primitive.material_->fragment_shader_name = "ray.frag"; + } node.set_extra_shader_data(initial_data); } @@ -316,7 +325,7 @@ input_profile::input_profile(scene & scene, const std::filesystem::path & json_p scene.world.emplace(find_node_by_name(scene.world, "Ray", ray_entity), space, 0); scene.world.emplace(find_node_by_name(scene.world, "Ray", ray_entity)); scene.world.emplace(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}); + setup_ray_shader(scene.world, find_node_by_name(scene.world, "Ray", ray_entity)); try { @@ -329,7 +338,7 @@ input_profile::input_profile(scene & scene, const std::filesystem::path & json_p scene.world.emplace(find_node_by_name(scene.world, "Trail -40ms", ray_entity), space, -40'000'000); scene.world.emplace(find_node_by_name(scene.world, "Trail", ray_entity)); scene.world.emplace(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}); + setup_ray_shader(scene.world, find_node_by_name(scene.world, "Trail", ray_entity)); } catch (...) { @@ -413,7 +422,7 @@ void input_profile::apply( bool hide_left_ray, bool hide_right_controller, bool hide_right_ray, - std::span pointer_limits) + std::span world_to_window) { for (auto && [entity, node, space]: scene.view().each()) { @@ -511,37 +520,24 @@ void input_profile::apply( 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}); + node.set_extra_shader_data(ray_extra_shader_data{{}, value}); } } for (auto && [entity, node]: scene.view().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(entity).visible = false; - // return; - // } - // } - // } - - size_t nb_clipping_planes = std::min(node.clipping_planes.size(), pointer_limits.size()); + auto & data = node.get_extra_shader_data(); + size_t nb_windows = std::min(data.window_to_world.size(), world_to_window.size()); auto copy_results = std::ranges::copy_n( - pointer_limits.begin(), - nb_clipping_planes, - node.clipping_planes.begin()); + world_to_window.begin(), + nb_windows, + data.window_to_world.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)); + data.window_to_world.size() - nb_windows, + glm::mat4{/* TODO */}); } } diff --git a/client/scenes/input_profile.h b/client/scenes/input_profile.h index 6e68c09f..83f3b144 100644 --- a/client/scenes/input_profile.h +++ b/client/scenes/input_profile.h @@ -48,5 +48,5 @@ struct input_profile bool hide_left_ray, bool hide_right_controller, bool hide_right_ray, - std::span pointer_limits); + std::span world_to_window); }; diff --git a/client/scenes/lobby.cpp b/client/scenes/lobby.cpp index 8210872f..46b7c9fc 100644 --- a/client/scenes/lobby.cpp +++ b/client/scenes/lobby.cpp @@ -626,28 +626,7 @@ std::optional scenes::lobby::check_recenter_gui(glm::vec3 head_positi return std::nullopt; } -// 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); -} - -static void stick_finger_to_gui(std::array & hand, const std::vector & layers) +static void stick_finger_to_gui(std::array & hand, const std::vector & layers) { // Move XR_HAND_JOINT_INDEX_{TIP,DISTAL,INTERMEDIATE,PROXIMAL}_EXT so that: // - Only the X rotation changes for distal and intermediate (relative to the parent bone) @@ -667,7 +646,7 @@ static void stick_finger_to_gui(std::array> intersections; // Target position of the finger tip, distance to the GUI plane - for (const imgui_context::viewport & layer: layers) + for (const imgui_context::window_viewport & layer: layers) { // Ignore layers that are not absolutely positionned if (layer.space != xr::spaces::world) @@ -1054,11 +1033,11 @@ void scenes::lobby::render(const XrFrameState & frame_state) #endif // Get the planes that limit the ray size from the composition layers - std::vector ray_limits; - for (auto & [z_index, layer]: imgui_layers) + std::vector world_to_window; + for (auto & window: imgui_ctx->windows()) { - if (z_index != constants::lobby::zindex_recenter_tip) - ray_limits.push_back(compute_ray_limits(layer.pose)); + if (window.space == xr::spaces::world) + world_to_window.push_back(glm::inverse(glm::translate(window.position) * glm::mat4(glm::mat3_cast(window.orientation)) * glm::scale(glm::vec3(window.size, 1)))); } input->apply(world, @@ -1068,7 +1047,7 @@ void scenes::lobby::render(const XrFrameState & frame_state) not imgui_ctx->is_aim_interaction()[0], hide_right_controller, not imgui_ctx->is_aim_interaction()[1], - ray_limits); + world_to_window); renderer::animate(world, frame_state.predictedDisplayPeriod * 1.0e-9); diff --git a/client/scenes/stream_gui.cpp b/client/scenes/stream_gui.cpp index 9b1f0863..feaef556 100644 --- a/client/scenes/stream_gui.cpp +++ b/client/scenes/stream_gui.cpp @@ -32,6 +32,8 @@ #include #include #include +#include +#include #include #include #include @@ -729,27 +731,6 @@ void scenes::stream::gui_applications() ImGui::PopStyleVar(3); } -// 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::stream::draw_gui(XrTime predicted_display_time, XrDuration predicted_display_period) { if (not(plots_toggle_1 and plots_toggle_2)) @@ -1053,10 +1034,12 @@ void scenes::stream::draw_gui(XrTime predicted_display_time, XrDuration predicte else recentering_context.reset(); - std::vector ray_limits; - - for (auto [_, layer]: layers) - ray_limits.push_back(compute_ray_limits(layer.pose)); + std::vector world_to_window; + for (auto & window: imgui_ctx->windows()) + { + if (window.space == xr::spaces::world) + world_to_window.push_back(glm::inverse(glm::translate(window.position) * glm::mat4(glm::mat3_cast(window.orientation)) * glm::scale(glm::vec3(window.size, 1)))); + } bool hide_left_controller = false; bool hide_right_controller = false; @@ -1080,7 +1063,7 @@ void scenes::stream::draw_gui(XrTime predicted_display_time, XrDuration predicte hide_left_controller, hide_right_controller, hide_right_controller, - ray_limits); + world_to_window); // Add the layer with the controllers if (composition_layer_depth_test_supported) diff --git a/client/shaders/common.glsl.inc b/client/shaders/common.glsl.inc index 7c4f52bf..a5c07d2d 100644 --- a/client/shaders/common.glsl.inc +++ b/client/shaders/common.glsl.inc @@ -1,7 +1,6 @@ // kate: hl glsl const int nb_views = 2; -const int nb_clipping = 4; const int nb_texcoords = 2; const bool use_pbr = false; @@ -20,7 +19,6 @@ layout(set = 0, binding = 1) uniform mesh_ubo mat4 model; mat4 modelview[nb_views]; mat4 modelviewproj[nb_views]; - vec4 clipping_plane[nb_clipping]; } mesh; layout(set = 0, binding = 2) uniform joints_ubo diff --git a/client/shaders/debug_draw.glsl b/client/shaders/debug_draw.glsl index 1b554151..4a5fbb86 100644 --- a/client/shaders/debug_draw.glsl +++ b/client/shaders/debug_draw.glsl @@ -27,7 +27,6 @@ layout (constant_id = 2) const bool alpha_cutout = false; layout (constant_id = 3) const bool skinning = false; const int nb_views = 2; -const int nb_clipping = 4; layout(set = 0, binding = 0) uniform scene_ubo { @@ -42,7 +41,6 @@ layout(set = 0, binding = 1) uniform mesh_ubo mat4 model; mat4 modelview[nb_views]; mat4 modelviewproj[nb_views]; - vec4 clipping_plane[nb_clipping]; } mesh; layout(set = 0, binding = 2) uniform joints_ubo diff --git a/client/shaders/lit.vert.glsl b/client/shaders/lit.vert.glsl index 7d638bc8..a83cb489 100644 --- a/client/shaders/lit.vert.glsl +++ b/client/shaders/lit.vert.glsl @@ -34,7 +34,6 @@ layout(location = 5) in vec4 in_color; out gl_PerVertex { vec4 gl_Position; - float gl_ClipDistance[nb_clipping]; }; void main() @@ -59,9 +58,4 @@ void main() frag_pos = mesh.modelview[gl_ViewIndex] * vec4(in_position, 1.0); light_pos = scene.view[gl_ViewIndex] * scene.light_position; vertex_color = in_color; - - for (int i = 0; i < nb_clipping; i++) - { - gl_ClipDistance[i] = dot(mesh.clipping_plane[i], mesh.model * vec4(in_position, 1.0)); - } } diff --git a/client/shaders/lit_skinned.vert.glsl b/client/shaders/lit_skinned.vert.glsl index 6190c939..a4ce04e9 100644 --- a/client/shaders/lit_skinned.vert.glsl +++ b/client/shaders/lit_skinned.vert.glsl @@ -36,7 +36,6 @@ layout(location = 7) in vec4 in_weights; out gl_PerVertex { vec4 gl_Position; - float gl_ClipDistance[nb_clipping]; }; void main() @@ -53,8 +52,8 @@ void main() in_weights.z * joints.joint_matrices[int(in_joints.z)] + in_weights.w * joints.joint_matrices[int(in_joints.w)]; - // TODO on CPU? - mat3 mv = inverse(transpose(mat3(mesh.modelview[gl_ViewIndex]))); + + mat3 mv = inverse(transpose(mat3(mesh.modelview[gl_ViewIndex] * skinMatrix))); tangent = vec4(normalize(mv * in_tangent.xyz), in_tangent.w); if (any(isnan(tangent))) // avoid NaNs if no tangent data is provided @@ -62,15 +61,9 @@ void main() normal = normalize(mv * in_normal); - normal = vec3(mesh.modelview[gl_ViewIndex] * skinMatrix * vec4(in_normal, 0.0)); gl_Position = mesh.modelviewproj[gl_ViewIndex] * skinMatrix * vec4(in_position, 1.0); frag_pos = mesh.modelview[gl_ViewIndex] * skinMatrix * vec4(in_position, 1.0); light_pos = scene.view[gl_ViewIndex] * scene.light_position; vertex_color = in_color; - - for(int i = 0; i < nb_clipping; i++) - { - gl_ClipDistance[i] = dot(mesh.clipping_plane[i], mesh.model * skinMatrix * vec4(in_position, 1.0)); - } } diff --git a/client/shaders/ray.frag.glsl b/client/shaders/ray.frag.glsl index 3de4dbb7..5f12e391 100644 --- a/client/shaders/ray.frag.glsl +++ b/client/shaders/ray.frag.glsl @@ -1,7 +1,6 @@ /* * WiVRn VR streaming - * Copyright (C) 2022 Guillaume Meunier - * Copyright (C) 2022 Patrick Nicolas + * Copyright (C) 2026 Guillaume Meunier * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -19,66 +18,6 @@ #version 450 -const int nb_views = 2; -const int nb_clipping = 4; +#extension GL_GOOGLE_include_directive : require -layout(set = 0, binding = 1) uniform mesh_ubo -{ - mat4 model; - mat4 modelview[nb_views]; - mat4 modelviewproj[nb_views]; - vec4 clipping_plane[nb_clipping]; - - // Extra data - float emissive_factor; -} mesh; - -layout(set = 0, binding = 3) uniform sampler2D base_color_texture; -layout(set = 0, binding = 6) uniform sampler2D emissive_texture; - -struct texture_info -{ - int texcoord; - float rotation; - vec2 offset; - vec2 scale; -}; - -layout(std140, set = 0, binding = 8) uniform material_ubo -{ - vec4 base_color_factor; - vec4 base_emissive_factor; - float metallic_factor; - float roughness_factor; - float occlusion_strength; - float normal_scale; - float alpha_cutoff; - - texture_info base_color; - texture_info metallic_roughness; - texture_info occlusion; - texture_info emissive; - texture_info normal; -} material; - - -layout(location = 4) in vec2 texcoord_base_color; -layout(location = 7) in vec2 texcoord_emissive; -layout(location = 9) in vec4 vertex_color; - -const bool use_pbr = false; - -// Fragment output -layout(location = 0) out vec4 out_color; -layout(early_fragment_tests) in; - -void main() -{ - vec4 albedo = vertex_color * material.base_color_factor * texture(base_color_texture, texcoord_base_color); - vec3 emissive_color = mesh.emissive_factor * texture(emissive_texture, texcoord_emissive).rgb; - - out_color = vec4(emissive_color, albedo.a); - - if (use_pbr) - out_color = vec4(out_color.rgb / (out_color.rgb + 1), out_color.a); -} +#include "ray_common.glsl.inc" diff --git a/client/shaders/ray.vert.glsl b/client/shaders/ray.vert.glsl new file mode 100644 index 00000000..8e43d235 --- /dev/null +++ b/client/shaders/ray.vert.glsl @@ -0,0 +1,25 @@ +/* + * WiVRn VR streaming + * Copyright (C) 2026 Guillaume Meunier + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#version 450 +#extension GL_EXT_multiview : require +#extension GL_GOOGLE_include_directive : require + +#define SKINNED 0 + +#include "ray_common.glsl.inc" diff --git a/client/shaders/ray_common.glsl.inc b/client/shaders/ray_common.glsl.inc new file mode 100644 index 00000000..96b1d52e --- /dev/null +++ b/client/shaders/ray_common.glsl.inc @@ -0,0 +1,171 @@ +// kate: hl glsl + +/* + * WiVRn VR streaming + * Copyright (C) 2026 Guillaume Meunier + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +const int nb_views = 2; +const int nb_texcoords = 2; +const int nb_windows = 4; +const bool use_pbr = false; + +layout(set = 0, binding = 0) uniform scene_ubo +{ + mat4 view[nb_views]; + vec4 light_position; + vec4 ambient_color; + vec4 light_color; +} scene; + +layout(set = 0, binding = 1) uniform mesh_ubo +{ + mat4 model; + mat4 modelview[nb_views]; + mat4 modelviewproj[nb_views]; + + // Extra data + mat4 world_to_window[nb_windows]; + float emissive_factor; +} mesh; + +layout(set = 0, binding = 2) uniform joints_ubo +{ + mat4 joint_matrices[32]; +} joints; + +layout(set = 0, binding = 3) uniform sampler2D base_color_texture; +layout(set = 0, binding = 4) uniform sampler2D metallic_roughness_texture; +layout(set = 0, binding = 5) uniform sampler2D occlusion_texture; +layout(set = 0, binding = 6) uniform sampler2D emissive_texture; +layout(set = 0, binding = 7) uniform sampler2D normal_map_texture; + +struct texture_info +{ + int texcoord; + float rotation; + vec2 offset; + vec2 scale; +}; + +layout(std140, set = 0, binding = 8) uniform material_ubo +{ + vec4 base_color_factor; + vec4 base_emissive_factor; + float metallic_factor; + float roughness_factor; + float occlusion_strength; + float normal_scale; + float alpha_cutoff; + + texture_info base_color; + texture_info metallic_roughness; + texture_info occlusion; + texture_info emissive; + texture_info normal; +} material; + +#ifdef VERT_SHADER +// Vertex input +layout(location = 0) in vec3 in_position; +layout(location = 1) in vec3 in_normal; +layout(location = 2) in vec4 in_tangent; +layout(location = 3) in vec2 in_texcoord[2]; +layout(location = 5) in vec4 in_color; + +#if SKINNED +layout(location = 6) in vec4 in_joints; +layout(location = 7) in vec4 in_weights; +#endif + +#endif + +#ifdef FRAG_SHADER +#define VS_TO_FS in +#else +#define VS_TO_FS out +#endif + +// Vertex-to-fragment +layout(location = 0) VS_TO_FS vec2 texcoord; +layout(location = 1) VS_TO_FS vec4 position_wrt_window[nb_windows]; +layout(location = 1 + nb_windows) VS_TO_FS vec4 base_wrt_window[nb_windows]; + +#ifdef VERT_SHADER +out gl_PerVertex +{ + vec4 gl_Position; +}; + +void main() +{ + texcoord = in_texcoord[0]; + +#if SKINNED + mat4 skinMatrix = + in_weights.x * joints.joint_matrices[int(in_joints.x)] + + in_weights.y * joints.joint_matrices[int(in_joints.y)] + + in_weights.z * joints.joint_matrices[int(in_joints.z)] + + in_weights.w * joints.joint_matrices[int(in_joints.w)]; + + gl_Position = mesh.modelviewproj[gl_ViewIndex] * skinMatrix * vec4(in_position, 1.0); + vec4 world_position = mesh.model * skinMatrix * vec4(in_position, 1.0); + vec4 base_position = mesh.model * skinMatrix * vec4(in_position.xy, 0.0, 1.0); +#else + gl_Position = mesh.modelviewproj[gl_ViewIndex] * vec4(in_position, 1.0); + vec4 world_position = mesh.model * vec4(in_position, 1.0); + vec4 base_position = mesh.model * vec4(in_position.xy, 0.0, 1.0); +#endif + + for(uint i = 0; i < nb_windows; i++) + { + position_wrt_window[i] = mesh.world_to_window[i] * world_position; + base_wrt_window[i] = mesh.world_to_window[i] * base_position; + } +} +#endif + +#ifdef FRAG_SHADER +// Fragment output +layout(location = 0) out vec4 out_color; + +void main() +{ + for (uint i = 0; i < nb_windows; i++) + { + if (position_wrt_window[i].w < 0.5) + continue; + + // TODO check NaN + float lambda = -base_wrt_window[i].z / (position_wrt_window[i].z - base_wrt_window[i].z); + + if (lambda < 0 || lambda > 1) + continue; + + vec2 intersection = base_wrt_window[i].xy + lambda * (position_wrt_window[i].xy - base_wrt_window[i].xy); + if (abs(intersection.x) < 0.5 && abs(intersection.y) < 0.5) + discard; + } + + vec4 albedo = material.base_color_factor * texture(base_color_texture, texcoord); + vec3 emissive_color = mesh.emissive_factor * texture(emissive_texture, texcoord).rgb; + + out_color = vec4(emissive_color, albedo.a); + + if (use_pbr) + out_color = vec4(out_color.rgb / (out_color.rgb + 1), out_color.a); +} +#endif diff --git a/client/shaders/ray_skinned.vert.glsl b/client/shaders/ray_skinned.vert.glsl new file mode 100644 index 00000000..55189385 --- /dev/null +++ b/client/shaders/ray_skinned.vert.glsl @@ -0,0 +1,25 @@ +/* + * WiVRn VR streaming + * Copyright (C) 2026 Guillaume Meunier + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#version 450 +#extension GL_EXT_multiview : require +#extension GL_GOOGLE_include_directive : require + +#define SKINNED 1 + +#include "ray_common.glsl.inc"