mesh_instance_editor_plugin.cpp 15 KB

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