mesh_library_editor_plugin.cpp 12 KB

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