mesh_library_editor_plugin.cpp 12 KB

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