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_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_instance.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. }
  46. void MeshLibraryEditor::_menu_remove_confirm() {
  47. switch (option) {
  48. case MENU_OPTION_REMOVE_ITEM: {
  49. mesh_library->remove_item(to_erase);
  50. } break;
  51. default: {
  52. };
  53. }
  54. }
  55. void MeshLibraryEditor::_menu_update_confirm(bool p_apply_xforms) {
  56. cd_update->hide();
  57. apply_xforms = p_apply_xforms;
  58. String existing = mesh_library->get_meta("_editor_source_scene");
  59. ERR_FAIL_COND(existing == "");
  60. _import_scene_cbk(existing);
  61. }
  62. void MeshLibraryEditor::_import_scene(Node *p_scene, Ref<MeshLibrary> p_library, bool p_merge, bool p_apply_xforms) {
  63. if (!p_merge) {
  64. p_library->clear();
  65. }
  66. Map<int, MeshInstance *> mesh_instances;
  67. for (int i = 0; i < p_scene->get_child_count(); i++) {
  68. Node *child = p_scene->get_child(i);
  69. if (!Object::cast_to<MeshInstance>(child)) {
  70. if (child->get_child_count() > 0) {
  71. child = child->get_child(0);
  72. if (!Object::cast_to<MeshInstance>(child)) {
  73. continue;
  74. }
  75. } else {
  76. continue;
  77. }
  78. }
  79. MeshInstance *mi = Object::cast_to<MeshInstance>(child);
  80. Ref<Mesh> mesh = mi->get_mesh();
  81. if (mesh.is_null()) {
  82. continue;
  83. }
  84. mesh = mesh->duplicate();
  85. for (int j = 0; j < mesh->get_surface_count(); ++j) {
  86. Ref<Material> mat = mi->get_surface_material(j);
  87. if (mat.is_valid()) {
  88. mesh->surface_set_material(j, mat);
  89. }
  90. }
  91. int id = p_library->find_item_by_name(mi->get_name());
  92. if (id < 0) {
  93. id = p_library->get_last_unused_item_id();
  94. p_library->create_item(id);
  95. p_library->set_item_name(id, mi->get_name());
  96. }
  97. p_library->set_item_mesh(id, mesh);
  98. if (p_apply_xforms) {
  99. p_library->set_item_mesh_transform(id, mi->get_transform());
  100. } else {
  101. p_library->set_item_mesh_transform(id, Transform());
  102. }
  103. mesh_instances[id] = mi;
  104. Vector<MeshLibrary::ShapeData> collisions;
  105. for (int j = 0; j < mi->get_child_count(); j++) {
  106. Node *child2 = mi->get_child(j);
  107. if (!Object::cast_to<StaticBody>(child2)) {
  108. continue;
  109. }
  110. StaticBody *sb = Object::cast_to<StaticBody>(child2);
  111. List<uint32_t> shapes;
  112. sb->get_shape_owners(&shapes);
  113. for (List<uint32_t>::Element *E = shapes.front(); E; E = E->next()) {
  114. if (sb->is_shape_owner_disabled(E->get())) {
  115. continue;
  116. }
  117. Transform shape_transform;
  118. if (p_apply_xforms) {
  119. shape_transform = mi->get_transform();
  120. }
  121. shape_transform *= sb->get_transform() * sb->shape_owner_get_transform(E->get());
  122. for (int k = 0; k < sb->shape_owner_get_shape_count(E->get()); k++) {
  123. Ref<Shape> collision = sb->shape_owner_get_shape(E->get(), k);
  124. if (!collision.is_valid()) {
  125. continue;
  126. }
  127. MeshLibrary::ShapeData shape_data;
  128. shape_data.shape = collision;
  129. shape_data.local_transform = shape_transform;
  130. collisions.push_back(shape_data);
  131. }
  132. }
  133. }
  134. p_library->set_item_shapes(id, collisions);
  135. Ref<NavigationMesh> navmesh;
  136. Transform navmesh_transform;
  137. for (int j = 0; j < mi->get_child_count(); j++) {
  138. Node *child2 = mi->get_child(j);
  139. if (!Object::cast_to<NavigationMeshInstance>(child2)) {
  140. continue;
  141. }
  142. NavigationMeshInstance *sb = Object::cast_to<NavigationMeshInstance>(child2);
  143. navmesh = sb->get_navigation_mesh();
  144. navmesh_transform = sb->get_transform();
  145. if (!navmesh.is_null()) {
  146. break;
  147. }
  148. }
  149. if (!navmesh.is_null()) {
  150. p_library->set_item_navmesh(id, navmesh);
  151. p_library->set_item_navmesh_transform(id, navmesh_transform);
  152. }
  153. }
  154. //generate previews!
  155. if (true) {
  156. Vector<Ref<Mesh>> meshes;
  157. Vector<Transform> transforms;
  158. Vector<int> ids = p_library->get_item_list();
  159. for (int i = 0; i < ids.size(); i++) {
  160. if (mesh_instances.find(ids[i])) {
  161. meshes.push_back(p_library->get_item_mesh(ids[i]));
  162. transforms.push_back(mesh_instances[ids[i]]->get_transform());
  163. }
  164. }
  165. Vector<Ref<Texture>> textures = EditorInterface::get_singleton()->make_mesh_previews(meshes, &transforms, EditorSettings::get_singleton()->get("editors/grid_map/preview_size"));
  166. int j = 0;
  167. for (int i = 0; i < ids.size(); i++) {
  168. if (mesh_instances.find(ids[i])) {
  169. p_library->set_item_preview(ids[i], textures[j]);
  170. j++;
  171. }
  172. }
  173. }
  174. }
  175. void MeshLibraryEditor::_import_scene_cbk(const String &p_str) {
  176. Ref<PackedScene> ps = ResourceLoader::load(p_str, "PackedScene");
  177. ERR_FAIL_COND(ps.is_null());
  178. Node *scene = ps->instance();
  179. ERR_FAIL_COND_MSG(!scene, "Cannot create an instance from PackedScene '" + p_str + "'.");
  180. _import_scene(scene, mesh_library, option == MENU_OPTION_UPDATE_FROM_SCENE, apply_xforms);
  181. memdelete(scene);
  182. mesh_library->set_meta("_editor_source_scene", p_str);
  183. menu->get_popup()->set_item_disabled(menu->get_popup()->get_item_index(MENU_OPTION_UPDATE_FROM_SCENE), false);
  184. }
  185. Error MeshLibraryEditor::update_library_file(Node *p_base_scene, Ref<MeshLibrary> ml, bool p_merge, bool p_apply_xforms) {
  186. _import_scene(p_base_scene, ml, p_merge, p_apply_xforms);
  187. return OK;
  188. }
  189. void MeshLibraryEditor::_menu_cbk(int p_option) {
  190. option = p_option;
  191. switch (p_option) {
  192. case MENU_OPTION_ADD_ITEM: {
  193. mesh_library->create_item(mesh_library->get_last_unused_item_id());
  194. } break;
  195. case MENU_OPTION_REMOVE_ITEM: {
  196. String p = editor->get_inspector()->get_selected_path();
  197. if (p.begins_with("item") && p.get_slice_count("/") >= 2) {
  198. to_erase = p.get_slice("/", 1).to_int();
  199. cd_remove->set_text(vformat(TTR("Remove item %d?"), to_erase));
  200. cd_remove->popup_centered(Size2(300, 60));
  201. }
  202. } break;
  203. case MENU_OPTION_IMPORT_FROM_SCENE: {
  204. apply_xforms = false;
  205. file->popup_centered_ratio();
  206. } break;
  207. case MENU_OPTION_IMPORT_FROM_SCENE_APPLY_XFORMS: {
  208. apply_xforms = true;
  209. file->popup_centered_ratio();
  210. } break;
  211. case MENU_OPTION_UPDATE_FROM_SCENE: {
  212. cd_update->set_text(vformat(TTR("Update from existing scene?:\n%s"), String(mesh_library->get_meta("_editor_source_scene"))));
  213. cd_update->popup_centered(Size2(500, 60));
  214. } break;
  215. }
  216. }
  217. void MeshLibraryEditor::_bind_methods() {
  218. ClassDB::bind_method("_menu_cbk", &MeshLibraryEditor::_menu_cbk);
  219. ClassDB::bind_method("_menu_remove_confirm", &MeshLibraryEditor::_menu_remove_confirm);
  220. ClassDB::bind_method("_menu_update_confirm", &MeshLibraryEditor::_menu_update_confirm);
  221. ClassDB::bind_method("_import_scene_cbk", &MeshLibraryEditor::_import_scene_cbk);
  222. }
  223. MeshLibraryEditor::MeshLibraryEditor(EditorNode *p_editor) {
  224. file = memnew(EditorFileDialog);
  225. file->set_mode(EditorFileDialog::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", this, "_import_scene_cbk");
  236. menu = memnew(MenuButton);
  237. SpatialEditor::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_icon("MeshLibrary", "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", this, "_menu_cbk");
  249. menu->hide();
  250. editor = p_editor;
  251. cd_remove = memnew(ConfirmationDialog);
  252. add_child(cd_remove);
  253. cd_remove->get_ok()->connect("pressed", this, "_menu_remove_confirm");
  254. cd_update = memnew(ConfirmationDialog);
  255. add_child(cd_update);
  256. cd_update->get_ok()->set_text(TTR("Apply without Transforms"));
  257. cd_update->get_ok()->connect("pressed", this, "_menu_update_confirm", varray(false));
  258. cd_update->add_button(TTR("Apply with Transforms"))->connect("pressed", this, "_menu_update_confirm", varray(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->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(EditorNode *p_node) {
  281. mesh_library_editor = memnew(MeshLibraryEditor(p_node));
  282. p_node->get_viewport()->add_child(mesh_library_editor);
  283. mesh_library_editor->set_anchors_and_margins_preset(Control::PRESET_TOP_WIDE);
  284. mesh_library_editor->set_end(Point2(0, 22));
  285. mesh_library_editor->hide();
  286. }