test_gltf_extras.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /**************************************************************************/
  2. /* test_gltf_extras.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_EXTRAS_H
  31. #define TEST_GLTF_EXTRAS_H
  32. #include "tests/test_macros.h"
  33. #ifdef TOOLS_ENABLED
  34. #include "core/os/os.h"
  35. #include "editor/import/3d/resource_importer_scene.h"
  36. #include "modules/gltf/editor/editor_scene_importer_gltf.h"
  37. #include "modules/gltf/gltf_document.h"
  38. #include "modules/gltf/gltf_state.h"
  39. #include "scene/3d/mesh_instance_3d.h"
  40. #include "scene/3d/skeleton_3d.h"
  41. #include "scene/main/window.h"
  42. #include "scene/resources/3d/primitive_meshes.h"
  43. #include "scene/resources/material.h"
  44. #include "scene/resources/packed_scene.h"
  45. namespace TestGltfExtras {
  46. static Node *_gltf_export_then_import(Node *p_root, String &p_tempfilebase) {
  47. Ref<GLTFDocument> doc;
  48. doc.instantiate();
  49. Ref<GLTFState> state;
  50. state.instantiate();
  51. Error err = doc->append_from_scene(p_root, state, EditorSceneFormatImporter::IMPORT_USE_NAMED_SKIN_BINDS);
  52. CHECK_MESSAGE(err == OK, "GLTF state generation failed.");
  53. err = doc->write_to_filesystem(state, p_tempfilebase + ".gltf");
  54. CHECK_MESSAGE(err == OK, "Writing GLTF to cache dir failed.");
  55. // Setting up importers.
  56. Ref<ResourceImporterScene> import_scene = memnew(ResourceImporterScene("PackedScene", true));
  57. ResourceFormatImporter::get_singleton()->add_importer(import_scene);
  58. Ref<EditorSceneFormatImporterGLTF> import_gltf;
  59. import_gltf.instantiate();
  60. ResourceImporterScene::add_scene_importer(import_gltf);
  61. // GTLF importer behaves differently outside of editor, it's too late to modify Engine::get_editor_hint
  62. // as the registration of runtime extensions already happened, so remove them. See modules/gltf/register_types.cpp
  63. GLTFDocument::unregister_all_gltf_document_extensions();
  64. HashMap<StringName, Variant> options(20);
  65. options["nodes/root_type"] = "";
  66. options["nodes/root_name"] = "";
  67. options["nodes/apply_root_scale"] = true;
  68. options["nodes/root_scale"] = 1.0;
  69. options["meshes/ensure_tangents"] = true;
  70. options["meshes/generate_lods"] = false;
  71. options["meshes/create_shadow_meshes"] = true;
  72. options["meshes/light_baking"] = 1;
  73. options["meshes/lightmap_texel_size"] = 0.2;
  74. options["meshes/force_disable_compression"] = false;
  75. options["skins/use_named_skins"] = true;
  76. options["animation/import"] = true;
  77. options["animation/fps"] = 30;
  78. options["animation/trimming"] = false;
  79. options["animation/remove_immutable_tracks"] = true;
  80. options["import_script/path"] = "";
  81. options["_subresources"] = Dictionary();
  82. options["gltf/naming_version"] = 1;
  83. // Process gltf file, note that this generates `.scn` resource from the 2nd argument.
  84. err = import_scene->import(0, p_tempfilebase + ".gltf", p_tempfilebase, options, nullptr, nullptr, nullptr);
  85. CHECK_MESSAGE(err == OK, "GLTF import failed.");
  86. ResourceImporterScene::remove_scene_importer(import_gltf);
  87. Ref<PackedScene> packed_scene = ResourceLoader::load(p_tempfilebase + ".scn", "", ResourceFormatLoader::CACHE_MODE_REPLACE, &err);
  88. CHECK_MESSAGE(err == OK, "Loading scene failed.");
  89. Node *p_scene = packed_scene->instantiate();
  90. return p_scene;
  91. }
  92. TEST_CASE("[SceneTree][Node] GLTF test mesh and material meta export and import") {
  93. // Setup scene.
  94. Ref<StandardMaterial3D> original_material = memnew(StandardMaterial3D);
  95. original_material->set_albedo(Color(1.0, .0, .0));
  96. original_material->set_name("material");
  97. Dictionary material_dict;
  98. material_dict["node_type"] = "material";
  99. original_material->set_meta("extras", material_dict);
  100. Ref<PlaneMesh> original_meshdata = memnew(PlaneMesh);
  101. original_meshdata->set_name("planemesh");
  102. Dictionary meshdata_dict;
  103. meshdata_dict["node_type"] = "planemesh";
  104. original_meshdata->set_meta("extras", meshdata_dict);
  105. original_meshdata->surface_set_material(0, original_material);
  106. MeshInstance3D *original_mesh_instance = memnew(MeshInstance3D);
  107. original_mesh_instance->set_mesh(original_meshdata);
  108. original_mesh_instance->set_name("mesh_instance_3d");
  109. Dictionary mesh_instance_dict;
  110. mesh_instance_dict["node_type"] = "mesh_instance_3d";
  111. original_mesh_instance->set_meta("extras", mesh_instance_dict);
  112. Node3D *original = memnew(Node3D);
  113. SceneTree::get_singleton()->get_root()->add_child(original);
  114. original->add_child(original_mesh_instance);
  115. original->set_name("node3d");
  116. Dictionary node_dict;
  117. node_dict["node_type"] = "node3d";
  118. original->set_meta("extras", node_dict);
  119. original->set_meta("meta_not_nested_under_extras", "should not propagate");
  120. // Convert to GLFT and back.
  121. String tempfile = OS::get_singleton()->get_cache_path().path_join("gltf_extras");
  122. Node *loaded = _gltf_export_then_import(original, tempfile);
  123. // Compare the results.
  124. CHECK(loaded->get_name() == "node3d");
  125. CHECK(Dictionary(loaded->get_meta("extras")).size() == 1);
  126. CHECK(Dictionary(loaded->get_meta("extras"))["node_type"] == "node3d");
  127. CHECK_FALSE(loaded->has_meta("meta_not_nested_under_extras"));
  128. CHECK_FALSE(Dictionary(loaded->get_meta("extras")).has("meta_not_nested_under_extras"));
  129. MeshInstance3D *mesh_instance_3d = Object::cast_to<MeshInstance3D>(loaded->find_child("mesh_instance_3d", false, true));
  130. CHECK(mesh_instance_3d->get_name() == "mesh_instance_3d");
  131. CHECK(Dictionary(mesh_instance_3d->get_meta("extras"))["node_type"] == "mesh_instance_3d");
  132. Ref<Mesh> mesh = mesh_instance_3d->get_mesh();
  133. CHECK(Dictionary(mesh->get_meta("extras"))["node_type"] == "planemesh");
  134. Ref<Material> material = mesh->surface_get_material(0);
  135. CHECK(material->get_name() == "material");
  136. CHECK(Dictionary(material->get_meta("extras"))["node_type"] == "material");
  137. memdelete(original_mesh_instance);
  138. memdelete(original);
  139. memdelete(loaded);
  140. }
  141. TEST_CASE("[SceneTree][Node] GLTF test skeleton and bone export and import") {
  142. // Setup scene.
  143. Skeleton3D *skeleton = memnew(Skeleton3D);
  144. skeleton->set_name("skeleton");
  145. Dictionary skeleton_extras;
  146. skeleton_extras["node_type"] = "skeleton";
  147. skeleton->set_meta("extras", skeleton_extras);
  148. skeleton->add_bone("parent");
  149. skeleton->set_bone_rest(0, Transform3D());
  150. Dictionary parent_bone_extras;
  151. parent_bone_extras["bone"] = "i_am_parent_bone";
  152. skeleton->set_bone_meta(0, "extras", parent_bone_extras);
  153. skeleton->add_bone("child");
  154. skeleton->set_bone_rest(1, Transform3D());
  155. skeleton->set_bone_parent(1, 0);
  156. Dictionary child_bone_extras;
  157. child_bone_extras["bone"] = "i_am_child_bone";
  158. skeleton->set_bone_meta(1, "extras", child_bone_extras);
  159. // We have to have a mesh to link with skeleton or it will not get imported.
  160. Ref<PlaneMesh> meshdata = memnew(PlaneMesh);
  161. meshdata->set_name("planemesh");
  162. MeshInstance3D *mesh = memnew(MeshInstance3D);
  163. mesh->set_mesh(meshdata);
  164. mesh->set_name("mesh_instance_3d");
  165. Node3D *scene = memnew(Node3D);
  166. SceneTree::get_singleton()->get_root()->add_child(scene);
  167. scene->add_child(skeleton);
  168. scene->add_child(mesh);
  169. scene->set_name("node3d");
  170. // Now that both skeleton and mesh are part of scene, link them.
  171. mesh->set_skeleton_path(mesh->get_path_to(skeleton));
  172. // Convert to GLFT and back.
  173. String tempfile = OS::get_singleton()->get_cache_path().path_join("gltf_bone_extras");
  174. Node *loaded = _gltf_export_then_import(scene, tempfile);
  175. // Compare the results.
  176. CHECK(loaded->get_name() == "node3d");
  177. Skeleton3D *result = Object::cast_to<Skeleton3D>(loaded->find_child("Skeleton3D", false, true));
  178. CHECK(result->get_bone_name(0) == "parent");
  179. CHECK(Dictionary(result->get_bone_meta(0, "extras"))["bone"] == "i_am_parent_bone");
  180. CHECK(result->get_bone_name(1) == "child");
  181. CHECK(Dictionary(result->get_bone_meta(1, "extras"))["bone"] == "i_am_child_bone");
  182. memdelete(skeleton);
  183. memdelete(mesh);
  184. memdelete(scene);
  185. memdelete(loaded);
  186. }
  187. } // namespace TestGltfExtras
  188. #endif // TOOLS_ENABLED
  189. #endif // TEST_GLTF_EXTRAS_H