From 258cdfd2bc29a920bbe749962abbcd58caa76422 Mon Sep 17 00:00:00 2001 From: Julian Knodt Date: Mon, 23 Dec 2024 12:57:13 -0800 Subject: [PATCH] Export tangents in GLTF (#5900) Previously tangents were not being exported. If they are present, they should also be properly exported. Co-authored-by: Kim Kulling --- code/AssetLib/glTF2/glTF2AssetWriter.inl | 1 + code/AssetLib/glTF2/glTF2Exporter.cpp | 14 ++++++++++++++ 2 files changed, 15 insertions(+) diff --git a/code/AssetLib/glTF2/glTF2AssetWriter.inl b/code/AssetLib/glTF2/glTF2AssetWriter.inl index 0ca23863c8..d08ea6f4ef 100644 --- a/code/AssetLib/glTF2/glTF2AssetWriter.inl +++ b/code/AssetLib/glTF2/glTF2AssetWriter.inl @@ -608,6 +608,7 @@ namespace glTF2 { { WriteAttrs(w, attrs, p.attributes.position, "POSITION"); WriteAttrs(w, attrs, p.attributes.normal, "NORMAL"); + WriteAttrs(w, attrs, p.attributes.tangent, "TANGENT"); WriteAttrs(w, attrs, p.attributes.texcoord, "TEXCOORD", true); WriteAttrs(w, attrs, p.attributes.color, "COLOR", true); WriteAttrs(w, attrs, p.attributes.joint, "JOINTS", true); diff --git a/code/AssetLib/glTF2/glTF2Exporter.cpp b/code/AssetLib/glTF2/glTF2Exporter.cpp index c3882290b1..6b6542939e 100644 --- a/code/AssetLib/glTF2/glTF2Exporter.cpp +++ b/code/AssetLib/glTF2/glTF2Exporter.cpp @@ -1215,6 +1215,20 @@ void glTF2Exporter::ExportMeshes() { p.attributes.normal.push_back(n); } + /******************** Tangents ********************/ + if (nullptr != aim->mTangents) { + for (uint32_t i = 0; i < aim->mNumVertices; ++i) { + aim->mTangents[i].NormalizeSafe(); + } + Ref t = ExportData( + *mAsset, meshId, b, aim->mNumVertices, aim->mTangents, AttribType::VEC3, + AttribType::VEC3, ComponentType_FLOAT, BufferViewTarget_ARRAY_BUFFER + ); + if (t) { + p.attributes.tangent.push_back(t); + } + } + /************** Texture coordinates **************/ for (int i = 0; i < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++i) { if (!aim->HasTextureCoords(i)) {