editor_scene_importer_gltf.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /**************************************************************************/
  2. /* editor_scene_importer_gltf.cpp */
  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. #include "editor_scene_importer_gltf.h"
  31. #ifdef TOOLS_ENABLED
  32. #include "../gltf_defines.h"
  33. #include "../gltf_document.h"
  34. uint32_t EditorSceneFormatImporterGLTF::get_import_flags() const {
  35. return ImportFlags::IMPORT_SCENE | ImportFlags::IMPORT_ANIMATION;
  36. }
  37. void EditorSceneFormatImporterGLTF::get_extensions(List<String> *r_extensions) const {
  38. r_extensions->push_back("gltf");
  39. r_extensions->push_back("glb");
  40. }
  41. Node *EditorSceneFormatImporterGLTF::import_scene(const String &p_path, uint32_t p_flags,
  42. const HashMap<StringName, Variant> &p_options,
  43. List<String> *r_missing_deps, Error *r_err) {
  44. Ref<GLTFDocument> gltf;
  45. gltf.instantiate();
  46. Ref<GLTFState> state;
  47. state.instantiate();
  48. if (p_options.has("gltf/naming_version")) {
  49. int naming_version = p_options["gltf/naming_version"];
  50. gltf->set_naming_version(naming_version);
  51. }
  52. if (p_options.has("gltf/embedded_image_handling")) {
  53. int32_t enum_option = p_options["gltf/embedded_image_handling"];
  54. state->set_handle_binary_image(enum_option);
  55. }
  56. if (p_options.has(SNAME("nodes/import_as_skeleton_bones")) ? (bool)p_options[SNAME("nodes/import_as_skeleton_bones")] : false) {
  57. state->set_import_as_skeleton_bones(true);
  58. }
  59. state->set_bake_fps(p_options["animation/fps"]);
  60. Error err = gltf->append_from_file(p_path, state, p_flags);
  61. if (err != OK) {
  62. if (r_err) {
  63. *r_err = err;
  64. }
  65. return nullptr;
  66. }
  67. if (p_options.has("animation/import")) {
  68. state->set_create_animations(bool(p_options["animation/import"]));
  69. }
  70. #ifndef DISABLE_DEPRECATED
  71. bool trimming = p_options.has("animation/trimming") ? (bool)p_options["animation/trimming"] : false;
  72. return gltf->generate_scene(state, state->get_bake_fps(), trimming, false);
  73. #else
  74. return gltf->generate_scene(state, state->get_bake_fps(), (bool)p_options["animation/trimming"], false);
  75. #endif
  76. }
  77. void EditorSceneFormatImporterGLTF::get_import_options(const String &p_path,
  78. List<ResourceImporter::ImportOption> *r_options) {
  79. String file_extension = p_path.get_extension().to_lower();
  80. // Returns all the options when path is empty because that means it's for the Project Settings.
  81. if (p_path.is_empty() || file_extension == "gltf" || file_extension == "glb") {
  82. r_options->push_back(ResourceImporterScene::ImportOption(PropertyInfo(Variant::INT, "gltf/naming_version", PROPERTY_HINT_ENUM, "Godot 4.1 or 4.0,Godot 4.2 or later"), 1));
  83. r_options->push_back(ResourceImporterScene::ImportOption(PropertyInfo(Variant::INT, "gltf/embedded_image_handling", PROPERTY_HINT_ENUM, "Discard All Textures,Extract Textures,Embed as Basis Universal,Embed as Uncompressed", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), GLTFState::HANDLE_BINARY_EXTRACT_TEXTURES));
  84. }
  85. }
  86. void EditorSceneFormatImporterGLTF::handle_compatibility_options(HashMap<StringName, Variant> &p_import_params) const {
  87. if (!p_import_params.has("gltf/naming_version")) {
  88. // If an existing import file is missing the glTF
  89. // compatibility version, we need to use version 0.
  90. p_import_params["gltf/naming_version"] = 0;
  91. }
  92. }
  93. Variant EditorSceneFormatImporterGLTF::get_option_visibility(const String &p_path, bool p_for_animation,
  94. const String &p_option, const HashMap<StringName, Variant> &p_options) {
  95. return true;
  96. }
  97. #endif // TOOLS_ENABLED