test_gltf_extras.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 "test_gltf.h"
  33. #include "tests/test_macros.h"
  34. #ifdef TOOLS_ENABLED
  35. #include "core/os/os.h"
  36. #include "editor/import/3d/resource_importer_scene.h"
  37. #include "scene/3d/mesh_instance_3d.h"
  38. #include "scene/3d/skeleton_3d.h"
  39. #include "scene/main/window.h"
  40. #include "scene/resources/3d/primitive_meshes.h"
  41. #include "scene/resources/material.h"
  42. #include "scene/resources/packed_scene.h"
  43. #include "modules/gltf/editor/editor_scene_importer_gltf.h"
  44. #include "modules/gltf/gltf_document.h"
  45. #include "modules/gltf/gltf_state.h"
  46. namespace TestGltf {
  47. TEST_CASE("[SceneTree][Node] GLTF test mesh and material meta export and import") {
  48. init("gltf_mesh_material_extras");
  49. // Setup scene.
  50. Ref<StandardMaterial3D> original_material = memnew(StandardMaterial3D);
  51. original_material->set_albedo(Color(1.0, .0, .0));
  52. original_material->set_name("material");
  53. Dictionary material_dict;
  54. material_dict["node_type"] = "material";
  55. original_material->set_meta("extras", material_dict);
  56. Ref<PlaneMesh> original_meshdata = memnew(PlaneMesh);
  57. original_meshdata->set_name("planemesh");
  58. Dictionary meshdata_dict;
  59. meshdata_dict["node_type"] = "planemesh";
  60. original_meshdata->set_meta("extras", meshdata_dict);
  61. original_meshdata->surface_set_material(0, original_material);
  62. MeshInstance3D *original_mesh_instance = memnew(MeshInstance3D);
  63. original_mesh_instance->set_mesh(original_meshdata);
  64. original_mesh_instance->set_name("mesh_instance_3d");
  65. Dictionary mesh_instance_dict;
  66. mesh_instance_dict["node_type"] = "mesh_instance_3d";
  67. original_mesh_instance->set_meta("extras", mesh_instance_dict);
  68. Node3D *original = memnew(Node3D);
  69. SceneTree::get_singleton()->get_root()->add_child(original);
  70. original->add_child(original_mesh_instance);
  71. original->set_name("node3d");
  72. Dictionary node_dict;
  73. node_dict["node_type"] = "node3d";
  74. original->set_meta("extras", node_dict);
  75. original->set_meta("meta_not_nested_under_extras", "should not propagate");
  76. original->set_owner(SceneTree::get_singleton()->get_root());
  77. original_mesh_instance->set_owner(SceneTree::get_singleton()->get_root());
  78. // Convert to GLFT and back.
  79. Node *loaded = gltf_export_then_import(original, "gltf_extras");
  80. // Compare the results.
  81. CHECK(loaded->get_name() == "node3d");
  82. CHECK(Dictionary(loaded->get_meta("extras")).size() == 1);
  83. CHECK(Dictionary(loaded->get_meta("extras"))["node_type"] == "node3d");
  84. CHECK_FALSE(loaded->has_meta("meta_not_nested_under_extras"));
  85. CHECK_FALSE(Dictionary(loaded->get_meta("extras")).has("meta_not_nested_under_extras"));
  86. MeshInstance3D *mesh_instance_3d = Object::cast_to<MeshInstance3D>(loaded->find_child("mesh_instance_3d", false, true));
  87. CHECK(mesh_instance_3d->get_name() == "mesh_instance_3d");
  88. CHECK(Dictionary(mesh_instance_3d->get_meta("extras"))["node_type"] == "mesh_instance_3d");
  89. Ref<Mesh> mesh = mesh_instance_3d->get_mesh();
  90. CHECK(Dictionary(mesh->get_meta("extras"))["node_type"] == "planemesh");
  91. Ref<Material> material = mesh->surface_get_material(0);
  92. CHECK(material->get_name() == "material");
  93. CHECK(Dictionary(material->get_meta("extras"))["node_type"] == "material");
  94. memdelete(original_mesh_instance);
  95. memdelete(original);
  96. memdelete(loaded);
  97. }
  98. TEST_CASE("[SceneTree][Node] GLTF test skeleton and bone export and import") {
  99. init("gltf_skeleton_extras");
  100. // Setup scene.
  101. Skeleton3D *skeleton = memnew(Skeleton3D);
  102. skeleton->set_name("skeleton");
  103. Dictionary skeleton_extras;
  104. skeleton_extras["node_type"] = "skeleton";
  105. skeleton->set_meta("extras", skeleton_extras);
  106. skeleton->add_bone("parent");
  107. skeleton->set_bone_rest(0, Transform3D());
  108. Dictionary parent_bone_extras;
  109. parent_bone_extras["bone"] = "i_am_parent_bone";
  110. skeleton->set_bone_meta(0, "extras", parent_bone_extras);
  111. skeleton->add_bone("child");
  112. skeleton->set_bone_rest(1, Transform3D());
  113. skeleton->set_bone_parent(1, 0);
  114. Dictionary child_bone_extras;
  115. child_bone_extras["bone"] = "i_am_child_bone";
  116. skeleton->set_bone_meta(1, "extras", child_bone_extras);
  117. // We have to have a mesh to link with skeleton or it will not get imported.
  118. Ref<PlaneMesh> meshdata = memnew(PlaneMesh);
  119. meshdata->set_name("planemesh");
  120. MeshInstance3D *mesh = memnew(MeshInstance3D);
  121. mesh->set_mesh(meshdata);
  122. mesh->set_name("mesh_instance_3d");
  123. Node3D *original = memnew(Node3D);
  124. SceneTree::get_singleton()->get_root()->add_child(original);
  125. original->add_child(skeleton);
  126. original->add_child(mesh);
  127. original->set_name("node3d");
  128. // Now that both skeleton and mesh are part of scene, link them.
  129. mesh->set_skeleton_path(mesh->get_path_to(skeleton));
  130. mesh->set_owner(SceneTree::get_singleton()->get_root());
  131. original->set_owner(SceneTree::get_singleton()->get_root());
  132. // Convert to GLFT and back.
  133. Node *loaded = gltf_export_then_import(original, "gltf_bone_extras");
  134. // Compare the results.
  135. CHECK(loaded->get_name() == "node3d");
  136. Skeleton3D *result = Object::cast_to<Skeleton3D>(loaded->find_child("Skeleton3D", false, true));
  137. CHECK(result->get_bone_name(0) == "parent");
  138. CHECK(Dictionary(result->get_bone_meta(0, "extras"))["bone"] == "i_am_parent_bone");
  139. CHECK(result->get_bone_name(1) == "child");
  140. CHECK(Dictionary(result->get_bone_meta(1, "extras"))["bone"] == "i_am_child_bone");
  141. memdelete(skeleton);
  142. memdelete(mesh);
  143. memdelete(original);
  144. memdelete(loaded);
  145. }
  146. } //namespace TestGltf
  147. #endif // TOOLS_ENABLED
  148. #endif // TEST_GLTF_EXTRAS_H