navigation_obstacle_3d_editor_plugin.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. /**************************************************************************/
  2. /* navigation_obstacle_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 "navigation_obstacle_3d_editor_plugin.h"
  31. #include "canvas_item_editor_plugin.h"
  32. #include "core/input/input.h"
  33. #include "core/io/file_access.h"
  34. #include "core/math/geometry_2d.h"
  35. #include "core/os/keyboard.h"
  36. #include "editor/editor_node.h"
  37. #include "editor/editor_settings.h"
  38. #include "editor/editor_string_names.h"
  39. #include "editor/editor_undo_redo_manager.h"
  40. #include "node_3d_editor_plugin.h"
  41. #include "scene/3d/camera_3d.h"
  42. #include "scene/gui/separator.h"
  43. void NavigationObstacle3DEditor::_notification(int p_what) {
  44. switch (p_what) {
  45. case NOTIFICATION_READY: {
  46. button_create->set_button_icon(get_editor_theme_icon(SNAME("Edit")));
  47. button_edit->set_button_icon(get_editor_theme_icon(SNAME("MovePoint")));
  48. button_edit->set_pressed(true);
  49. get_tree()->connect("node_removed", callable_mp(this, &NavigationObstacle3DEditor::_node_removed));
  50. } break;
  51. }
  52. }
  53. void NavigationObstacle3DEditor::_node_removed(Node *p_node) {
  54. if (p_node == obstacle_node) {
  55. obstacle_node = nullptr;
  56. if (point_lines_meshinstance->get_parent() == p_node) {
  57. p_node->remove_child(point_lines_meshinstance);
  58. }
  59. hide();
  60. }
  61. }
  62. void NavigationObstacle3DEditor::_menu_option(int p_option) {
  63. switch (p_option) {
  64. case MODE_CREATE: {
  65. mode = MODE_CREATE;
  66. button_create->set_pressed(true);
  67. button_edit->set_pressed(false);
  68. } break;
  69. case MODE_EDIT: {
  70. mode = MODE_EDIT;
  71. button_create->set_pressed(false);
  72. button_edit->set_pressed(true);
  73. } break;
  74. }
  75. }
  76. void NavigationObstacle3DEditor::_wip_close() {
  77. ERR_FAIL_NULL_MSG(obstacle_node, "Edited NavigationObstacle3D is not valid.");
  78. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  79. undo_redo->create_action(TTR("Set NavigationObstacle3D Vertices"));
  80. undo_redo->add_undo_method(obstacle_node, "set_vertices", obstacle_node->get_vertices());
  81. PackedVector3Array polygon_3d_vertices;
  82. Vector<int> triangulated_polygon_2d_indices = Geometry2D::triangulate_polygon(wip);
  83. if (!triangulated_polygon_2d_indices.is_empty()) {
  84. polygon_3d_vertices.resize(wip.size());
  85. Vector3 *polygon_3d_vertices_ptr = polygon_3d_vertices.ptrw();
  86. for (int i = 0; i < wip.size(); i++) {
  87. const Vector2 &vert = wip[i];
  88. polygon_3d_vertices_ptr[i] = Vector3(vert.x, 0.0, vert.y);
  89. }
  90. }
  91. undo_redo->add_do_method(obstacle_node, "set_vertices", polygon_3d_vertices);
  92. undo_redo->add_do_method(this, "_polygon_draw");
  93. undo_redo->add_undo_method(this, "_polygon_draw");
  94. wip.clear();
  95. wip_active = false;
  96. mode = MODE_EDIT;
  97. button_edit->set_pressed(true);
  98. button_create->set_pressed(false);
  99. edited_point = -1;
  100. undo_redo->commit_action();
  101. }
  102. EditorPlugin::AfterGUIInput NavigationObstacle3DEditor::forward_3d_gui_input(Camera3D *p_camera, const Ref<InputEvent> &p_event) {
  103. if (!obstacle_node) {
  104. return EditorPlugin::AFTER_GUI_INPUT_PASS;
  105. }
  106. Transform3D gt = obstacle_node->get_global_transform();
  107. Transform3D gi = gt.affine_inverse();
  108. Plane p(Vector3(0.0, 1.0, 0.0), gt.origin);
  109. Ref<InputEventMouseButton> mb = p_event;
  110. if (mb.is_valid()) {
  111. Vector2 gpoint = mb->get_position();
  112. Vector3 ray_from = p_camera->project_ray_origin(gpoint);
  113. Vector3 ray_dir = p_camera->project_ray_normal(gpoint);
  114. Vector3 spoint;
  115. if (!p.intersects_ray(ray_from, ray_dir, &spoint)) {
  116. return EditorPlugin::AFTER_GUI_INPUT_PASS;
  117. }
  118. spoint = gi.xform(spoint);
  119. Vector2 cpoint(spoint.x, spoint.z);
  120. //DO NOT snap here, it's confusing in 3D for adding points.
  121. //Let the snap happen when the point is being moved, instead.
  122. //cpoint = CanvasItemEditor::get_singleton()->snap_point(cpoint);
  123. PackedVector2Array poly = _get_polygon();
  124. //first check if a point is to be added (segment split)
  125. real_t grab_threshold = EDITOR_GET("editors/polygon_editor/point_grab_radius");
  126. switch (mode) {
  127. case MODE_CREATE: {
  128. if (mb->get_button_index() == MouseButton::LEFT && mb->is_pressed()) {
  129. if (!wip_active) {
  130. wip.clear();
  131. wip.push_back(cpoint);
  132. wip_active = true;
  133. edited_point_pos = cpoint;
  134. snap_ignore = false;
  135. _polygon_draw();
  136. edited_point = 1;
  137. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  138. } else {
  139. if (wip.size() > 1 && p_camera->unproject_position(gt.xform(Vector3(wip[0].x, 0.0, wip[0].y))).distance_to(gpoint) < grab_threshold) {
  140. //wip closed
  141. _wip_close();
  142. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  143. } else {
  144. wip.push_back(cpoint);
  145. edited_point = wip.size();
  146. snap_ignore = false;
  147. _polygon_draw();
  148. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  149. }
  150. }
  151. } else if (mb->get_button_index() == MouseButton::RIGHT && mb->is_pressed() && wip_active) {
  152. _wip_close();
  153. }
  154. } break;
  155. case MODE_EDIT: {
  156. if (mb->get_button_index() == MouseButton::LEFT) {
  157. if (mb->is_pressed()) {
  158. if (mb->is_ctrl_pressed()) {
  159. if (poly.size() < 3) {
  160. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  161. undo_redo->create_action(TTR("Edit Vertices"));
  162. undo_redo->add_undo_method(obstacle_node, "set_vertices", obstacle_node->get_vertices());
  163. poly.push_back(cpoint);
  164. undo_redo->add_do_method(this, "_polygon_draw");
  165. undo_redo->add_undo_method(this, "_polygon_draw");
  166. undo_redo->commit_action();
  167. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  168. }
  169. //search edges
  170. int closest_idx = -1;
  171. Vector2 closest_pos;
  172. real_t closest_dist = 1e10;
  173. for (int i = 0; i < poly.size(); i++) {
  174. Vector2 points[2] = {
  175. p_camera->unproject_position(gt.xform(Vector3(poly[i].x, 0.0, poly[i].y))),
  176. p_camera->unproject_position(gt.xform(Vector3(poly[(i + 1) % poly.size()].x, 0.0, poly[(i + 1) % poly.size()].y)))
  177. };
  178. Vector2 cp = Geometry2D::get_closest_point_to_segment(gpoint, points);
  179. if (cp.distance_squared_to(points[0]) < CMP_EPSILON2 || cp.distance_squared_to(points[1]) < CMP_EPSILON2) {
  180. continue; //not valid to reuse point
  181. }
  182. real_t d = cp.distance_to(gpoint);
  183. if (d < closest_dist && d < grab_threshold) {
  184. closest_dist = d;
  185. closest_pos = cp;
  186. closest_idx = i;
  187. }
  188. }
  189. if (closest_idx >= 0) {
  190. pre_move_edit = poly;
  191. poly.insert(closest_idx + 1, cpoint);
  192. edited_point = closest_idx + 1;
  193. edited_point_pos = cpoint;
  194. _set_polygon(poly);
  195. _polygon_draw();
  196. snap_ignore = true;
  197. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  198. }
  199. } else {
  200. //look for points to move
  201. int closest_idx = -1;
  202. Vector2 closest_pos;
  203. real_t closest_dist = 1e10;
  204. for (int i = 0; i < poly.size(); i++) {
  205. Vector2 cp = p_camera->unproject_position(gt.xform(Vector3(poly[i].x, 0.0, poly[i].y)));
  206. real_t d = cp.distance_to(gpoint);
  207. if (d < closest_dist && d < grab_threshold) {
  208. closest_dist = d;
  209. closest_pos = cp;
  210. closest_idx = i;
  211. }
  212. }
  213. if (closest_idx >= 0) {
  214. pre_move_edit = poly;
  215. edited_point = closest_idx;
  216. edited_point_pos = poly[closest_idx];
  217. _polygon_draw();
  218. snap_ignore = false;
  219. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  220. }
  221. }
  222. } else {
  223. snap_ignore = false;
  224. if (edited_point != -1) {
  225. //apply
  226. ERR_FAIL_INDEX_V(edited_point, poly.size(), EditorPlugin::AFTER_GUI_INPUT_PASS);
  227. poly.write[edited_point] = edited_point_pos;
  228. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  229. undo_redo->create_action(TTR("Edit Poly"));
  230. //undo_redo->add_do_method(obj, "set_polygon", poly);
  231. //undo_redo->add_undo_method(obj, "set_polygon", pre_move_edit);
  232. undo_redo->add_do_method(this, "_polygon_draw");
  233. undo_redo->add_undo_method(this, "_polygon_draw");
  234. undo_redo->commit_action();
  235. edited_point = -1;
  236. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  237. }
  238. }
  239. }
  240. if (mb->get_button_index() == MouseButton::RIGHT && mb->is_pressed() && edited_point == -1) {
  241. int closest_idx = -1;
  242. Vector2 closest_pos;
  243. real_t closest_dist = 1e10;
  244. for (int i = 0; i < poly.size(); i++) {
  245. Vector2 cp = p_camera->unproject_position(gt.xform(Vector3(poly[i].x, 0.0, poly[i].y)));
  246. real_t d = cp.distance_to(gpoint);
  247. if (d < closest_dist && d < grab_threshold) {
  248. closest_dist = d;
  249. closest_pos = cp;
  250. closest_idx = i;
  251. }
  252. }
  253. if (closest_idx >= 0) {
  254. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  255. undo_redo->create_action(TTR("Edit Poly (Remove Point)"));
  256. //undo_redo->add_undo_method(obj, "set_polygon", poly);
  257. poly.remove_at(closest_idx);
  258. //undo_redo->add_do_method(obj, "set_polygon", poly);
  259. undo_redo->add_do_method(this, "_polygon_draw");
  260. undo_redo->add_undo_method(this, "_polygon_draw");
  261. undo_redo->commit_action();
  262. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  263. }
  264. }
  265. } break;
  266. }
  267. }
  268. Ref<InputEventMouseMotion> mm = p_event;
  269. if (mm.is_valid()) {
  270. if (edited_point != -1 && (wip_active || mm->get_button_mask().has_flag(MouseButtonMask::LEFT))) {
  271. Vector2 gpoint = mm->get_position();
  272. Vector3 ray_from = p_camera->project_ray_origin(gpoint);
  273. Vector3 ray_dir = p_camera->project_ray_normal(gpoint);
  274. Vector3 spoint;
  275. if (!p.intersects_ray(ray_from, ray_dir, &spoint)) {
  276. return EditorPlugin::AFTER_GUI_INPUT_PASS;
  277. }
  278. spoint = gi.xform(spoint);
  279. Vector2 cpoint(spoint.x, spoint.z);
  280. if (snap_ignore && !Input::get_singleton()->is_key_pressed(Key::CTRL)) {
  281. snap_ignore = false;
  282. }
  283. if (!snap_ignore && Node3DEditor::get_singleton()->is_snap_enabled()) {
  284. cpoint = cpoint.snappedf(Node3DEditor::get_singleton()->get_translate_snap());
  285. }
  286. edited_point_pos = cpoint;
  287. _polygon_draw();
  288. }
  289. }
  290. return EditorPlugin::AFTER_GUI_INPUT_PASS;
  291. }
  292. PackedVector2Array NavigationObstacle3DEditor::_get_polygon() {
  293. ERR_FAIL_NULL_V_MSG(obstacle_node, PackedVector2Array(), "Edited object is not valid.");
  294. return PackedVector2Array(obstacle_node->call("get_polygon"));
  295. }
  296. void NavigationObstacle3DEditor::_set_polygon(const PackedVector2Array &p_poly) {
  297. ERR_FAIL_NULL_MSG(obstacle_node, "Edited object is not valid.");
  298. obstacle_node->call("set_polygon", p_poly);
  299. }
  300. void NavigationObstacle3DEditor::_polygon_draw() {
  301. if (!obstacle_node) {
  302. return;
  303. }
  304. PackedVector2Array poly;
  305. PackedVector3Array polygon_3d_vertices;
  306. if (wip_active) {
  307. poly = wip;
  308. } else {
  309. poly = _get_polygon();
  310. }
  311. polygon_3d_vertices.resize(poly.size());
  312. Vector3 *polygon_3d_vertices_ptr = polygon_3d_vertices.ptrw();
  313. for (int i = 0; i < poly.size(); i++) {
  314. const Vector2 &vert = poly[i];
  315. polygon_3d_vertices_ptr[i] = Vector3(vert.x, 0.0, vert.y);
  316. }
  317. point_handle_mesh->clear_surfaces();
  318. point_lines_mesh->clear_surfaces();
  319. point_lines_meshinstance->set_material_override(line_material);
  320. point_lines_mesh->surface_begin(Mesh::PRIMITIVE_LINES);
  321. Rect2 rect;
  322. for (int i = 0; i < poly.size(); i++) {
  323. Vector2 p, p2;
  324. if (i == edited_point) {
  325. p = edited_point_pos;
  326. } else {
  327. p = poly[i];
  328. }
  329. if ((wip_active && i == poly.size() - 1) || (((i + 1) % poly.size()) == edited_point)) {
  330. p2 = edited_point_pos;
  331. } else {
  332. p2 = poly[(i + 1) % poly.size()];
  333. }
  334. if (i == 0) {
  335. rect.position = p;
  336. } else {
  337. rect.expand_to(p);
  338. }
  339. Vector3 point = Vector3(p.x, 0.0, p.y);
  340. Vector3 next_point = Vector3(p2.x, 0.0, p2.y);
  341. point_lines_mesh->surface_set_color(Color(1, 0.3, 0.1, 0.8));
  342. point_lines_mesh->surface_add_vertex(point);
  343. point_lines_mesh->surface_set_color(Color(1, 0.3, 0.1, 0.8));
  344. point_lines_mesh->surface_add_vertex(next_point);
  345. //Color col=Color(1,0.3,0.1,0.8);
  346. //vpc->draw_line(point,next_point,col,2);
  347. //vpc->draw_texture(handle,point-handle->get_size()*0.5);
  348. }
  349. rect = rect.grow(1);
  350. AABB r;
  351. r.position.x = rect.position.x;
  352. r.position.y = 0.0;
  353. r.position.z = rect.position.y;
  354. r.size.x = rect.size.x;
  355. r.size.y = 0;
  356. r.size.z = rect.size.y;
  357. point_lines_mesh->surface_set_color(Color(0.8, 0.8, 0.8, 0.2));
  358. point_lines_mesh->surface_add_vertex(r.position);
  359. point_lines_mesh->surface_set_color(Color(0.8, 0.8, 0.8, 0.2));
  360. point_lines_mesh->surface_add_vertex(r.position + Vector3(0.3, 0, 0));
  361. point_lines_mesh->surface_set_color(Color(0.8, 0.8, 0.8, 0.2));
  362. point_lines_mesh->surface_add_vertex(r.position);
  363. point_lines_mesh->surface_set_color(Color(0.8, 0.8, 0.8, 0.2));
  364. point_lines_mesh->surface_add_vertex(r.position + Vector3(0.0, 0.3, 0));
  365. point_lines_mesh->surface_set_color(Color(0.8, 0.8, 0.8, 0.2));
  366. point_lines_mesh->surface_add_vertex(r.position + Vector3(r.size.x, 0, 0));
  367. point_lines_mesh->surface_set_color(Color(0.8, 0.8, 0.8, 0.2));
  368. point_lines_mesh->surface_add_vertex(r.position + Vector3(r.size.x, 0, 0) - Vector3(0.3, 0, 0));
  369. point_lines_mesh->surface_set_color(Color(0.8, 0.8, 0.8, 0.2));
  370. point_lines_mesh->surface_add_vertex(r.position + Vector3(r.size.x, 0, 0));
  371. point_lines_mesh->surface_set_color(Color(0.8, 0.8, 0.8, 0.2));
  372. point_lines_mesh->surface_add_vertex(r.position + Vector3(r.size.x, 0, 0) + Vector3(0, 0.3, 0));
  373. point_lines_mesh->surface_set_color(Color(0.8, 0.8, 0.8, 0.2));
  374. point_lines_mesh->surface_add_vertex(r.position + Vector3(0, r.size.y, 0));
  375. point_lines_mesh->surface_set_color(Color(0.8, 0.8, 0.8, 0.2));
  376. point_lines_mesh->surface_add_vertex(r.position + Vector3(0, r.size.y, 0) - Vector3(0, 0.3, 0));
  377. point_lines_mesh->surface_set_color(Color(0.8, 0.8, 0.8, 0.2));
  378. point_lines_mesh->surface_add_vertex(r.position + Vector3(0, r.size.y, 0));
  379. point_lines_mesh->surface_set_color(Color(0.8, 0.8, 0.8, 0.2));
  380. point_lines_mesh->surface_add_vertex(r.position + Vector3(0, r.size.y, 0) + Vector3(0.3, 0, 0));
  381. point_lines_mesh->surface_set_color(Color(0.8, 0.8, 0.8, 0.2));
  382. point_lines_mesh->surface_add_vertex(r.position + r.size);
  383. point_lines_mesh->surface_set_color(Color(0.8, 0.8, 0.8, 0.2));
  384. point_lines_mesh->surface_add_vertex(r.position + r.size - Vector3(0.3, 0, 0));
  385. point_lines_mesh->surface_set_color(Color(0.8, 0.8, 0.8, 0.2));
  386. point_lines_mesh->surface_add_vertex(r.position + r.size);
  387. point_lines_mesh->surface_set_color(Color(0.8, 0.8, 0.8, 0.2));
  388. point_lines_mesh->surface_add_vertex(r.position + r.size - Vector3(0.0, 0.3, 0));
  389. point_lines_mesh->surface_end();
  390. if (poly.size() == 0) {
  391. return;
  392. }
  393. Array point_handle_mesh_array;
  394. point_handle_mesh_array.resize(Mesh::ARRAY_MAX);
  395. Vector<Vector3> point_handle_mesh_vertices;
  396. point_handle_mesh_vertices.resize(poly.size());
  397. Vector3 *point_handle_mesh_vertices_ptr = point_handle_mesh_vertices.ptrw();
  398. for (int i = 0; i < poly.size(); i++) {
  399. Vector2 point_2d;
  400. Vector2 p2;
  401. if (i == edited_point) {
  402. point_2d = edited_point_pos;
  403. } else {
  404. point_2d = poly[i];
  405. }
  406. Vector3 point_handle_3d = Vector3(point_2d.x, 0.0, point_2d.y);
  407. point_handle_mesh_vertices_ptr[i] = point_handle_3d;
  408. }
  409. point_handle_mesh_array[Mesh::ARRAY_VERTEX] = point_handle_mesh_vertices;
  410. point_handle_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_POINTS, point_handle_mesh_array);
  411. point_handle_mesh->surface_set_material(0, handle_material);
  412. }
  413. void NavigationObstacle3DEditor::edit(Node *p_node) {
  414. obstacle_node = Object::cast_to<NavigationObstacle3D>(p_node);
  415. if (obstacle_node) {
  416. //Enable the pencil tool if the polygon is empty
  417. if (_get_polygon().is_empty()) {
  418. _menu_option(MODE_CREATE);
  419. }
  420. wip.clear();
  421. wip_active = false;
  422. edited_point = -1;
  423. if (point_lines_meshinstance->get_parent()) {
  424. point_lines_meshinstance->reparent(p_node, false);
  425. } else {
  426. p_node->add_child(point_lines_meshinstance);
  427. }
  428. _polygon_draw();
  429. } else {
  430. obstacle_node = nullptr;
  431. if (point_lines_meshinstance->get_parent()) {
  432. point_lines_meshinstance->get_parent()->remove_child(point_lines_meshinstance);
  433. }
  434. }
  435. }
  436. void NavigationObstacle3DEditor::_bind_methods() {
  437. ClassDB::bind_method(D_METHOD("_polygon_draw"), &NavigationObstacle3DEditor::_polygon_draw);
  438. }
  439. NavigationObstacle3DEditor::NavigationObstacle3DEditor() {
  440. obstacle_node = nullptr;
  441. button_create = memnew(Button);
  442. button_create->set_theme_type_variation("FlatButton");
  443. add_child(button_create);
  444. button_create->connect(SceneStringName(pressed), callable_mp(this, &NavigationObstacle3DEditor::_menu_option).bind(MODE_CREATE));
  445. button_create->set_toggle_mode(true);
  446. button_edit = memnew(Button);
  447. button_edit->set_theme_type_variation("FlatButton");
  448. add_child(button_edit);
  449. button_edit->connect(SceneStringName(pressed), callable_mp(this, &NavigationObstacle3DEditor::_menu_option).bind(MODE_EDIT));
  450. button_edit->set_toggle_mode(true);
  451. mode = MODE_EDIT;
  452. wip_active = false;
  453. point_lines_meshinstance = memnew(MeshInstance3D);
  454. point_lines_mesh.instantiate();
  455. point_lines_meshinstance->set_mesh(point_lines_mesh);
  456. point_lines_meshinstance->set_transform(Transform3D(Basis(), Vector3(0, 0, 0.00001)));
  457. line_material = Ref<StandardMaterial3D>(memnew(StandardMaterial3D));
  458. line_material->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
  459. line_material->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA);
  460. line_material->set_flag(StandardMaterial3D::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
  461. line_material->set_flag(StandardMaterial3D::FLAG_SRGB_VERTEX_COLOR, true);
  462. line_material->set_flag(StandardMaterial3D::FLAG_DISABLE_FOG, true);
  463. line_material->set_albedo(Color(1, 1, 1));
  464. handle_material = Ref<StandardMaterial3D>(memnew(StandardMaterial3D));
  465. handle_material->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
  466. handle_material->set_flag(StandardMaterial3D::FLAG_USE_POINT_SIZE, true);
  467. handle_material->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA);
  468. handle_material->set_flag(StandardMaterial3D::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
  469. handle_material->set_flag(StandardMaterial3D::FLAG_SRGB_VERTEX_COLOR, true);
  470. handle_material->set_flag(StandardMaterial3D::FLAG_DISABLE_FOG, true);
  471. Ref<Texture2D> handle = EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("Editor3DHandle"), EditorStringName(EditorIcons));
  472. handle_material->set_point_size(handle->get_width());
  473. handle_material->set_texture(StandardMaterial3D::TEXTURE_ALBEDO, handle);
  474. point_handles_meshinstance = memnew(MeshInstance3D);
  475. point_lines_meshinstance->add_child(point_handles_meshinstance);
  476. point_handle_mesh.instantiate();
  477. point_handles_meshinstance->set_mesh(point_handle_mesh);
  478. point_handles_meshinstance->set_transform(Transform3D(Basis(), Vector3(0, 0, 0.00001)));
  479. snap_ignore = false;
  480. }
  481. NavigationObstacle3DEditor::~NavigationObstacle3DEditor() {
  482. memdelete(point_lines_meshinstance);
  483. }
  484. void NavigationObstacle3DEditorPlugin::edit(Object *p_object) {
  485. obstacle_editor->edit(Object::cast_to<Node>(p_object));
  486. }
  487. bool NavigationObstacle3DEditorPlugin::handles(Object *p_object) const {
  488. return Object::cast_to<NavigationObstacle3D>(p_object);
  489. }
  490. void NavigationObstacle3DEditorPlugin::make_visible(bool p_visible) {
  491. if (p_visible) {
  492. obstacle_editor->show();
  493. } else {
  494. obstacle_editor->hide();
  495. obstacle_editor->edit(nullptr);
  496. }
  497. }
  498. NavigationObstacle3DEditorPlugin::NavigationObstacle3DEditorPlugin() {
  499. obstacle_editor = memnew(NavigationObstacle3DEditor);
  500. Node3DEditor::get_singleton()->add_control_to_menu_panel(obstacle_editor);
  501. obstacle_editor->hide();
  502. }
  503. NavigationObstacle3DEditorPlugin::~NavigationObstacle3DEditorPlugin() {
  504. }