mesh_instance_3d_editor_plugin.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. /*************************************************************************/
  2. /* mesh_instance_3d_editor_plugin.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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_3d_editor_plugin.h"
  31. #include "editor/editor_node.h"
  32. #include "editor/editor_scale.h"
  33. #include "node_3d_editor_plugin.h"
  34. #include "scene/3d/collision_shape_3d.h"
  35. #include "scene/3d/navigation_region_3d.h"
  36. #include "scene/3d/physics_body_3d.h"
  37. #include "scene/gui/box_container.h"
  38. void MeshInstance3DEditor::_node_removed(Node *p_node) {
  39. if (p_node == node) {
  40. node = nullptr;
  41. options->hide();
  42. }
  43. }
  44. void MeshInstance3DEditor::edit(MeshInstance3D *p_mesh) {
  45. node = p_mesh;
  46. }
  47. void MeshInstance3DEditor::_menu_option(int p_option) {
  48. Ref<Mesh> mesh = node->get_mesh();
  49. if (mesh.is_null()) {
  50. err_dialog->set_text(TTR("Mesh is empty!"));
  51. err_dialog->popup_centered();
  52. return;
  53. }
  54. switch (p_option) {
  55. case 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.is_empty()) {
  60. Ref<Shape3D> shape = mesh->create_trimesh_shape();
  61. if (shape.is_null()) {
  62. err_dialog->set_text(TTR("Couldn't create a Trimesh collision shape."));
  63. err_dialog->popup_centered();
  64. return;
  65. }
  66. CollisionShape3D *cshape = memnew(CollisionShape3D);
  67. cshape->set_shape(shape);
  68. StaticBody3D *body = memnew(StaticBody3D);
  69. body->add_child(cshape, true);
  70. Node *owner = get_tree()->get_edited_scene_root();
  71. ur->create_action(TTR("Create Static Trimesh Body"));
  72. ur->add_do_method(node, "add_child", body, true);
  73. ur->add_do_method(body, "set_owner", owner);
  74. ur->add_do_method(cshape, "set_owner", owner);
  75. ur->add_do_method(Node3DEditor::get_singleton(), "_request_gizmo", body);
  76. ur->add_do_method(Node3DEditor::get_singleton(), "_request_gizmo", cshape);
  77. ur->add_do_reference(body);
  78. ur->add_undo_method(node, "remove_child", body);
  79. ur->commit_action();
  80. return;
  81. }
  82. ur->create_action(TTR("Create Static Trimesh Body"));
  83. for (Node *E : selection) {
  84. MeshInstance3D *instance = Object::cast_to<MeshInstance3D>(E);
  85. if (!instance) {
  86. continue;
  87. }
  88. Ref<Mesh> m = instance->get_mesh();
  89. if (m.is_null()) {
  90. continue;
  91. }
  92. Ref<Shape3D> shape = m->create_trimesh_shape();
  93. if (shape.is_null()) {
  94. continue;
  95. }
  96. CollisionShape3D *cshape = memnew(CollisionShape3D);
  97. cshape->set_shape(shape);
  98. StaticBody3D *body = memnew(StaticBody3D);
  99. body->add_child(cshape, true);
  100. Node *owner = get_tree()->get_edited_scene_root();
  101. ur->add_do_method(instance, "add_child", body, true);
  102. ur->add_do_method(body, "set_owner", owner);
  103. ur->add_do_method(cshape, "set_owner", owner);
  104. ur->add_do_method(Node3DEditor::get_singleton(), "_request_gizmo", body);
  105. ur->add_do_method(Node3DEditor::get_singleton(), "_request_gizmo", cshape);
  106. ur->add_do_reference(body);
  107. ur->add_undo_method(instance, "remove_child", body);
  108. }
  109. ur->commit_action();
  110. } break;
  111. case MENU_OPTION_CREATE_TRIMESH_COLLISION_SHAPE: {
  112. if (node == get_tree()->get_edited_scene_root()) {
  113. err_dialog->set_text(TTR("This doesn't work on scene root!"));
  114. err_dialog->popup_centered();
  115. return;
  116. }
  117. Ref<Shape3D> shape = mesh->create_trimesh_shape();
  118. if (shape.is_null()) {
  119. return;
  120. }
  121. CollisionShape3D *cshape = memnew(CollisionShape3D);
  122. cshape->set_shape(shape);
  123. cshape->set_transform(node->get_transform());
  124. Node *owner = get_tree()->get_edited_scene_root();
  125. UndoRedo *ur = EditorNode::get_singleton()->get_undo_redo();
  126. ur->create_action(TTR("Create Trimesh Static Shape"));
  127. ur->add_do_method(node->get_parent(), "add_child", cshape, true);
  128. ur->add_do_method(node->get_parent(), "move_child", cshape, node->get_index() + 1);
  129. ur->add_do_method(cshape, "set_owner", owner);
  130. ur->add_do_method(Node3DEditor::get_singleton(), "_request_gizmo", cshape);
  131. ur->add_do_reference(cshape);
  132. ur->add_undo_method(node->get_parent(), "remove_child", cshape);
  133. ur->commit_action();
  134. } break;
  135. case MENU_OPTION_CREATE_SINGLE_CONVEX_COLLISION_SHAPE:
  136. case MENU_OPTION_CREATE_SIMPLIFIED_CONVEX_COLLISION_SHAPE: {
  137. if (node == get_tree()->get_edited_scene_root()) {
  138. err_dialog->set_text(TTR("Can't create a single convex collision shape for the scene root."));
  139. err_dialog->popup_centered();
  140. return;
  141. }
  142. bool simplify = (p_option == MENU_OPTION_CREATE_SIMPLIFIED_CONVEX_COLLISION_SHAPE);
  143. Ref<Shape3D> shape = mesh->create_convex_shape(true, simplify);
  144. if (shape.is_null()) {
  145. err_dialog->set_text(TTR("Couldn't create a single convex collision shape."));
  146. err_dialog->popup_centered();
  147. return;
  148. }
  149. UndoRedo *ur = EditorNode::get_singleton()->get_undo_redo();
  150. if (simplify) {
  151. ur->create_action(TTR("Create Simplified Convex Shape"));
  152. } else {
  153. ur->create_action(TTR("Create Single Convex Shape"));
  154. }
  155. CollisionShape3D *cshape = memnew(CollisionShape3D);
  156. cshape->set_shape(shape);
  157. cshape->set_transform(node->get_transform());
  158. Node *owner = get_tree()->get_edited_scene_root();
  159. ur->add_do_method(node->get_parent(), "add_child", cshape, true);
  160. ur->add_do_method(node->get_parent(), "move_child", cshape, node->get_index() + 1);
  161. ur->add_do_method(cshape, "set_owner", owner);
  162. ur->add_do_method(Node3DEditor::get_singleton(), "_request_gizmo", cshape);
  163. ur->add_do_reference(cshape);
  164. ur->add_undo_method(node->get_parent(), "remove_child", cshape);
  165. ur->commit_action();
  166. } break;
  167. case MENU_OPTION_CREATE_MULTIPLE_CONVEX_COLLISION_SHAPES: {
  168. if (node == get_tree()->get_edited_scene_root()) {
  169. err_dialog->set_text(TTR("Can't create multiple convex collision shapes for the scene root."));
  170. err_dialog->popup_centered();
  171. return;
  172. }
  173. Mesh::ConvexDecompositionSettings settings;
  174. Vector<Ref<Shape3D>> shapes = mesh->convex_decompose(settings);
  175. if (!shapes.size()) {
  176. err_dialog->set_text(TTR("Couldn't create any collision shapes."));
  177. err_dialog->popup_centered();
  178. return;
  179. }
  180. UndoRedo *ur = EditorNode::get_singleton()->get_undo_redo();
  181. ur->create_action(TTR("Create Multiple Convex Shapes"));
  182. for (int i = 0; i < shapes.size(); i++) {
  183. CollisionShape3D *cshape = memnew(CollisionShape3D);
  184. cshape->set_name("CollisionShape3D");
  185. cshape->set_shape(shapes[i]);
  186. cshape->set_transform(node->get_transform());
  187. Node *owner = get_tree()->get_edited_scene_root();
  188. ur->add_do_method(node->get_parent(), "add_child", cshape);
  189. ur->add_do_method(node->get_parent(), "move_child", cshape, node->get_index() + 1);
  190. ur->add_do_method(cshape, "set_owner", owner);
  191. ur->add_do_method(Node3DEditor::get_singleton(), "_request_gizmo", cshape);
  192. ur->add_do_reference(cshape);
  193. ur->add_undo_method(node->get_parent(), "remove_child", cshape);
  194. }
  195. ur->commit_action();
  196. } break;
  197. case MENU_OPTION_CREATE_NAVMESH: {
  198. Ref<NavigationMesh> nmesh = memnew(NavigationMesh);
  199. if (nmesh.is_null()) {
  200. return;
  201. }
  202. nmesh->create_from_mesh(mesh);
  203. NavigationRegion3D *nmi = memnew(NavigationRegion3D);
  204. nmi->set_navigation_mesh(nmesh);
  205. Node *owner = get_tree()->get_edited_scene_root();
  206. UndoRedo *ur = EditorNode::get_singleton()->get_undo_redo();
  207. ur->create_action(TTR("Create Navigation Mesh"));
  208. ur->add_do_method(node, "add_child", nmi, true);
  209. ur->add_do_method(nmi, "set_owner", owner);
  210. ur->add_do_method(Node3DEditor::get_singleton(), "_request_gizmo", nmi);
  211. ur->add_do_reference(nmi);
  212. ur->add_undo_method(node, "remove_child", nmi);
  213. ur->commit_action();
  214. } break;
  215. case MENU_OPTION_CREATE_OUTLINE_MESH: {
  216. outline_dialog->popup_centered(Vector2(200, 90));
  217. } break;
  218. case MENU_OPTION_CREATE_UV2: {
  219. Ref<ArrayMesh> mesh2 = node->get_mesh();
  220. if (!mesh2.is_valid()) {
  221. err_dialog->set_text(TTR("Contained Mesh is not of type ArrayMesh."));
  222. err_dialog->popup_centered();
  223. return;
  224. }
  225. String path = mesh2->get_path();
  226. int srpos = path.find("::");
  227. if (srpos != -1) {
  228. String base = path.substr(0, srpos);
  229. if (ResourceLoader::get_resource_type(base) == "PackedScene") {
  230. if (!get_tree()->get_edited_scene_root() || get_tree()->get_edited_scene_root()->get_scene_file_path() != base) {
  231. err_dialog->set_text(TTR("Mesh cannot unwrap UVs because it does not belong to the edited scene. Make it unique first."));
  232. err_dialog->popup_centered();
  233. return;
  234. }
  235. } else {
  236. if (FileAccess::exists(path + ".import")) {
  237. err_dialog->set_text(TTR("Mesh cannot unwrap UVs because it belongs to another resource which was imported from another file type. Make it unique first."));
  238. err_dialog->popup_centered();
  239. return;
  240. }
  241. }
  242. } else {
  243. if (FileAccess::exists(path + ".import")) {
  244. err_dialog->set_text(TTR("Mesh cannot unwrap UVs because it was imported from another file type. Make it unique first."));
  245. err_dialog->popup_centered();
  246. return;
  247. }
  248. }
  249. Ref<ArrayMesh> unwrapped_mesh = mesh2->duplicate(false);
  250. Error err = unwrapped_mesh->lightmap_unwrap(node->get_global_transform());
  251. if (err != OK) {
  252. err_dialog->set_text(TTR("UV Unwrap failed, mesh may not be manifold?"));
  253. err_dialog->popup_centered();
  254. return;
  255. }
  256. UndoRedo *ur = EditorNode::get_singleton()->get_undo_redo();
  257. ur->create_action(TTR("Unwrap UV2"));
  258. ur->add_do_method(node, "set_mesh", unwrapped_mesh);
  259. ur->add_do_reference(node);
  260. ur->add_do_reference(mesh2.ptr());
  261. ur->add_undo_method(node, "set_mesh", mesh2);
  262. ur->add_undo_reference(unwrapped_mesh.ptr());
  263. ur->commit_action();
  264. } break;
  265. case MENU_OPTION_DEBUG_UV1: {
  266. Ref<Mesh> mesh2 = node->get_mesh();
  267. if (!mesh2.is_valid()) {
  268. err_dialog->set_text(TTR("No mesh to debug."));
  269. err_dialog->popup_centered();
  270. return;
  271. }
  272. _create_uv_lines(0);
  273. } break;
  274. case MENU_OPTION_DEBUG_UV2: {
  275. Ref<Mesh> mesh2 = node->get_mesh();
  276. if (!mesh2.is_valid()) {
  277. err_dialog->set_text(TTR("No mesh to debug."));
  278. err_dialog->popup_centered();
  279. return;
  280. }
  281. _create_uv_lines(1);
  282. } break;
  283. }
  284. }
  285. struct MeshInstance3DEditorEdgeSort {
  286. Vector2 a;
  287. Vector2 b;
  288. static uint32_t hash(const MeshInstance3DEditorEdgeSort &p_edge) {
  289. uint32_t h = hash_djb2_one_32(HashMapHasherDefault::hash(p_edge.a));
  290. return hash_djb2_one_32(HashMapHasherDefault::hash(p_edge.b), h);
  291. }
  292. bool operator==(const MeshInstance3DEditorEdgeSort &p_b) const {
  293. return a == p_b.a && b == p_b.b;
  294. }
  295. MeshInstance3DEditorEdgeSort() {}
  296. MeshInstance3DEditorEdgeSort(const Vector2 &p_a, const Vector2 &p_b) {
  297. if (p_a < p_b) {
  298. a = p_a;
  299. b = p_b;
  300. } else {
  301. b = p_a;
  302. a = p_b;
  303. }
  304. }
  305. };
  306. void MeshInstance3DEditor::_create_uv_lines(int p_layer) {
  307. Ref<Mesh> mesh = node->get_mesh();
  308. ERR_FAIL_COND(!mesh.is_valid());
  309. HashSet<MeshInstance3DEditorEdgeSort, MeshInstance3DEditorEdgeSort> edges;
  310. uv_lines.clear();
  311. for (int i = 0; i < mesh->get_surface_count(); i++) {
  312. if (mesh->surface_get_primitive_type(i) != Mesh::PRIMITIVE_TRIANGLES) {
  313. continue;
  314. }
  315. Array a = mesh->surface_get_arrays(i);
  316. Vector<Vector2> uv = a[p_layer == 0 ? Mesh::ARRAY_TEX_UV : Mesh::ARRAY_TEX_UV2];
  317. if (uv.size() == 0) {
  318. err_dialog->set_text(vformat(TTR("Mesh has no UV in layer %d."), p_layer + 1));
  319. err_dialog->popup_centered();
  320. return;
  321. }
  322. const Vector2 *r = uv.ptr();
  323. Vector<int> indices = a[Mesh::ARRAY_INDEX];
  324. const int *ri = nullptr;
  325. int ic;
  326. if (indices.size()) {
  327. ic = indices.size();
  328. ri = indices.ptr();
  329. } else {
  330. ic = uv.size();
  331. }
  332. for (int j = 0; j < ic; j += 3) {
  333. for (int k = 0; k < 3; k++) {
  334. MeshInstance3DEditorEdgeSort edge;
  335. if (ri) {
  336. edge.a = r[ri[j + k]];
  337. edge.b = r[ri[j + ((k + 1) % 3)]];
  338. } else {
  339. edge.a = r[j + k];
  340. edge.b = r[j + ((k + 1) % 3)];
  341. }
  342. if (edges.has(edge)) {
  343. continue;
  344. }
  345. uv_lines.push_back(edge.a);
  346. uv_lines.push_back(edge.b);
  347. edges.insert(edge);
  348. }
  349. }
  350. }
  351. debug_uv_dialog->popup_centered();
  352. }
  353. void MeshInstance3DEditor::_debug_uv_draw() {
  354. if (uv_lines.size() == 0) {
  355. return;
  356. }
  357. debug_uv->set_clip_contents(true);
  358. debug_uv->draw_rect(Rect2(Vector2(), debug_uv->get_size()), get_theme_color(SNAME("dark_color_3"), SNAME("Editor")));
  359. debug_uv->draw_set_transform(Vector2(), 0, debug_uv->get_size());
  360. // Use a translucent color to allow overlapping triangles to be visible.
  361. debug_uv->draw_multiline(uv_lines, get_theme_color(SNAME("mono_color"), SNAME("Editor")) * Color(1, 1, 1, 0.5), Math::round(EDSCALE));
  362. }
  363. void MeshInstance3DEditor::_create_outline_mesh() {
  364. Ref<Mesh> mesh = node->get_mesh();
  365. if (mesh.is_null()) {
  366. err_dialog->set_text(TTR("MeshInstance3D lacks a Mesh."));
  367. err_dialog->popup_centered();
  368. return;
  369. }
  370. if (mesh->get_surface_count() == 0) {
  371. err_dialog->set_text(TTR("Mesh has no surface to create outlines from."));
  372. err_dialog->popup_centered();
  373. return;
  374. } else if (mesh->get_surface_count() == 1 && mesh->surface_get_primitive_type(0) != Mesh::PRIMITIVE_TRIANGLES) {
  375. err_dialog->set_text(TTR("Mesh primitive type is not PRIMITIVE_TRIANGLES."));
  376. err_dialog->popup_centered();
  377. return;
  378. }
  379. Ref<Mesh> mesho = mesh->create_outline(outline_size->get_value());
  380. if (mesho.is_null()) {
  381. err_dialog->set_text(TTR("Could not create outline."));
  382. err_dialog->popup_centered();
  383. return;
  384. }
  385. MeshInstance3D *mi = memnew(MeshInstance3D);
  386. mi->set_mesh(mesho);
  387. Node *owner = get_tree()->get_edited_scene_root();
  388. UndoRedo *ur = EditorNode::get_singleton()->get_undo_redo();
  389. ur->create_action(TTR("Create Outline"));
  390. ur->add_do_method(node, "add_child", mi, true);
  391. ur->add_do_method(mi, "set_owner", owner);
  392. ur->add_do_method(Node3DEditor::get_singleton(), "_request_gizmo", mi);
  393. ur->add_do_reference(mi);
  394. ur->add_undo_method(node, "remove_child", mi);
  395. ur->commit_action();
  396. }
  397. void MeshInstance3DEditor::_bind_methods() {
  398. }
  399. MeshInstance3DEditor::MeshInstance3DEditor() {
  400. options = memnew(MenuButton);
  401. options->set_switch_on_hover(true);
  402. Node3DEditor::get_singleton()->add_control_to_menu_panel(options);
  403. options->set_text(TTR("Mesh"));
  404. options->set_icon(EditorNode::get_singleton()->get_gui_base()->get_theme_icon(SNAME("MeshInstance3D"), SNAME("EditorIcons")));
  405. options->get_popup()->add_item(TTR("Create Trimesh Static Body"), MENU_OPTION_CREATE_STATIC_TRIMESH_BODY);
  406. options->get_popup()->set_item_tooltip(-1, TTR("Creates a StaticBody3D and assigns a polygon-based collision shape to it automatically.\nThis is the most accurate (but slowest) option for collision detection."));
  407. options->get_popup()->add_separator();
  408. options->get_popup()->add_item(TTR("Create Trimesh Collision Sibling"), MENU_OPTION_CREATE_TRIMESH_COLLISION_SHAPE);
  409. options->get_popup()->set_item_tooltip(-1, TTR("Creates a polygon-based collision shape.\nThis is the most accurate (but slowest) option for collision detection."));
  410. options->get_popup()->add_item(TTR("Create Single Convex Collision Sibling"), MENU_OPTION_CREATE_SINGLE_CONVEX_COLLISION_SHAPE);
  411. options->get_popup()->set_item_tooltip(-1, TTR("Creates a single convex collision shape.\nThis is the fastest (but least accurate) option for collision detection."));
  412. options->get_popup()->add_item(TTR("Create Simplified Convex Collision Sibling"), MENU_OPTION_CREATE_SIMPLIFIED_CONVEX_COLLISION_SHAPE);
  413. options->get_popup()->set_item_tooltip(-1, TTR("Creates a simplified convex collision shape.\nThis is similar to single collision shape, but can result in a simpler geometry in some cases, at the cost of accuracy."));
  414. options->get_popup()->add_item(TTR("Create Multiple Convex Collision Siblings"), MENU_OPTION_CREATE_MULTIPLE_CONVEX_COLLISION_SHAPES);
  415. options->get_popup()->set_item_tooltip(-1, TTR("Creates a polygon-based collision shape.\nThis is a performance middle-ground between a single convex collision and a polygon-based collision."));
  416. options->get_popup()->add_separator();
  417. options->get_popup()->add_item(TTR("Create Navigation Mesh"), MENU_OPTION_CREATE_NAVMESH);
  418. options->get_popup()->add_separator();
  419. options->get_popup()->add_item(TTR("Create Outline Mesh..."), MENU_OPTION_CREATE_OUTLINE_MESH);
  420. 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 StandardMaterial Grow property when using that property isn't possible."));
  421. options->get_popup()->add_separator();
  422. options->get_popup()->add_item(TTR("View UV1"), MENU_OPTION_DEBUG_UV1);
  423. options->get_popup()->add_item(TTR("View UV2"), MENU_OPTION_DEBUG_UV2);
  424. options->get_popup()->add_item(TTR("Unwrap UV2 for Lightmap/AO"), MENU_OPTION_CREATE_UV2);
  425. options->get_popup()->connect("id_pressed", callable_mp(this, &MeshInstance3DEditor::_menu_option));
  426. outline_dialog = memnew(ConfirmationDialog);
  427. outline_dialog->set_title(TTR("Create Outline Mesh"));
  428. outline_dialog->get_ok_button()->set_text(TTR("Create"));
  429. VBoxContainer *outline_dialog_vbc = memnew(VBoxContainer);
  430. outline_dialog->add_child(outline_dialog_vbc);
  431. //outline_dialog->set_child_rect(outline_dialog_vbc);
  432. outline_size = memnew(SpinBox);
  433. outline_size->set_min(0.001);
  434. outline_size->set_max(1024);
  435. outline_size->set_step(0.001);
  436. outline_size->set_value(0.05);
  437. outline_dialog_vbc->add_margin_child(TTR("Outline Size:"), outline_size);
  438. add_child(outline_dialog);
  439. outline_dialog->connect("confirmed", callable_mp(this, &MeshInstance3DEditor::_create_outline_mesh));
  440. err_dialog = memnew(AcceptDialog);
  441. add_child(err_dialog);
  442. debug_uv_dialog = memnew(AcceptDialog);
  443. debug_uv_dialog->set_title(TTR("UV Channel Debug"));
  444. add_child(debug_uv_dialog);
  445. debug_uv = memnew(Control);
  446. debug_uv->set_custom_minimum_size(Size2(600, 600) * EDSCALE);
  447. debug_uv->connect("draw", callable_mp(this, &MeshInstance3DEditor::_debug_uv_draw));
  448. debug_uv_dialog->add_child(debug_uv);
  449. }
  450. void MeshInstance3DEditorPlugin::edit(Object *p_object) {
  451. mesh_editor->edit(Object::cast_to<MeshInstance3D>(p_object));
  452. }
  453. bool MeshInstance3DEditorPlugin::handles(Object *p_object) const {
  454. return p_object->is_class("MeshInstance3D");
  455. }
  456. void MeshInstance3DEditorPlugin::make_visible(bool p_visible) {
  457. if (p_visible) {
  458. mesh_editor->options->show();
  459. } else {
  460. mesh_editor->options->hide();
  461. mesh_editor->edit(nullptr);
  462. }
  463. }
  464. MeshInstance3DEditorPlugin::MeshInstance3DEditorPlugin() {
  465. mesh_editor = memnew(MeshInstance3DEditor);
  466. EditorNode::get_singleton()->get_main_control()->add_child(mesh_editor);
  467. mesh_editor->options->hide();
  468. }
  469. MeshInstance3DEditorPlugin::~MeshInstance3DEditorPlugin() {
  470. }