88#include < format>
99#include < iostream>
1010#include < numbers>
11+ #include < algorithm>
1112
1213bool 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
216217std::size_t GLTFExporter::buildPrimitiveTexcoord (std::vector<Face> faces)
@@ -229,7 +230,8 @@ std::size_t GLTFExporter::buildPrimitiveTexcoord(std::vector<Face> faces)
229230}
230231
231232template <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}
0 commit comments