test_gltf_document.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /**************************************************************************/
  2. /* test_gltf_document.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef TEST_GLTF_DOCUMENT_H
  31. #define TEST_GLTF_DOCUMENT_H
  32. #include "modules/gltf/extensions/gltf_document_extension_convert_importer_mesh.h"
  33. #include "modules/gltf/gltf_document.h"
  34. #include "tests/test_macros.h"
  35. #include "tests/test_utils.h"
  36. namespace TestGLTFDocument {
  37. struct GLTFArraySize {
  38. String key;
  39. int val;
  40. };
  41. struct GLTFKeyValue {
  42. String key;
  43. Variant val;
  44. };
  45. struct GLTFTestCase {
  46. String filename;
  47. String copyright;
  48. String generator;
  49. String version;
  50. Vector<GLTFArraySize> array_sizes;
  51. Vector<GLTFArraySize> json_array_sizes;
  52. Vector<GLTFKeyValue> keyvalues;
  53. };
  54. const GLTFTestCase glTF_test_cases[] = {
  55. { "models/cube.gltf",
  56. "",
  57. "Khronos glTF Blender I/O v4.3.47",
  58. "2.0",
  59. // Here are the array sizes.
  60. {
  61. { "nodes", 1 },
  62. { "buffers", 1 },
  63. { "buffer_views", 13 },
  64. { "accessors", 13 },
  65. { "meshes", 1 },
  66. { "materials", 2 },
  67. { "root_nodes", 1 },
  68. { "textures", 0 },
  69. { "texture_samplers", 0 },
  70. { "images", 0 },
  71. { "skins", 0 },
  72. { "cameras", 0 },
  73. { "lights", 0 },
  74. { "skeletons", 0 },
  75. { "animations", 1 },
  76. },
  77. // Here are the json array sizes.
  78. {
  79. { "scenes", 1 },
  80. { "nodes", 1 },
  81. { "animations", 1 },
  82. { "meshes", 1 },
  83. { "accessors", 13 },
  84. { "bufferViews", 13 },
  85. { "buffers", 1 },
  86. },
  87. // Here are the key-value pairs.
  88. {
  89. { "major_version", 2 },
  90. { "minor_version", 0 },
  91. { "scene_name", "cube" },
  92. { "filename", "cube" } } },
  93. { "models/suzanne.glb",
  94. "this is example text",
  95. "Khronos glTF Blender I/O v4.3.47",
  96. "2.0",
  97. // Here are the array sizes.
  98. {
  99. { "glb_data", 68908 },
  100. { "nodes", 2 },
  101. { "buffers", 1 },
  102. { "buffer_views", 5 },
  103. { "accessors", 4 },
  104. { "meshes", 1 },
  105. { "materials", 1 },
  106. { "root_nodes", 2 },
  107. { "textures", 1 },
  108. { "texture_samplers", 1 },
  109. { "images", 1 },
  110. { "skins", 0 },
  111. { "cameras", 1 },
  112. { "lights", 0 },
  113. { "unique_names", 4 },
  114. { "skeletons", 0 },
  115. { "animations", 0 },
  116. },
  117. // Here are the json array sizes.
  118. {
  119. { "scenes", 1 },
  120. { "nodes", 2 },
  121. { "cameras", 1 },
  122. { "materials", 1 },
  123. { "meshes", 1 },
  124. { "textures", 1 },
  125. { "images", 1 },
  126. { "accessors", 4 },
  127. { "bufferViews", 5 },
  128. { "buffers", 1 },
  129. },
  130. // Here are the key-value pairs.
  131. {
  132. { "major_version", 2 },
  133. { "minor_version", 0 },
  134. { "scene_name", "suzanne" },
  135. { "filename", "suzanne" } } },
  136. };
  137. void register_gltf_extension() {
  138. GLTFDocument::unregister_all_gltf_document_extensions();
  139. // Ensures meshes become a MeshInstance3D and not an ImporterMeshInstance3D.
  140. Ref<GLTFDocumentExtensionConvertImporterMesh> extension_GLTFDocumentExtensionConvertImporterMesh;
  141. extension_GLTFDocumentExtensionConvertImporterMesh.instantiate();
  142. GLTFDocument::register_gltf_document_extension(extension_GLTFDocumentExtensionConvertImporterMesh);
  143. }
  144. void test_gltf_document_values(Ref<GLTFDocument> &p_gltf_document, Ref<GLTFState> &p_gltf_state, const GLTFTestCase &p_test_case) {
  145. const Error err = p_gltf_document->append_from_file(TestUtils::get_data_path(p_test_case.filename), p_gltf_state);
  146. REQUIRE(err == OK);
  147. for (GLTFArraySize array_size : p_test_case.array_sizes) {
  148. CHECK_MESSAGE(((Array)(p_gltf_state->getvar(array_size.key))).size() == array_size.val, "Expected \"", array_size.key, "\" to have ", array_size.val, " elements.");
  149. }
  150. for (GLTFArraySize array_size : p_test_case.json_array_sizes) {
  151. CHECK(p_gltf_state->get_json().has(array_size.key));
  152. CHECK_MESSAGE(((Array)(p_gltf_state->get_json()[array_size.key])).size() == array_size.val, "Expected \"", array_size.key, "\" to have ", array_size.val, " elements.");
  153. }
  154. for (GLTFKeyValue key_value : p_test_case.keyvalues) {
  155. CHECK_MESSAGE(p_gltf_state->getvar(key_value.key) == key_value.val, "Expected \"", key_value.key, "\" to be \"", key_value.val, "\".");
  156. }
  157. CHECK(p_gltf_state->get_copyright() == p_test_case.copyright);
  158. CHECK(((Dictionary)p_gltf_state->get_json()["asset"])["generator"] == p_test_case.generator);
  159. CHECK(((Dictionary)p_gltf_state->get_json()["asset"])["version"] == p_test_case.version);
  160. }
  161. void test_gltf_save(Node *p_node) {
  162. Ref<GLTFDocument> gltf_document_save;
  163. gltf_document_save.instantiate();
  164. Ref<GLTFState> gltf_state_save;
  165. gltf_state_save.instantiate();
  166. gltf_document_save->append_from_scene(p_node, gltf_state_save);
  167. // Check saving the scene to gltf and glb.
  168. const Error err_save_gltf = gltf_document_save->write_to_filesystem(gltf_state_save, TestUtils::get_temp_path("cube.gltf"));
  169. const Error err_save_glb = gltf_document_save->write_to_filesystem(gltf_state_save, TestUtils::get_temp_path("cube.glb"));
  170. CHECK(err_save_gltf == OK);
  171. CHECK(err_save_glb == OK);
  172. }
  173. TEST_CASE("[SceneTree][GLTFDocument] Load cube.gltf") {
  174. register_gltf_extension();
  175. Ref<GLTFDocument> gltf_document;
  176. gltf_document.instantiate();
  177. Ref<GLTFState> gltf_state;
  178. gltf_state.instantiate();
  179. test_gltf_document_values(gltf_document, gltf_state, glTF_test_cases[0]);
  180. Node *node = gltf_document->generate_scene(gltf_state);
  181. // Check the loaded scene.
  182. CHECK(node->is_class("Node3D"));
  183. CHECK(node->get_name() == "cube");
  184. CHECK(node->get_child(0)->is_class("MeshInstance3D"));
  185. CHECK(node->get_child(0)->get_name() == "Cube");
  186. CHECK(node->get_child(1)->is_class("AnimationPlayer"));
  187. CHECK(node->get_child(1)->get_name() == "AnimationPlayer");
  188. test_gltf_save(node);
  189. // Clean up the node.
  190. memdelete(node);
  191. }
  192. TEST_CASE("[SceneTree][GLTFDocument] Load suzanne.glb") {
  193. register_gltf_extension();
  194. Ref<GLTFDocument> gltf_document;
  195. gltf_document.instantiate();
  196. Ref<GLTFState> gltf_state;
  197. gltf_state.instantiate();
  198. test_gltf_document_values(gltf_document, gltf_state, glTF_test_cases[1]);
  199. Node *node = gltf_document->generate_scene(gltf_state);
  200. // Check the loaded scene.
  201. CHECK(node->is_class("Node3D"));
  202. CHECK(node->get_name() == "suzanne");
  203. CHECK(node->get_child(0)->is_class("MeshInstance3D"));
  204. CHECK(node->get_child(0)->get_name() == "Suzanne");
  205. CHECK(node->get_child(1)->is_class("Camera3D"));
  206. CHECK(node->get_child(1)->get_name() == "Camera");
  207. test_gltf_save(node);
  208. // Clean up the node.
  209. memdelete(node);
  210. }
  211. } // namespace TestGLTFDocument
  212. #endif // TEST_GLTF_DOCUMENT_H