test_gltf.h 7.5 KB

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