mesh_instance_editor_plugin.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. /*************************************************************************/
  2. /* mesh_instance_editor_plugin.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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_instance_editor_plugin.h"
  31. #include "editor/editor_scale.h"
  32. #include "scene/3d/collision_shape.h"
  33. #include "scene/3d/navigation_mesh.h"
  34. #include "scene/3d/physics_body.h"
  35. #include "scene/gui/box_container.h"
  36. #include "spatial_editor_plugin.h"
  37. void MeshInstanceEditor::_node_removed(Node *p_node) {
  38. if (p_node == node) {
  39. node = NULL;
  40. options->hide();
  41. }
  42. }
  43. void MeshInstanceEditor::edit(MeshInstance *p_mesh) {
  44. node = p_mesh;
  45. }
  46. void MeshInstanceEditor::_menu_option(int p_option) {
  47. Ref<Mesh> mesh = node->get_mesh();
  48. if (mesh.is_null()) {
  49. err_dialog->set_text(TTR("Mesh is empty!"));
  50. err_dialog->popup_centered_minsize();
  51. return;
  52. }
  53. switch (p_option) {
  54. case MENU_OPTION_CREATE_STATIC_TRIMESH_BODY: {
  55. EditorSelection *editor_selection = EditorNode::get_singleton()->get_editor_selection();
  56. UndoRedo *ur = EditorNode::get_singleton()->get_undo_redo();
  57. List<Node *> selection = editor_selection->get_selected_node_list();
  58. if (selection.empty()) {
  59. Ref<Shape> shape = mesh->create_trimesh_shape();
  60. if (shape.is_null()) {
  61. err_dialog->set_text(TTR("Couldn't create a Trimesh collision shape."));
  62. err_dialog->popup_centered_minsize();
  63. return;
  64. }
  65. CollisionShape *cshape = memnew(CollisionShape);
  66. cshape->set_shape(shape);
  67. StaticBody *body = memnew(StaticBody);
  68. body->add_child(cshape);
  69. Node *owner = node == get_tree()->get_edited_scene_root() ? node : node->get_owner();
  70. ur->create_action(TTR("Create Static Trimesh Body"));
  71. ur->add_do_method(node, "add_child", body);
  72. ur->add_do_method(body, "set_owner", owner);
  73. ur->add_do_method(cshape, "set_owner", owner);
  74. ur->add_do_reference(body);
  75. ur->add_undo_method(node, "remove_child", body);
  76. ur->commit_action();
  77. return;
  78. }
  79. ur->create_action(TTR("Create Static Trimesh Body"));
  80. for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
  81. MeshInstance *instance = Object::cast_to<MeshInstance>(E->get());
  82. if (!instance)
  83. continue;
  84. Ref<Mesh> m = instance->get_mesh();
  85. if (m.is_null())
  86. continue;
  87. Ref<Shape> shape = m->create_trimesh_shape();
  88. if (shape.is_null())
  89. continue;
  90. CollisionShape *cshape = memnew(CollisionShape);
  91. cshape->set_shape(shape);
  92. StaticBody *body = memnew(StaticBody);
  93. body->add_child(cshape);
  94. Node *owner = instance == get_tree()->get_edited_scene_root() ? instance : instance->get_owner();
  95. ur->add_do_method(instance, "add_child", body);
  96. ur->add_do_method(body, "set_owner", owner);
  97. ur->add_do_method(cshape, "set_owner", owner);
  98. ur->add_do_reference(body);
  99. ur->add_undo_method(instance, "remove_child", body);
  100. }
  101. ur->commit_action();
  102. } break;
  103. case MENU_OPTION_CREATE_TRIMESH_COLLISION_SHAPE: {
  104. if (node == get_tree()->get_edited_scene_root()) {
  105. err_dialog->set_text(TTR("This doesn't work on scene root!"));
  106. err_dialog->popup_centered_minsize();
  107. return;
  108. }
  109. Ref<Shape> shape = mesh->create_trimesh_shape();
  110. if (shape.is_null())
  111. return;
  112. CollisionShape *cshape = memnew(CollisionShape);
  113. cshape->set_shape(shape);
  114. cshape->set_transform(node->get_transform());
  115. Node *owner = node->get_owner();
  116. UndoRedo *ur = EditorNode::get_singleton()->get_undo_redo();
  117. ur->create_action(TTR("Create Trimesh Static Shape"));
  118. ur->add_do_method(node->get_parent(), "add_child", cshape);
  119. ur->add_do_method(node->get_parent(), "move_child", cshape, node->get_index() + 1);
  120. ur->add_do_method(cshape, "set_owner", owner);
  121. ur->add_do_reference(cshape);
  122. ur->add_undo_method(node->get_parent(), "remove_child", cshape);
  123. ur->commit_action();
  124. } break;
  125. case MENU_OPTION_CREATE_SINGLE_CONVEX_COLLISION_SHAPE: {
  126. if (node == get_tree()->get_edited_scene_root()) {
  127. err_dialog->set_text(TTR("Can't create a single convex collision shape for the scene root."));
  128. err_dialog->popup_centered_minsize();
  129. return;
  130. }
  131. Ref<Shape> shape = mesh->create_convex_shape();
  132. if (shape.is_null()) {
  133. err_dialog->set_text(TTR("Couldn't create a single convex collision shape."));
  134. err_dialog->popup_centered_minsize();
  135. return;
  136. }
  137. UndoRedo *ur = EditorNode::get_singleton()->get_undo_redo();
  138. ur->create_action(TTR("Create Single Convex Shape"));
  139. CollisionShape *cshape = memnew(CollisionShape);
  140. cshape->set_shape(shape);
  141. cshape->set_transform(node->get_transform());
  142. Node *owner = node->get_owner();
  143. ur->add_do_method(node->get_parent(), "add_child", cshape);
  144. ur->add_do_method(node->get_parent(), "move_child", cshape, node->get_index() + 1);
  145. ur->add_do_method(cshape, "set_owner", owner);
  146. ur->add_do_reference(cshape);
  147. ur->add_undo_method(node->get_parent(), "remove_child", cshape);
  148. ur->commit_action();
  149. } break;
  150. case MENU_OPTION_CREATE_MULTIPLE_CONVEX_COLLISION_SHAPES: {
  151. if (node == get_tree()->get_edited_scene_root()) {
  152. err_dialog->set_text(TTR("Can't create multiple convex collision shapes for the scene root."));
  153. err_dialog->popup_centered_minsize();
  154. return;
  155. }
  156. Vector<Ref<Shape> > shapes = mesh->convex_decompose();
  157. if (!shapes.size()) {
  158. err_dialog->set_text(TTR("Couldn't create any collision shapes."));
  159. err_dialog->popup_centered_minsize();
  160. return;
  161. }
  162. UndoRedo *ur = EditorNode::get_singleton()->get_undo_redo();
  163. ur->create_action(TTR("Create Multiple Convex Shapes"));
  164. for (int i = 0; i < shapes.size(); i++) {
  165. CollisionShape *cshape = memnew(CollisionShape);
  166. cshape->set_shape(shapes[i]);
  167. cshape->set_transform(node->get_transform());
  168. Node *owner = node->get_owner();
  169. ur->add_do_method(node->get_parent(), "add_child", cshape);
  170. ur->add_do_method(node->get_parent(), "move_child", cshape, node->get_index() + 1);
  171. ur->add_do_method(cshape, "set_owner", owner);
  172. ur->add_do_reference(cshape);
  173. ur->add_undo_method(node->get_parent(), "remove_child", cshape);
  174. }
  175. ur->commit_action();
  176. } break;
  177. case MENU_OPTION_CREATE_NAVMESH: {
  178. Ref<NavigationMesh> nmesh = memnew(NavigationMesh);
  179. if (nmesh.is_null())
  180. return;
  181. nmesh->create_from_mesh(mesh);
  182. NavigationMeshInstance *nmi = memnew(NavigationMeshInstance);
  183. nmi->set_navigation_mesh(nmesh);
  184. Node *owner = node == get_tree()->get_edited_scene_root() ? node : node->get_owner();
  185. UndoRedo *ur = EditorNode::get_singleton()->get_undo_redo();
  186. ur->create_action(TTR("Create Navigation Mesh"));
  187. ur->add_do_method(node, "add_child", nmi);
  188. ur->add_do_method(nmi, "set_owner", owner);
  189. ur->add_do_reference(nmi);
  190. ur->add_undo_method(node, "remove_child", nmi);
  191. ur->commit_action();
  192. } break;
  193. case MENU_OPTION_CREATE_OUTLINE_MESH: {
  194. outline_dialog->popup_centered(Vector2(200, 90));
  195. } break;
  196. case MENU_OPTION_CREATE_UV2: {
  197. Ref<ArrayMesh> mesh2 = node->get_mesh();
  198. if (!mesh2.is_valid()) {
  199. err_dialog->set_text(TTR("Contained Mesh is not of type ArrayMesh."));
  200. err_dialog->popup_centered_minsize();
  201. return;
  202. }
  203. Error err = mesh2->lightmap_unwrap(node->get_global_transform());
  204. if (err != OK) {
  205. err_dialog->set_text(TTR("UV Unwrap failed, mesh may not be manifold?"));
  206. err_dialog->popup_centered_minsize();
  207. return;
  208. }
  209. } break;
  210. case MENU_OPTION_DEBUG_UV1: {
  211. Ref<Mesh> mesh2 = node->get_mesh();
  212. if (!mesh2.is_valid()) {
  213. err_dialog->set_text(TTR("No mesh to debug."));
  214. err_dialog->popup_centered_minsize();
  215. return;
  216. }
  217. _create_uv_lines(0);
  218. } break;
  219. case MENU_OPTION_DEBUG_UV2: {
  220. Ref<Mesh> mesh2 = node->get_mesh();
  221. if (!mesh2.is_valid()) {
  222. err_dialog->set_text(TTR("No mesh to debug."));
  223. err_dialog->popup_centered_minsize();
  224. return;
  225. }
  226. _create_uv_lines(1);
  227. } break;
  228. }
  229. }
  230. struct MeshInstanceEditorEdgeSort {
  231. Vector2 a;
  232. Vector2 b;
  233. bool operator<(const MeshInstanceEditorEdgeSort &p_b) const {
  234. if (a == p_b.a)
  235. return b < p_b.b;
  236. else
  237. return a < p_b.a;
  238. }
  239. MeshInstanceEditorEdgeSort() {}
  240. MeshInstanceEditorEdgeSort(const Vector2 &p_a, const Vector2 &p_b) {
  241. if (p_a < p_b) {
  242. a = p_a;
  243. b = p_b;
  244. } else {
  245. b = p_a;
  246. a = p_b;
  247. }
  248. }
  249. };
  250. void MeshInstanceEditor::_create_uv_lines(int p_layer) {
  251. Ref<Mesh> mesh = node->get_mesh();
  252. ERR_FAIL_COND(!mesh.is_valid());
  253. Set<MeshInstanceEditorEdgeSort> edges;
  254. uv_lines.clear();
  255. for (int i = 0; i < mesh->get_surface_count(); i++) {
  256. if (mesh->surface_get_primitive_type(i) != Mesh::PRIMITIVE_TRIANGLES)
  257. continue;
  258. Array a = mesh->surface_get_arrays(i);
  259. PoolVector<Vector2> uv = a[p_layer == 0 ? Mesh::ARRAY_TEX_UV : Mesh::ARRAY_TEX_UV2];
  260. if (uv.size() == 0) {
  261. err_dialog->set_text(TTR("Model has no UV in this layer"));
  262. err_dialog->popup_centered_minsize();
  263. return;
  264. }
  265. PoolVector<Vector2>::Read r = uv.read();
  266. PoolVector<int> indices = a[Mesh::ARRAY_INDEX];
  267. PoolVector<int>::Read ri;
  268. int ic;
  269. bool use_indices;
  270. if (indices.size()) {
  271. ic = indices.size();
  272. ri = indices.read();
  273. use_indices = true;
  274. } else {
  275. ic = uv.size();
  276. use_indices = false;
  277. }
  278. for (int j = 0; j < ic; j += 3) {
  279. for (int k = 0; k < 3; k++) {
  280. MeshInstanceEditorEdgeSort edge;
  281. if (use_indices) {
  282. edge.a = r[ri[j + k]];
  283. edge.b = r[ri[j + ((k + 1) % 3)]];
  284. } else {
  285. edge.a = r[j + k];
  286. edge.b = r[j + ((k + 1) % 3)];
  287. }
  288. if (edges.has(edge))
  289. continue;
  290. uv_lines.push_back(edge.a);
  291. uv_lines.push_back(edge.b);
  292. edges.insert(edge);
  293. }
  294. }
  295. }
  296. debug_uv_dialog->popup_centered_minsize();
  297. }
  298. void MeshInstanceEditor::_debug_uv_draw() {
  299. if (uv_lines.size() == 0)
  300. return;
  301. debug_uv->set_clip_contents(true);
  302. debug_uv->draw_rect(Rect2(Vector2(), debug_uv->get_size()), Color(0.2, 0.2, 0.0));
  303. debug_uv->draw_set_transform(Vector2(), 0, debug_uv->get_size());
  304. debug_uv->draw_multiline(uv_lines, Color(1.0, 0.8, 0.7));
  305. }
  306. void MeshInstanceEditor::_create_outline_mesh() {
  307. Ref<Mesh> mesh = node->get_mesh();
  308. if (mesh.is_null()) {
  309. err_dialog->set_text(TTR("MeshInstance lacks a Mesh!"));
  310. err_dialog->popup_centered_minsize();
  311. return;
  312. }
  313. if (mesh->get_surface_count() == 0) {
  314. err_dialog->set_text(TTR("Mesh has not surface to create outlines from!"));
  315. err_dialog->popup_centered_minsize();
  316. return;
  317. } else if (mesh->get_surface_count() == 1 && mesh->surface_get_primitive_type(0) != Mesh::PRIMITIVE_TRIANGLES) {
  318. err_dialog->set_text(TTR("Mesh primitive type is not PRIMITIVE_TRIANGLES!"));
  319. err_dialog->popup_centered_minsize();
  320. return;
  321. }
  322. Ref<Mesh> mesho = mesh->create_outline(outline_size->get_value());
  323. if (mesho.is_null()) {
  324. err_dialog->set_text(TTR("Could not create outline!"));
  325. err_dialog->popup_centered_minsize();
  326. return;
  327. }
  328. MeshInstance *mi = memnew(MeshInstance);
  329. mi->set_mesh(mesho);
  330. Node *owner = node->get_owner();
  331. if (get_tree()->get_edited_scene_root() == node) {
  332. owner = node;
  333. }
  334. UndoRedo *ur = EditorNode::get_singleton()->get_undo_redo();
  335. ur->create_action(TTR("Create Outline"));
  336. ur->add_do_method(node, "add_child", mi);
  337. ur->add_do_method(mi, "set_owner", owner);
  338. ur->add_do_reference(mi);
  339. ur->add_undo_method(node, "remove_child", mi);
  340. ur->commit_action();
  341. }
  342. void MeshInstanceEditor::_bind_methods() {
  343. ClassDB::bind_method("_menu_option", &MeshInstanceEditor::_menu_option);
  344. ClassDB::bind_method("_create_outline_mesh", &MeshInstanceEditor::_create_outline_mesh);
  345. ClassDB::bind_method("_debug_uv_draw", &MeshInstanceEditor::_debug_uv_draw);
  346. }
  347. MeshInstanceEditor::MeshInstanceEditor() {
  348. options = memnew(MenuButton);
  349. options->set_switch_on_hover(true);
  350. SpatialEditor::get_singleton()->add_control_to_menu_panel(options);
  351. options->set_text(TTR("Mesh"));
  352. options->set_icon(EditorNode::get_singleton()->get_gui_base()->get_icon("MeshInstance", "EditorIcons"));
  353. options->get_popup()->add_item(TTR("Create Trimesh Static Body"), MENU_OPTION_CREATE_STATIC_TRIMESH_BODY);
  354. options->get_popup()->set_item_tooltip(options->get_popup()->get_item_count() - 1, TTR("Creates a StaticBody and assigns a polygon-based collision shape to it automatically.\nThis is the most accurate (but slowest) option for collision detection."));
  355. options->get_popup()->add_separator();
  356. options->get_popup()->add_item(TTR("Create Trimesh Collision Sibling"), MENU_OPTION_CREATE_TRIMESH_COLLISION_SHAPE);
  357. options->get_popup()->set_item_tooltip(options->get_popup()->get_item_count() - 1, TTR("Creates a polygon-based collision shape.\nThis is the most accurate (but slowest) option for collision detection."));
  358. options->get_popup()->add_item(TTR("Create Single Convex Collision Sibling"), MENU_OPTION_CREATE_SINGLE_CONVEX_COLLISION_SHAPE);
  359. options->get_popup()->set_item_tooltip(options->get_popup()->get_item_count() - 1, TTR("Creates a single convex collision shape.\nThis is the fastest (but least accurate) option for collision detection."));
  360. options->get_popup()->add_item(TTR("Create Multiple Convex Collision Siblings"), MENU_OPTION_CREATE_MULTIPLE_CONVEX_COLLISION_SHAPES);
  361. options->get_popup()->set_item_tooltip(options->get_popup()->get_item_count() - 1, TTR("Creates a polygon-based collision shape.\nThis is a performance middle-ground between the two above options."));
  362. options->get_popup()->add_separator();
  363. options->get_popup()->add_item(TTR("Create Navigation Mesh"), MENU_OPTION_CREATE_NAVMESH);
  364. options->get_popup()->add_separator();
  365. options->get_popup()->add_item(TTR("Create Outline Mesh..."), MENU_OPTION_CREATE_OUTLINE_MESH);
  366. options->get_popup()->set_item_tooltip(options->get_popup()->get_item_count() - 1, TTR("Creates a static outline mesh. The outline mesh will have its normals flipped automatically.\nThis can be used instead of the SpatialMaterial Grow property when using that property isn't possible."));
  367. options->get_popup()->add_separator();
  368. options->get_popup()->add_item(TTR("View UV1"), MENU_OPTION_DEBUG_UV1);
  369. options->get_popup()->add_item(TTR("View UV2"), MENU_OPTION_DEBUG_UV2);
  370. options->get_popup()->add_item(TTR("Unwrap UV2 for Lightmap/AO"), MENU_OPTION_CREATE_UV2);
  371. options->get_popup()->connect("id_pressed", this, "_menu_option");
  372. outline_dialog = memnew(ConfirmationDialog);
  373. outline_dialog->set_title(TTR("Create Outline Mesh"));
  374. outline_dialog->get_ok()->set_text(TTR("Create"));
  375. VBoxContainer *outline_dialog_vbc = memnew(VBoxContainer);
  376. outline_dialog->add_child(outline_dialog_vbc);
  377. //outline_dialog->set_child_rect(outline_dialog_vbc);
  378. outline_size = memnew(SpinBox);
  379. outline_size->set_min(0.001);
  380. outline_size->set_max(1024);
  381. outline_size->set_step(0.001);
  382. outline_size->set_value(0.05);
  383. outline_dialog_vbc->add_margin_child(TTR("Outline Size:"), outline_size);
  384. add_child(outline_dialog);
  385. outline_dialog->connect("confirmed", this, "_create_outline_mesh");
  386. err_dialog = memnew(AcceptDialog);
  387. add_child(err_dialog);
  388. debug_uv_dialog = memnew(AcceptDialog);
  389. debug_uv_dialog->set_title(TTR("UV Channel Debug"));
  390. add_child(debug_uv_dialog);
  391. debug_uv = memnew(Control);
  392. debug_uv->set_custom_minimum_size(Size2(600, 600) * EDSCALE);
  393. debug_uv->connect("draw", this, "_debug_uv_draw");
  394. debug_uv_dialog->add_child(debug_uv);
  395. }
  396. void MeshInstanceEditorPlugin::edit(Object *p_object) {
  397. mesh_editor->edit(Object::cast_to<MeshInstance>(p_object));
  398. }
  399. bool MeshInstanceEditorPlugin::handles(Object *p_object) const {
  400. return p_object->is_class("MeshInstance");
  401. }
  402. void MeshInstanceEditorPlugin::make_visible(bool p_visible) {
  403. if (p_visible) {
  404. mesh_editor->options->show();
  405. } else {
  406. mesh_editor->options->hide();
  407. mesh_editor->edit(NULL);
  408. }
  409. }
  410. MeshInstanceEditorPlugin::MeshInstanceEditorPlugin(EditorNode *p_node) {
  411. editor = p_node;
  412. mesh_editor = memnew(MeshInstanceEditor);
  413. editor->get_viewport()->add_child(mesh_editor);
  414. mesh_editor->options->hide();
  415. }
  416. MeshInstanceEditorPlugin::~MeshInstanceEditorPlugin() {
  417. }