| 1 | /**************************************************************************/ |
| 2 | /* mesh_library_editor_plugin.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 "mesh_library_editor_plugin.h" |
| 32 | |
| 33 | #include "editor/editor_interface.h" |
| 34 | #include "editor/editor_node.h" |
| 35 | #include "editor/editor_settings.h" |
| 36 | #include "editor/editor_string_names.h" |
| 37 | #include "editor/gui/editor_file_dialog.h" |
| 38 | #include "editor/inspector_dock.h" |
| 39 | #include "editor/plugins/node_3d_editor_plugin.h" |
| 40 | #include "main/main.h" |
| 41 | #include "scene/3d/mesh_instance_3d.h" |
| 42 | #include "scene/3d/navigation_region_3d.h" |
| 43 | #include "scene/3d/physics_body_3d.h" |
| 44 | #include "scene/gui/menu_button.h" |
| 45 | #include "scene/main/window.h" |
| 46 | #include "scene/resources/packed_scene.h" |
| 47 | |
| 48 | void MeshLibraryEditor::edit(const Ref<MeshLibrary> &p_mesh_library) { |
| 49 | mesh_library = p_mesh_library; |
| 50 | if (mesh_library.is_valid()) { |
| 51 | menu->get_popup()->set_item_disabled(menu->get_popup()->get_item_index(MENU_OPTION_UPDATE_FROM_SCENE), !mesh_library->has_meta("_editor_source_scene" )); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | void MeshLibraryEditor::() { |
| 56 | switch (option) { |
| 57 | case MENU_OPTION_REMOVE_ITEM: { |
| 58 | mesh_library->remove_item(to_erase); |
| 59 | } break; |
| 60 | default: { |
| 61 | }; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | void MeshLibraryEditor::(bool p_apply_xforms) { |
| 66 | cd_update->hide(); |
| 67 | apply_xforms = p_apply_xforms; |
| 68 | String existing = mesh_library->get_meta("_editor_source_scene" ); |
| 69 | ERR_FAIL_COND(existing.is_empty()); |
| 70 | _import_scene_cbk(existing); |
| 71 | } |
| 72 | |
| 73 | void MeshLibraryEditor::_import_scene(Node *p_scene, Ref<MeshLibrary> p_library, bool p_merge, bool p_apply_xforms) { |
| 74 | if (!p_merge) { |
| 75 | p_library->clear(); |
| 76 | } |
| 77 | |
| 78 | HashMap<int, MeshInstance3D *> mesh_instances; |
| 79 | |
| 80 | for (int i = 0; i < p_scene->get_child_count(); i++) { |
| 81 | Node *child = p_scene->get_child(i); |
| 82 | |
| 83 | if (!Object::cast_to<MeshInstance3D>(child)) { |
| 84 | if (child->get_child_count() > 0) { |
| 85 | child = child->get_child(0); |
| 86 | if (!Object::cast_to<MeshInstance3D>(child)) { |
| 87 | continue; |
| 88 | } |
| 89 | |
| 90 | } else { |
| 91 | continue; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | MeshInstance3D *mi = Object::cast_to<MeshInstance3D>(child); |
| 96 | Ref<Mesh> mesh = mi->get_mesh(); |
| 97 | if (mesh.is_null()) { |
| 98 | continue; |
| 99 | } |
| 100 | |
| 101 | mesh = mesh->duplicate(); |
| 102 | for (int j = 0; j < mesh->get_surface_count(); ++j) { |
| 103 | Ref<Material> mat = mi->get_surface_override_material(j); |
| 104 | |
| 105 | if (mat.is_valid()) { |
| 106 | mesh->surface_set_material(j, mat); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | int id = p_library->find_item_by_name(mi->get_name()); |
| 111 | if (id < 0) { |
| 112 | id = p_library->get_last_unused_item_id(); |
| 113 | p_library->create_item(id); |
| 114 | p_library->set_item_name(id, mi->get_name()); |
| 115 | } |
| 116 | |
| 117 | p_library->set_item_mesh(id, mesh); |
| 118 | |
| 119 | if (p_apply_xforms) { |
| 120 | p_library->set_item_mesh_transform(id, mi->get_transform()); |
| 121 | } else { |
| 122 | p_library->set_item_mesh_transform(id, Transform3D()); |
| 123 | } |
| 124 | |
| 125 | mesh_instances[id] = mi; |
| 126 | |
| 127 | Vector<MeshLibrary::ShapeData> collisions; |
| 128 | |
| 129 | for (int j = 0; j < mi->get_child_count(); j++) { |
| 130 | Node *child2 = mi->get_child(j); |
| 131 | if (!Object::cast_to<StaticBody3D>(child2)) { |
| 132 | continue; |
| 133 | } |
| 134 | |
| 135 | StaticBody3D *sb = Object::cast_to<StaticBody3D>(child2); |
| 136 | List<uint32_t> shapes; |
| 137 | sb->get_shape_owners(&shapes); |
| 138 | |
| 139 | for (uint32_t &E : shapes) { |
| 140 | if (sb->is_shape_owner_disabled(E)) { |
| 141 | continue; |
| 142 | } |
| 143 | |
| 144 | Transform3D shape_transform; |
| 145 | if (p_apply_xforms) { |
| 146 | shape_transform = mi->get_transform(); |
| 147 | } |
| 148 | shape_transform *= sb->get_transform() * sb->shape_owner_get_transform(E); |
| 149 | |
| 150 | for (int k = 0; k < sb->shape_owner_get_shape_count(E); k++) { |
| 151 | Ref<Shape3D> collision = sb->shape_owner_get_shape(E, k); |
| 152 | if (!collision.is_valid()) { |
| 153 | continue; |
| 154 | } |
| 155 | MeshLibrary::ShapeData shape_data; |
| 156 | shape_data.shape = collision; |
| 157 | shape_data.local_transform = shape_transform; |
| 158 | collisions.push_back(shape_data); |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | p_library->set_item_shapes(id, collisions); |
| 164 | |
| 165 | Ref<NavigationMesh> navigation_mesh; |
| 166 | Transform3D navigation_mesh_transform; |
| 167 | for (int j = 0; j < mi->get_child_count(); j++) { |
| 168 | Node *child2 = mi->get_child(j); |
| 169 | if (!Object::cast_to<NavigationRegion3D>(child2)) { |
| 170 | continue; |
| 171 | } |
| 172 | NavigationRegion3D *sb = Object::cast_to<NavigationRegion3D>(child2); |
| 173 | navigation_mesh = sb->get_navigation_mesh(); |
| 174 | navigation_mesh_transform = sb->get_transform(); |
| 175 | if (!navigation_mesh.is_null()) { |
| 176 | break; |
| 177 | } |
| 178 | } |
| 179 | if (!navigation_mesh.is_null()) { |
| 180 | p_library->set_item_navigation_mesh(id, navigation_mesh); |
| 181 | p_library->set_item_navigation_mesh_transform(id, navigation_mesh_transform); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | //generate previews! |
| 186 | |
| 187 | if (true) { |
| 188 | Vector<Ref<Mesh>> meshes; |
| 189 | Vector<Transform3D> transforms; |
| 190 | Vector<int> ids = p_library->get_item_list(); |
| 191 | for (int i = 0; i < ids.size(); i++) { |
| 192 | if (mesh_instances.find(ids[i])) { |
| 193 | meshes.push_back(p_library->get_item_mesh(ids[i])); |
| 194 | transforms.push_back(mesh_instances[ids[i]]->get_transform()); |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | Vector<Ref<Texture2D>> textures = EditorInterface::get_singleton()->make_mesh_previews(meshes, &transforms, EDITOR_GET("editors/grid_map/preview_size" )); |
| 199 | int j = 0; |
| 200 | for (int i = 0; i < ids.size(); i++) { |
| 201 | if (mesh_instances.find(ids[i])) { |
| 202 | p_library->set_item_preview(ids[i], textures[j]); |
| 203 | j++; |
| 204 | } |
| 205 | } |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | void MeshLibraryEditor::_import_scene_cbk(const String &p_str) { |
| 210 | Ref<PackedScene> ps = ResourceLoader::load(p_str, "PackedScene" ); |
| 211 | ERR_FAIL_COND(ps.is_null()); |
| 212 | Node *scene = ps->instantiate(); |
| 213 | |
| 214 | ERR_FAIL_NULL_MSG(scene, "Cannot create an instance from PackedScene '" + p_str + "'." ); |
| 215 | |
| 216 | _import_scene(scene, mesh_library, option == MENU_OPTION_UPDATE_FROM_SCENE, apply_xforms); |
| 217 | |
| 218 | memdelete(scene); |
| 219 | mesh_library->set_meta("_editor_source_scene" , p_str); |
| 220 | |
| 221 | menu->get_popup()->set_item_disabled(menu->get_popup()->get_item_index(MENU_OPTION_UPDATE_FROM_SCENE), false); |
| 222 | } |
| 223 | |
| 224 | Error MeshLibraryEditor::update_library_file(Node *p_base_scene, Ref<MeshLibrary> ml, bool p_merge, bool p_apply_xforms) { |
| 225 | _import_scene(p_base_scene, ml, p_merge, p_apply_xforms); |
| 226 | return OK; |
| 227 | } |
| 228 | |
| 229 | void MeshLibraryEditor::(int p_option) { |
| 230 | option = p_option; |
| 231 | switch (p_option) { |
| 232 | case MENU_OPTION_ADD_ITEM: { |
| 233 | mesh_library->create_item(mesh_library->get_last_unused_item_id()); |
| 234 | } break; |
| 235 | case MENU_OPTION_REMOVE_ITEM: { |
| 236 | String p = InspectorDock::get_inspector_singleton()->get_selected_path(); |
| 237 | if (p.begins_with("item" ) && p.get_slice_count("/" ) >= 2) { |
| 238 | to_erase = p.get_slice("/" , 1).to_int(); |
| 239 | cd_remove->set_text(vformat(TTR("Remove item %d?" ), to_erase)); |
| 240 | cd_remove->popup_centered(Size2(300, 60)); |
| 241 | } |
| 242 | } break; |
| 243 | case MENU_OPTION_IMPORT_FROM_SCENE: { |
| 244 | apply_xforms = false; |
| 245 | file->popup_file_dialog(); |
| 246 | } break; |
| 247 | case MENU_OPTION_IMPORT_FROM_SCENE_APPLY_XFORMS: { |
| 248 | apply_xforms = true; |
| 249 | file->popup_file_dialog(); |
| 250 | } break; |
| 251 | case MENU_OPTION_UPDATE_FROM_SCENE: { |
| 252 | cd_update->set_text(vformat(TTR("Update from existing scene?:\n%s" ), String(mesh_library->get_meta("_editor_source_scene" )))); |
| 253 | cd_update->popup_centered(Size2(500, 60)); |
| 254 | } break; |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | void MeshLibraryEditor::_bind_methods() { |
| 259 | } |
| 260 | |
| 261 | MeshLibraryEditor::MeshLibraryEditor() { |
| 262 | file = memnew(EditorFileDialog); |
| 263 | file->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILE); |
| 264 | //not for now? |
| 265 | List<String> extensions; |
| 266 | ResourceLoader::get_recognized_extensions_for_type("PackedScene" , &extensions); |
| 267 | file->clear_filters(); |
| 268 | file->set_title(TTR("Import Scene" )); |
| 269 | for (int i = 0; i < extensions.size(); i++) { |
| 270 | file->add_filter("*." + extensions[i], extensions[i].to_upper()); |
| 271 | } |
| 272 | add_child(file); |
| 273 | file->connect("file_selected" , callable_mp(this, &MeshLibraryEditor::_import_scene_cbk)); |
| 274 | |
| 275 | menu = memnew(MenuButton); |
| 276 | Node3DEditor::get_singleton()->add_control_to_menu_panel(menu); |
| 277 | menu->set_position(Point2(1, 1)); |
| 278 | menu->set_text(TTR("MeshLibrary" )); |
| 279 | menu->set_icon(EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("MeshLibrary" ), EditorStringName(EditorIcons))); |
| 280 | menu->get_popup()->add_item(TTR("Add Item" ), MENU_OPTION_ADD_ITEM); |
| 281 | menu->get_popup()->add_item(TTR("Remove Selected Item" ), MENU_OPTION_REMOVE_ITEM); |
| 282 | menu->get_popup()->add_separator(); |
| 283 | menu->get_popup()->add_item(TTR("Import from Scene (Ignore Transforms)" ), MENU_OPTION_IMPORT_FROM_SCENE); |
| 284 | menu->get_popup()->add_item(TTR("Import from Scene (Apply Transforms)" ), MENU_OPTION_IMPORT_FROM_SCENE_APPLY_XFORMS); |
| 285 | menu->get_popup()->add_item(TTR("Update from Scene" ), MENU_OPTION_UPDATE_FROM_SCENE); |
| 286 | menu->get_popup()->set_item_disabled(menu->get_popup()->get_item_index(MENU_OPTION_UPDATE_FROM_SCENE), true); |
| 287 | menu->get_popup()->connect("id_pressed" , callable_mp(this, &MeshLibraryEditor::_menu_cbk)); |
| 288 | menu->hide(); |
| 289 | |
| 290 | cd_remove = memnew(ConfirmationDialog); |
| 291 | add_child(cd_remove); |
| 292 | cd_remove->get_ok_button()->connect("pressed" , callable_mp(this, &MeshLibraryEditor::_menu_remove_confirm)); |
| 293 | cd_update = memnew(ConfirmationDialog); |
| 294 | add_child(cd_update); |
| 295 | cd_update->set_ok_button_text(TTR("Apply without Transforms" )); |
| 296 | cd_update->get_ok_button()->connect("pressed" , callable_mp(this, &MeshLibraryEditor::_menu_update_confirm).bind(false)); |
| 297 | cd_update->add_button(TTR("Apply with Transforms" ))->connect("pressed" , callable_mp(this, &MeshLibraryEditor::_menu_update_confirm).bind(true)); |
| 298 | } |
| 299 | |
| 300 | void MeshLibraryEditorPlugin::edit(Object *p_node) { |
| 301 | if (Object::cast_to<MeshLibrary>(p_node)) { |
| 302 | mesh_library_editor->edit(Object::cast_to<MeshLibrary>(p_node)); |
| 303 | mesh_library_editor->show(); |
| 304 | } else { |
| 305 | mesh_library_editor->edit(Ref<MeshLibrary>()); |
| 306 | mesh_library_editor->hide(); |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | bool MeshLibraryEditorPlugin::handles(Object *p_node) const { |
| 311 | return p_node->is_class("MeshLibrary" ); |
| 312 | } |
| 313 | |
| 314 | void MeshLibraryEditorPlugin::make_visible(bool p_visible) { |
| 315 | if (p_visible) { |
| 316 | mesh_library_editor->show(); |
| 317 | mesh_library_editor->get_menu_button()->show(); |
| 318 | } else { |
| 319 | mesh_library_editor->hide(); |
| 320 | mesh_library_editor->get_menu_button()->hide(); |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | MeshLibraryEditorPlugin::MeshLibraryEditorPlugin() { |
| 325 | mesh_library_editor = memnew(MeshLibraryEditor); |
| 326 | |
| 327 | EditorNode::get_singleton()->get_main_screen_control()->add_child(mesh_library_editor); |
| 328 | mesh_library_editor->set_anchors_and_offsets_preset(Control::PRESET_TOP_WIDE); |
| 329 | mesh_library_editor->set_end(Point2(0, 22)); |
| 330 | mesh_library_editor->hide(); |
| 331 | } |
| 332 | |