From 06ad530e2f384019dac8d354b0f64bd3433056d7 Mon Sep 17 00:00:00 2001 From: Guillaume Meunier Date: Sat, 13 Dec 2025 15:21:29 +0100 Subject: [PATCH] Use vertex colors, use normal maps --- client/render/scene_loader.cpp | 80 +++++++++++++++++++++++++--- client/shaders/common.glsl.inc | 3 +- client/shaders/lit.frag.glsl | 6 +-- client/shaders/lit.vert.glsl | 1 + client/shaders/lit_mask.frag.glsl | 10 ++-- client/shaders/lit_skinned.vert.glsl | 1 + 6 files changed, 86 insertions(+), 15 deletions(-) diff --git a/client/render/scene_loader.cpp b/client/render/scene_loader.cpp index 5d8a4c22..ef44967f 100644 --- a/client/render/scene_loader.cpp +++ b/client/render/scene_loader.cpp @@ -18,12 +18,10 @@ #include "scene_loader.h" -#include "application.h" #include "gpu_buffer.h" #include "image_loader.h" #include "render/scene_components.h" #include "render/vertex_layout.h" -#include "utils/contains.h" #include "utils/files.h" #include "utils/json_string.h" #include "utils/mapped_file.h" @@ -273,19 +271,45 @@ void write_vertex_attribute_aux(std::byte * output, size_t stride, int component { case 1: for (size_t i = 0, n = input.size(); i < n; i++, output += stride) - *reinterpret_cast *>(output) = glm::vec<1, T>(input[i].x); + *reinterpret_cast *>(output) = input[i]; break; case 2: for (size_t i = 0, n = input.size(); i < n; i++, output += stride) - *reinterpret_cast *>(output) = glm::vec<2, T>(input[i].x, input[i].y); + *reinterpret_cast *>(output) = input[i]; break; case 3: for (size_t i = 0, n = input.size(); i < n; i++, output += stride) - *reinterpret_cast *>(output) = glm::vec<3, T>(input[i].x, input[i].y, input[i].z); + *reinterpret_cast *>(output) = input[i]; break; case 4: for (size_t i = 0, n = input.size(); i < n; i++, output += stride) - *reinterpret_cast *>(output) = glm::vec<4, T>(input[i].x, input[i].y, input[i].z, input[i].w); + *reinterpret_cast *>(output) = input[i]; + break; + default: + abort(); + } +} + +template +void write_vertex_attribute_aux(std::byte * output, size_t stride, int components, glm::vec4 value, size_t size) +{ + switch (components) + { + case 1: + for (size_t i = 0, n = size; i < n; i++, output += stride) + *reinterpret_cast *>(output) = value; + break; + case 2: + for (size_t i = 0, n = size; i < n; i++, output += stride) + *reinterpret_cast *>(output) = value; + break; + case 3: + for (size_t i = 0, n = size; i < n; i++, output += stride) + *reinterpret_cast *>(output) = value; + break; + case 4: + for (size_t i = 0, n = size; i < n; i++, output += stride) + *reinterpret_cast *>(output) = value; break; default: abort(); @@ -310,6 +334,24 @@ void write_vertex_attribute(std::vector & output, size_t offset, size } } +void write_vertex_attribute(std::vector & output, size_t offset, size_t stride, vk::Format format, glm::vec4 value, size_t size) +{ + assert(output.size() >= size * stride); + + switch (format) + { + case vk::Format::eR32Sfloat: + case vk::Format::eR32G32Sfloat: + case vk::Format::eR32G32B32Sfloat: + case vk::Format::eR32G32B32A32Sfloat: + write_vertex_attribute_aux(output.data() + offset, stride, vk::componentCount(format), value, size); + return; + + default: + return; + } +} + void copy_vertex_attributes( const fastgltf::Asset & asset, const fastgltf::Accessor & accessor, @@ -322,6 +364,18 @@ void copy_vertex_attributes( write_vertex_attribute(buffer, attribute.offset, binding.stride, attribute.format, read_vertex_attribute(asset, accessor)); } +void fill_vertex_attributes( + glm::vec4 value, + size_t size, + std::vector & buffer, + const vk::VertexInputBindingDescription & binding, + const vk::VertexInputAttributeDescription & attribute) +{ + assert(buffer.size() >= size * binding.stride); + + write_vertex_attribute(buffer, attribute.offset, binding.stride, attribute.format, value, size); +} + std::pair>> create_vertex_buffers( const renderer::vertex_layout & layout, const fastgltf::Asset & asset, @@ -370,7 +424,20 @@ std::pair>> create_vertex_buffers( gltf_attribute = primitive.findAttribute(name + "_0"); if (gltf_attribute == primitive.attributes.cend()) + { + // If the attribute is not specified in the glTF, use the default value or 0 if none is found + static const std::unordered_map default_values = { + std::pair{"COLOR", glm::vec4{1, 1, 1, 1}}}; + + if (auto iter = default_values.find(name); iter != default_values.end()) + fill_vertex_attributes(iter->second, vertex_count, buffer, binding, attribute); + else + { + // The buffer is already initialized at 0 + } + continue; + } } const fastgltf::Accessor & accessor = asset.accessors.at(gltf_attribute->accessorIndex); @@ -842,6 +909,7 @@ public: int binding; + // Attributes that affect the vertices position go in a different buffer if (semantic == "POSITION" or semantic == "JOINTS" or semantic == "WEIGHTS") diff --git a/client/shaders/common.glsl.inc b/client/shaders/common.glsl.inc index b19183fd..7c4f52bf 100644 --- a/client/shaders/common.glsl.inc +++ b/client/shaders/common.glsl.inc @@ -5,7 +5,7 @@ const int nb_clipping = 4; const int nb_texcoords = 2; const bool use_pbr = false; -const bool use_normal_maps = false; +const bool use_normal_maps = true; layout(set = 0, binding = 0) uniform scene_ubo { @@ -136,3 +136,4 @@ layout(location = 5) VS_TO_FS vec2 texcoord_metallic_roughness; layout(location = 6) VS_TO_FS vec2 texcoord_occlusion; layout(location = 7) VS_TO_FS vec2 texcoord_emissive; layout(location = 8) VS_TO_FS vec2 texcoord_normal; +layout(location = 9) VS_TO_FS vec4 vertex_color; diff --git a/client/shaders/lit.frag.glsl b/client/shaders/lit.frag.glsl index 8e7b5811..d85259a5 100644 --- a/client/shaders/lit.frag.glsl +++ b/client/shaders/lit.frag.glsl @@ -30,7 +30,7 @@ layout(early_fragment_tests) in; void main() { - vec4 albedo = material.base_color_factor * texture(base_color_texture, texcoord_base_color); + vec4 albedo = vertex_color * material.base_color_factor * texture(base_color_texture, texcoord_base_color); vec3 emissive_color = (material.base_emissive_factor * texture(emissive_texture, texcoord_emissive)).rgb; vec3 normal_unit; @@ -41,10 +41,10 @@ void main() // glTF spec §3.7.2.1 vec3 bitangent = cross(normal, tangent.xyz) * tangent.w; - mat3 TBN = mat3(tangent.xyz, bitangent, normal); // View space to tangent space? + mat3 TBN = mat3(tangent.xyz, bitangent, normal); // Normal in view space - normal_unit = TBN * sampled_normal; + normal_unit = normalize(TBN * sampled_normal); } else normal_unit = normalize(normal); diff --git a/client/shaders/lit.vert.glsl b/client/shaders/lit.vert.glsl index a612d7c9..7d638bc8 100644 --- a/client/shaders/lit.vert.glsl +++ b/client/shaders/lit.vert.glsl @@ -58,6 +58,7 @@ 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++) { diff --git a/client/shaders/lit_mask.frag.glsl b/client/shaders/lit_mask.frag.glsl index a802bee3..ada559f4 100644 --- a/client/shaders/lit_mask.frag.glsl +++ b/client/shaders/lit_mask.frag.glsl @@ -30,7 +30,7 @@ layout(location = 0) out vec4 out_color; void main() { - vec4 albedo = material.base_color_factor * texture(base_color_texture, texcoord_base_color); + vec4 albedo = vertex_color * material.base_color_factor * texture(base_color_texture, texcoord_base_color); vec3 emissive_color = (material.base_emissive_factor * texture(emissive_texture, texcoord_emissive)).rgb; vec3 normal_unit; @@ -41,10 +41,10 @@ void main() // glTF spec §3.7.2.1 vec3 bitangent = cross(normal, tangent.xyz) * tangent.w; - mat3 TBN = mat3(tangent.xyz, bitangent, normal); // View space to tangent space? + mat3 TBN = mat3(tangent.xyz, bitangent, normal); // Normal in view space - normal_unit = TBN * sampled_normal; + normal_unit = normalize(TBN * sampled_normal); } else normal_unit = normalize(normal); @@ -65,8 +65,8 @@ void main() vec3 light_colour = scene.light_color.rgb; float attenuation = 1; vec3 radiance = - per_light_radiance(albedo.rgb, metallic, roughness, light_colour, light_dir, normal_unit, view_dir, half_vec, attenuation) + - vec3(scene.ambient_color) * albedo.rgb * ao; + per_light_radiance(albedo.rgb, metallic, roughness, light_colour, light_dir, normal_unit, view_dir, half_vec, attenuation) + + vec3(scene.ambient_color) * albedo.rgb * ao; vec3 tmp = radiance + emissive_color; diff --git a/client/shaders/lit_skinned.vert.glsl b/client/shaders/lit_skinned.vert.glsl index 5c8e7261..51bd6082 100644 --- a/client/shaders/lit_skinned.vert.glsl +++ b/client/shaders/lit_skinned.vert.glsl @@ -67,6 +67,7 @@ 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++) {