mesh_instance_editor_plugin.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  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-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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. Node *owner = node->get_owner();
  115. UndoRedo *ur = EditorNode::get_singleton()->get_undo_redo();
  116. ur->create_action(TTR("Create Trimesh Static Shape"));
  117. ur->add_do_method(node->get_parent(), "add_child", cshape);
  118. ur->add_do_method(node->get_parent(), "move_child", cshape, node->get_index() + 1);
  119. ur->add_do_method(cshape, "set_owner", owner);
  120. ur->add_do_reference(cshape);
  121. ur->add_undo_method(node->get_parent(), "remove_child", cshape);
  122. ur->commit_action();
  123. } break;
  124. case MENU_OPTION_CREATE_SINGLE_CONVEX_COLLISION_SHAPE: {
  125. if (node == get_tree()->get_edited_scene_root()) {
  126. err_dialog->set_text(TTR("Can't create a single convex collision shape for the scene root."));
  127. err_dialog->popup_centered_minsize();
  128. return;
  129. }
  130. Ref<Shape> shape = mesh->create_convex_shape();
  131. if (shape.is_null()) {
  132. err_dialog->set_text(TTR("Couldn't create a single convex collision shape."));
  133. err_dialog->popup_centered_minsize();
  134. return;
  135. }
  136. UndoRedo *ur = EditorNode::get_singleton()->get_undo_redo();
  137. ur->create_action(TTR("Create Single Convex Shape"));
  138. CollisionShape *cshape = memnew(CollisionShape);
  139. cshape->set_shape(shape);
  140. cshape->set_transform(node->get_transform());
  141. Node *owner = node->get_owner();
  142. ur->add_do_method(node->get_parent(), "add_child", cshape);
  143. ur->add_do_method(node->get_parent(), "move_child", cshape, node->get_index() + 1);
  144. ur->add_do_method(cshape, "set_owner", owner);
  145. ur->add_do_reference(cshape);
  146. ur->add_undo_method(node->get_parent(), "remove_child", cshape);
  147. ur->commit_action();
  148. } break;
  149. case MENU_OPTION_CREATE_MULTIPLE_CONVEX_COLLISION_SHAPES: {
  150. if (node == get_tree()->get_edited_scene_root()) {
  151. err_dialog->set_text(TTR("Can't create multiple convex collision shapes for the scene root."));
  152. err_dialog->popup_centered_minsize();
  153. return;
  154. }
  155. Vector<Ref<Shape> > shapes = mesh->convex_decompose();
  156. if (!shapes.size()) {
  157. err_dialog->set_text(TTR("Couldn't create any collision shapes."));
  158. err_dialog->popup_centered_minsize();
  159. return;
  160. }
  161. UndoRedo *ur = EditorNode::get_singleton()->get_undo_redo();
  162. ur->create_action(TTR("Create Multiple Convex Shapes"));
  163. for (int i = 0; i < shapes.size(); i++) {
  164. CollisionShape *cshape = memnew(CollisionShape);
  165. cshape->set_shape(shapes[i]);
  166. cshape->set_transform(node->get_transform());
  167. Node *owner = node->get_owner();
  168. ur->add_do_method(node->get_parent(), "add_child", cshape);
  169. ur->add_do_method(node->get_parent(), "move_child", cshape, node->get_index() + 1);
  170. ur->add_do_method(cshape, "set_owner", owner);
  171. ur->add_do_reference(cshape);
  172. ur->add_undo_method(node->get_parent(), "remove_child", cshape);
  173. }
  174. ur->commit_action();
  175. } break;
  176. case MENU_OPTION_CREATE_NAVMESH: {
  177. Ref<NavigationMesh> nmesh = memnew(NavigationMesh);
  178. if (nmesh.is_null())
  179. return;
  180. nmesh->create_from_mesh(mesh);
  181. NavigationMeshInstance *nmi = memnew(NavigationMeshInstance);
  182. nmi->set_navigation_mesh(nmesh);
  183. Node *owner = node == get_tree()->get_edited_scene_root() ? node : node->get_owner();
  184. UndoRedo *ur = EditorNode::get_singleton()->get_undo_redo();
  185. ur->create_action(TTR("Create Navigation Mesh"));
  186. ur->add_do_method(node, "add_child", nmi);
  187. ur->add_do_method(nmi, "set_owner", owner);
  188. ur->add_do_reference(nmi);
  189. ur->add_undo_method(node, "remove_child", nmi);
  190. ur->commit_action();
  191. } break;
  192. case MENU_OPTION_CREATE_OUTLINE_MESH: {
  193. outline_dialog->popup_centered(Vector2(200, 90));
  194. } break;
  195. case MENU_OPTION_CREATE_UV2: {
  196. Ref<ArrayMesh> mesh2 = node->get_mesh();
  197. if (!mesh2.is_valid()) {
  198. err_dialog->set_text(TTR("Contained Mesh is not of type ArrayMesh."));
  199. err_dialog->popup_centered_minsize();
  200. return;
  201. }
  202. Error err = mesh2->lightmap_unwrap(node->get_global_transform());
  203. if (err != OK) {
  204. err_dialog->set_text(TTR("UV Unwrap failed, mesh may not be manifold?"));
  205. err_dialog->popup_centered_minsize();
  206. return;
  207. }
  208. } break;
  209. case MENU_OPTION_DEBUG_UV1: {
  210. Ref<Mesh> mesh2 = node->get_mesh();
  211. if (!mesh2.is_valid()) {
  212. err_dialog->set_text(TTR("No mesh to debug."));
  213. err_dialog->popup_centered_minsize();
  214. return;
  215. }
  216. _create_uv_lines(0);
  217. } break;
  218. case MENU_OPTION_DEBUG_UV2: {
  219. Ref<Mesh> mesh2 = node->get_mesh();
  220. if (!mesh2.is_valid()) {
  221. err_dialog->set_text(TTR("No mesh to debug."));
  222. err_dialog->popup_centered_minsize();
  223. return;
  224. }
  225. _create_uv_lines(1);
  226. } break;
  227. }
  228. }
  229. struct MeshInstanceEditorEdgeSort {
  230. Vector2 a;
  231. Vector2 b;
  232. bool operator<(const MeshInstanceEditorEdgeSort &p_b) const {
  233. if (a == p_b.a)
  234. return b < p_b.b;
  235. else
  236. return a < p_b.a;
  237. }
  238. MeshInstanceEditorEdgeSort() {}
  239. MeshInstanceEditorEdgeSort(const Vector2 &p_a, const Vector2 &p_b) {
  240. if (p_a < p_b) {
  241. a = p_a;
  242. b = p_b;
  243. } else {
  244. b = p_a;
  245. a = p_b;
  246. }
  247. }
  248. };
  249. void MeshInstanceEditor::_create_uv_lines(int p_layer) {
  250. Ref<Mesh> mesh = node->get_mesh();
  251. ERR_FAIL_COND(!mesh.is_valid());
  252. Set<MeshInstanceEditorEdgeSort> edges;
  253. uv_lines.clear();
  254. for (int i = 0; i < mesh->get_surface_count(); i++) {
  255. if (mesh->surface_get_primitive_type(i) != Mesh::PRIMITIVE_TRIANGLES)
  256. continue;
  257. Array a = mesh->surface_get_arrays(i);
  258. PoolVector<Vector2> uv = a[p_layer == 0 ? Mesh::ARRAY_TEX_UV : Mesh::ARRAY_TEX_UV2];
  259. if (uv.size() == 0) {
  260. err_dialog->set_text(TTR("Model has no UV in this layer"));
  261. err_dialog->popup_centered_minsize();
  262. return;
  263. }
  264. PoolVector<Vector2>::Read r = uv.read();
  265. PoolVector<int> indices = a[Mesh::ARRAY_INDEX];
  266. PoolVector<int>::Read ri;
  267. int ic;
  268. bool use_indices;
  269. if (indices.size()) {
  270. ic = indices.size();
  271. ri = indices.read();
  272. use_indices = true;
  273. } else {
  274. ic = uv.size();
  275. use_indices = false;
  276. }
  277. for (int j = 0; j < ic; j += 3) {
  278. for (int k = 0; k < 3; k++) {
  279. MeshInstanceEditorEdgeSort edge;
  280. if (use_indices) {
  281. edge.a = r[ri[j + k]];
  282. edge.b = r[ri[j + ((k + 1) % 3)]];
  283. } else {
  284. edge.a = r[j + k];
  285. edge.b = r[j + ((k + 1) % 3)];
  286. }
  287. if (edges.has(edge))
  288. continue;
  289. uv_lines.push_back(edge.a);
  290. uv_lines.push_back(edge.b);
  291. edges.insert(edge);
  292. }
  293. }
  294. }
  295. debug_uv_dialog->popup_centered_minsize();
  296. }
  297. void MeshInstanceEditor::_debug_uv_draw() {
  298. if (uv_lines.size() == 0)
  299. return;
  300. debug_uv->set_clip_contents(true);
  301. debug_uv->draw_rect(Rect2(Vector2(), debug_uv->get_size()), Color(0.2, 0.2, 0.0));
  302. debug_uv->draw_set_transform(Vector2(), 0, debug_uv->get_size());
  303. debug_uv->draw_multiline(uv_lines, Color(1.0, 0.8, 0.7));
  304. }
  305. void MeshInstanceEditor::_create_outline_mesh() {
  306. Ref<Mesh> mesh = node->get_mesh();
  307. if (mesh.is_null()) {
  308. err_dialog->set_text(TTR("MeshInstance lacks a Mesh!"));
  309. err_dialog->popup_centered_minsize();
  310. return;
  311. }
  312. if (mesh->get_surface_count() == 0) {
  313. err_dialog->set_text(TTR("Mesh has not surface to create outlines from!"));
  314. err_dialog->popup_centered_minsize();
  315. return;
  316. } else if (mesh->get_surface_count() == 1 && mesh->surface_get_primitive_type(0) != Mesh::PRIMITIVE_TRIANGLES) {
  317. err_dialog->set_text(TTR("Mesh primitive type is not PRIMITIVE_TRIANGLES!"));
  318. err_dialog->popup_centered_minsize();
  319. return;
  320. }
  321. Ref<Mesh> mesho = mesh->create_outline(outline_size->get_value());
  322. if (mesho.is_null()) {
  323. err_dialog->set_text(TTR("Could not create outline!"));
  324. err_dialog->popup_centered_minsize();
  325. return;
  326. }
  327. MeshInstance *mi = memnew(MeshInstance);
  328. mi->set_mesh(mesho);
  329. Node *owner = node->get_owner();
  330. if (get_tree()->get_edited_scene_root() == node) {
  331. owner = node;
  332. }
  333. UndoRedo *ur = EditorNode::get_singleton()->get_undo_redo();
  334. ur->create_action(TTR("Create Outline"));
  335. ur->add_do_method(node, "add_child", mi);
  336. ur->add_do_method(mi, "set_owner", owner);
  337. ur->add_do_reference(mi);
  338. ur->add_undo_method(node, "remove_child", mi);
  339. ur->commit_action();
  340. }
  341. void MeshInstanceEditor::_bind_methods() {
  342. ClassDB::bind_method("_menu_option", &MeshInstanceEditor::_menu_option);
  343. ClassDB::bind_method("_create_outline_mesh", &MeshInstanceEditor::_create_outline_mesh);
  344. ClassDB::bind_method("_debug_uv_draw", &MeshInstanceEditor::_debug_uv_draw);
  345. }
  346. MeshInstanceEditor::MeshInstanceEditor() {
  347. options = memnew(MenuButton);
  348. options->set_switch_on_hover(true);
  349. SpatialEditor::get_singleton()->add_control_to_menu_panel(options);
  350. options->set_text(TTR("Mesh"));
  351. options->set_icon(EditorNode::get_singleton()->get_gui_base()->get_icon("MeshInstance", "EditorIcons"));
  352. options->get_popup()->add_item(TTR("Create Trimesh Static Body"), MENU_OPTION_CREATE_STATIC_TRIMESH_BODY);
  353. 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."));
  354. options->get_popup()->add_separator();
  355. options->get_popup()->add_item(TTR("Create Trimesh Collision Sibling"), MENU_OPTION_CREATE_TRIMESH_COLLISION_SHAPE);
  356. 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."));
  357. options->get_popup()->add_item(TTR("Create Single Convex Collision Sibling"), MENU_OPTION_CREATE_SINGLE_CONVEX_COLLISION_SHAPE);
  358. 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."));
  359. options->get_popup()->add_item(TTR("Create Multiple Convex Collision Siblings"), MENU_OPTION_CREATE_MULTIPLE_CONVEX_COLLISION_SHAPES);
  360. 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."));
  361. options->get_popup()->add_separator();
  362. options->get_popup()->add_item(TTR("Create Navigation Mesh"), MENU_OPTION_CREATE_NAVMESH);
  363. options->get_popup()->add_separator();
  364. options->get_popup()->add_item(TTR("Create Outline Mesh..."), MENU_OPTION_CREATE_OUTLINE_MESH);
  365. 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."));
  366. options->get_popup()->add_separator();
  367. options->get_popup()->add_item(TTR("View UV1"), MENU_OPTION_DEBUG_UV1);
  368. options->get_popup()->add_item(TTR("View UV2"), MENU_OPTION_DEBUG_UV2);
  369. options->get_popup()->add_item(TTR("Unwrap UV2 for Lightmap/AO"), MENU_OPTION_CREATE_UV2);
  370. options->get_popup()->connect("id_pressed", this, "_menu_option");
  371. outline_dialog = memnew(ConfirmationDialog);
  372. outline_dialog->set_title(TTR("Create Outline Mesh"));
  373. outline_dialog->get_ok()->set_text(TTR("Create"));
  374. VBoxContainer *outline_dialog_vbc = memnew(VBoxContainer);
  375. outline_dialog->add_child(outline_dialog_vbc);
  376. //outline_dialog->set_child_rect(outline_dialog_vbc);
  377. outline_size = memnew(SpinBox);
  378. outline_size->set_min(0.001);
  379. outline_size->set_max(1024);
  380. outline_size->set_step(0.001);
  381. outline_size->set_value(0.05);
  382. outline_dialog_vbc->add_margin_child(TTR("Outline Size:"), outline_size);
  383. add_child(outline_dialog);
  384. outline_dialog->connect("confirmed", this, "_create_outline_mesh");
  385. err_dialog = memnew(AcceptDialog);
  386. add_child(err_dialog);
  387. debug_uv_dialog = memnew(AcceptDialog);
  388. debug_uv_dialog->set_title(TTR("UV Channel Debug"));
  389. add_child(debug_uv_dialog);
  390. debug_uv = memnew(Control);
  391. debug_uv->set_custom_minimum_size(Size2(600, 600) * EDSCALE);
  392. debug_uv->connect("draw", this, "_debug_uv_draw");
  393. debug_uv_dialog->add_child(debug_uv);
  394. }
  395. void MeshInstanceEditorPlugin::edit(Object *p_object) {
  396. mesh_editor->edit(Object::cast_to<MeshInstance>(p_object));
  397. }
  398. bool MeshInstanceEditorPlugin::handles(Object *p_object) const {
  399. return p_object->is_class("MeshInstance");
  400. }
  401. void MeshInstanceEditorPlugin::make_visible(bool p_visible) {
  402. if (p_visible) {
  403. mesh_editor->options->show();
  404. } else {
  405. mesh_editor->options->hide();
  406. mesh_editor->edit(NULL);
  407. }
  408. }
  409. MeshInstanceEditorPlugin::MeshInstanceEditorPlugin(EditorNode *p_node) {
  410. editor = p_node;
  411. mesh_editor = memnew(MeshInstanceEditor);
  412. editor->get_viewport()->add_child(mesh_editor);
  413. mesh_editor->options->hide();
  414. }
  415. MeshInstanceEditorPlugin::~MeshInstanceEditorPlugin() {
  416. }