navigation_obstacle_3d_editor_plugin.cpp 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902
  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 "core/math/geometry_2d.h"
  32. #include "editor/editor_node.h"
  33. #include "editor/editor_settings.h"
  34. #include "editor/editor_string_names.h"
  35. #include "editor/editor_undo_redo_manager.h"
  36. #include "editor/plugins/node_3d_editor_plugin.h"
  37. #include "scene/3d/navigation/navigation_obstacle_3d.h"
  38. #include "scene/gui/button.h"
  39. #include "scene/gui/dialogs.h"
  40. #include "servers/navigation_server_3d.h"
  41. bool NavigationObstacle3DGizmoPlugin::has_gizmo(Node3D *p_spatial) {
  42. return Object::cast_to<NavigationObstacle3D>(p_spatial) != nullptr;
  43. }
  44. String NavigationObstacle3DGizmoPlugin::get_gizmo_name() const {
  45. return "NavigationObstacle3D";
  46. }
  47. void NavigationObstacle3DGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {
  48. p_gizmo->clear();
  49. if (!p_gizmo->is_selected() && get_state() == HIDDEN) {
  50. return;
  51. }
  52. NavigationObstacle3D *obstacle = Object::cast_to<NavigationObstacle3D>(p_gizmo->get_node_3d());
  53. if (!obstacle) {
  54. return;
  55. }
  56. const Vector<Vector3> &vertices = obstacle->get_vertices();
  57. if (vertices.is_empty()) {
  58. return;
  59. }
  60. float height = obstacle->get_height();
  61. const Basis safe_basis = Basis(Vector3(0.0, 1.0, 0.0), obstacle->get_global_rotation().y, obstacle->get_global_basis().get_scale().abs().maxf(0.001));
  62. const Basis gbi = obstacle->get_global_basis().inverse();
  63. const Basis safe_global_basis = gbi * safe_basis;
  64. const int vertex_count = vertices.size();
  65. Vector<Vector3> lines_mesh_vertices;
  66. lines_mesh_vertices.resize(vertex_count * 8);
  67. Vector3 *lines_mesh_vertices_ptrw = lines_mesh_vertices.ptrw();
  68. int vertex_index = 0;
  69. for (int i = 0; i < vertex_count; i++) {
  70. Vector3 point = vertices[i];
  71. Vector3 next_point = vertices[(i + 1) % vertex_count];
  72. Vector3 direction = safe_basis.xform(next_point.direction_to(point));
  73. Vector3 arrow_dir = direction.cross(Vector3(0.0, 1.0, 0.0));
  74. Vector3 edge_middle = point + ((next_point - point) * 0.5);
  75. // Ensure vector stays perpendicular even when scaled non-uniformly.
  76. lines_mesh_vertices_ptrw[vertex_index++] = safe_global_basis.xform(edge_middle);
  77. lines_mesh_vertices_ptrw[vertex_index++] = safe_global_basis.xform(edge_middle) + gbi.xform(arrow_dir) * 0.5;
  78. lines_mesh_vertices_ptrw[vertex_index++] = safe_global_basis.xform(point);
  79. lines_mesh_vertices_ptrw[vertex_index++] = safe_global_basis.xform(next_point);
  80. lines_mesh_vertices_ptrw[vertex_index++] = safe_global_basis.xform(Vector3(point.x, height, point.z));
  81. lines_mesh_vertices_ptrw[vertex_index++] = safe_global_basis.xform(Vector3(next_point.x, height, next_point.z));
  82. lines_mesh_vertices_ptrw[vertex_index++] = safe_global_basis.xform(point);
  83. lines_mesh_vertices_ptrw[vertex_index++] = safe_global_basis.xform(Vector3(point.x, height, point.z));
  84. }
  85. NavigationServer3D *ns3d = NavigationServer3D::get_singleton();
  86. if (obstacle->are_vertices_valid()) {
  87. p_gizmo->add_lines(lines_mesh_vertices, ns3d->get_debug_navigation_avoidance_static_obstacle_pushout_edge_material());
  88. } else {
  89. p_gizmo->add_lines(lines_mesh_vertices, ns3d->get_debug_navigation_avoidance_static_obstacle_pushin_edge_material());
  90. }
  91. p_gizmo->add_collision_segments(lines_mesh_vertices);
  92. if (p_gizmo->is_selected()) {
  93. NavigationObstacle3DEditorPlugin::singleton->redraw();
  94. }
  95. }
  96. bool NavigationObstacle3DGizmoPlugin::can_be_hidden() const {
  97. return true;
  98. }
  99. int NavigationObstacle3DGizmoPlugin::get_priority() const {
  100. return -1;
  101. }
  102. int NavigationObstacle3DGizmoPlugin::subgizmos_intersect_ray(const EditorNode3DGizmo *p_gizmo, Camera3D *p_camera, const Vector2 &p_point) const {
  103. if (NavigationObstacle3DEditorPlugin::singleton->get_mode() != 1) { // MODE_EDIT
  104. return -1;
  105. }
  106. NavigationObstacle3D *obstacle_node = Object::cast_to<NavigationObstacle3D>(p_gizmo->get_node_3d());
  107. ERR_FAIL_NULL_V(obstacle_node, -1);
  108. const Vector3 safe_scale = obstacle_node->get_global_basis().get_scale().abs().maxf(0.001);
  109. const Transform3D gt = Transform3D(Basis().scaled(safe_scale).rotated(Vector3(0.0, 1.0, 0.0), obstacle_node->get_global_rotation().y), obstacle_node->get_global_position());
  110. const Vector<Vector3> &vertices = obstacle_node->get_vertices();
  111. for (int idx = 0; idx < vertices.size(); ++idx) {
  112. Vector3 pos = gt.xform(vertices[idx]);
  113. if (p_camera->unproject_position(pos).distance_to(p_point) < 20) {
  114. return idx;
  115. }
  116. }
  117. return -1;
  118. }
  119. Vector<int> NavigationObstacle3DGizmoPlugin::subgizmos_intersect_frustum(const EditorNode3DGizmo *p_gizmo, const Camera3D *p_camera, const Vector<Plane> &p_frustum) const {
  120. Vector<int> contained_points;
  121. if (NavigationObstacle3DEditorPlugin::singleton->get_mode() != 1) { // MODE_EDIT
  122. return contained_points;
  123. }
  124. NavigationObstacle3D *obstacle_node = Object::cast_to<NavigationObstacle3D>(p_gizmo->get_node_3d());
  125. ERR_FAIL_NULL_V(obstacle_node, contained_points);
  126. const Vector3 safe_scale = obstacle_node->get_global_basis().get_scale().abs().maxf(0.001);
  127. const Transform3D gt = Transform3D(Basis().scaled(safe_scale).rotated(Vector3(0.0, 1.0, 0.0), obstacle_node->get_global_rotation().y), obstacle_node->get_global_position());
  128. const Vector<Vector3> &vertices = obstacle_node->get_vertices();
  129. for (int idx = 0; idx < vertices.size(); ++idx) {
  130. Vector3 pos = gt.xform(vertices[idx]);
  131. bool is_contained_in_frustum = true;
  132. for (int i = 0; i < p_frustum.size(); ++i) {
  133. if (p_frustum[i].distance_to(pos) > 0) {
  134. is_contained_in_frustum = false;
  135. break;
  136. }
  137. }
  138. if (is_contained_in_frustum) {
  139. contained_points.push_back(idx);
  140. }
  141. }
  142. return contained_points;
  143. }
  144. Transform3D NavigationObstacle3DGizmoPlugin::get_subgizmo_transform(const EditorNode3DGizmo *p_gizmo, int p_id) const {
  145. NavigationObstacle3D *obstacle_node = Object::cast_to<NavigationObstacle3D>(p_gizmo->get_node_3d());
  146. ERR_FAIL_NULL_V(obstacle_node, Transform3D());
  147. const Vector<Vector3> &vertices = obstacle_node->get_vertices();
  148. ERR_FAIL_INDEX_V(p_id, vertices.size(), Transform3D());
  149. const Basis safe_basis_inverse = Basis(Vector3(0.0, 1.0, 0.0), obstacle_node->get_global_rotation().y, obstacle_node->get_global_basis().get_scale().abs().maxf(0.001)).inverse();
  150. Transform3D subgizmo_transform = Transform3D(Basis(), safe_basis_inverse.xform(vertices[p_id]));
  151. return subgizmo_transform;
  152. }
  153. void NavigationObstacle3DGizmoPlugin::set_subgizmo_transform(const EditorNode3DGizmo *p_gizmo, int p_id, Transform3D p_transform) {
  154. NavigationObstacle3D *obstacle_node = Object::cast_to<NavigationObstacle3D>(p_gizmo->get_node_3d());
  155. ERR_FAIL_NULL(obstacle_node);
  156. const Basis safe_basis = Basis(Vector3(0.0, 1.0, 0.0), obstacle_node->get_global_rotation().y, obstacle_node->get_global_basis().get_scale().abs().maxf(0.001));
  157. Vector3 new_vertex_pos = p_transform.origin;
  158. Vector<Vector3> vertices = obstacle_node->get_vertices();
  159. ERR_FAIL_INDEX(p_id, vertices.size());
  160. Vector3 vertex = safe_basis.xform(new_vertex_pos);
  161. vertex.y = 0.0;
  162. vertices.write[p_id] = vertex;
  163. obstacle_node->set_vertices(vertices);
  164. }
  165. void NavigationObstacle3DGizmoPlugin::commit_subgizmos(const EditorNode3DGizmo *p_gizmo, const Vector<int> &p_ids, const Vector<Transform3D> &p_restore, bool p_cancel) {
  166. NavigationObstacle3D *obstacle_node = Object::cast_to<NavigationObstacle3D>(p_gizmo->get_node_3d());
  167. ERR_FAIL_NULL(obstacle_node);
  168. const Basis safe_basis = Basis(Vector3(0.0, 1.0, 0.0), obstacle_node->get_global_rotation().y, obstacle_node->get_global_basis().get_scale().abs().maxf(0.001));
  169. Vector<Vector3> vertices = obstacle_node->get_vertices();
  170. Vector<Vector3> restore_vertices = vertices;
  171. for (int i = 0; i < p_ids.size(); ++i) {
  172. const int idx = p_ids[i];
  173. Vector3 vertex = safe_basis.xform(p_restore[i].origin);
  174. vertex.y = 0.0;
  175. restore_vertices.write[idx] = vertex;
  176. }
  177. if (p_cancel) {
  178. obstacle_node->set_vertices(restore_vertices);
  179. return;
  180. }
  181. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  182. undo_redo->create_action(TTR("Set Obstacle Vertices"));
  183. undo_redo->add_do_method(obstacle_node, "set_vertices", vertices);
  184. undo_redo->add_undo_method(obstacle_node, "set_vertices", restore_vertices);
  185. undo_redo->commit_action();
  186. }
  187. NavigationObstacle3DGizmoPlugin::NavigationObstacle3DGizmoPlugin() {
  188. current_state = VISIBLE;
  189. }
  190. void NavigationObstacle3DEditorPlugin::_notification(int p_what) {
  191. switch (p_what) {
  192. case NOTIFICATION_ENTER_TREE: {
  193. _update_theme();
  194. } break;
  195. case NOTIFICATION_READY: {
  196. _update_theme();
  197. button_edit->set_pressed(true);
  198. get_tree()->connect("node_removed", callable_mp(this, &NavigationObstacle3DEditorPlugin::_node_removed));
  199. EditorNode::get_singleton()->get_gui_base()->connect(SceneStringName(theme_changed), callable_mp(this, &NavigationObstacle3DEditorPlugin::_update_theme));
  200. } break;
  201. case NOTIFICATION_EXIT_TREE: {
  202. get_tree()->disconnect("node_removed", callable_mp(this, &NavigationObstacle3DEditorPlugin::_node_removed));
  203. EditorNode::get_singleton()->get_gui_base()->disconnect(SceneStringName(theme_changed), callable_mp(this, &NavigationObstacle3DEditorPlugin::_update_theme));
  204. } break;
  205. }
  206. }
  207. void NavigationObstacle3DEditorPlugin::edit(Object *p_object) {
  208. obstacle_node = Object::cast_to<NavigationObstacle3D>(p_object);
  209. RenderingServer *rs = RenderingServer::get_singleton();
  210. if (obstacle_node) {
  211. if (obstacle_node->get_vertices().is_empty()) {
  212. set_mode(MODE_CREATE);
  213. } else {
  214. set_mode(MODE_EDIT);
  215. }
  216. wip_vertices.clear();
  217. wip_active = false;
  218. edited_point = -1;
  219. rs->instance_set_scenario(point_lines_instance_rid, obstacle_node->get_world_3d()->get_scenario());
  220. rs->instance_set_scenario(point_handles_instance_rid, obstacle_node->get_world_3d()->get_scenario());
  221. redraw();
  222. } else {
  223. obstacle_node = nullptr;
  224. rs->mesh_clear(point_lines_mesh_rid);
  225. rs->mesh_clear(point_handle_mesh_rid);
  226. rs->instance_set_scenario(point_lines_instance_rid, RID());
  227. rs->instance_set_scenario(point_handles_instance_rid, RID());
  228. }
  229. }
  230. bool NavigationObstacle3DEditorPlugin::handles(Object *p_object) const {
  231. return Object::cast_to<NavigationObstacle3D>(p_object);
  232. }
  233. void NavigationObstacle3DEditorPlugin::make_visible(bool p_visible) {
  234. if (p_visible) {
  235. obstacle_editor->show();
  236. } else {
  237. obstacle_editor->hide();
  238. edit(nullptr);
  239. }
  240. }
  241. void NavigationObstacle3DEditorPlugin::action_flip_vertices() {
  242. if (!obstacle_node) {
  243. return;
  244. }
  245. Vector<Vector3> flipped_vertices = obstacle_node->get_vertices();
  246. flipped_vertices.reverse();
  247. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  248. undo_redo->create_action(TTR("Edit Obstacle (Flip Winding)"));
  249. undo_redo->add_do_method(obstacle_node, "set_vertices", flipped_vertices);
  250. undo_redo->add_undo_method(obstacle_node, "set_vertices", obstacle_node->get_vertices());
  251. undo_redo->commit_action();
  252. obstacle_node->update_gizmos();
  253. }
  254. void NavigationObstacle3DEditorPlugin::action_clear_vertices() {
  255. if (!obstacle_node) {
  256. return;
  257. }
  258. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  259. undo_redo->create_action(TTR("Edit Obstacle (Clear Vertices)"));
  260. undo_redo->add_do_method(obstacle_node, "set_vertices", Vector<Vector3>());
  261. undo_redo->add_undo_method(obstacle_node, "set_vertices", obstacle_node->get_vertices());
  262. undo_redo->commit_action();
  263. obstacle_node->update_gizmos();
  264. edit(obstacle_node);
  265. }
  266. void NavigationObstacle3DEditorPlugin::_update_theme() {
  267. button_create->set_tooltip_text(TTR("Add Vertex"));
  268. button_edit->set_tooltip_text(TTR("Edit Vertex"));
  269. button_delete->set_tooltip_text(TTR("Delete Vertex"));
  270. button_flip->set_tooltip_text(TTR("Flip Winding"));
  271. button_clear->set_tooltip_text(TTR("Clear Vertices"));
  272. button_create->set_button_icon(button_create->get_editor_theme_icon(SNAME("CurveCreate")));
  273. button_edit->set_button_icon(button_edit->get_editor_theme_icon(SNAME("CurveEdit")));
  274. button_delete->set_button_icon(button_delete->get_editor_theme_icon(SNAME("CurveDelete")));
  275. button_flip->set_button_icon(button_flip->get_editor_theme_icon(SNAME("FlipWinding")));
  276. button_clear->set_button_icon(button_clear->get_editor_theme_icon(SNAME("Clear")));
  277. }
  278. void NavigationObstacle3DEditorPlugin::_node_removed(Node *p_node) {
  279. if (obstacle_node == p_node) {
  280. obstacle_node = nullptr;
  281. RenderingServer *rs = RenderingServer::get_singleton();
  282. rs->mesh_clear(point_lines_mesh_rid);
  283. rs->mesh_clear(point_handle_mesh_rid);
  284. obstacle_editor->hide();
  285. }
  286. }
  287. void NavigationObstacle3DEditorPlugin::set_mode(int p_option) {
  288. if (p_option == NavigationObstacle3DEditorPlugin::ACTION_FLIP) {
  289. button_flip->set_pressed(false);
  290. action_flip_vertices();
  291. return;
  292. }
  293. if (p_option == NavigationObstacle3DEditorPlugin::ACTION_CLEAR) {
  294. button_clear->set_pressed(false);
  295. button_clear_dialog->reset_size();
  296. button_clear_dialog->popup_centered();
  297. return;
  298. }
  299. mode = p_option;
  300. button_create->set_pressed(p_option == NavigationObstacle3DEditorPlugin::MODE_CREATE);
  301. button_edit->set_pressed(p_option == NavigationObstacle3DEditorPlugin::MODE_EDIT);
  302. button_delete->set_pressed(p_option == NavigationObstacle3DEditorPlugin::MODE_DELETE);
  303. button_flip->set_pressed(false);
  304. button_clear->set_pressed(false);
  305. }
  306. void NavigationObstacle3DEditorPlugin::_wip_cancel() {
  307. wip_vertices.clear();
  308. wip_active = false;
  309. edited_point = -1;
  310. redraw();
  311. }
  312. void NavigationObstacle3DEditorPlugin::_wip_close() {
  313. ERR_FAIL_NULL_MSG(obstacle_node, "Edited NavigationObstacle3D is not valid.");
  314. Vector<Vector2> wip_2d_vertices;
  315. wip_2d_vertices.resize(wip_vertices.size());
  316. for (int i = 0; i < wip_vertices.size(); i++) {
  317. const Vector3 &vert = wip_vertices[i];
  318. wip_2d_vertices.write[i] = Vector2(vert.x, vert.z);
  319. }
  320. Vector<int> triangulated_polygon_2d_indices = Geometry2D::triangulate_polygon(wip_2d_vertices);
  321. if (!triangulated_polygon_2d_indices.is_empty()) {
  322. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  323. undo_redo->create_action(TTR("Set Obstacle Vertices"));
  324. undo_redo->add_do_method(obstacle_node, "set_vertices", wip_vertices);
  325. undo_redo->add_undo_method(obstacle_node, "set_vertices", obstacle_node->get_vertices());
  326. undo_redo->commit_action();
  327. wip_vertices.clear();
  328. wip_active = false;
  329. //mode = MODE_EDIT;
  330. NavigationObstacle3DEditorPlugin::singleton->set_mode(NavigationObstacle3DEditorPlugin::MODE_EDIT);
  331. button_edit->set_pressed(true);
  332. button_create->set_pressed(false);
  333. edited_point = -1;
  334. }
  335. }
  336. EditorPlugin::AfterGUIInput NavigationObstacle3DEditorPlugin::forward_3d_gui_input(Camera3D *p_camera, const Ref<InputEvent> &p_event) {
  337. if (!obstacle_node) {
  338. return EditorPlugin::AFTER_GUI_INPUT_PASS;
  339. }
  340. if (!obstacle_node->is_visible_in_tree()) {
  341. return EditorPlugin::AFTER_GUI_INPUT_PASS;
  342. }
  343. Ref<InputEventMouse> mouse_event = p_event;
  344. if (mouse_event.is_null()) {
  345. return EditorPlugin::AFTER_GUI_INPUT_PASS;
  346. }
  347. Ref<InputEventMouseButton> mb = p_event;
  348. if (mb.is_valid()) {
  349. Vector2 mouse_position = mb->get_position();
  350. Vector3 ray_from = p_camera->project_ray_origin(mouse_position);
  351. Vector3 ray_dir = p_camera->project_ray_normal(mouse_position);
  352. const Vector3 safe_scale = obstacle_node->get_global_basis().get_scale().abs().maxf(0.001);
  353. const Transform3D gt = Transform3D(Basis().scaled(safe_scale).rotated(Vector3(0.0, 1.0, 0.0), obstacle_node->get_global_rotation().y), obstacle_node->get_global_position());
  354. Transform3D gi = gt.affine_inverse();
  355. Plane projection_plane(Vector3(0.0, 1.0, 0.0), gt.origin);
  356. Vector3 spoint;
  357. if (!projection_plane.intersects_ray(ray_from, ray_dir, &spoint)) {
  358. return EditorPlugin::AFTER_GUI_INPUT_PASS;
  359. }
  360. spoint = gi.xform(spoint);
  361. Vector3 cpoint = Vector3(spoint.x, 0.0, spoint.z);
  362. Vector<Vector3> obstacle_vertices = obstacle_node->get_vertices();
  363. real_t grab_threshold = EDITOR_GET("editors/polygon_editor/point_grab_radius");
  364. switch (mode) {
  365. case MODE_CREATE: {
  366. if (mb->get_button_index() == MouseButton::LEFT && mb->is_pressed()) {
  367. if (obstacle_vertices.size() >= 3) {
  368. int closest_idx = -1;
  369. Vector2 closest_edge_point;
  370. real_t closest_dist = 1e10;
  371. for (int i = 0; i < obstacle_vertices.size(); i++) {
  372. const Vector2 a = p_camera->unproject_position(gt.xform(obstacle_vertices[i]));
  373. const Vector2 b = p_camera->unproject_position(gt.xform(obstacle_vertices[(i + 1) % obstacle_vertices.size()]));
  374. Vector2 cp = Geometry2D::get_closest_point_to_segment(mouse_position, a, b);
  375. if (cp.distance_squared_to(a) < grab_threshold || cp.distance_squared_to(b) < grab_threshold) {
  376. continue; // Skip edge as clicked point is too close to existing vertex.
  377. }
  378. real_t d = cp.distance_to(mouse_position);
  379. if (d < closest_dist && d < grab_threshold) {
  380. closest_dist = d;
  381. closest_edge_point = cp;
  382. closest_idx = i;
  383. }
  384. }
  385. if (closest_idx >= 0) {
  386. edited_point = -1;
  387. Vector3 _ray_from = p_camera->project_ray_origin(closest_edge_point);
  388. Vector3 _ray_dir = p_camera->project_ray_normal(closest_edge_point);
  389. Vector3 edge_intersection_point;
  390. if (projection_plane.intersects_ray(_ray_from, _ray_dir, &edge_intersection_point)) {
  391. edge_intersection_point = gi.xform(edge_intersection_point);
  392. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  393. undo_redo->create_action(TTR("Edit Obstacle (Add Vertex)"));
  394. undo_redo->add_undo_method(obstacle_node, "set_vertices", obstacle_vertices);
  395. obstacle_vertices.insert(closest_idx + 1, edge_intersection_point);
  396. undo_redo->add_do_method(obstacle_node, "set_vertices", obstacle_vertices);
  397. undo_redo->commit_action();
  398. redraw();
  399. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  400. }
  401. }
  402. }
  403. if (!wip_active) {
  404. wip_vertices.clear();
  405. wip_vertices.push_back(cpoint);
  406. wip_active = true;
  407. edited_point_pos = cpoint;
  408. snap_ignore = false;
  409. redraw();
  410. edited_point = 1;
  411. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  412. } else {
  413. if (wip_vertices.size() > 1 && p_camera->unproject_position(gt.xform(wip_vertices[0])).distance_to(mouse_position) < grab_threshold) {
  414. _wip_close();
  415. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  416. } else {
  417. wip_vertices.push_back(cpoint);
  418. edited_point = wip_vertices.size();
  419. snap_ignore = false;
  420. redraw();
  421. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  422. }
  423. }
  424. } else if (mb->get_button_index() == MouseButton::RIGHT && mb->is_pressed() && wip_active) {
  425. _wip_close();
  426. }
  427. } break;
  428. case MODE_EDIT: {
  429. if (mb->get_button_index() == MouseButton::LEFT) {
  430. if (mb->is_pressed()) {
  431. if (mb->is_ctrl_pressed()) {
  432. if (obstacle_vertices.size() < 3) {
  433. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  434. undo_redo->create_action(TTR("Edit Obstacle (Add Vertex)"));
  435. undo_redo->add_undo_method(obstacle_node, "set_vertices", obstacle_node->get_vertices());
  436. obstacle_vertices.push_back(cpoint);
  437. undo_redo->commit_action();
  438. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  439. }
  440. //search edges
  441. int closest_idx = -1;
  442. Vector2 closest_pos;
  443. real_t closest_dist = 1e10;
  444. for (int i = 0; i < obstacle_vertices.size(); i++) {
  445. const Vector2 a = p_camera->unproject_position(gt.xform(obstacle_vertices[i]));
  446. const Vector2 b = p_camera->unproject_position(gt.xform(obstacle_vertices[(i + 1) % obstacle_vertices.size()]));
  447. Vector2 cp = Geometry2D::get_closest_point_to_segment(mouse_position, a, b);
  448. if (cp.distance_squared_to(a) < CMP_EPSILON2 || cp.distance_squared_to(b) < CMP_EPSILON2) {
  449. continue; //not valid to reuse point
  450. }
  451. real_t d = cp.distance_to(mouse_position);
  452. if (d < closest_dist && d < grab_threshold) {
  453. closest_dist = d;
  454. closest_pos = cp;
  455. closest_idx = i;
  456. }
  457. }
  458. if (closest_idx >= 0) {
  459. pre_move_edit = obstacle_vertices;
  460. obstacle_vertices.insert(closest_idx + 1, cpoint);
  461. edited_point = closest_idx + 1;
  462. edited_point_pos = cpoint;
  463. obstacle_node->set_vertices(obstacle_vertices);
  464. redraw();
  465. snap_ignore = true;
  466. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  467. }
  468. } else {
  469. int closest_idx = -1;
  470. Vector2 closest_pos;
  471. real_t closest_dist = 1e10;
  472. for (int i = 0; i < obstacle_vertices.size(); i++) {
  473. Vector2 cp = p_camera->unproject_position(gt.xform(obstacle_vertices[i]));
  474. real_t d = cp.distance_to(mouse_position);
  475. if (d < closest_dist && d < grab_threshold) {
  476. closest_dist = d;
  477. closest_pos = cp;
  478. closest_idx = i;
  479. }
  480. }
  481. if (closest_idx >= 0) {
  482. pre_move_edit = obstacle_vertices;
  483. edited_point = closest_idx;
  484. edited_point_pos = obstacle_vertices[closest_idx];
  485. redraw();
  486. snap_ignore = false;
  487. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  488. }
  489. }
  490. } else {
  491. snap_ignore = false;
  492. if (edited_point != -1) {
  493. ERR_FAIL_INDEX_V(edited_point, obstacle_vertices.size(), EditorPlugin::AFTER_GUI_INPUT_PASS);
  494. obstacle_vertices.write[edited_point] = edited_point_pos;
  495. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  496. undo_redo->create_action(TTR("Edit Obstacle (Move Vertex)"));
  497. undo_redo->add_undo_method(obstacle_node, "set_vertices", obstacle_node->get_vertices());
  498. undo_redo->add_do_method(obstacle_node, "set_vertices", obstacle_vertices);
  499. undo_redo->commit_action();
  500. edited_point = -1;
  501. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  502. }
  503. }
  504. }
  505. } break;
  506. case MODE_DELETE: {
  507. if (mb->get_button_index() == MouseButton::LEFT && mb->is_pressed()) {
  508. int closest_idx = -1;
  509. real_t closest_dist = 1e10;
  510. for (int i = 0; i < obstacle_vertices.size(); i++) {
  511. Vector2 point = p_camera->unproject_position(gt.xform(obstacle_vertices[i]));
  512. real_t d = point.distance_to(mouse_position);
  513. if (d < closest_dist && d < grab_threshold) {
  514. closest_dist = d;
  515. closest_idx = i;
  516. }
  517. }
  518. if (closest_idx >= 0) {
  519. edited_point = -1;
  520. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  521. undo_redo->create_action(TTR("Edit Obstacle (Remove Vertex)"));
  522. undo_redo->add_undo_method(obstacle_node, "set_vertices", obstacle_vertices);
  523. obstacle_vertices.remove_at(closest_idx);
  524. undo_redo->add_do_method(obstacle_node, "set_vertices", obstacle_vertices);
  525. undo_redo->commit_action();
  526. redraw();
  527. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  528. }
  529. }
  530. } break;
  531. }
  532. }
  533. Ref<InputEventMouseMotion> mm = p_event;
  534. if (mm.is_valid()) {
  535. if (edited_point != -1 && (wip_active || mm->get_button_mask().has_flag(MouseButtonMask::LEFT))) {
  536. Vector2 mouse_position = mm->get_position();
  537. Vector3 ray_from = p_camera->project_ray_origin(mouse_position);
  538. Vector3 ray_dir = p_camera->project_ray_normal(mouse_position);
  539. const Vector3 safe_scale = obstacle_node->get_global_basis().get_scale().abs().maxf(0.001);
  540. const Transform3D gt = Transform3D(Basis().scaled(safe_scale).rotated(Vector3(0.0, 1.0, 0.0), obstacle_node->get_global_rotation().y), obstacle_node->get_global_position());
  541. Transform3D gi = gt.affine_inverse();
  542. Plane projection_plane(Vector3(0.0, 1.0, 0.0), gt.origin);
  543. Vector3 intersection_point;
  544. if (!projection_plane.intersects_ray(ray_from, ray_dir, &intersection_point)) {
  545. return EditorPlugin::AFTER_GUI_INPUT_PASS;
  546. }
  547. intersection_point = gi.xform(intersection_point);
  548. Vector2 cpoint(intersection_point.x, intersection_point.z);
  549. if (snap_ignore && !Input::get_singleton()->is_key_pressed(Key::CTRL)) {
  550. snap_ignore = false;
  551. }
  552. if (!snap_ignore && Node3DEditor::get_singleton()->is_snap_enabled()) {
  553. cpoint = cpoint.snappedf(Node3DEditor::get_singleton()->get_translate_snap());
  554. }
  555. edited_point_pos = Vector3(cpoint.x, 0.0, cpoint.y);
  556. redraw();
  557. }
  558. }
  559. Ref<InputEventKey> k = p_event;
  560. if (k.is_valid() && k->is_pressed()) {
  561. if (wip_active && k->get_keycode() == Key::ENTER) {
  562. _wip_close();
  563. } else if (wip_active && k->get_keycode() == Key::ESCAPE) {
  564. _wip_cancel();
  565. }
  566. }
  567. return EditorPlugin::AFTER_GUI_INPUT_PASS;
  568. }
  569. void NavigationObstacle3DEditorPlugin::redraw() {
  570. if (!obstacle_node) {
  571. return;
  572. }
  573. RenderingServer *rs = RenderingServer::get_singleton();
  574. rs->mesh_clear(point_lines_mesh_rid);
  575. rs->mesh_clear(point_handle_mesh_rid);
  576. if (!obstacle_node->is_visible_in_tree()) {
  577. return;
  578. }
  579. Vector<Vector3> edited_vertices;
  580. if (wip_active) {
  581. edited_vertices = wip_vertices;
  582. } else {
  583. edited_vertices = obstacle_node->get_vertices();
  584. }
  585. if (edited_vertices.is_empty()) {
  586. return;
  587. }
  588. Array point_lines_mesh_array;
  589. point_lines_mesh_array.resize(Mesh::ARRAY_MAX);
  590. Vector<Vector3> point_lines_mesh_vertices;
  591. point_lines_mesh_vertices.resize(edited_vertices.size() * 2);
  592. Vector3 *point_lines_mesh_vertices_ptr = point_lines_mesh_vertices.ptrw();
  593. int vertex_index = 0;
  594. for (int i = 0; i < edited_vertices.size(); i++) {
  595. Vector3 point, next_point;
  596. if (i == edited_point) {
  597. point = edited_point_pos;
  598. } else {
  599. point = edited_vertices[i];
  600. }
  601. if ((wip_active && i == edited_vertices.size() - 1) || (((i + 1) % edited_vertices.size()) == edited_point)) {
  602. next_point = edited_point_pos;
  603. } else {
  604. next_point = edited_vertices[(i + 1) % edited_vertices.size()];
  605. }
  606. point_lines_mesh_vertices_ptr[vertex_index++] = point;
  607. point_lines_mesh_vertices_ptr[vertex_index++] = next_point;
  608. }
  609. point_lines_mesh_array[Mesh::ARRAY_VERTEX] = point_lines_mesh_vertices;
  610. rs->mesh_add_surface_from_arrays(point_lines_mesh_rid, RS::PRIMITIVE_LINES, point_lines_mesh_array);
  611. rs->instance_set_surface_override_material(point_lines_instance_rid, 0, line_material->get_rid());
  612. const Vector3 safe_scale = obstacle_node->get_global_basis().get_scale().abs().maxf(0.001);
  613. const Transform3D gt = Transform3D(Basis().scaled(safe_scale).rotated(Vector3(0.0, 1.0, 0.0), obstacle_node->get_global_rotation().y), obstacle_node->get_global_position());
  614. rs->instance_set_transform(point_lines_instance_rid, gt);
  615. Array point_handle_mesh_array;
  616. point_handle_mesh_array.resize(Mesh::ARRAY_MAX);
  617. Vector<Vector3> point_handle_mesh_vertices;
  618. point_handle_mesh_vertices.resize(edited_vertices.size());
  619. Vector3 *point_handle_mesh_vertices_ptr = point_handle_mesh_vertices.ptrw();
  620. for (int i = 0; i < edited_vertices.size(); i++) {
  621. Vector3 point_handle_3d;
  622. if (i == edited_point) {
  623. point_handle_3d = edited_point_pos;
  624. } else {
  625. point_handle_3d = edited_vertices[i];
  626. }
  627. point_handle_mesh_vertices_ptr[i] = point_handle_3d;
  628. }
  629. point_handle_mesh_array[Mesh::ARRAY_VERTEX] = point_handle_mesh_vertices;
  630. rs->mesh_add_surface_from_arrays(point_handle_mesh_rid, RS::PRIMITIVE_POINTS, point_handle_mesh_array);
  631. rs->instance_set_surface_override_material(point_handles_instance_rid, 0, handle_material->get_rid());
  632. rs->instance_set_transform(point_handles_instance_rid, gt);
  633. }
  634. NavigationObstacle3DEditorPlugin *NavigationObstacle3DEditorPlugin::singleton = nullptr;
  635. NavigationObstacle3DEditorPlugin::NavigationObstacle3DEditorPlugin() {
  636. singleton = this;
  637. line_material = Ref<StandardMaterial3D>(memnew(StandardMaterial3D));
  638. line_material->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
  639. line_material->set_flag(StandardMaterial3D::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
  640. line_material->set_flag(StandardMaterial3D::FLAG_SRGB_VERTEX_COLOR, true);
  641. line_material->set_flag(StandardMaterial3D::FLAG_DISABLE_FOG, true);
  642. line_material->set_albedo(Color(1, 0.3, 0.1, 0.8));
  643. line_material->set_flag(StandardMaterial3D::FLAG_DISABLE_DEPTH_TEST, true);
  644. handle_material = Ref<StandardMaterial3D>(memnew(StandardMaterial3D));
  645. handle_material->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
  646. handle_material->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA);
  647. handle_material->set_flag(StandardMaterial3D::FLAG_USE_POINT_SIZE, true);
  648. handle_material->set_flag(StandardMaterial3D::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
  649. handle_material->set_flag(StandardMaterial3D::FLAG_SRGB_VERTEX_COLOR, true);
  650. handle_material->set_flag(StandardMaterial3D::FLAG_DISABLE_FOG, true);
  651. Ref<Texture2D> handle = EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("Editor3DHandle"), EditorStringName(EditorIcons));
  652. handle_material->set_point_size(handle->get_width());
  653. handle_material->set_texture(StandardMaterial3D::TEXTURE_ALBEDO, handle);
  654. handle_material->set_flag(StandardMaterial3D::FLAG_DISABLE_DEPTH_TEST, true);
  655. RenderingServer *rs = RenderingServer::get_singleton();
  656. point_lines_mesh_rid = rs->mesh_create();
  657. point_handle_mesh_rid = rs->mesh_create();
  658. point_lines_instance_rid = rs->instance_create();
  659. point_handles_instance_rid = rs->instance_create();
  660. rs->instance_set_base(point_lines_instance_rid, point_lines_mesh_rid);
  661. rs->instance_set_base(point_handles_instance_rid, point_handle_mesh_rid);
  662. obstacle_editor = memnew(HBoxContainer);
  663. obstacle_editor->hide();
  664. Ref<ButtonGroup> bg;
  665. bg.instantiate();
  666. button_create = memnew(Button);
  667. button_create->set_theme_type_variation(SceneStringName(FlatButton));
  668. obstacle_editor->add_child(button_create);
  669. button_create->set_tooltip_text(TTR("Add Vertex"));
  670. button_create->set_accessibility_name(TTRC("Add Vertex"));
  671. button_create->connect(SceneStringName(pressed), callable_mp(this, &NavigationObstacle3DEditorPlugin::set_mode).bind(NavigationObstacle3DEditorPlugin::MODE_CREATE));
  672. button_create->set_toggle_mode(true);
  673. button_create->set_button_group(bg);
  674. button_edit = memnew(Button);
  675. button_edit->set_theme_type_variation(SceneStringName(FlatButton));
  676. button_edit->set_accessibility_name(TTRC("Edit"));
  677. obstacle_editor->add_child(button_edit);
  678. button_edit->connect(SceneStringName(pressed), callable_mp(this, &NavigationObstacle3DEditorPlugin::set_mode).bind(NavigationObstacle3DEditorPlugin::MODE_EDIT));
  679. button_edit->set_toggle_mode(true);
  680. button_edit->set_button_group(bg);
  681. button_delete = memnew(Button);
  682. button_delete->set_theme_type_variation(SceneStringName(FlatButton));
  683. button_delete->set_accessibility_name(TTRC("Delete"));
  684. obstacle_editor->add_child(button_delete);
  685. button_delete->connect(SceneStringName(pressed), callable_mp(this, &NavigationObstacle3DEditorPlugin::set_mode).bind(NavigationObstacle3DEditorPlugin::MODE_DELETE));
  686. button_delete->set_toggle_mode(true);
  687. button_delete->set_button_group(bg);
  688. button_flip = memnew(Button);
  689. button_flip->set_theme_type_variation(SceneStringName(FlatButton));
  690. button_flip->set_accessibility_name(TTRC("Flip"));
  691. obstacle_editor->add_child(button_flip);
  692. button_flip->connect(SceneStringName(pressed), callable_mp(this, &NavigationObstacle3DEditorPlugin::set_mode).bind(NavigationObstacle3DEditorPlugin::ACTION_FLIP));
  693. button_flip->set_toggle_mode(true);
  694. button_clear = memnew(Button);
  695. button_clear->set_theme_type_variation(SceneStringName(FlatButton));
  696. button_clear->set_accessibility_name(TTRC("Clear"));
  697. obstacle_editor->add_child(button_clear);
  698. button_clear->connect(SceneStringName(pressed), callable_mp(this, &NavigationObstacle3DEditorPlugin::set_mode).bind(NavigationObstacle3DEditorPlugin::ACTION_CLEAR));
  699. button_clear->set_toggle_mode(true);
  700. button_clear_dialog = memnew(ConfirmationDialog);
  701. button_clear_dialog->set_title(TTR("Please Confirm..."));
  702. button_clear_dialog->set_text(TTR("Remove all vertices?"));
  703. button_clear_dialog->connect(SceneStringName(confirmed), callable_mp(NavigationObstacle3DEditorPlugin::singleton, &NavigationObstacle3DEditorPlugin::action_clear_vertices));
  704. obstacle_editor->add_child(button_clear_dialog);
  705. Node3DEditor::get_singleton()->add_control_to_menu_panel(obstacle_editor);
  706. Ref<NavigationObstacle3DGizmoPlugin> gizmo_plugin = memnew(NavigationObstacle3DGizmoPlugin());
  707. obstacle_3d_gizmo_plugin = gizmo_plugin;
  708. Node3DEditor::get_singleton()->add_gizmo_plugin(gizmo_plugin);
  709. }
  710. NavigationObstacle3DEditorPlugin::~NavigationObstacle3DEditorPlugin() {
  711. RenderingServer *rs = RenderingServer::get_singleton();
  712. ERR_FAIL_NULL(rs);
  713. if (point_lines_instance_rid.is_valid()) {
  714. rs->free(point_lines_instance_rid);
  715. point_lines_instance_rid = RID();
  716. }
  717. if (point_lines_mesh_rid.is_valid()) {
  718. rs->free(point_lines_mesh_rid);
  719. point_lines_mesh_rid = RID();
  720. }
  721. if (point_handles_instance_rid.is_valid()) {
  722. rs->free(point_handles_instance_rid);
  723. point_handles_instance_rid = RID();
  724. }
  725. if (point_handle_mesh_rid.is_valid()) {
  726. rs->free(point_handle_mesh_rid);
  727. point_handle_mesh_rid = RID();
  728. }
  729. }