Skip to content

Commit aa10e7b

Browse files
committed
Fixed TANE export
The original model is broken and contains wrong normal data
1 parent 44a4390 commit aa10e7b

1 file changed

Lines changed: 25 additions & 4 deletions

File tree

src/GLTF.cpp

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,19 @@
55

66
#include "GLTF.hpp"
77

8+
#include <iostream>
89
#include <numbers>
910

11+
constexpr bool hasValidNormals(Mesh& mesh, Face& face)
12+
{
13+
const std::size_t size = mesh.normals.size();
14+
15+
if (face.n1 >= size) return false;
16+
if (face.n2 >= size) return false;
17+
if (face.n3 >= size) return false;
18+
return true;
19+
}
20+
1021
struct Quaternion
1122
{
1223
float x;
@@ -169,9 +180,19 @@ std::size_t GLTFExporter::buildPrimitiveNormal(Mesh& mesh, std::vector<Face> fac
169180

170181
for (auto& face : faces)
171182
{
172-
data.push_back(mesh.normals[face.n1]);
173-
data.push_back(mesh.normals[face.n2]);
174-
data.push_back(mesh.normals[face.n3]);
183+
if (hasValidNormals(mesh, face))
184+
{
185+
data.push_back(mesh.normals[face.n1]);
186+
data.push_back(mesh.normals[face.n2]);
187+
data.push_back(mesh.normals[face.n3]);
188+
}
189+
else
190+
{
191+
data.push_back({ 0.0f, 1.0f, 0.0f });
192+
data.push_back({ 0.0f, 1.0f, 0.0f });
193+
data.push_back({ 0.0f, 1.0f, 0.0f });
194+
std::cout << "Source model contains invalid normal data, using empty fallback values." << std::endl;
195+
}
175196
}
176197

177198
return buildAccessor(data, TINYGLTF_COMPONENT_TYPE_FLOAT, TINYGLTF_TYPE_VEC3, TINYGLTF_TARGET_ARRAY_BUFFER);
@@ -373,7 +394,7 @@ int32_t GLTFExporter::buildMaterial(MaterialMode mode)
373394

374395
void GLTFExporter::buildAnimations()
375396
{
376-
int i = 0;
397+
int i = 0;
377398
for (auto& raw : mmd.anims->anims)
378399
{
379400
Animation data(raw);

0 commit comments

Comments
 (0)