mesh_library_editor_plugin.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  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_node.h"
  32. #include "editor/editor_settings.h"
  33. #include "main/main.h"
  34. #include "scene/3d/mesh_instance.h"
  35. #include "scene/3d/navigation_mesh.h"
  36. #include "scene/3d/physics_body.h"
  37. #include "scene/main/viewport.h"
  38. #include "scene/resources/packed_scene.h"
  39. #include "spatial_editor_plugin.h"
  40. void MeshLibraryEditor::edit(const Ref<MeshLibrary> &p_mesh_library) {
  41. mesh_library = p_mesh_library;
  42. if (mesh_library.is_valid())
  43. menu->get_popup()->set_item_disabled(menu->get_popup()->get_item_index(MENU_OPTION_UPDATE_FROM_SCENE), !mesh_library->has_meta("_editor_source_scene"));
  44. }
  45. void MeshLibraryEditor::_menu_confirm() {
  46. switch (option) {
  47. case MENU_OPTION_REMOVE_ITEM: {
  48. mesh_library->remove_item(to_erase);
  49. } break;
  50. case MENU_OPTION_UPDATE_FROM_SCENE: {
  51. String existing = mesh_library->get_meta("_editor_source_scene");
  52. ERR_FAIL_COND(existing == "");
  53. _import_scene_cbk(existing);
  54. } break;
  55. default: {
  56. };
  57. }
  58. }
  59. void MeshLibraryEditor::_import_scene(Node *p_scene, Ref<MeshLibrary> p_library, bool p_merge) {
  60. if (!p_merge)
  61. p_library->clear();
  62. Map<int, MeshInstance *> mesh_instances;
  63. for (int i = 0; i < p_scene->get_child_count(); i++) {
  64. Node *child = p_scene->get_child(i);
  65. if (!Object::cast_to<MeshInstance>(child)) {
  66. if (child->get_child_count() > 0) {
  67. child = child->get_child(0);
  68. if (!Object::cast_to<MeshInstance>(child)) {
  69. continue;
  70. }
  71. } else
  72. continue;
  73. }
  74. MeshInstance *mi = Object::cast_to<MeshInstance>(child);
  75. Ref<Mesh> mesh = mi->get_mesh();
  76. if (mesh.is_null())
  77. continue;
  78. mesh = mesh->duplicate();
  79. for (int j = 0; j < mesh->get_surface_count(); ++j) {
  80. Ref<Material> mat = mi->get_surface_material(j);
  81. if (mat.is_valid()) {
  82. mesh->surface_set_material(j, mat);
  83. }
  84. }
  85. int id = p_library->find_item_by_name(mi->get_name());
  86. if (id < 0) {
  87. id = p_library->get_last_unused_item_id();
  88. p_library->create_item(id);
  89. p_library->set_item_name(id, mi->get_name());
  90. }
  91. p_library->set_item_mesh(id, mesh);
  92. mesh_instances[id] = mi;
  93. Vector<MeshLibrary::ShapeData> collisions;
  94. for (int j = 0; j < mi->get_child_count(); j++) {
  95. Node *child2 = mi->get_child(j);
  96. if (!Object::cast_to<StaticBody>(child2))
  97. continue;
  98. StaticBody *sb = Object::cast_to<StaticBody>(child2);
  99. List<uint32_t> shapes;
  100. sb->get_shape_owners(&shapes);
  101. for (List<uint32_t>::Element *E = shapes.front(); E; E = E->next()) {
  102. if (sb->is_shape_owner_disabled(E->get()))
  103. continue;
  104. //Transform shape_transform = sb->shape_owner_get_transform(E->get());
  105. //shape_transform.set_origin(shape_transform.get_origin() - phys_offset);
  106. for (int k = 0; k < sb->shape_owner_get_shape_count(E->get()); k++) {
  107. Ref<Shape> collision = sb->shape_owner_get_shape(E->get(), k);
  108. if (!collision.is_valid())
  109. continue;
  110. MeshLibrary::ShapeData shape_data;
  111. shape_data.shape = collision;
  112. shape_data.local_transform = sb->get_transform() * sb->shape_owner_get_transform(E->get());
  113. collisions.push_back(shape_data);
  114. }
  115. }
  116. }
  117. p_library->set_item_shapes(id, collisions);
  118. Ref<NavigationMesh> navmesh;
  119. Transform navmesh_transform;
  120. for (int j = 0; j < mi->get_child_count(); j++) {
  121. Node *child2 = mi->get_child(j);
  122. if (!Object::cast_to<NavigationMeshInstance>(child2))
  123. continue;
  124. NavigationMeshInstance *sb = Object::cast_to<NavigationMeshInstance>(child2);
  125. navmesh = sb->get_navigation_mesh();
  126. navmesh_transform = sb->get_transform();
  127. if (!navmesh.is_null())
  128. break;
  129. }
  130. if (!navmesh.is_null()) {
  131. p_library->set_item_navmesh(id, navmesh);
  132. p_library->set_item_navmesh_transform(id, navmesh_transform);
  133. }
  134. }
  135. //generate previews!
  136. if (1) {
  137. Vector<Ref<Mesh>> meshes;
  138. Vector<Transform> transforms;
  139. Vector<int> ids = p_library->get_item_list();
  140. for (int i = 0; i < ids.size(); i++) {
  141. if (mesh_instances.find(ids[i])) {
  142. meshes.push_back(p_library->get_item_mesh(ids[i]));
  143. transforms.push_back(mesh_instances[ids[i]]->get_transform());
  144. }
  145. }
  146. Vector<Ref<Texture>> textures = EditorInterface::get_singleton()->make_mesh_previews(meshes, &transforms, EditorSettings::get_singleton()->get("editors/grid_map/preview_size"));
  147. int j = 0;
  148. for (int i = 0; i < ids.size(); i++) {
  149. if (mesh_instances.find(ids[i])) {
  150. p_library->set_item_preview(ids[i], textures[j]);
  151. j++;
  152. }
  153. }
  154. }
  155. }
  156. void MeshLibraryEditor::_import_scene_cbk(const String &p_str) {
  157. Ref<PackedScene> ps = ResourceLoader::load(p_str, "PackedScene");
  158. ERR_FAIL_COND(ps.is_null());
  159. Node *scene = ps->instance();
  160. ERR_FAIL_COND_MSG(!scene, "Cannot create an instance from PackedScene '" + p_str + "'.");
  161. _import_scene(scene, mesh_library, option == MENU_OPTION_UPDATE_FROM_SCENE);
  162. memdelete(scene);
  163. mesh_library->set_meta("_editor_source_scene", p_str);
  164. menu->get_popup()->set_item_disabled(menu->get_popup()->get_item_index(MENU_OPTION_UPDATE_FROM_SCENE), false);
  165. }
  166. Error MeshLibraryEditor::update_library_file(Node *p_base_scene, Ref<MeshLibrary> ml, bool p_merge) {
  167. _import_scene(p_base_scene, ml, p_merge);
  168. return OK;
  169. }
  170. void MeshLibraryEditor::_menu_cbk(int p_option) {
  171. option = p_option;
  172. switch (p_option) {
  173. case MENU_OPTION_ADD_ITEM: {
  174. mesh_library->create_item(mesh_library->get_last_unused_item_id());
  175. } break;
  176. case MENU_OPTION_REMOVE_ITEM: {
  177. String p = editor->get_inspector()->get_selected_path();
  178. if (p.begins_with("/MeshLibrary/item") && p.get_slice_count("/") >= 3) {
  179. to_erase = p.get_slice("/", 3).to_int();
  180. cd->set_text(vformat(TTR("Remove item %d?"), to_erase));
  181. cd->popup_centered(Size2(300, 60));
  182. }
  183. } break;
  184. case MENU_OPTION_IMPORT_FROM_SCENE: {
  185. file->popup_centered_ratio();
  186. } break;
  187. case MENU_OPTION_UPDATE_FROM_SCENE: {
  188. cd->set_text(vformat(TTR("Update from existing scene?:\n%s"), String(mesh_library->get_meta("_editor_source_scene"))));
  189. cd->popup_centered(Size2(500, 60));
  190. } break;
  191. }
  192. }
  193. void MeshLibraryEditor::_bind_methods() {
  194. ClassDB::bind_method("_menu_cbk", &MeshLibraryEditor::_menu_cbk);
  195. ClassDB::bind_method("_menu_confirm", &MeshLibraryEditor::_menu_confirm);
  196. ClassDB::bind_method("_import_scene_cbk", &MeshLibraryEditor::_import_scene_cbk);
  197. }
  198. MeshLibraryEditor::MeshLibraryEditor(EditorNode *p_editor) {
  199. file = memnew(EditorFileDialog);
  200. file->set_mode(EditorFileDialog::MODE_OPEN_FILE);
  201. //not for now?
  202. List<String> extensions;
  203. ResourceLoader::get_recognized_extensions_for_type("PackedScene", &extensions);
  204. file->clear_filters();
  205. file->set_title(TTR("Import Scene"));
  206. for (int i = 0; i < extensions.size(); i++) {
  207. file->add_filter("*." + extensions[i] + " ; " + extensions[i].to_upper());
  208. }
  209. add_child(file);
  210. file->connect("file_selected", this, "_import_scene_cbk");
  211. menu = memnew(MenuButton);
  212. SpatialEditor::get_singleton()->add_control_to_menu_panel(menu);
  213. menu->set_position(Point2(1, 1));
  214. menu->set_text(TTR("Mesh Library"));
  215. menu->set_icon(EditorNode::get_singleton()->get_gui_base()->get_icon("MeshLibrary", "EditorIcons"));
  216. menu->get_popup()->add_item(TTR("Add Item"), MENU_OPTION_ADD_ITEM);
  217. menu->get_popup()->add_item(TTR("Remove Selected Item"), MENU_OPTION_REMOVE_ITEM);
  218. menu->get_popup()->add_separator();
  219. menu->get_popup()->add_item(TTR("Import from Scene"), MENU_OPTION_IMPORT_FROM_SCENE);
  220. menu->get_popup()->add_item(TTR("Update from Scene"), MENU_OPTION_UPDATE_FROM_SCENE);
  221. menu->get_popup()->set_item_disabled(menu->get_popup()->get_item_index(MENU_OPTION_UPDATE_FROM_SCENE), true);
  222. menu->get_popup()->connect("id_pressed", this, "_menu_cbk");
  223. menu->hide();
  224. editor = p_editor;
  225. cd = memnew(ConfirmationDialog);
  226. add_child(cd);
  227. cd->get_ok()->connect("pressed", this, "_menu_confirm");
  228. }
  229. void MeshLibraryEditorPlugin::edit(Object *p_node) {
  230. if (Object::cast_to<MeshLibrary>(p_node)) {
  231. mesh_library_editor->edit(Object::cast_to<MeshLibrary>(p_node));
  232. mesh_library_editor->show();
  233. } else
  234. mesh_library_editor->hide();
  235. }
  236. bool MeshLibraryEditorPlugin::handles(Object *p_node) const {
  237. return p_node->is_class("MeshLibrary");
  238. }
  239. void MeshLibraryEditorPlugin::make_visible(bool p_visible) {
  240. if (p_visible) {
  241. mesh_library_editor->show();
  242. mesh_library_editor->get_menu_button()->show();
  243. } else {
  244. mesh_library_editor->hide();
  245. mesh_library_editor->get_menu_button()->hide();
  246. }
  247. }
  248. MeshLibraryEditorPlugin::MeshLibraryEditorPlugin(EditorNode *p_node) {
  249. EDITOR_DEF("editors/grid_map/preview_size", 64);
  250. mesh_library_editor = memnew(MeshLibraryEditor(p_node));
  251. p_node->get_viewport()->add_child(mesh_library_editor);
  252. mesh_library_editor->set_anchors_and_margins_preset(Control::PRESET_TOP_WIDE);
  253. mesh_library_editor->set_end(Point2(0, 22));
  254. mesh_library_editor->hide();
  255. }