| 1 | /**************************************************************************/ |
| 2 | /* editor_scene_exporter_gltf_settings.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_exporter_gltf_settings.h" |
| 32 | |
| 33 | const uint32_t PROP_EDITOR_SCRIPT_VAR = PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_SCRIPT_VARIABLE; |
| 34 | |
| 35 | bool EditorSceneExporterGLTFSettings::_set(const StringName &p_name, const Variant &p_value) { |
| 36 | String name_str = String(p_name); |
| 37 | if (name_str.contains_char('/')) { |
| 38 | return _set_extension_setting(name_str, p_value); |
| 39 | } |
| 40 | if (p_name == StringName("image_format")) { |
| 41 | _document->set_image_format(p_value); |
| 42 | emit_signal(CoreStringName(property_list_changed)); |
| 43 | return true; |
| 44 | } |
| 45 | if (p_name == StringName("lossy_quality")) { |
| 46 | _document->set_lossy_quality(p_value); |
| 47 | return true; |
| 48 | } |
| 49 | if (p_name == StringName("fallback_image_format")) { |
| 50 | _document->set_fallback_image_format(p_value); |
| 51 | emit_signal(CoreStringName(property_list_changed)); |
| 52 | return true; |
| 53 | } |
| 54 | if (p_name == StringName("fallback_image_quality")) { |
| 55 | _document->set_fallback_image_quality(p_value); |
| 56 | return true; |
| 57 | } |
| 58 | if (p_name == StringName("root_node_mode")) { |
| 59 | _document->set_root_node_mode((GLTFDocument::RootNodeMode)(int64_t)p_value); |
| 60 | return true; |
| 61 | } |
| 62 | if (p_name == StringName("visibility_mode")) { |
| 63 | _document->set_visibility_mode((GLTFDocument::VisibilityMode)(int64_t)p_value); |
| 64 | return true; |
| 65 | } |
| 66 | return false; |
| 67 | } |
| 68 | |
| 69 | bool EditorSceneExporterGLTFSettings::_get(const StringName &p_name, Variant &r_ret) const { |
| 70 | String name_str = String(p_name); |
| 71 | if (name_str.contains_char('/')) { |
| 72 | return _get_extension_setting(name_str, r_ret); |
| 73 | } |
| 74 | if (p_name == StringName("image_format")) { |
| 75 | r_ret = _document->get_image_format(); |
| 76 | return true; |
| 77 | } |
| 78 | if (p_name == StringName("lossy_quality")) { |
| 79 | r_ret = _document->get_lossy_quality(); |
| 80 | return true; |
| 81 | } |
| 82 | if (p_name == StringName("fallback_image_format")) { |
| 83 | r_ret = _document->get_fallback_image_format(); |
| 84 | return true; |
| 85 | } |
| 86 | if (p_name == StringName("fallback_image_quality")) { |
| 87 | r_ret = _document->get_fallback_image_quality(); |
| 88 | return true; |
| 89 | } |
| 90 | if (p_name == StringName("root_node_mode")) { |
| 91 | r_ret = _document->get_root_node_mode(); |
| 92 | return true; |
| 93 | } |
| 94 | if (p_name == StringName("visibility_mode")) { |
| 95 | r_ret = _document->get_visibility_mode(); |
| 96 | return true; |
| 97 | } |
| 98 | return false; |
| 99 | } |
| 100 | |
| 101 | void EditorSceneExporterGLTFSettings::_get_property_list(List<PropertyInfo> *p_list) const { |
| 102 | for (PropertyInfo prop : _property_list) { |
| 103 | if (prop.name == "lossy_quality") { |
| 104 | const String image_format = get("image_format"); |
| 105 | const bool is_image_format_lossy = image_format == "JPEG" || image_format.containsn("Lossy"); |
| 106 | prop.usage = is_image_format_lossy ? PROPERTY_USAGE_DEFAULT : PROPERTY_USAGE_STORAGE; |
| 107 | } |
| 108 | if (prop.name == "fallback_image_format") { |
| 109 | const String image_format = get("image_format"); |
| 110 | const bool is_image_format_extension = image_format != "None" && image_format != "PNG" && image_format != "JPEG"; |
| 111 | prop.usage = is_image_format_extension ? PROPERTY_USAGE_DEFAULT : PROPERTY_USAGE_STORAGE; |
| 112 | } |
| 113 | if (prop.name == "fallback_image_quality") { |
| 114 | const String image_format = get("image_format"); |
| 115 | const bool is_image_format_extension = image_format != "None" && image_format != "PNG" && image_format != "JPEG"; |
| 116 | const String fallback_format = get("fallback_image_format"); |
| 117 | prop.usage = (is_image_format_extension && fallback_format != "None") ? PROPERTY_USAGE_DEFAULT : PROPERTY_USAGE_STORAGE; |
| 118 | } |
| 119 | p_list->push_back(prop); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | void EditorSceneExporterGLTFSettings::_on_extension_property_list_changed() { |
| 124 | generate_property_list(_document); |
| 125 | emit_signal(CoreStringName(property_list_changed)); |
| 126 | } |
| 127 | |
| 128 | bool EditorSceneExporterGLTFSettings::_set_extension_setting(const String &p_name_str, const Variant &p_value) { |
| 129 | PackedStringArray split = String(p_name_str).split("/", true, 1); |
| 130 | if (!_config_name_to_extension_map.has(split[0])) { |
| 131 | return false; |
| 132 | } |
| 133 | Ref<GLTFDocumentExtension> extension = _config_name_to_extension_map[split[0]]; |
| 134 | bool valid; |
| 135 | extension->set(split[1], p_value, &valid); |
| 136 | return valid; |
| 137 | } |
| 138 | |
| 139 | bool EditorSceneExporterGLTFSettings::_get_extension_setting(const String &p_name_str, Variant &r_ret) const { |
| 140 | PackedStringArray split = String(p_name_str).split("/", true, 1); |
| 141 | if (!_config_name_to_extension_map.has(split[0])) { |
| 142 | return false; |
| 143 | } |
| 144 | Ref<GLTFDocumentExtension> extension = _config_name_to_extension_map[split[0]]; |
| 145 | bool valid; |
| 146 | r_ret = extension->get(split[1], &valid); |
| 147 | return valid; |
| 148 | } |
| 149 | |
| 150 | String get_friendly_config_prefix(Ref<GLTFDocumentExtension> p_extension) { |
| 151 | String config_prefix = p_extension->get_name(); |
| 152 | if (!config_prefix.is_empty()) { |
| 153 | return config_prefix; |
| 154 | } |
| 155 | const String class_name = p_extension->get_class_name(); |
| 156 | config_prefix = class_name.trim_prefix("GLTFDocumentExtension").trim_suffix("GLTFDocumentExtension").capitalize(); |
| 157 | if (!config_prefix.is_empty()) { |
| 158 | return config_prefix; |
| 159 | } |
| 160 | PackedStringArray supported_extensions = p_extension->get_supported_extensions(); |
| 161 | if (supported_extensions.size() > 0) { |
| 162 | return supported_extensions[0]; |
| 163 | } |
| 164 | return "Unknown GLTFDocumentExtension"; |
| 165 | } |
| 166 | |
| 167 | bool is_any_node_invisible(Node *p_node) { |
| 168 | if (p_node->has_method("is_visible")) { |
| 169 | bool visible = p_node->call("is_visible"); |
| 170 | if (!visible) { |
| 171 | return true; |
| 172 | } |
| 173 | } |
| 174 | for (int i = 0; i < p_node->get_child_count(); i++) { |
| 175 | if (is_any_node_invisible(p_node->get_child(i))) { |
| 176 | return true; |
| 177 | } |
| 178 | } |
| 179 | return false; |
| 180 | } |
| 181 | |
| 182 | // Run this before popping up the export settings, because the extensions may have changed. |
| 183 | void EditorSceneExporterGLTFSettings::generate_property_list(Ref<GLTFDocument> p_document, Node *p_root) { |
| 184 | _property_list.clear(); |
| 185 | _document = p_document; |
| 186 | String image_format_hint_string = "None,PNG,JPEG"; |
| 187 | // Add properties from all document extensions. |
| 188 | for (Ref<GLTFDocumentExtension> &extension : GLTFDocument::get_all_gltf_document_extensions()) { |
| 189 | const Callable on_prop_changed = callable_mp(this, &EditorSceneExporterGLTFSettings::_on_extension_property_list_changed); |
| 190 | if (!extension->is_connected(CoreStringName(property_list_changed), on_prop_changed)) { |
| 191 | extension->connect(CoreStringName(property_list_changed), on_prop_changed); |
| 192 | } |
| 193 | const String config_prefix = get_friendly_config_prefix(extension); |
| 194 | _config_name_to_extension_map[config_prefix] = extension; |
| 195 | // If the extension allows saving in different image formats, add to the enum. |
| 196 | PackedStringArray saveable_image_formats = extension->get_saveable_image_formats(); |
| 197 | for (int i = 0; i < saveable_image_formats.size(); i++) { |
| 198 | image_format_hint_string += "," + saveable_image_formats[i]; |
| 199 | } |
| 200 | // Look through the extension's properties and find the relevant ones. |
| 201 | List<PropertyInfo> ext_prop_list; |
| 202 | extension->get_property_list(&ext_prop_list); |
| 203 | for (const PropertyInfo &prop : ext_prop_list) { |
| 204 | // We only want properties that will show up in the exporter |
| 205 | // settings list. Exclude Resource's properties, as they are |
| 206 | // not relevant to the exporter. Include any user-defined script |
| 207 | // variables exposed to the editor (PROP_EDITOR_SCRIPT_VAR). |
| 208 | if ((prop.usage & PROP_EDITOR_SCRIPT_VAR) == PROP_EDITOR_SCRIPT_VAR) { |
| 209 | PropertyInfo ext_prop = prop; |
| 210 | ext_prop.name = config_prefix + "/" + prop.name; |
| 211 | _property_list.push_back(ext_prop); |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | // Add top-level properties (in addition to what _bind_methods registers). |
| 216 | PropertyInfo image_format_prop = PropertyInfo(Variant::STRING, "image_format", PROPERTY_HINT_ENUM, image_format_hint_string); |
| 217 | _property_list.push_back(image_format_prop); |
| 218 | PropertyInfo lossy_quality_prop = PropertyInfo(Variant::FLOAT, "lossy_quality", PROPERTY_HINT_RANGE, "0,1,0.01"); |
| 219 | _property_list.push_back(lossy_quality_prop); |
| 220 | PropertyInfo fallback_image_format_prop = PropertyInfo(Variant::STRING, "fallback_image_format", PROPERTY_HINT_ENUM, "None,PNG,JPEG"); |
| 221 | _property_list.push_back(fallback_image_format_prop); |
| 222 | PropertyInfo fallback_image_quality_prop = PropertyInfo(Variant::FLOAT, "fallback_image_quality", PROPERTY_HINT_RANGE, "0,1,0.01"); |
| 223 | _property_list.push_back(fallback_image_quality_prop); |
| 224 | PropertyInfo root_node_mode_prop = PropertyInfo(Variant::INT, "root_node_mode", PROPERTY_HINT_ENUM, "Single Root,Keep Root,Multi Root"); |
| 225 | _property_list.push_back(root_node_mode_prop); |
| 226 | // If the scene contains any non-visible nodes, show the visibility mode setting. |
| 227 | if (p_root != nullptr && is_any_node_invisible(p_root)) { |
| 228 | PropertyInfo visibility_mode_prop = PropertyInfo(Variant::INT, "visibility_mode", PROPERTY_HINT_ENUM, "Include & Required,Include & Optional,Exclude"); |
| 229 | _property_list.push_back(visibility_mode_prop); |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | String EditorSceneExporterGLTFSettings::get_copyright() const { |
| 234 | return _copyright; |
| 235 | } |
| 236 | |
| 237 | void EditorSceneExporterGLTFSettings::set_copyright(const String &p_copyright) { |
| 238 | _copyright = p_copyright; |
| 239 | } |
| 240 | |
| 241 | void EditorSceneExporterGLTFSettings::_bind_methods() { |
| 242 | ClassDB::bind_method(D_METHOD("get_copyright"), &EditorSceneExporterGLTFSettings::get_copyright); |
| 243 | ClassDB::bind_method(D_METHOD("set_copyright", "copyright"), &EditorSceneExporterGLTFSettings::set_copyright); |
| 244 | ADD_PROPERTY(PropertyInfo(Variant::STRING, "copyright", PROPERTY_HINT_PLACEHOLDER_TEXT, "Example: 2014 Godette"), "set_copyright", "get_copyright"); |
| 245 | |
| 246 | ClassDB::bind_method(D_METHOD("get_bake_fps"), &EditorSceneExporterGLTFSettings::get_bake_fps); |
| 247 | ClassDB::bind_method(D_METHOD("set_bake_fps", "bake_fps"), &EditorSceneExporterGLTFSettings::set_bake_fps); |
| 248 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "bake_fps", PROPERTY_HINT_RANGE, "0.001,120,0.0001,or_greater"), "set_bake_fps", "get_bake_fps"); |
| 249 | } |
| 250 | |
| 251 | double EditorSceneExporterGLTFSettings::get_bake_fps() const { |
| 252 | return _bake_fps; |
| 253 | } |
| 254 | |
| 255 | void EditorSceneExporterGLTFSettings::set_bake_fps(const double p_bake_fps) { |
| 256 | _bake_fps = p_bake_fps; |
| 257 | } |