mesh_instance_3d_editor_plugin.cpp 23 KB

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