utglTF2ImportExport.cpp 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2025, assimp team
  6. All rights reserved.
  7. Redistribution and use of this software in source and binary forms,
  8. with or without modification, are permitted provided that the following
  9. conditions are met:
  10. * Redistributions of source code must retain the above
  11. copyright notice, this list of conditions and the
  12. following disclaimer.
  13. * Redistributions in binary form must reproduce the above
  14. copyright notice, this list of conditions and the
  15. following disclaimer in the documentation and/or other
  16. materials provided with the distribution.
  17. * Neither the name of the assimp team, nor the names of its
  18. contributors may be used to endorse or promote products
  19. derived from this software without specific prior
  20. written permission of the assimp team.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. ---------------------------------------------------------------------------
  33. */
  34. #include "AbstractImportExportBase.h"
  35. #include "UnitTestPCH.h"
  36. #include "Tools/TestTools.h"
  37. #include <assimp/commonMetaData.h>
  38. #include <assimp/postprocess.h>
  39. #include <assimp/config.h>
  40. #include <assimp/scene.h>
  41. #include <assimp/Exporter.hpp>
  42. #include <assimp/Importer.hpp>
  43. #include <assimp/LogStream.hpp>
  44. #include <assimp/DefaultLogger.hpp>
  45. #include <rapidjson/schema.h>
  46. #include <array>
  47. #include <assimp/material.h>
  48. #include <assimp/GltfMaterial.h>
  49. using namespace Assimp;
  50. class utglTF2ImportExport : public AbstractImportExportBase {
  51. public:
  52. virtual bool importerMatTest(const char *file, bool spec, bool gloss, std::array<aiTextureMapMode, 2> exp_modes = { aiTextureMapMode_Wrap, aiTextureMapMode_Wrap }) {
  53. Assimp::Importer importer;
  54. const aiScene *scene = importer.ReadFile(file, aiProcess_ValidateDataStructure);
  55. EXPECT_NE(scene, nullptr);
  56. if (!scene) {
  57. return false;
  58. }
  59. EXPECT_TRUE(scene->HasMaterials());
  60. if (!scene->HasMaterials()) {
  61. return false;
  62. }
  63. const aiMaterial *material = scene->mMaterials[0];
  64. // This Material should be a PBR
  65. aiShadingMode shadingMode;
  66. EXPECT_EQ(aiReturn_SUCCESS, material->Get(AI_MATKEY_SHADING_MODEL, shadingMode));
  67. EXPECT_EQ(aiShadingMode_PBR_BRDF, shadingMode);
  68. // Should import the texture as diffuse and as base color
  69. aiString path;
  70. std::array<aiTextureMapMode,2> modes;
  71. EXPECT_EQ(aiReturn_SUCCESS, material->GetTexture(aiTextureType_DIFFUSE, 0, &path, nullptr, nullptr,
  72. nullptr, nullptr, modes.data()));
  73. EXPECT_STREQ(path.C_Str(), "CesiumLogoFlat.png");
  74. EXPECT_EQ(exp_modes, modes);
  75. // Also as Base Color
  76. EXPECT_EQ(aiReturn_SUCCESS, material->GetTexture(aiTextureType_BASE_COLOR, 0, &path, nullptr, nullptr,
  77. nullptr, nullptr, modes.data()));
  78. EXPECT_STREQ(path.C_Str(), "CesiumLogoFlat.png");
  79. EXPECT_EQ(exp_modes, modes);
  80. // Should have a MetallicFactor (default is 1.0)
  81. ai_real metal_factor = ai_real(0.5);
  82. EXPECT_EQ(aiReturn_SUCCESS, material->Get(AI_MATKEY_METALLIC_FACTOR, metal_factor));
  83. EXPECT_EQ(ai_real(0.0), metal_factor);
  84. // And a roughness factor (default is 1.0)
  85. ai_real roughness_factor = ai_real(0.5);
  86. EXPECT_EQ(aiReturn_SUCCESS, material->Get(AI_MATKEY_ROUGHNESS_FACTOR, roughness_factor));
  87. EXPECT_EQ(ai_real(1.0), roughness_factor);
  88. aiColor3D spec_color = { 0, 0, 0 };
  89. ai_real glossiness = ai_real(0.5);
  90. if (spec) {
  91. EXPECT_EQ(aiReturn_SUCCESS, material->Get(AI_MATKEY_COLOR_SPECULAR, spec_color));
  92. constexpr ai_real spec_val(0.20000000298023225); // From the file
  93. EXPECT_EQ(spec_val, spec_color.r);
  94. EXPECT_EQ(spec_val, spec_color.g);
  95. EXPECT_EQ(spec_val, spec_color.b);
  96. } else {
  97. EXPECT_EQ(aiReturn_FAILURE, material->Get(AI_MATKEY_COLOR_SPECULAR, spec_color));
  98. }
  99. if (gloss) {
  100. EXPECT_EQ(aiReturn_SUCCESS, material->Get(AI_MATKEY_GLOSSINESS_FACTOR, glossiness));
  101. EXPECT_EQ(ai_real(1.0), glossiness);
  102. } else {
  103. EXPECT_EQ(aiReturn_FAILURE, material->Get(AI_MATKEY_GLOSSINESS_FACTOR, glossiness));
  104. }
  105. return true;
  106. }
  107. virtual bool binaryImporterTest() {
  108. Assimp::Importer importer;
  109. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/2CylinderEngine-glTF-Binary/2CylinderEngine.glb",
  110. aiProcess_ValidateDataStructure);
  111. return nullptr != scene;
  112. }
  113. #ifndef ASSIMP_BUILD_NO_EXPORT
  114. virtual bool exporterTest() {
  115. Assimp::Importer importer;
  116. Assimp::Exporter exporter;
  117. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/BoxTextured-glTF/BoxTextured.gltf",
  118. aiProcess_ValidateDataStructure);
  119. EXPECT_NE(nullptr, scene);
  120. EXPECT_EQ(aiReturn_SUCCESS, exporter.Export(scene, "gltf2", ASSIMP_TEST_MODELS_DIR "/glTF2/BoxTextured-glTF/BoxTextured_out.gltf"));
  121. return true;
  122. }
  123. #endif // ASSIMP_BUILD_NO_EXPORT
  124. };
  125. TEST_F(utglTF2ImportExport, importglTF2FromFileTest) {
  126. EXPECT_TRUE(importerMatTest(ASSIMP_TEST_MODELS_DIR "/glTF2/BoxTextured-glTF/BoxTextured.gltf", false, false, {aiTextureMapMode_Mirror, aiTextureMapMode_Clamp}));
  127. }
  128. TEST_F(utglTF2ImportExport, importBinaryglTF2FromFileTest) {
  129. EXPECT_TRUE(binaryImporterTest());
  130. }
  131. TEST_F(utglTF2ImportExport, importglTF2_KHR_materials_pbrSpecularGlossiness) {
  132. EXPECT_TRUE(importerMatTest(ASSIMP_TEST_MODELS_DIR "/glTF2/BoxTextured-glTF-pbrSpecularGlossiness/BoxTextured.gltf", true, true));
  133. }
  134. void VerifyClearCoatScene(const aiScene *scene) {
  135. ASSERT_NE(nullptr, scene);
  136. ASSERT_TRUE(scene->HasMaterials());
  137. // Find a specific Clearcoat material and check the values
  138. const aiString partial_coated("Partial_Coated");
  139. bool found_partial_coat = false;
  140. for (size_t i = 0; i < scene->mNumMaterials; ++i) {
  141. const aiMaterial *material = scene->mMaterials[i];
  142. ASSERT_NE(nullptr, material);
  143. if (material->GetName() == partial_coated) {
  144. found_partial_coat = true;
  145. ai_real clearcoat_factor(0.0f);
  146. EXPECT_EQ(aiReturn_SUCCESS, material->Get(AI_MATKEY_CLEARCOAT_FACTOR, clearcoat_factor));
  147. EXPECT_EQ(ai_real(1.0f), clearcoat_factor);
  148. ai_real clearcoat_rough_factor(0.0f);
  149. EXPECT_EQ(aiReturn_SUCCESS, material->Get(AI_MATKEY_CLEARCOAT_ROUGHNESS_FACTOR, clearcoat_rough_factor));
  150. EXPECT_EQ(ai_real(0.03f), clearcoat_rough_factor);
  151. // Should import the texture as diffuse and as base color
  152. aiString path;
  153. std::array<aiTextureMapMode, 2> modes;
  154. static const std::array<aiTextureMapMode, 2> exp_modes = { aiTextureMapMode_Wrap, aiTextureMapMode_Wrap };
  155. EXPECT_EQ(aiReturn_SUCCESS, material->GetTexture(AI_MATKEY_CLEARCOAT_TEXTURE, &path, nullptr, nullptr,
  156. nullptr, nullptr, modes.data()));
  157. EXPECT_STREQ(path.C_Str(), "PartialCoating.png");
  158. EXPECT_EQ(exp_modes, modes);
  159. }
  160. }
  161. EXPECT_TRUE(found_partial_coat);
  162. }
  163. TEST_F(utglTF2ImportExport, importglTF2_KHR_materials_clearcoat) {
  164. Assimp::Importer importer;
  165. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/ClearCoat-glTF/ClearCoatTest.gltf", aiProcess_ValidateDataStructure);
  166. VerifyClearCoatScene(scene);
  167. }
  168. #ifndef ASSIMP_BUILD_NO_EXPORT
  169. TEST_F(utglTF2ImportExport, importglTF2AndExport_KHR_materials_clearcoat) {
  170. {
  171. Assimp::Importer importer;
  172. Assimp::Exporter exporter;
  173. const aiScene* scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/ClearCoat-glTF/ClearCoatTest.gltf", aiProcess_ValidateDataStructure);
  174. ASSERT_NE(nullptr, scene);
  175. // Export
  176. EXPECT_EQ(aiReturn_SUCCESS, exporter.Export(scene, "glb2", ASSIMP_TEST_MODELS_DIR "/glTF2/ClearCoat-glTF/ClearCoatTest_out.glb"));
  177. }
  178. // And re-import
  179. Assimp::Importer importer;
  180. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/ClearCoat-glTF/ClearCoatTest_out.glb", aiProcess_ValidateDataStructure);
  181. VerifyClearCoatScene(scene);
  182. }
  183. TEST_F(utglTF2ImportExport, importglTF2AndExport_KHR_materials_pbrSpecularGlossiness) {
  184. Assimp::Importer importer;
  185. Assimp::Exporter exporter;
  186. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/BoxTextured-glTF-pbrSpecularGlossiness/BoxTextured.gltf",
  187. aiProcess_ValidateDataStructure);
  188. EXPECT_NE(nullptr, scene);
  189. // Export with specular glossiness disabled
  190. EXPECT_EQ(aiReturn_SUCCESS, exporter.Export(scene, "glb2", ASSIMP_TEST_MODELS_DIR "/glTF2/BoxTextured-glTF-pbrSpecularGlossiness/BoxTextured_out.glb"));
  191. // And re-import
  192. EXPECT_TRUE(importerMatTest(ASSIMP_TEST_MODELS_DIR "/glTF2/BoxTextured-glTF-pbrSpecularGlossiness/BoxTextured_out.glb", true, false));
  193. // Export with specular glossiness enabled
  194. ExportProperties props;
  195. props.SetPropertyBool(AI_CONFIG_USE_GLTF_PBR_SPECULAR_GLOSSINESS, true);
  196. EXPECT_EQ(aiReturn_SUCCESS, exporter.Export(scene, "glb2", ASSIMP_TEST_MODELS_DIR "/glTF2/BoxTextured-glTF-pbrSpecularGlossiness/BoxTextured_out.glb", 0, &props));
  197. // And re-import
  198. EXPECT_TRUE(importerMatTest(ASSIMP_TEST_MODELS_DIR "/glTF2/BoxTextured-glTF-pbrSpecularGlossiness/BoxTextured_out.glb", true, true));
  199. }
  200. TEST_F(utglTF2ImportExport, importglTF2AndExportToOBJ) {
  201. Assimp::Importer importer;
  202. Assimp::Exporter exporter;
  203. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/BoxTextured-glTF/BoxTextured.gltf",
  204. aiProcess_ValidateDataStructure);
  205. EXPECT_NE(nullptr, scene);
  206. EXPECT_EQ(aiReturn_SUCCESS, exporter.Export(scene, "obj", ASSIMP_TEST_MODELS_DIR "/glTF2/BoxTextured-glTF/BoxTextured_out.obj"));
  207. }
  208. TEST_F(utglTF2ImportExport, importglTF2EmbeddedAndExportToOBJ) {
  209. Assimp::Importer importer;
  210. Assimp::Exporter exporter;
  211. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/BoxTextured-glTF-Embedded/BoxTextured.gltf",
  212. aiProcess_ValidateDataStructure);
  213. EXPECT_NE(nullptr, scene);
  214. EXPECT_EQ(aiReturn_SUCCESS, exporter.Export(scene, "obj", ASSIMP_TEST_MODELS_DIR "/glTF2/BoxTextured-glTF-Embedded/BoxTextured_out.obj"));
  215. }
  216. #endif // ASSIMP_BUILD_NO_EXPORT
  217. TEST_F(utglTF2ImportExport, importglTF2PrimitiveModePointsWithoutIndices) {
  218. Assimp::Importer importer;
  219. //Points without indices
  220. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/glTF-Asset-Generator/Mesh_PrimitiveMode/Mesh_PrimitiveMode_00.gltf", aiProcess_ValidateDataStructure);
  221. EXPECT_NE(nullptr, scene);
  222. EXPECT_EQ(scene->mMeshes[0]->mNumVertices, 1024u);
  223. for (unsigned int i = 0; i < scene->mMeshes[0]->mNumFaces; ++i) {
  224. EXPECT_EQ(scene->mMeshes[0]->mFaces[i].mNumIndices, 1u);
  225. EXPECT_EQ(scene->mMeshes[0]->mFaces[i].mIndices[0], i);
  226. }
  227. }
  228. TEST_F(utglTF2ImportExport, importglTF2PrimitiveModeLinesWithoutIndices) {
  229. Assimp::Importer importer;
  230. //Lines without indices
  231. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/glTF-Asset-Generator/Mesh_PrimitiveMode/Mesh_PrimitiveMode_01.gltf", aiProcess_ValidateDataStructure);
  232. EXPECT_NE(nullptr, scene);
  233. EXPECT_EQ(scene->mMeshes[0]->mNumVertices, 8u);
  234. for (unsigned int i = 0; i < scene->mMeshes[0]->mNumFaces; ++i) {
  235. EXPECT_EQ(scene->mMeshes[0]->mFaces[i].mNumIndices, 2u);
  236. EXPECT_EQ(scene->mMeshes[0]->mFaces[i].mIndices[0], i * 2u);
  237. EXPECT_EQ(scene->mMeshes[0]->mFaces[i].mIndices[1], i * 2u + 1u);
  238. }
  239. }
  240. TEST_F(utglTF2ImportExport, importglTF2PrimitiveModeLinesLoopWithoutIndices) {
  241. Assimp::Importer importer;
  242. //Lines loop without indices
  243. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/glTF-Asset-Generator/Mesh_PrimitiveMode/Mesh_PrimitiveMode_02.gltf", aiProcess_ValidateDataStructure);
  244. EXPECT_NE(nullptr, scene);
  245. EXPECT_EQ(scene->mMeshes[0]->mNumVertices, 4u);
  246. std::array<unsigned int, 5> l1 = { { 0u, 1u, 2u, 3u, 0u } };
  247. EXPECT_EQ(scene->mMeshes[0]->mFaces[0].mNumIndices, 2u);
  248. for (unsigned int i = 0; i < scene->mMeshes[0]->mNumFaces; ++i) {
  249. EXPECT_EQ(scene->mMeshes[0]->mFaces[i].mNumIndices, 2u);
  250. EXPECT_EQ(scene->mMeshes[0]->mFaces[i].mIndices[0], l1[i]);
  251. EXPECT_EQ(scene->mMeshes[0]->mFaces[i].mIndices[1], l1[i + 1u]);
  252. }
  253. }
  254. TEST_F(utglTF2ImportExport, importglTF2PrimitiveModeLinesStripWithoutIndices) {
  255. Assimp::Importer importer;
  256. //Lines strip without indices
  257. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/glTF-Asset-Generator/Mesh_PrimitiveMode/Mesh_PrimitiveMode_03.gltf", aiProcess_ValidateDataStructure);
  258. EXPECT_NE(nullptr, scene);
  259. EXPECT_EQ(scene->mMeshes[0]->mNumVertices, 5u);
  260. EXPECT_EQ(scene->mMeshes[0]->mFaces[0].mNumIndices, 2u);
  261. for (unsigned int i = 0; i < scene->mMeshes[0]->mNumFaces; ++i) {
  262. EXPECT_EQ(scene->mMeshes[0]->mFaces[i].mNumIndices, 2u);
  263. EXPECT_EQ(scene->mMeshes[0]->mFaces[i].mIndices[0], i);
  264. EXPECT_EQ(scene->mMeshes[0]->mFaces[i].mIndices[1], i + 1u);
  265. }
  266. }
  267. TEST_F(utglTF2ImportExport, importglTF2PrimitiveModeTrianglesStripWithoutIndices) {
  268. Assimp::Importer importer;
  269. //Triangles strip without indices
  270. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/glTF-Asset-Generator/Mesh_PrimitiveMode/Mesh_PrimitiveMode_04.gltf", aiProcess_ValidateDataStructure);
  271. EXPECT_NE(nullptr, scene);
  272. EXPECT_EQ(scene->mMeshes[0]->mNumFaces, 2u);
  273. EXPECT_EQ(scene->mMeshes[0]->mNumVertices, 4u);
  274. std::array<unsigned int, 3> f1 = { { 0u, 1u, 2u } };
  275. EXPECT_EQ(scene->mMeshes[0]->mFaces[0].mNumIndices, 3u);
  276. for (unsigned int i = 0; i < 3; ++i) {
  277. EXPECT_EQ(scene->mMeshes[0]->mFaces[0].mIndices[i], f1[i]);
  278. }
  279. std::array<unsigned int, 3> f2 = { { 2u, 1u, 3u } };
  280. EXPECT_EQ(scene->mMeshes[0]->mFaces[1].mNumIndices, 3u);
  281. for (size_t i = 0; i < 3; ++i) {
  282. EXPECT_EQ(scene->mMeshes[0]->mFaces[1].mIndices[i], f2[i]);
  283. }
  284. }
  285. TEST_F(utglTF2ImportExport, importglTF2PrimitiveModeTrianglesFanWithoutIndices) {
  286. Assimp::Importer importer;
  287. //Triangles fan without indices
  288. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/glTF-Asset-Generator/Mesh_PrimitiveMode/Mesh_PrimitiveMode_05.gltf", aiProcess_ValidateDataStructure);
  289. EXPECT_NE(nullptr, scene);
  290. EXPECT_EQ(scene->mMeshes[0]->mNumFaces, 2u);
  291. EXPECT_EQ(scene->mMeshes[0]->mNumVertices, 4u);
  292. std::array<unsigned int, 3> f1 = { { 0u, 1u, 2u } };
  293. EXPECT_EQ(scene->mMeshes[0]->mFaces[0].mNumIndices, 3u);
  294. for (size_t i = 0; i < 3; ++i) {
  295. EXPECT_EQ(scene->mMeshes[0]->mFaces[0].mIndices[i], f1[i]);
  296. }
  297. std::array<unsigned int, 3> f2 = { { 0u, 2u, 3u } };
  298. EXPECT_EQ(scene->mMeshes[0]->mFaces[1].mNumIndices, 3u);
  299. for (size_t i = 0; i < 3; ++i) {
  300. EXPECT_EQ(scene->mMeshes[0]->mFaces[1].mIndices[i], f2[i]);
  301. }
  302. }
  303. TEST_F(utglTF2ImportExport, importglTF2PrimitiveModeTrianglesWithoutIndices) {
  304. Assimp::Importer importer;
  305. //Triangles without indices
  306. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/glTF-Asset-Generator/Mesh_PrimitiveMode/Mesh_PrimitiveMode_06.gltf", aiProcess_ValidateDataStructure);
  307. EXPECT_NE(nullptr, scene);
  308. EXPECT_EQ(scene->mMeshes[0]->mNumFaces, 2u);
  309. EXPECT_EQ(scene->mMeshes[0]->mNumVertices, 6u);
  310. std::array<unsigned int, 3> f1 = { { 0u, 1u, 2u } };
  311. EXPECT_EQ(scene->mMeshes[0]->mFaces[0].mNumIndices, 3u);
  312. for (size_t i = 0; i < 3; ++i) {
  313. EXPECT_EQ(scene->mMeshes[0]->mFaces[0].mIndices[i], f1[i]);
  314. }
  315. std::array<unsigned int, 3> f2 = { { 3u, 4u, 5u } };
  316. EXPECT_EQ(scene->mMeshes[0]->mFaces[1].mNumIndices, 3u);
  317. for (size_t i = 0; i < 3; ++i) {
  318. EXPECT_EQ(scene->mMeshes[0]->mFaces[1].mIndices[i], f2[i]);
  319. }
  320. }
  321. TEST_F(utglTF2ImportExport, importglTF2PrimitiveModePoints) {
  322. Assimp::Importer importer;
  323. //Line loop
  324. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/glTF-Asset-Generator/Mesh_PrimitiveMode/Mesh_PrimitiveMode_07.gltf", aiProcess_ValidateDataStructure);
  325. EXPECT_NE(nullptr, scene);
  326. EXPECT_EQ(scene->mMeshes[0]->mNumVertices, 1024u);
  327. for (unsigned int i = 0; i < scene->mMeshes[0]->mNumFaces; ++i) {
  328. EXPECT_EQ(scene->mMeshes[0]->mFaces[i].mNumIndices, 1u);
  329. EXPECT_EQ(scene->mMeshes[0]->mFaces[i].mIndices[0], i);
  330. }
  331. }
  332. TEST_F(utglTF2ImportExport, importglTF2PrimitiveModeLines) {
  333. Assimp::Importer importer;
  334. //Lines
  335. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/glTF-Asset-Generator/Mesh_PrimitiveMode/Mesh_PrimitiveMode_08.gltf", aiProcess_ValidateDataStructure);
  336. EXPECT_NE(nullptr, scene);
  337. EXPECT_EQ(scene->mMeshes[0]->mNumVertices, 4u);
  338. std::array<unsigned int, 5> l1 = { { 0u, 1u, 2u, 3u, 0u } };
  339. EXPECT_EQ(scene->mMeshes[0]->mFaces[0].mNumIndices, 2u);
  340. for (unsigned int i = 0; i < scene->mMeshes[0]->mNumFaces; ++i) {
  341. EXPECT_EQ(scene->mMeshes[0]->mFaces[i].mIndices[0], l1[i]);
  342. EXPECT_EQ(scene->mMeshes[0]->mFaces[i].mIndices[1], l1[i + 1]);
  343. }
  344. }
  345. TEST_F(utglTF2ImportExport, importglTF2PrimitiveModeLineLoop) {
  346. Assimp::Importer importer;
  347. //Line loop
  348. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/glTF-Asset-Generator/Mesh_PrimitiveMode/Mesh_PrimitiveMode_09.gltf", aiProcess_ValidateDataStructure);
  349. EXPECT_NE(nullptr, scene);
  350. EXPECT_EQ(scene->mMeshes[0]->mNumVertices, 4u);
  351. std::array<unsigned int, 5> l1 = { { 0, 1u, 2u, 3u, 0u } };
  352. EXPECT_EQ(scene->mMeshes[0]->mFaces[0].mNumIndices, 2u);
  353. for (unsigned int i = 0; i < scene->mMeshes[0]->mNumFaces; ++i) {
  354. EXPECT_EQ(scene->mMeshes[0]->mFaces[i].mIndices[0], l1[i]);
  355. EXPECT_EQ(scene->mMeshes[0]->mFaces[i].mIndices[1], l1[i + 1]);
  356. }
  357. }
  358. TEST_F(utglTF2ImportExport, importglTF2PrimitiveModeLineStrip) {
  359. Assimp::Importer importer;
  360. //Lines Strip
  361. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/glTF-Asset-Generator/Mesh_PrimitiveMode/Mesh_PrimitiveMode_10.gltf", aiProcess_ValidateDataStructure);
  362. EXPECT_NE(nullptr, scene);
  363. EXPECT_EQ(scene->mMeshes[0]->mNumVertices, 4u);
  364. std::array<unsigned int, 5> l1 = { { 0u, 1u, 2u, 3u, 0u } };
  365. EXPECT_EQ(scene->mMeshes[0]->mFaces[0].mNumIndices, 2u);
  366. for (unsigned int i = 0; i < scene->mMeshes[0]->mNumFaces; ++i) {
  367. EXPECT_EQ(scene->mMeshes[0]->mFaces[i].mIndices[0], l1[i]);
  368. EXPECT_EQ(scene->mMeshes[0]->mFaces[i].mIndices[1], l1[i + 1]);
  369. }
  370. }
  371. TEST_F(utglTF2ImportExport, importglTF2PrimitiveModeTrianglesStrip) {
  372. Assimp::Importer importer;
  373. //Triangles strip
  374. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/glTF-Asset-Generator/Mesh_PrimitiveMode/Mesh_PrimitiveMode_11.gltf", aiProcess_ValidateDataStructure);
  375. EXPECT_NE(nullptr, scene);
  376. EXPECT_EQ(scene->mMeshes[0]->mNumFaces, 2u);
  377. EXPECT_EQ(scene->mMeshes[0]->mNumVertices, 4u);
  378. std::array<unsigned int, 3> f1 = { { 0u, 1u, 2u } };
  379. EXPECT_EQ(scene->mMeshes[0]->mFaces[0].mNumIndices, 3u);
  380. for (size_t i = 0; i < 3; ++i) {
  381. EXPECT_EQ(scene->mMeshes[0]->mFaces[0].mIndices[i], f1[i]);
  382. }
  383. std::array<unsigned int, 3> f2 = { { 2u, 1u, 3u } };
  384. EXPECT_EQ(scene->mMeshes[0]->mFaces[1].mNumIndices, 3u);
  385. for (size_t i = 0; i < 3; ++i) {
  386. EXPECT_EQ(scene->mMeshes[0]->mFaces[1].mIndices[i], f2[i]);
  387. }
  388. }
  389. TEST_F(utglTF2ImportExport, importglTF2PrimitiveModeTrianglesFan) {
  390. Assimp::Importer importer;
  391. //Triangles fan
  392. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/glTF-Asset-Generator/Mesh_PrimitiveMode/Mesh_PrimitiveMode_12.gltf", aiProcess_ValidateDataStructure);
  393. EXPECT_NE(nullptr, scene);
  394. EXPECT_EQ(scene->mMeshes[0]->mNumVertices, 4u);
  395. EXPECT_EQ(scene->mMeshes[0]->mNumFaces, 2u);
  396. std::array<unsigned int, 3> f1 = { { 0u, 1u, 2u } };
  397. EXPECT_EQ(scene->mMeshes[0]->mFaces[0].mNumIndices, 3u);
  398. for (size_t i = 0; i < 3; ++i) {
  399. EXPECT_EQ(scene->mMeshes[0]->mFaces[0].mIndices[i], f1[i]);
  400. }
  401. std::array<unsigned int, 3> f2 = { { 0u, 2u, 3u } };
  402. EXPECT_EQ(scene->mMeshes[0]->mFaces[1].mNumIndices, 3u);
  403. for (size_t i = 0; i < 3; ++i) {
  404. EXPECT_EQ(scene->mMeshes[0]->mFaces[1].mIndices[i], f2[i]);
  405. }
  406. }
  407. std::vector<char> ReadFile(const char *name) {
  408. std::vector<char> ret;
  409. FILE *p{ nullptr };
  410. EXPECT_TRUE(Unittest::TestTools::openFilestream(&p, name, "r"));
  411. if (nullptr == p) {
  412. return ret;
  413. }
  414. ::fseek(p, 0, SEEK_END);
  415. const size_t size = ::ftell(p);
  416. ::fseek(p, 0, SEEK_SET);
  417. ret.resize(size);
  418. const size_t readSize = ::fread(&ret[0], 1, size, p);
  419. EXPECT_EQ(readSize, size);
  420. ::fclose(p);
  421. return ret;
  422. }
  423. TEST_F(utglTF2ImportExport, importglTF2FromMemory) {
  424. /*const auto flags = aiProcess_CalcTangentSpace | aiProcess_Triangulate | aiProcess_RemoveComponent |
  425. aiProcess_GenSmoothNormals | aiProcess_PreTransformVertices | aiProcess_FixInfacingNormals |
  426. aiProcess_FindDegenerates | aiProcess_GenUVCoords | aiProcess_SortByPType;
  427. const auto& buff = ReadFile("C:\\Users\\kimkulling\\Downloads\\camel\\camel\\scene.gltf");*/
  428. /*const aiScene* Scene = ::aiImportFileFromMemory(&buff[0], buff.size(), flags, ".gltf");
  429. EXPECT_EQ( nullptr, Scene );*/
  430. }
  431. TEST_F(utglTF2ImportExport, bug_import_simple_skin) {
  432. Assimp::Importer importer;
  433. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/simple_skin/simple_skin.gltf",
  434. aiProcess_ValidateDataStructure);
  435. EXPECT_NE(nullptr, scene);
  436. }
  437. bool checkSkinnedScene(const aiScene *scene){
  438. float eps = 0.001f;
  439. bool result = true;
  440. EXPECT_EQ(scene->mNumMeshes, 1u);
  441. EXPECT_EQ(scene->mMeshes[0]->mNumBones, 10u);
  442. EXPECT_EQ(scene->mMeshes[0]->mNumVertices, 4u);
  443. EXPECT_LT(abs(scene->mMeshes[0]->mVertices[0].x - -1), eps);
  444. EXPECT_LT(abs(scene->mMeshes[0]->mVertices[0].y - -1), eps);
  445. EXPECT_LT(abs(scene->mMeshes[0]->mVertices[0].z - 0), eps);
  446. EXPECT_LT(abs(scene->mMeshes[0]->mVertices[1].x - 1), eps);
  447. EXPECT_LT(abs(scene->mMeshes[0]->mVertices[1].y - -1), eps);
  448. EXPECT_LT(abs(scene->mMeshes[0]->mVertices[1].z - 0), eps);
  449. EXPECT_LT(abs(scene->mMeshes[0]->mVertices[2].x - 1), eps);
  450. EXPECT_LT(abs(scene->mMeshes[0]->mVertices[2].y - 1), eps);
  451. EXPECT_LT(abs(scene->mMeshes[0]->mVertices[2].z - 0), eps);
  452. EXPECT_LT(abs(scene->mMeshes[0]->mVertices[3].x - -1), eps);
  453. EXPECT_LT(abs(scene->mMeshes[0]->mVertices[3].y - 1), eps);
  454. EXPECT_LT(abs(scene->mMeshes[0]->mVertices[3].z - 0), eps);
  455. unsigned int numWeights[] = {4u, 4u, 4u, 4u, 2u , 1u, 1u, 2u, 1u, 1u};
  456. float weights[10][4] = {{0.207f, 0.291f, 0.057f, 0.303f},
  457. {0.113f, 0.243f, 0.499f, 0.251f},
  458. {0.005f, 0.010f, 0.041f, 0.093f},
  459. {0.090f, 0.234f, 0.404f, 0.243f},
  460. {0.090f, 0.222f, 0.000f, 0.000f},
  461. {0.216f, 0.000f, 0.000f, 0.000f},
  462. {0.058f, 0.000f, 0.000f, 0.000f},
  463. {0.086f, 0.000f, 0.000f, 0.111f},
  464. {0.088f, 0.000f, 0.000f, 0.000f},
  465. {0.049f, 0.000f, 0.000f, 0.000f}};
  466. for (size_t boneIndex = 0; boneIndex < 10u; ++boneIndex) {
  467. EXPECT_EQ(scene->mMeshes[0]->mBones[boneIndex]->mNumWeights, numWeights[boneIndex]);
  468. std::map<unsigned int, float> map;
  469. for (size_t jointIndex = 0; jointIndex < scene->mMeshes[0]->mBones[boneIndex]->mNumWeights; ++jointIndex){
  470. auto key = scene->mMeshes[0]->mBones[boneIndex]->mWeights[jointIndex].mVertexId;
  471. auto weight = scene->mMeshes[0]->mBones[boneIndex]->mWeights[jointIndex].mWeight;
  472. map[key] = weight;
  473. }
  474. for (unsigned int jointIndex = 0; jointIndex < scene->mMeshes[0]->mBones[boneIndex]->mNumWeights; ++jointIndex) {
  475. auto weight = map[jointIndex];
  476. EXPECT_LT(abs(ai_real(weight) - ai_real(weights[boneIndex][jointIndex])), 0.002);
  477. }
  478. }
  479. return result;
  480. }
  481. void checkSkinnedSceneLimited(const aiScene *scene){
  482. float eps = 0.001f;
  483. EXPECT_EQ(scene->mNumMeshes, 1u);
  484. EXPECT_EQ(scene->mMeshes[0]->mNumBones, 10u);
  485. EXPECT_EQ(scene->mMeshes[0]->mNumVertices, 4u);
  486. EXPECT_LT(abs(scene->mMeshes[0]->mVertices[0].x - -1), eps);
  487. EXPECT_LT(abs(scene->mMeshes[0]->mVertices[0].y - -1), eps);
  488. EXPECT_LT(abs(scene->mMeshes[0]->mVertices[0].z - 0), eps);
  489. EXPECT_LT(abs(scene->mMeshes[0]->mVertices[1].x - 1), eps);
  490. EXPECT_LT(abs(scene->mMeshes[0]->mVertices[1].y - -1), eps);
  491. EXPECT_LT(abs(scene->mMeshes[0]->mVertices[1].z - 0), eps);
  492. EXPECT_LT(abs(scene->mMeshes[0]->mVertices[2].x - 1), eps);
  493. EXPECT_LT(abs(scene->mMeshes[0]->mVertices[2].y - 1), eps);
  494. EXPECT_LT(abs(scene->mMeshes[0]->mVertices[2].z - 0), eps);
  495. EXPECT_LT(abs(scene->mMeshes[0]->mVertices[3].x - -1), eps);
  496. EXPECT_LT(abs(scene->mMeshes[0]->mVertices[3].y - 1), eps);
  497. EXPECT_LT(abs(scene->mMeshes[0]->mVertices[3].z - 0), eps);
  498. unsigned int numWeights[] = {4u, 4u, 1u, 4u, 1u , 1u, 1u, 1u, 1u, 1u};
  499. float weights[10][4] = {{0.207f, 0.291f, 0.057f, 0.303f},
  500. {0.113f, 0.243f, 0.499f, 0.251f},
  501. {0.000f, 0.000f, 0.041f, 0.000f},
  502. {0.090f, 0.234f, 0.404f, 0.243f},
  503. {0.000f, 0.222f, 0.000f, 0.000f},
  504. {0.216f, 0.000f, 0.000f, 0.000f},
  505. {0.000f, 0.000f, 0.000f, 0.000f},
  506. {0.000f, 0.000f, 0.000f, 0.111f},
  507. {0.000f, 0.000f, 0.000f, 0.000f},
  508. {0.000f, 0.000f, 0.000f, 0.000f}};
  509. for (unsigned int boneIndex = 0; boneIndex < 10u; ++boneIndex) {
  510. EXPECT_EQ(scene->mMeshes[0]->mBones[boneIndex]->mNumWeights, numWeights[boneIndex]);
  511. std::map<unsigned int, float> map;
  512. for (unsigned int jointIndex = 0; jointIndex < scene->mMeshes[0]->mBones[boneIndex]->mNumWeights; ++jointIndex){
  513. auto key = scene->mMeshes[0]->mBones[boneIndex]->mWeights[jointIndex].mVertexId;
  514. auto weight = scene->mMeshes[0]->mBones[boneIndex]->mWeights[jointIndex].mWeight;
  515. map[key] = weight;
  516. }
  517. for (unsigned int jointIndex = 0; jointIndex < scene->mMeshes[0]->mBones[boneIndex]->mNumWeights; ++jointIndex) {
  518. auto weight = map[jointIndex];
  519. EXPECT_LT(std::abs(ai_real(weight) - ai_real(weights[boneIndex][jointIndex])), 0.002);
  520. }
  521. }
  522. }
  523. TEST_F(utglTF2ImportExport, bug_import_simple_skin2) {
  524. Assimp::Importer importer;
  525. Assimp::Exporter exporter;
  526. const aiScene *scene = importer.ReadFile(
  527. ASSIMP_TEST_MODELS_DIR "/glTF2/simple_skin/quad_skin.glb",
  528. aiProcess_ValidateDataStructure);
  529. checkSkinnedScene(scene);
  530. ASSERT_EQ(aiReturn_SUCCESS, exporter.Export(scene, "glb2",
  531. ASSIMP_TEST_MODELS_DIR "/glTF2/simple_skin/quad_four_out.glb"));
  532. ASSERT_EQ(aiReturn_SUCCESS, exporter.Export(scene, "gltf2",
  533. ASSIMP_TEST_MODELS_DIR "/glTF2/simple_skin/quad_four_out.gltf"));
  534. // enable more than four bones per vertex
  535. Assimp::ExportProperties properties = Assimp::ExportProperties();
  536. properties.SetPropertyBool(
  537. AI_CONFIG_EXPORT_GLTF_UNLIMITED_SKINNING_BONES_PER_VERTEX, true);
  538. ASSERT_EQ(aiReturn_SUCCESS, exporter.Export(scene, "glb2",
  539. ASSIMP_TEST_MODELS_DIR "/glTF2/simple_skin/quad_all_out.glb", 0u, &properties));
  540. ASSERT_EQ(aiReturn_SUCCESS, exporter.Export(scene, "gltf2",
  541. ASSIMP_TEST_MODELS_DIR "/glTF2/simple_skin/quad_all_out.gltf", 0u, &properties));
  542. // check skinning data of both exported files for limited number bones per vertex
  543. const aiScene *limitedSceneImported = importer.ReadFile(
  544. ASSIMP_TEST_MODELS_DIR "/glTF2/simple_skin/quad_four_out.gltf",
  545. aiProcess_ValidateDataStructure);
  546. checkSkinnedSceneLimited(limitedSceneImported);
  547. limitedSceneImported = importer.ReadFile(
  548. ASSIMP_TEST_MODELS_DIR "/glTF2/simple_skin/quad_four_out.glb",
  549. aiProcess_ValidateDataStructure);
  550. checkSkinnedSceneLimited(limitedSceneImported);
  551. // check skinning data of both exported files for unlimited number bones per vertex
  552. const aiScene *sceneImported = importer.ReadFile(
  553. ASSIMP_TEST_MODELS_DIR "/glTF2/simple_skin/quad_all_out.gltf",
  554. aiProcess_ValidateDataStructure);
  555. checkSkinnedScene(sceneImported);
  556. sceneImported = importer.ReadFile(
  557. ASSIMP_TEST_MODELS_DIR "/glTF2/simple_skin/quad_all_out.glb",
  558. aiProcess_ValidateDataStructure);
  559. checkSkinnedScene(sceneImported);
  560. }
  561. TEST_F(utglTF2ImportExport, import_cameras) {
  562. Assimp::Importer importer;
  563. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/cameras/Cameras.gltf",
  564. aiProcess_ValidateDataStructure);
  565. EXPECT_NE(nullptr, scene);
  566. }
  567. TEST_F(utglTF2ImportExport, incorrect_vertex_arrays) {
  568. Assimp::Importer importer;
  569. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/IncorrectVertexArrays/Cube.gltf",
  570. aiProcess_ValidateDataStructure);
  571. EXPECT_NE(nullptr, scene);
  572. EXPECT_EQ(scene->mMeshes[0]->mNumVertices, 36u);
  573. EXPECT_EQ(scene->mMeshes[0]->mNumFaces, 12u);
  574. EXPECT_EQ(scene->mMeshes[1]->mNumVertices, 35u);
  575. EXPECT_EQ(scene->mMeshes[1]->mNumFaces, 11u);
  576. EXPECT_EQ(scene->mMeshes[2]->mNumVertices, 36u);
  577. EXPECT_EQ(scene->mMeshes[2]->mNumFaces, 18u);
  578. EXPECT_EQ(scene->mMeshes[3]->mNumVertices, 35u);
  579. EXPECT_EQ(scene->mMeshes[3]->mNumFaces, 17u);
  580. EXPECT_EQ(scene->mMeshes[4]->mNumVertices, 36u);
  581. EXPECT_EQ(scene->mMeshes[4]->mNumFaces, 12u);
  582. EXPECT_EQ(scene->mMeshes[5]->mNumVertices, 35u);
  583. EXPECT_EQ(scene->mMeshes[5]->mNumFaces, 11u);
  584. EXPECT_EQ(scene->mMeshes[6]->mNumVertices, 36u);
  585. EXPECT_EQ(scene->mMeshes[6]->mNumFaces, 18u);
  586. EXPECT_EQ(scene->mMeshes[7]->mNumVertices, 35u);
  587. EXPECT_EQ(scene->mMeshes[7]->mNumFaces, 17u);
  588. }
  589. TEST_F(utglTF2ImportExport, texture_transform_test) {
  590. Assimp::Importer importer;
  591. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/textureTransform/TextureTransformTest.gltf",
  592. aiProcess_ValidateDataStructure);
  593. EXPECT_NE(nullptr, scene);
  594. }
  595. #ifndef ASSIMP_BUILD_NO_EXPORT
  596. TEST_F(utglTF2ImportExport, exportglTF2FromFileTest) {
  597. EXPECT_TRUE(exporterTest());
  598. }
  599. TEST_F(utglTF2ImportExport, crash_in_anim_mesh_destructor) {
  600. Assimp::Importer importer;
  601. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/glTF-Sample-Models/AnimatedMorphCube-glTF/AnimatedMorphCube.gltf",
  602. aiProcess_ValidateDataStructure);
  603. ASSERT_NE(nullptr, scene);
  604. Assimp::Exporter exporter;
  605. ASSERT_EQ(aiReturn_SUCCESS, exporter.Export(scene, "glb2", ASSIMP_TEST_MODELS_DIR "/glTF2/glTF-Sample-Models/AnimatedMorphCube-glTF/AnimatedMorphCube_out.glTF"));
  606. }
  607. TEST_F(utglTF2ImportExport, error_string_preserved) {
  608. Assimp::Importer importer;
  609. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/MissingBin/BoxTextured.gltf",
  610. aiProcess_ValidateDataStructure);
  611. ASSERT_EQ(nullptr, scene);
  612. std::string error = importer.GetErrorString();
  613. ASSERT_NE(error.find("BoxTextured0.bin"), std::string::npos) << "Error string should contain an error about missing .bin file";
  614. }
  615. TEST_F(utglTF2ImportExport, export_bad_accessor_bounds) {
  616. Assimp::Importer importer;
  617. Assimp::Exporter exporter;
  618. const aiScene* scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/BoxWithInfinites-glTF-Binary/BoxWithInfinites.glb", aiProcess_ValidateDataStructure);
  619. ASSERT_NE(scene, nullptr);
  620. EXPECT_EQ(aiReturn_SUCCESS, exporter.Export(scene, "glb2", ASSIMP_TEST_MODELS_DIR "/glTF2/BoxWithInfinites-glTF-Binary/BoxWithInfinites_out.glb"));
  621. EXPECT_EQ(aiReturn_SUCCESS, exporter.Export(scene, "gltf2", ASSIMP_TEST_MODELS_DIR "/glTF2/BoxWithInfinites-glTF-Binary/BoxWithInfinites_out.gltf"));
  622. }
  623. TEST_F(utglTF2ImportExport, export_normalized_normals) {
  624. Assimp::Importer importer;
  625. Assimp::Exporter exporter;
  626. const aiScene* scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/BoxBadNormals-glTF-Binary/BoxBadNormals.glb", aiProcess_ValidateDataStructure);
  627. ASSERT_NE(scene, nullptr);
  628. EXPECT_EQ(aiReturn_SUCCESS, exporter.Export(scene, "glb2", ASSIMP_TEST_MODELS_DIR "/glTF2/BoxBadNormals-glTF-Binary/BoxBadNormals_out.glb"));
  629. // load in again and ensure normal-length normals but no Nan's or Inf's introduced
  630. scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/BoxBadNormals-glTF-Binary/BoxBadNormals_out.glb", aiProcess_ValidateDataStructure);
  631. for ( auto i = 0u; i < scene->mMeshes[0]->mNumVertices; ++i ) {
  632. const auto length = scene->mMeshes[0]->mNormals[i].Length();
  633. EXPECT_TRUE(abs(length) < 1e-6 || abs(length - 1) < ai_epsilon);
  634. }
  635. }
  636. #endif // ASSIMP_BUILD_NO_EXPORT
  637. TEST_F(utglTF2ImportExport, sceneMetadata) {
  638. Assimp::Importer importer;
  639. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/BoxTextured-glTF/BoxTextured.gltf",
  640. aiProcess_ValidateDataStructure);
  641. ASSERT_NE(scene, nullptr);
  642. ASSERT_NE(scene->mMetaData, nullptr);
  643. {
  644. ASSERT_TRUE(scene->mMetaData->HasKey(AI_METADATA_SOURCE_FORMAT));
  645. aiString format;
  646. ASSERT_TRUE(scene->mMetaData->Get(AI_METADATA_SOURCE_FORMAT, format));
  647. ASSERT_EQ(strcmp(format.C_Str(), "glTF2 Importer"), 0);
  648. }
  649. {
  650. ASSERT_TRUE(scene->mMetaData->HasKey(AI_METADATA_SOURCE_FORMAT_VERSION));
  651. aiString version;
  652. ASSERT_TRUE(scene->mMetaData->Get(AI_METADATA_SOURCE_FORMAT_VERSION, version));
  653. ASSERT_EQ(strcmp(version.C_Str(), "2.0"), 0);
  654. }
  655. {
  656. ASSERT_TRUE(scene->mMetaData->HasKey(AI_METADATA_SOURCE_GENERATOR));
  657. aiString generator;
  658. ASSERT_TRUE(scene->mMetaData->Get(AI_METADATA_SOURCE_GENERATOR, generator));
  659. ASSERT_EQ(strcmp(generator.C_Str(), "COLLADA2GLTF"), 0);
  660. }
  661. }
  662. TEST_F(utglTF2ImportExport, texcoords) {
  663. Assimp::Importer importer;
  664. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/BoxTexcoords-glTF/boxTexcoords.gltf", aiProcess_ValidateDataStructure);
  665. ASSERT_NE(scene, nullptr);
  666. ASSERT_TRUE(scene->HasMaterials());
  667. const aiMaterial *material = scene->mMaterials[0];
  668. aiString path;
  669. unsigned int uvIndex = 255;
  670. aiTextureMapMode modes[2];
  671. EXPECT_EQ(aiReturn_SUCCESS, material->GetTexture(AI_MATKEY_BASE_COLOR_TEXTURE, &path, nullptr, &uvIndex, nullptr, nullptr, modes));
  672. EXPECT_STREQ(path.C_Str(), "texture.png");
  673. EXPECT_EQ(uvIndex, 0u);
  674. uvIndex = 255;
  675. EXPECT_EQ(aiReturn_SUCCESS, material->GetTexture(AI_MATKEY_GLTF_PBRMETALLICROUGHNESS_METALLICROUGHNESS_TEXTURE, &path, nullptr, &uvIndex, nullptr, nullptr, modes));
  676. EXPECT_STREQ(path.C_Str(), "texture.png");
  677. EXPECT_EQ(uvIndex, 1u);
  678. }
  679. #ifndef ASSIMP_BUILD_NO_EXPORT
  680. TEST_F(utglTF2ImportExport, texcoords_export) {
  681. {
  682. Assimp::Importer importer;
  683. Assimp::Exporter exporter;
  684. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/BoxTexcoords-glTF/boxTexcoords.gltf", aiProcess_ValidateDataStructure);
  685. ASSERT_NE(scene, nullptr);
  686. ASSERT_EQ(aiReturn_SUCCESS, exporter.Export(scene, "glb2", ASSIMP_TEST_MODELS_DIR "/glTF2/BoxTexcoords-glTF/boxTexcoords.gltf_out.glb"));
  687. }
  688. Assimp::Importer importer;
  689. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/BoxTexcoords-glTF/boxTexcoords.gltf", aiProcess_ValidateDataStructure);
  690. ASSERT_NE(scene, nullptr);
  691. ASSERT_TRUE(scene->HasMaterials());
  692. const aiMaterial *material = scene->mMaterials[0];
  693. aiString path;
  694. unsigned int uvIndex = 255;
  695. aiTextureMapMode modes[2];
  696. EXPECT_EQ(aiReturn_SUCCESS, material->GetTexture(AI_MATKEY_BASE_COLOR_TEXTURE, &path, nullptr, &uvIndex, nullptr, nullptr, modes));
  697. EXPECT_STREQ(path.C_Str(), "texture.png");
  698. EXPECT_EQ(uvIndex, 0u);
  699. uvIndex = 255;
  700. EXPECT_EQ(aiReturn_SUCCESS, material->GetTexture(AI_MATKEY_GLTF_PBRMETALLICROUGHNESS_METALLICROUGHNESS_TEXTURE, &path, nullptr, &uvIndex, nullptr, nullptr, modes));
  701. EXPECT_STREQ(path.C_Str(), "texture.png");
  702. EXPECT_EQ(uvIndex, 1u);
  703. }
  704. #endif // ASSIMP_BUILD_NO_EXPORT
  705. TEST_F(utglTF2ImportExport, recursive_nodes) {
  706. Assimp::Importer importer;
  707. const aiScene* scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/RecursiveNodes/RecursiveNodes.gltf", aiProcess_ValidateDataStructure);
  708. EXPECT_EQ(nullptr, scene);
  709. }
  710. TEST_F(utglTF2ImportExport, norootnode_noscene) {
  711. Assimp::Importer importer;
  712. const aiScene* scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/TestNoRootNode/NoScene.gltf", aiProcess_ValidateDataStructure);
  713. ASSERT_EQ(scene, nullptr);
  714. }
  715. TEST_F(utglTF2ImportExport, norootnode_scenewithoutnodes) {
  716. Assimp::Importer importer;
  717. const aiScene* scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/TestNoRootNode/SceneWithoutNodes.gltf", aiProcess_ValidateDataStructure);
  718. ASSERT_NE(scene, nullptr);
  719. ASSERT_NE(scene->mRootNode, nullptr);
  720. }
  721. // Shall not crash!
  722. TEST_F(utglTF2ImportExport, norootnode_issue_3269) {
  723. Assimp::Importer importer;
  724. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/issue_3269/texcoord_crash.gltf", aiProcess_ValidateDataStructure);
  725. ASSERT_EQ(scene, nullptr);
  726. }
  727. TEST_F(utglTF2ImportExport, indexOutOfRange) {
  728. // The contents of an asset should not lead to an assert.
  729. Assimp::Importer importer;
  730. struct LogObserver : Assimp::LogStream {
  731. bool m_observedWarning = false;
  732. void write(const char *message) override {
  733. m_observedWarning = m_observedWarning || std::strstr(message, "faces were dropped");
  734. }
  735. };
  736. LogObserver logObserver;
  737. DefaultLogger::get()->attachStream(&logObserver);
  738. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/IndexOutOfRange/IndexOutOfRange.gltf", aiProcess_ValidateDataStructure);
  739. ASSERT_NE(scene, nullptr);
  740. ASSERT_NE(scene->mRootNode, nullptr);
  741. ASSERT_EQ(scene->mNumMeshes, 1u);
  742. EXPECT_EQ(scene->mMeshes[0]->mNumFaces, 11u);
  743. DefaultLogger::get()->detachStream(&logObserver);
  744. EXPECT_TRUE(logObserver.m_observedWarning);
  745. }
  746. TEST_F(utglTF2ImportExport, allIndicesOutOfRange) {
  747. // The contents of an asset should not lead to an assert.
  748. Assimp::Importer importer;
  749. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/IndexOutOfRange/AllIndicesOutOfRange.gltf", aiProcess_ValidateDataStructure);
  750. ASSERT_EQ(scene, nullptr);
  751. std::string error = importer.GetErrorString();
  752. ASSERT_NE(error.find("Mesh \"Mesh\" has no faces"), std::string::npos);
  753. }
  754. /////////////////////////////////
  755. // Draco decoding
  756. TEST_F(utglTF2ImportExport, import_dracoEncoded) {
  757. Assimp::Importer importer;
  758. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/draco/2CylinderEngine.gltf",
  759. aiProcess_ValidateDataStructure);
  760. #ifndef ASSIMP_ENABLE_DRACO
  761. // No draco support, scene should not load
  762. ASSERT_EQ(scene, nullptr);
  763. #else
  764. ASSERT_NE(scene, nullptr);
  765. ASSERT_NE(scene->mMetaData, nullptr);
  766. {
  767. ASSERT_TRUE(scene->mMetaData->HasKey(AI_METADATA_SOURCE_FORMAT));
  768. aiString format;
  769. ASSERT_TRUE(scene->mMetaData->Get(AI_METADATA_SOURCE_FORMAT, format));
  770. ASSERT_EQ(strcmp(format.C_Str(), "glTF2 Importer"), 0);
  771. }
  772. {
  773. ASSERT_TRUE(scene->mMetaData->HasKey(AI_METADATA_SOURCE_FORMAT_VERSION));
  774. aiString version;
  775. ASSERT_TRUE(scene->mMetaData->Get(AI_METADATA_SOURCE_FORMAT_VERSION, version));
  776. ASSERT_EQ(strcmp(version.C_Str(), "2.0"), 0);
  777. }
  778. {
  779. ASSERT_TRUE(scene->mMetaData->HasKey(AI_METADATA_SOURCE_GENERATOR));
  780. aiString generator;
  781. ASSERT_TRUE(scene->mMetaData->Get(AI_METADATA_SOURCE_GENERATOR, generator));
  782. ASSERT_EQ(strcmp(generator.C_Str(), "COLLADA2GLTF"), 0);
  783. }
  784. #endif
  785. const aiScene *robotScene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/draco/robot.glb",
  786. aiProcess_ValidateDataStructure);
  787. #ifndef ASSIMP_ENABLE_DRACO
  788. // No draco support, scene should not load
  789. ASSERT_EQ(robotScene, nullptr);
  790. #else
  791. ASSERT_NE(robotScene, nullptr);
  792. ASSERT_NE(robotScene->mMetaData, nullptr);
  793. {
  794. ASSERT_TRUE(robotScene->mMetaData->HasKey(AI_METADATA_SOURCE_FORMAT));
  795. aiString format;
  796. ASSERT_TRUE(robotScene->mMetaData->Get(AI_METADATA_SOURCE_FORMAT, format));
  797. ASSERT_EQ(strcmp(format.C_Str(), "glTF2 Importer"), 0);
  798. }
  799. {
  800. ASSERT_TRUE(robotScene->mMetaData->HasKey(AI_METADATA_SOURCE_FORMAT_VERSION));
  801. aiString version;
  802. ASSERT_TRUE(robotScene->mMetaData->Get(AI_METADATA_SOURCE_FORMAT_VERSION, version));
  803. ASSERT_EQ(strcmp(version.C_Str(), "2.0"), 0);
  804. }
  805. #endif
  806. }
  807. TEST_F(utglTF2ImportExport, wrongTypes) {
  808. // Deliberately broken version of the BoxTextured.gltf asset.
  809. using tup_T = std::tuple<std::string, std::string, std::string, std::string>;
  810. std::vector<tup_T> wrongTypes = {
  811. #ifdef __cpp_lib_constexpr_tuple
  812. #define TUPLE(x, y, z, w) {x, y, z, w}
  813. #else
  814. #define TUPLE(x, y, z, w) tup_T(x, y, z, w)
  815. #endif
  816. TUPLE("/glTF2/wrongTypes/badArray.gltf", "array", "primitives", "meshes[0]"),
  817. TUPLE("/glTF2/wrongTypes/badString.gltf", "string", "name", "scenes[0]"),
  818. TUPLE("/glTF2/wrongTypes/badUint.gltf", "uint", "index", "materials[0]"),
  819. TUPLE("/glTF2/wrongTypes/badNumber.gltf", "number", "scale", "materials[0]"),
  820. //TUPLE("/glTF2/wrongTypes/badObject.gltf", "object", "pbrMetallicRoughness", "materials[0]"),
  821. //TUPLE("/glTF2/wrongTypes/badExtension.gltf", "object", "KHR_texture_transform", "materials[0]")
  822. #undef TUPLE
  823. };
  824. for (const auto& tuple : wrongTypes)
  825. {
  826. const auto& file = std::get<0>(tuple);
  827. const auto& type = std::get<1>(tuple);
  828. const auto& member = std::get<2>(tuple);
  829. const auto& context = std::get<3>(tuple);
  830. Assimp::Importer importer;
  831. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR + file , aiProcess_ValidateDataStructure);
  832. ASSERT_EQ(scene, nullptr);
  833. const std::string error = importer.GetErrorString();
  834. EXPECT_FALSE(error.empty());
  835. EXPECT_NE(error.find(member + "\" was not of type \"" + type + "\" when reading " + context), std::string::npos);
  836. }
  837. }
  838. TEST_F(utglTF2ImportExport, wrongObject) {
  839. // Deliberately broken version of the BoxTextured.gltf asset.
  840. using tup_T = std::tuple<std::string, std::string, std::string, std::string>;
  841. std::vector<tup_T> wrongTypes = {
  842. #ifdef __cpp_lib_constexpr_tuple
  843. #define TUPLE(x, y, z, w) \
  844. { x, y, z, w }
  845. #else
  846. #define TUPLE(x, y, z, w) tup_T(x, y, z, w)
  847. #endif
  848. TUPLE("/glTF2/wrongTypes/badObject.gltf", "object", "pbrMetallicRoughness", "materials[0]"),
  849. TUPLE("/glTF2/wrongTypes/badExtension.gltf", "object", "KHR_texture_transform", "materials[0]")
  850. #undef TUPLE
  851. };
  852. for (const auto &tuple : wrongTypes) {
  853. const auto &file = std::get<0>(tuple);
  854. Assimp::Importer importer;
  855. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR + file, aiProcess_ValidateDataStructure);
  856. ASSERT_NE(scene, nullptr);
  857. }
  858. }
  859. namespace {
  860. /// This class provides a fake schema to the GLTF importer.
  861. /// It just checks that the file has a top-level "scene" property which is an integer.
  862. class FakeSchemaProvider : public rapidjson::IRemoteSchemaDocumentProvider
  863. {
  864. public:
  865. FakeSchemaProvider(const char* schemaName) :
  866. m_schemaName(schemaName)
  867. {
  868. rapidjson::Document schemaDoc;
  869. schemaDoc.Parse(R"==({"properties":{"scene" : { "type" : "integer" }}, "required": [ "scene" ]})==");
  870. EXPECT_FALSE(schemaDoc.HasParseError());
  871. m_schema.reset(new rapidjson::SchemaDocument(schemaDoc, m_schemaName.c_str(), static_cast<rapidjson::SizeType>(m_schemaName.size()), this));
  872. }
  873. const rapidjson::SchemaDocument* GetRemoteDocument(const char* uri, rapidjson::SizeType) override {
  874. if (m_schemaName == uri) {
  875. return m_schema.get();
  876. }
  877. return nullptr;
  878. }
  879. private:
  880. std::string m_schemaName;
  881. std::unique_ptr<const rapidjson::SchemaDocument> m_schema;
  882. };
  883. }
  884. TEST_F(utglTF2ImportExport, schemaCheckPass) {
  885. FakeSchemaProvider schemaProvider("glTF.schema.json");
  886. Assimp::Importer importer;
  887. importer.SetPropertyPointer(AI_CONFIG_IMPORT_SCHEMA_DOCUMENT_PROVIDER, &schemaProvider);
  888. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/BoxTextured-glTF/BoxTextured.gltf", aiProcess_ValidateDataStructure);
  889. EXPECT_NE(scene, nullptr);
  890. EXPECT_STREQ(importer.GetErrorString(), "");
  891. }
  892. TEST_F(utglTF2ImportExport, schemaCheckFail) {
  893. FakeSchemaProvider schemaProvider("glTF.schema.json");
  894. Assimp::Importer importer;
  895. importer.SetPropertyPointer(AI_CONFIG_IMPORT_SCHEMA_DOCUMENT_PROVIDER, &schemaProvider);
  896. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/SchemaFailures/sceneWrongType.gltf", aiProcess_ValidateDataStructure);
  897. EXPECT_EQ(scene, nullptr);
  898. const std::string errorString = importer.GetErrorString();
  899. EXPECT_NE(errorString.find("The JSON document did not satisfy the glTF2 schema"), std::string::npos);
  900. }
  901. TEST_F(utglTF2ImportExport, noSchemaFound) {
  902. // More than one importer might make use the provider, but not all schemas might be present.
  903. // Check that the glTF importer handles the case when an non-null provider returns null when asked for schemas.
  904. FakeSchemaProvider schemaProvider("missingSchema.json");
  905. Assimp::Importer importer;
  906. importer.SetPropertyPointer(AI_CONFIG_IMPORT_SCHEMA_DOCUMENT_PROVIDER, &schemaProvider);
  907. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/BoxTextured-glTF/BoxTextured.gltf", aiProcess_ValidateDataStructure);
  908. EXPECT_NE(scene, nullptr);
  909. EXPECT_STREQ(importer.GetErrorString(), "");
  910. }
  911. // ------------------------------------------------------------------------------------------------
  912. TEST_F(utglTF2ImportExport, testSetIdentityMatrixEpsilon) {
  913. // Assimp::Exporter exporter;
  914. Assimp::ExportProperties properties = Assimp::ExportProperties();
  915. EXPECT_EQ(AI_CONFIG_CHECK_IDENTITY_MATRIX_EPSILON_DEFAULT,
  916. properties.GetPropertyFloat("CHECK_IDENTITY_MATRIX_EPSILON",
  917. AI_CONFIG_CHECK_IDENTITY_MATRIX_EPSILON_DEFAULT));
  918. aiMatrix4x4 m;
  919. m = aiMatrix4x4(1.02f, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1);
  920. EXPECT_FALSE(m.IsIdentity());
  921. m = aiMatrix4x4(1.001f, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1);
  922. EXPECT_TRUE(m.IsIdentity());
  923. bool b = properties.SetPropertyFloat("CHECK_IDENTITY_MATRIX_EPSILON", 0.0001f);
  924. EXPECT_TRUE(!b);
  925. ai_real epsilon = properties.GetPropertyFloat("CHECK_IDENTITY_MATRIX_EPSILON", 0.01f);
  926. EXPECT_EQ(0.0001f, epsilon);
  927. m = aiMatrix4x4(1.0002f, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1);
  928. EXPECT_FALSE(m.IsIdentity(epsilon));
  929. m = aiMatrix4x4(1.00009f, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1);
  930. EXPECT_TRUE(m.IsIdentity(epsilon));
  931. }