diff --git a/shaders/mesh.vert b/shaders/mesh.vert index 0d8afe3..fee5ffa 100644 --- a/shaders/mesh.vert +++ b/shaders/mesh.vert @@ -37,15 +37,19 @@ void main() { Vertex v = PushConstants.vertexBuffer.vertices[gl_VertexIndex]; - vec4 worldPos = PushConstants.render_matrix * vec4(v.position, 1.0f); + mat3 M = mat3(PushConstants.render_matrix); + mat3 normalMatrix = transpose(inverse(M)); + + vec4 worldPos = PushConstants.render_matrix * vec4(v.position, 1.0); gl_Position = sceneData.viewproj * worldPos; - outNormal = (PushConstants.render_matrix * vec4(v.normal, 0.f)).xyz; - vec3 worldTangent = (PushConstants.render_matrix * vec4(v.tangent.xyz, 0.f)).xyz; - outTangent = vec4(normalize(worldTangent), v.tangent.w); - // Pass pure vertex color; apply baseColorFactor only in fragment + outNormal = normalize(normalMatrix * v.normal); + + vec3 worldTangent = normalize(normalMatrix * v.tangent.xyz); + outTangent = vec4(worldTangent, v.tangent.w); + outColor = v.color.xyz; - outUV.x = v.uv_x; - outUV.y = v.uv_y; + outUV = vec2(v.uv_x, v.uv_y); outWorldPos = worldPos.xyz; } + diff --git a/src/scene/vk_loader.cpp b/src/scene/vk_loader.cpp index 92fdbbe..df642be 100644 --- a/src/scene/vk_loader.cpp +++ b/src/scene/vk_loader.cpp @@ -612,8 +612,24 @@ std::optional > loadGltf(VulkanEngine *engine, std:: }); } - // Generate tangents if missing and we have UVs - if (!hasTangents) + // Generate tangents only when needed for normal mapping. + // If the bound material has no normal map, we can skip TBN generation. + bool materialHasNormalMap = false; + if (p.materialIndex.has_value()) + { + size_t matIndex = p.materialIndex.value(); + if (matIndex < gltf.materials.size()) + { + materialHasNormalMap = gltf.materials[matIndex].normalTexture.has_value(); + } + } + else if (!gltf.materials.empty()) + { + // Primitives without an explicit material fall back to material 0. + materialHasNormalMap = gltf.materials[0].normalTexture.has_value(); + } + + if (!hasTangents && materialHasNormalMap) { size_t primIndexStart = newSurface.startIndex; size_t primIndexCount = newSurface.count; diff --git a/texture_compression.py b/texture_compression.py index c871329..1829489 100644 --- a/texture_compression.py +++ b/texture_compression.py @@ -10,7 +10,7 @@ except Exception: DEFAULT_SUFFIX = { "albedo": ["_albedo", "_basecolor", "_base_colour", "_base_color", "_base", "baseColor", "BaseColor"], - "mr": ["_mr", "_orm", "_metalrough", "_metallicroughness", "metallicRoughness", "Metallic"], + "mr": ["_mr", "_orm", "_metalrough", "_metallicroughness", "metallicRoughness", "Metallic", "Metalness"], "normal": ["_normal", "_norm", "_nrm", "_normalgl", "Normal"], "occlusion": ["_occlusion", "_occ", "_ao"], "emissive": ["_emissive", "_emission", "_emit"],