Debian Codegraph @

editor/scene/3d/mesh_library_editor_plugin.cpp

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