mesh_library_editor_plugin.cpp 12 KB

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