mesh_library_editor_plugin.cpp 14 KB

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