test_gltf.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /**************************************************************************/
  2. /* test_gltf.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_H
  31. #define TEST_GLTF_H
  32. #include "tests/test_macros.h"
  33. #ifdef TOOLS_ENABLED
  34. #include "core/os/os.h"
  35. #include "drivers/png/image_loader_png.h"
  36. #include "editor/editor_resource_preview.h"
  37. #include "editor/import/3d/resource_importer_scene.h"
  38. #include "editor/import/resource_importer_texture.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/compressed_texture.h"
  44. #include "scene/resources/material.h"
  45. #include "scene/resources/packed_scene.h"
  46. #include "tests/core/config/test_project_settings.h"
  47. #include "modules/gltf/editor/editor_scene_importer_gltf.h"
  48. #include "modules/gltf/gltf_document.h"
  49. #include "modules/gltf/gltf_state.h"
  50. namespace TestGltf {
  51. static Node *gltf_import(const String &p_file) {
  52. // Setting up importers.
  53. Ref<ResourceImporterScene> import_scene;
  54. import_scene.instantiate("PackedScene", true);
  55. ResourceFormatImporter::get_singleton()->add_importer(import_scene);
  56. Ref<EditorSceneFormatImporterGLTF> import_gltf;
  57. import_gltf.instantiate();
  58. ResourceImporterScene::add_scene_importer(import_gltf);
  59. // Support processing png files in editor import.
  60. Ref<ResourceImporterTexture> import_texture;
  61. import_texture.instantiate(true);
  62. ResourceFormatImporter::get_singleton()->add_importer(import_texture);
  63. // Once editor import convert pngs to ctex, we will need to load it as ctex resource.
  64. Ref<ResourceFormatLoaderCompressedTexture2D> resource_loader_stream_texture;
  65. resource_loader_stream_texture.instantiate();
  66. ResourceLoader::add_resource_format_loader(resource_loader_stream_texture);
  67. HashMap<StringName, Variant> options(21);
  68. options["nodes/root_type"] = "";
  69. options["nodes/root_name"] = "";
  70. options["nodes/apply_root_scale"] = true;
  71. options["nodes/root_scale"] = 1.0;
  72. options["meshes/ensure_tangents"] = true;
  73. options["meshes/generate_lods"] = false;
  74. options["meshes/create_shadow_meshes"] = true;
  75. options["meshes/light_baking"] = 1;
  76. options["meshes/lightmap_texel_size"] = 0.2;
  77. options["meshes/force_disable_compression"] = false;
  78. options["skins/use_named_skins"] = true;
  79. options["animation/import"] = true;
  80. options["animation/fps"] = 30;
  81. options["animation/trimming"] = false;
  82. options["animation/remove_immutable_tracks"] = true;
  83. options["import_script/path"] = "";
  84. options["extract_path"] = "res://";
  85. options["_subresources"] = Dictionary();
  86. options["gltf/naming_version"] = 1;
  87. // Process gltf file, note that this generates `.scn` resource from the 2nd argument.
  88. String scene_file = "res://" + p_file.get_file().get_basename();
  89. Error err = import_scene->import(0, p_file, scene_file, options, nullptr, nullptr, nullptr);
  90. CHECK_MESSAGE(err == OK, "GLTF import failed.");
  91. Ref<PackedScene> packed_scene = ResourceLoader::load(scene_file + ".scn", "", ResourceFormatLoader::CACHE_MODE_REPLACE, &err);
  92. CHECK_MESSAGE(err == OK, "Loading scene failed.");
  93. Node *p_scene = packed_scene->instantiate();
  94. ResourceImporterScene::remove_scene_importer(import_gltf);
  95. ResourceFormatImporter::get_singleton()->remove_importer(import_texture);
  96. ResourceLoader::remove_resource_format_loader(resource_loader_stream_texture);
  97. return p_scene;
  98. }
  99. static Node *gltf_export_then_import(Node *p_root, const String &p_test_name) {
  100. String tempfile = TestUtils::get_temp_path(p_test_name);
  101. Ref<GLTFDocument> doc;
  102. doc.instantiate();
  103. Ref<GLTFState> state;
  104. state.instantiate();
  105. Error err = doc->append_from_scene(p_root, state, EditorSceneFormatImporter::IMPORT_USE_NAMED_SKIN_BINDS);
  106. CHECK_MESSAGE(err == OK, "GLTF state generation failed.");
  107. err = doc->write_to_filesystem(state, tempfile + ".gltf");
  108. CHECK_MESSAGE(err == OK, "Writing GLTF to cache dir failed.");
  109. return gltf_import(tempfile + ".gltf");
  110. }
  111. void init(const String &p_test, const String &p_copy_target = String()) {
  112. Error err;
  113. // Setup project settings since it's needed for the import process.
  114. String project_folder = TestUtils::get_temp_path(p_test.get_file().get_basename());
  115. Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  116. da->make_dir_recursive(project_folder.path_join(".godot").path_join("imported"));
  117. // Initialize res:// to `project_folder`.
  118. TestProjectSettingsInternalsAccessor::resource_path() = project_folder;
  119. err = ProjectSettings::get_singleton()->setup(project_folder, String(), true);
  120. if (p_copy_target.is_empty()) {
  121. return;
  122. }
  123. // Copy all the necessary test data files to the res:// directory.
  124. da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  125. String test_data = String("modules/gltf/tests/data/").path_join(p_test);
  126. da = DirAccess::open(test_data);
  127. CHECK_MESSAGE(da.is_valid(), "Unable to open folder.");
  128. da->list_dir_begin();
  129. for (String item = da->get_next(); !item.is_empty(); item = da->get_next()) {
  130. if (!FileAccess::exists(test_data.path_join(item))) {
  131. continue;
  132. }
  133. Ref<FileAccess> output = FileAccess::open(p_copy_target.path_join(item), FileAccess::WRITE, &err);
  134. CHECK_MESSAGE(err == OK, "Unable to open output file.");
  135. output->store_buffer(FileAccess::get_file_as_bytes(test_data.path_join(item)));
  136. output->close();
  137. }
  138. da->list_dir_end();
  139. }
  140. } //namespace TestGltf
  141. #endif // TOOLS_ENABLED
  142. #endif // TEST_GLTF_H