From f99fb8fa1856af285ffe1f4d1b89243d66708d75 Mon Sep 17 00:00:00 2001 From: Guillaume Meunier Date: Sun, 30 Nov 2025 18:51:43 +0100 Subject: [PATCH] Add support for KHR_texture_transform --- client/render/scene_components.h | 20 ++++++---- client/render/scene_loader.cpp | 57 ++++++++++++++++------------ client/render/scene_renderer.cpp | 2 +- client/shaders/common.glsl.inc | 41 +++++++++++++++++--- client/shaders/lit.vert.glsl | 13 ++++--- client/shaders/lit_skinned.vert.glsl | 8 ++-- 6 files changed, 92 insertions(+), 49 deletions(-) diff --git a/client/render/scene_components.h b/client/render/scene_components.h index ca3bcad3..28b1b2a9 100644 --- a/client/render/scene_components.h +++ b/client/render/scene_components.h @@ -55,6 +55,14 @@ struct texture struct material { + struct alignas(16) texture_info + { + alignas(4) uint32_t texcoord = 0; + alignas(4) float rotation = 0; + alignas(8) glm::vec2 offset = {0, 0}; + alignas(8) glm::vec2 scale = {1, 1}; + }; + struct gpu_data { glm::vec4 base_color_factor = glm::vec4(1, 1, 1, 1); @@ -65,13 +73,11 @@ struct material float normal_scale = 1; float alpha_cutoff = 0.5; - uint32_t base_color_texcoord = 0; - uint32_t metallic_roughness_texcoord = 0; - uint32_t occlusion_texcoord = 0; - uint32_t emissive_texcoord = 0; - uint32_t normal_texcoord = 0; - - // TODO: add fastgltf::TextureTransform? + texture_info base_color; + texture_info metallic_roughness; + texture_info occlusion; + texture_info emissive; + texture_info normal; }; std::shared_ptr base_color_texture; diff --git a/client/render/scene_loader.cpp b/client/render/scene_loader.cpp index b22b7bfa..5d8a4c22 100644 --- a/client/render/scene_loader.cpp +++ b/client/render/scene_loader.cpp @@ -168,6 +168,11 @@ glm::vec3 convert(const fastgltf::math::nvec3 & v) return {v[0], v[1], v[2]}; } +glm::vec2 convert(const fastgltf::math::nvec2 & v) +{ + return {v[0], v[1]}; +} + components::animation_track_base::interpolation_t convert(fastgltf::AnimationInterpolation interpolation) { switch (interpolation) @@ -443,7 +448,9 @@ struct : std::error_category fastgltf::Asset load_gltf_asset(fastgltf::GltfDataBuffer & buffer, const std::filesystem::path & directory) { - fastgltf::Parser parser(fastgltf::Extensions::KHR_texture_basisu); + fastgltf::Parser parser( + fastgltf::Extensions::KHR_texture_basisu | + fastgltf::Extensions::KHR_texture_transform); auto gltf_options = fastgltf::Options::DontRequireValidAssetMember | @@ -752,37 +759,37 @@ public: material_data.roughness_factor = gltf_material.pbrData.roughnessFactor; material_data.alpha_cutoff = gltf_material.alphaCutoff; - if (gltf_material.pbrData.baseColorTexture) - { - material.base_color_texture = textures.at(gltf_material.pbrData.baseColorTexture->textureIndex); - material_data.base_color_texcoord = gltf_material.pbrData.baseColorTexture->texCoordIndex; - } + auto f = [&](const auto & texture_info, + std::shared_ptr & texture, + renderer::material::texture_info & info) { + if (not texture_info) + return; - if (gltf_material.pbrData.metallicRoughnessTexture) - { - material.metallic_roughness_texture = textures.at(gltf_material.pbrData.metallicRoughnessTexture->textureIndex); - material_data.metallic_roughness_texcoord = gltf_material.pbrData.metallicRoughnessTexture->texCoordIndex; - } + texture = textures.at(texture_info->textureIndex); + info.texcoord = texture_info->texCoordIndex; + + if (auto & xform = texture_info->transform) + { + if (xform->texCoordIndex) + info.texcoord = *xform->texCoordIndex; + + info.rotation = xform->rotation; + info.offset = convert(xform->uvOffset); + info.scale = convert(xform->uvScale); + } + }; + + f(gltf_material.pbrData.baseColorTexture, material.base_color_texture, material_data.base_color); + f(gltf_material.pbrData.metallicRoughnessTexture, material.metallic_roughness_texture, material_data.metallic_roughness); + f(gltf_material.occlusionTexture, material.occlusion_texture, material_data.occlusion); + f(gltf_material.emissiveTexture, material.emissive_texture, material_data.emissive); + f(gltf_material.normalTexture, material.normal_texture, material_data.normal); if (gltf_material.occlusionTexture) - { - material.occlusion_texture = textures.at(gltf_material.occlusionTexture->textureIndex); - material_data.occlusion_texcoord = gltf_material.occlusionTexture->texCoordIndex; material_data.occlusion_strength = gltf_material.occlusionTexture->strength; - } - - if (gltf_material.emissiveTexture) - { - material.emissive_texture = textures.at(gltf_material.emissiveTexture->textureIndex); - material_data.emissive_texcoord = gltf_material.emissiveTexture->texCoordIndex; - } if (gltf_material.normalTexture) - { - material.normal_texture = textures.at(gltf_material.normalTexture->textureIndex); - material_data.normal_texcoord = gltf_material.normalTexture->texCoordIndex; material_data.normal_scale = gltf_material.normalTexture->scale; - } material.offset = staging_buffer.add_uniform(material_data); } diff --git a/client/render/scene_renderer.cpp b/client/render/scene_renderer.cpp index 7372f5fa..4b73386d 100644 --- a/client/render/scene_renderer.cpp +++ b/client/render/scene_renderer.cpp @@ -261,7 +261,7 @@ static std::array layout_bindings_0{ .binding = 8, .descriptorType = vk::DescriptorType::eUniformBuffer, .descriptorCount = 1, - .stageFlags = vk::ShaderStageFlagBits::eFragment, + .stageFlags = vk::ShaderStageFlagBits::eVertex | vk::ShaderStageFlagBits::eFragment, }, }; diff --git a/client/shaders/common.glsl.inc b/client/shaders/common.glsl.inc index d385dce8..ea0c249a 100644 --- a/client/shaders/common.glsl.inc +++ b/client/shaders/common.glsl.inc @@ -33,7 +33,16 @@ 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; -layout(set = 0, binding = 8) uniform material_ubo + +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; @@ -43,11 +52,11 @@ layout(set = 0, binding = 8) uniform material_ubo float normal_scale; float alpha_cutoff; - int base_color_texcoord; - int metallic_roughness_texcoord; - int occlusion_texcoord; - int emissive_texcoord; - int normal_texcoord; + texture_info base_color; + texture_info metallic_roughness; + texture_info occlusion; + texture_info emissive; + texture_info normal; } material; const float dither_pattern[4][4] = { @@ -91,6 +100,26 @@ vec4 dither(ivec2 frag_coord, vec4 colour) return colour + one_over_d_linear_to_srgb(colour) * vec4(dither_thd, dither_thd, dither_thd, 0) / 255.0f; } +vec2 compute_texcoord(texture_info info, vec2 in_texcoord) +{ + mat3 translation = mat3( + 1, 0, 0, + 0, 1, 0, + info.offset.x, info.offset.y, 1); + mat3 rotation = mat3( + cos(info.rotation), sin(info.rotation), 0, + -sin(info.rotation), cos(info.rotation), 0, + 0, 0, 1); + mat3 scale = mat3( + info.scale.x, 0, 0, + 0, info.scale.y, 0, + 0, 0, 1); + + mat3 matrix = translation * rotation * scale; + + return (matrix * vec3(in_texcoord, 1)).xy; +} + #ifdef FRAG_SHADER #define VS_TO_FS in #else diff --git a/client/shaders/lit.vert.glsl b/client/shaders/lit.vert.glsl index bacbebaa..a0b9e8df 100644 --- a/client/shaders/lit.vert.glsl +++ b/client/shaders/lit.vert.glsl @@ -34,14 +34,15 @@ layout(location = 5) in vec4 in_color; out gl_PerVertex { vec4 gl_Position; - float gl_ClipDistance[nb_clipping]; + float gl_ClipDistance[nb_clipping]; }; void main() { - // TODO: use base_color_texcoord et al instead of always using texcoord 0 - for (int i = 0; i < nb_texcoords; i++) - texcoord[i] = in_texcoord[i]; + // TODO use base_color_texcoord et al instead of always using texcoord 0 + // TODO use the correct transform + texcoord[0] = compute_texcoord(material.base_color, in_texcoord[material.base_color.texcoord]); + // TODO on CPU? mat3 mv = inverse(transpose(mat3(mesh.modelview[gl_ViewIndex]))); @@ -57,8 +58,8 @@ void main() frag_pos = mesh.modelview[gl_ViewIndex] * vec4(in_position, 1.0); light_pos = scene.view[gl_ViewIndex] * scene.light_position; - for(int i = 0; i < nb_clipping; i++) + for (int i = 0; i < nb_clipping; i++) { - gl_ClipDistance[i] = dot(mesh.clipping_plane[i], mesh.model * vec4(in_position, 1.0)); + 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 8d87c566..4be67996 100644 --- a/client/shaders/lit_skinned.vert.glsl +++ b/client/shaders/lit_skinned.vert.glsl @@ -36,14 +36,14 @@ layout(location = 7) in vec4 in_weights; out gl_PerVertex { vec4 gl_Position; - float gl_ClipDistance[nb_clipping]; + float gl_ClipDistance[nb_clipping]; }; void main() { - // TODO: use base_color_texcoord et al instead of always using texcoord 0 - for (int i = 0; i < nb_texcoords; i++) - texcoord[i] = in_texcoord[i]; + // TODO use base_color_texcoord et al instead of always using texcoord 0 + // TODO use the correct transform + texcoord[0] = compute_texcoord(material.base_color, in_texcoord[material.base_color.texcoord]); mat4 skinMatrix = in_weights.x * joints.joint_matrices[int(in_joints.x)] +