mesh_library_editor_plugin.cpp 12 KB

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