| 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 | |
| 31 | #include "editor_scene_importer_gltf.h" |
| 32 | |
| 33 | #include "../gltf_defines.h" |
| 34 | #include "../gltf_document.h" |
| 35 | |
| 36 | void EditorSceneFormatImporterGLTF::get_extensions(List<String> *r_extensions) const { |
| 37 | r_extensions->push_back("gltf"); |
| 38 | r_extensions->push_back("glb"); |
| 39 | } |
| 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_mode((GLTFState::HandleBinaryImageMode)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 | if (p_options.has(SNAME("extract_path"))) { |
| 60 | state->set_extract_path(p_options["extract_path"]); |
| 61 | } |
| 62 | state->set_bake_fps(p_options["animation/fps"]); |
| 63 | Error err = gltf->append_from_file(p_path, state, p_flags); |
| 64 | if (err != OK) { |
| 65 | if (r_err) { |
| 66 | *r_err = err; |
| 67 | } |
| 68 | return nullptr; |
| 69 | } |
| 70 | if (p_options.has("animation/import")) { |
| 71 | state->set_create_animations(bool(p_options["animation/import"])); |
| 72 | } |
| 73 | |
| 74 | #ifndef DISABLE_DEPRECATED |
| 75 | bool trimming = p_options.has("animation/trimming") ? (bool)p_options["animation/trimming"] : false; |
| 76 | return gltf->generate_scene(state, state->get_bake_fps(), trimming, false); |
| 77 | #else |
| 78 | return gltf->generate_scene(state, state->get_bake_fps(), (bool)p_options["animation/trimming"], false); |
| 79 | #endif |
| 80 | } |
| 81 | |
| 82 | void EditorSceneFormatImporterGLTF::get_import_options(const String &p_path, |
| 83 | List<ResourceImporter::ImportOption> *r_options) { |
| 84 | String file_extension = p_path.get_extension().to_lower(); |
| 85 | // Returns all the options when path is empty because that means it's for the Project Settings. |
| 86 | if (p_path.is_empty() || file_extension == "gltf" || file_extension == "glb") { |
| 87 | r_options->push_back(ResourceImporterScene::ImportOption(PropertyInfo(Variant::INT, "gltf/naming_version", PROPERTY_HINT_ENUM, "Godot 4.0 or 4.1,Godot 4.2 to 4.4,Godot 4.5 or later"), 2)); |
| 88 | 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_IMAGE_MODE_EXTRACT_TEXTURES)); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | Variant EditorSceneFormatImporterGLTF::get_option_visibility(const String &p_path, const String &p_scene_import_type, |
| 93 | const String &p_option, const HashMap<StringName, Variant> &p_options) { |
| 94 | return true; |
| 95 | } |