Skip to content

Commit f2ecf21

Browse files
committed
Fix GLTF validation failures
1 parent c34fae4 commit f2ecf21

2 files changed

Lines changed: 14 additions & 5 deletions

File tree

src/GLTF.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <format>
99
#include <iostream>
1010
#include <numbers>
11+
#include <algorithm>
1112

1213
bool hasValidNormals(const Mesh& mesh, const Face& face)
1314
{
@@ -210,7 +211,7 @@ std::size_t GLTFExporter::buildPrimitiveColor(std::vector<Face> faces)
210211
data.push_back(face.color3);
211212
}
212213

213-
return buildAccessor(data, TINYGLTF_COMPONENT_TYPE_UNSIGNED_BYTE, TINYGLTF_TYPE_VEC3, TINYGLTF_TARGET_ARRAY_BUFFER);
214+
return buildAccessor(data, TINYGLTF_COMPONENT_TYPE_UNSIGNED_BYTE, TINYGLTF_TYPE_VEC3, TINYGLTF_TARGET_ARRAY_BUFFER, true);
214215
}
215216

216217
std::size_t GLTFExporter::buildPrimitiveTexcoord(std::vector<Face> faces)
@@ -229,7 +230,8 @@ std::size_t GLTFExporter::buildPrimitiveTexcoord(std::vector<Face> faces)
229230
}
230231

231232
template<typename T>
232-
std::size_t GLTFExporter::buildAccessor(std::vector<T> data, int componentType, int type, int target)
233+
std::size_t
234+
GLTFExporter::buildAccessor(std::vector<T> data, int componentType, int type, int target, bool normalized)
233235
{
234236
tinygltf::Buffer buffer;
235237
T min = data[0];
@@ -248,6 +250,8 @@ std::size_t GLTFExporter::buildAccessor(std::vector<T> data, int componentType,
248250
tinygltf::BufferView view;
249251
view.buffer = bufferId;
250252
view.byteLength = buffer.data.size();
253+
if(target == TINYGLTF_TARGET_ARRAY_BUFFER)
254+
view.byteStride = sizeof(T);
251255
view.byteOffset = 0;
252256
view.target = target;
253257

@@ -259,6 +263,7 @@ std::size_t GLTFExporter::buildAccessor(std::vector<T> data, int componentType,
259263
accessor.componentType = componentType;
260264
accessor.count = data.size();
261265
accessor.type = type;
266+
accessor.normalized = normalized;
262267
push_to_vector(accessor.maxValues, max);
263268
push_to_vector(accessor.minValues, min);
264269

@@ -387,10 +392,13 @@ int32_t GLTFExporter::buildMaterial(MaterialMode mode)
387392
{
388393
tinygltf::Value unlit;
389394
mat.extensions["KHR_materials_unlit"] = unlit;
395+
const auto& extUsed = model.extensionsUsed;
396+
if (std::find(extUsed.begin(), extUsed.end(), "KHR_materials_unlit") == extUsed.end())
397+
model.extensionsUsed.push_back("KHR_materials_unlit");
390398
}
391399

392400
mat.pbrMetallicRoughness.baseColorFactor = { 1.0f, 1.0f, 1.0f, 1.0f };
393-
mat.pbrMetallicRoughness.metallicFactor = 0.0f;
401+
mat.pbrMetallicRoughness.metallicFactor = 0.0f;
394402
if (mode.type != MaterialType::COLOR) mat.pbrMetallicRoughness.baseColorTexture.index = 0;
395403
auto id = push(model.materials, mat);
396404
materialMapping[mode] = id;
@@ -527,7 +535,7 @@ void GLTFExporter::buildAnimations()
527535
}
528536
if (!data.sound.empty()) extras.emplace("sounds", soundArray);
529537
if (!data.texture.empty()) extras.emplace("textures", textureArray);
530-
anim.extras = tinygltf::Value(extras);
538+
if (!extras.empty()) anim.extras = tinygltf::Value(extras);
531539
push(model.animations, anim);
532540
}
533541
}

src/GLTF.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ struct ColorRGB
1010
uint8_t r;
1111
uint8_t g;
1212
uint8_t b;
13+
uint8_t unused;
1314

1415
ColorRGB() = default;
1516
ColorRGB(Color color)
@@ -62,7 +63,7 @@ class GLTFExporter
6263
void buildAnimations();
6364
void buildTexture();
6465

65-
template<typename T> std::size_t buildAccessor(std::vector<T> data, int componentType, int type, int target);
66+
template<typename T> std::size_t buildAccessor(std::vector<T> data, int componentType, int type, int target, bool normalized = false);
6667

6768
int32_t buildMaterial(MaterialMode mode);
6869
tinygltf::Primitive buildPrimitive(const Mesh& mesh, MaterialMode material, std::vector<Face> faces);

0 commit comments

Comments
 (0)