mesh_instance_editor_plugin.cpp 15 KB

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