navigation_obstacle_3d_editor_plugin.cpp 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901
  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_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. Vector2 points[2] = {
  373. p_camera->unproject_position(gt.xform(obstacle_vertices[i])),
  374. p_camera->unproject_position(gt.xform(obstacle_vertices[(i + 1) % obstacle_vertices.size()]))
  375. };
  376. Vector2 cp = Geometry2D::get_closest_point_to_segment(mouse_position, points);
  377. if (cp.distance_squared_to(points[0]) < grab_threshold || cp.distance_squared_to(points[1]) < grab_threshold) {
  378. continue; // Skip edge as clicked point is too close to existing vertex.
  379. }
  380. real_t d = cp.distance_to(mouse_position);
  381. if (d < closest_dist && d < grab_threshold) {
  382. closest_dist = d;
  383. closest_edge_point = cp;
  384. closest_idx = i;
  385. }
  386. }
  387. if (closest_idx >= 0) {
  388. edited_point = -1;
  389. Vector3 _ray_from = p_camera->project_ray_origin(closest_edge_point);
  390. Vector3 _ray_dir = p_camera->project_ray_normal(closest_edge_point);
  391. Vector3 edge_intersection_point;
  392. if (projection_plane.intersects_ray(_ray_from, _ray_dir, &edge_intersection_point)) {
  393. edge_intersection_point = gi.xform(edge_intersection_point);
  394. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  395. undo_redo->create_action(TTR("Edit Obstacle (Add Vertex)"));
  396. undo_redo->add_undo_method(obstacle_node, "set_vertices", obstacle_vertices);
  397. obstacle_vertices.insert(closest_idx + 1, edge_intersection_point);
  398. undo_redo->add_do_method(obstacle_node, "set_vertices", obstacle_vertices);
  399. undo_redo->commit_action();
  400. redraw();
  401. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  402. }
  403. }
  404. }
  405. if (!wip_active) {
  406. wip_vertices.clear();
  407. wip_vertices.push_back(cpoint);
  408. wip_active = true;
  409. edited_point_pos = cpoint;
  410. snap_ignore = false;
  411. redraw();
  412. edited_point = 1;
  413. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  414. } else {
  415. if (wip_vertices.size() > 1 && p_camera->unproject_position(gt.xform(wip_vertices[0])).distance_to(mouse_position) < grab_threshold) {
  416. _wip_close();
  417. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  418. } else {
  419. wip_vertices.push_back(cpoint);
  420. edited_point = wip_vertices.size();
  421. snap_ignore = false;
  422. redraw();
  423. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  424. }
  425. }
  426. } else if (mb->get_button_index() == MouseButton::RIGHT && mb->is_pressed() && wip_active) {
  427. _wip_close();
  428. }
  429. } break;
  430. case MODE_EDIT: {
  431. if (mb->get_button_index() == MouseButton::LEFT) {
  432. if (mb->is_pressed()) {
  433. if (mb->is_ctrl_pressed()) {
  434. if (obstacle_vertices.size() < 3) {
  435. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  436. undo_redo->create_action(TTR("Edit Obstacle (Add Vertex)"));
  437. undo_redo->add_undo_method(obstacle_node, "set_vertices", obstacle_node->get_vertices());
  438. obstacle_vertices.push_back(cpoint);
  439. undo_redo->commit_action();
  440. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  441. }
  442. //search edges
  443. int closest_idx = -1;
  444. Vector2 closest_pos;
  445. real_t closest_dist = 1e10;
  446. for (int i = 0; i < obstacle_vertices.size(); i++) {
  447. Vector2 points[2] = {
  448. p_camera->unproject_position(gt.xform(obstacle_vertices[i])),
  449. p_camera->unproject_position(gt.xform(obstacle_vertices[(i + 1) % obstacle_vertices.size()]))
  450. };
  451. Vector2 cp = Geometry2D::get_closest_point_to_segment(mouse_position, points);
  452. if (cp.distance_squared_to(points[0]) < CMP_EPSILON2 || cp.distance_squared_to(points[1]) < CMP_EPSILON2) {
  453. continue; //not valid to reuse point
  454. }
  455. real_t d = cp.distance_to(mouse_position);
  456. if (d < closest_dist && d < grab_threshold) {
  457. closest_dist = d;
  458. closest_pos = cp;
  459. closest_idx = i;
  460. }
  461. }
  462. if (closest_idx >= 0) {
  463. pre_move_edit = obstacle_vertices;
  464. obstacle_vertices.insert(closest_idx + 1, cpoint);
  465. edited_point = closest_idx + 1;
  466. edited_point_pos = cpoint;
  467. obstacle_node->set_vertices(obstacle_vertices);
  468. redraw();
  469. snap_ignore = true;
  470. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  471. }
  472. } else {
  473. int closest_idx = -1;
  474. Vector2 closest_pos;
  475. real_t closest_dist = 1e10;
  476. for (int i = 0; i < obstacle_vertices.size(); i++) {
  477. Vector2 cp = p_camera->unproject_position(gt.xform(obstacle_vertices[i]));
  478. real_t d = cp.distance_to(mouse_position);
  479. if (d < closest_dist && d < grab_threshold) {
  480. closest_dist = d;
  481. closest_pos = cp;
  482. closest_idx = i;
  483. }
  484. }
  485. if (closest_idx >= 0) {
  486. pre_move_edit = obstacle_vertices;
  487. edited_point = closest_idx;
  488. edited_point_pos = obstacle_vertices[closest_idx];
  489. redraw();
  490. snap_ignore = false;
  491. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  492. }
  493. }
  494. } else {
  495. snap_ignore = false;
  496. if (edited_point != -1) {
  497. ERR_FAIL_INDEX_V(edited_point, obstacle_vertices.size(), EditorPlugin::AFTER_GUI_INPUT_PASS);
  498. obstacle_vertices.write[edited_point] = edited_point_pos;
  499. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  500. undo_redo->create_action(TTR("Edit Obstacle (Move Vertex)"));
  501. undo_redo->add_undo_method(obstacle_node, "set_vertices", obstacle_node->get_vertices());
  502. undo_redo->add_do_method(obstacle_node, "set_vertices", obstacle_vertices);
  503. undo_redo->commit_action();
  504. edited_point = -1;
  505. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  506. }
  507. }
  508. }
  509. } break;
  510. case MODE_DELETE: {
  511. if (mb->get_button_index() == MouseButton::LEFT && mb->is_pressed()) {
  512. int closest_idx = -1;
  513. real_t closest_dist = 1e10;
  514. for (int i = 0; i < obstacle_vertices.size(); i++) {
  515. Vector2 point = p_camera->unproject_position(gt.xform(obstacle_vertices[i]));
  516. real_t d = point.distance_to(mouse_position);
  517. if (d < closest_dist && d < grab_threshold) {
  518. closest_dist = d;
  519. closest_idx = i;
  520. }
  521. }
  522. if (closest_idx >= 0) {
  523. edited_point = -1;
  524. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  525. undo_redo->create_action(TTR("Edit Obstacle (Remove Vertex)"));
  526. undo_redo->add_undo_method(obstacle_node, "set_vertices", obstacle_vertices);
  527. obstacle_vertices.remove_at(closest_idx);
  528. undo_redo->add_do_method(obstacle_node, "set_vertices", obstacle_vertices);
  529. undo_redo->commit_action();
  530. redraw();
  531. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  532. }
  533. }
  534. } break;
  535. }
  536. }
  537. Ref<InputEventMouseMotion> mm = p_event;
  538. if (mm.is_valid()) {
  539. if (edited_point != -1 && (wip_active || mm->get_button_mask().has_flag(MouseButtonMask::LEFT))) {
  540. Vector2 mouse_position = mm->get_position();
  541. Vector3 ray_from = p_camera->project_ray_origin(mouse_position);
  542. Vector3 ray_dir = p_camera->project_ray_normal(mouse_position);
  543. const Vector3 safe_scale = obstacle_node->get_global_basis().get_scale().abs().maxf(0.001);
  544. 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());
  545. Transform3D gi = gt.affine_inverse();
  546. Plane projection_plane(Vector3(0.0, 1.0, 0.0), gt.origin);
  547. Vector3 intersection_point;
  548. if (!projection_plane.intersects_ray(ray_from, ray_dir, &intersection_point)) {
  549. return EditorPlugin::AFTER_GUI_INPUT_PASS;
  550. }
  551. intersection_point = gi.xform(intersection_point);
  552. Vector2 cpoint(intersection_point.x, intersection_point.z);
  553. if (snap_ignore && !Input::get_singleton()->is_key_pressed(Key::CTRL)) {
  554. snap_ignore = false;
  555. }
  556. if (!snap_ignore && Node3DEditor::get_singleton()->is_snap_enabled()) {
  557. cpoint = cpoint.snappedf(Node3DEditor::get_singleton()->get_translate_snap());
  558. }
  559. edited_point_pos = Vector3(cpoint.x, 0.0, cpoint.y);
  560. redraw();
  561. }
  562. }
  563. Ref<InputEventKey> k = p_event;
  564. if (k.is_valid() && k->is_pressed()) {
  565. if (wip_active && k->get_keycode() == Key::ENTER) {
  566. _wip_close();
  567. } else if (wip_active && k->get_keycode() == Key::ESCAPE) {
  568. _wip_cancel();
  569. }
  570. }
  571. return EditorPlugin::AFTER_GUI_INPUT_PASS;
  572. }
  573. void NavigationObstacle3DEditorPlugin::redraw() {
  574. if (!obstacle_node) {
  575. return;
  576. }
  577. RenderingServer *rs = RenderingServer::get_singleton();
  578. rs->mesh_clear(point_lines_mesh_rid);
  579. rs->mesh_clear(point_handle_mesh_rid);
  580. if (!obstacle_node->is_visible_in_tree()) {
  581. return;
  582. }
  583. Vector<Vector3> edited_vertices;
  584. if (wip_active) {
  585. edited_vertices = wip_vertices;
  586. } else {
  587. edited_vertices = obstacle_node->get_vertices();
  588. }
  589. if (edited_vertices.is_empty()) {
  590. return;
  591. }
  592. Array point_lines_mesh_array;
  593. point_lines_mesh_array.resize(Mesh::ARRAY_MAX);
  594. Vector<Vector3> point_lines_mesh_vertices;
  595. point_lines_mesh_vertices.resize(edited_vertices.size() * 2);
  596. Vector3 *point_lines_mesh_vertices_ptr = point_lines_mesh_vertices.ptrw();
  597. int vertex_index = 0;
  598. for (int i = 0; i < edited_vertices.size(); i++) {
  599. Vector3 point, next_point;
  600. if (i == edited_point) {
  601. point = edited_point_pos;
  602. } else {
  603. point = edited_vertices[i];
  604. }
  605. if ((wip_active && i == edited_vertices.size() - 1) || (((i + 1) % edited_vertices.size()) == edited_point)) {
  606. next_point = edited_point_pos;
  607. } else {
  608. next_point = edited_vertices[(i + 1) % edited_vertices.size()];
  609. }
  610. point_lines_mesh_vertices_ptr[vertex_index++] = point;
  611. point_lines_mesh_vertices_ptr[vertex_index++] = next_point;
  612. }
  613. point_lines_mesh_array[Mesh::ARRAY_VERTEX] = point_lines_mesh_vertices;
  614. rs->mesh_add_surface_from_arrays(point_lines_mesh_rid, RS::PRIMITIVE_LINES, point_lines_mesh_array);
  615. rs->instance_set_surface_override_material(point_lines_instance_rid, 0, line_material->get_rid());
  616. const Vector3 safe_scale = obstacle_node->get_global_basis().get_scale().abs().maxf(0.001);
  617. 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());
  618. rs->instance_set_transform(point_lines_instance_rid, gt);
  619. Array point_handle_mesh_array;
  620. point_handle_mesh_array.resize(Mesh::ARRAY_MAX);
  621. Vector<Vector3> point_handle_mesh_vertices;
  622. point_handle_mesh_vertices.resize(edited_vertices.size());
  623. Vector3 *point_handle_mesh_vertices_ptr = point_handle_mesh_vertices.ptrw();
  624. for (int i = 0; i < edited_vertices.size(); i++) {
  625. Vector3 point_handle_3d;
  626. if (i == edited_point) {
  627. point_handle_3d = edited_point_pos;
  628. } else {
  629. point_handle_3d = edited_vertices[i];
  630. }
  631. point_handle_mesh_vertices_ptr[i] = point_handle_3d;
  632. }
  633. point_handle_mesh_array[Mesh::ARRAY_VERTEX] = point_handle_mesh_vertices;
  634. rs->mesh_add_surface_from_arrays(point_handle_mesh_rid, RS::PRIMITIVE_POINTS, point_handle_mesh_array);
  635. rs->instance_set_surface_override_material(point_handles_instance_rid, 0, handle_material->get_rid());
  636. rs->instance_set_transform(point_handles_instance_rid, gt);
  637. }
  638. NavigationObstacle3DEditorPlugin *NavigationObstacle3DEditorPlugin::singleton = nullptr;
  639. NavigationObstacle3DEditorPlugin::NavigationObstacle3DEditorPlugin() {
  640. singleton = this;
  641. line_material = Ref<StandardMaterial3D>(memnew(StandardMaterial3D));
  642. line_material->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
  643. line_material->set_flag(StandardMaterial3D::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
  644. line_material->set_flag(StandardMaterial3D::FLAG_SRGB_VERTEX_COLOR, true);
  645. line_material->set_flag(StandardMaterial3D::FLAG_DISABLE_FOG, true);
  646. line_material->set_albedo(Color(1, 0.3, 0.1, 0.8));
  647. line_material->set_flag(StandardMaterial3D::FLAG_DISABLE_DEPTH_TEST, true);
  648. handle_material = Ref<StandardMaterial3D>(memnew(StandardMaterial3D));
  649. handle_material->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
  650. handle_material->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA);
  651. handle_material->set_flag(StandardMaterial3D::FLAG_USE_POINT_SIZE, true);
  652. handle_material->set_flag(StandardMaterial3D::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
  653. handle_material->set_flag(StandardMaterial3D::FLAG_SRGB_VERTEX_COLOR, true);
  654. handle_material->set_flag(StandardMaterial3D::FLAG_DISABLE_FOG, true);
  655. Ref<Texture2D> handle = EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("Editor3DHandle"), EditorStringName(EditorIcons));
  656. handle_material->set_point_size(handle->get_width());
  657. handle_material->set_texture(StandardMaterial3D::TEXTURE_ALBEDO, handle);
  658. handle_material->set_flag(StandardMaterial3D::FLAG_DISABLE_DEPTH_TEST, true);
  659. RenderingServer *rs = RenderingServer::get_singleton();
  660. point_lines_mesh_rid = rs->mesh_create();
  661. point_handle_mesh_rid = rs->mesh_create();
  662. point_lines_instance_rid = rs->instance_create();
  663. point_handles_instance_rid = rs->instance_create();
  664. rs->instance_set_base(point_lines_instance_rid, point_lines_mesh_rid);
  665. rs->instance_set_base(point_handles_instance_rid, point_handle_mesh_rid);
  666. obstacle_editor = memnew(HBoxContainer);
  667. obstacle_editor->hide();
  668. Ref<ButtonGroup> bg;
  669. bg.instantiate();
  670. button_create = memnew(Button);
  671. button_create->set_theme_type_variation(SceneStringName(FlatButton));
  672. obstacle_editor->add_child(button_create);
  673. button_create->set_tooltip_text(TTR("Add Vertex"));
  674. button_create->connect(SceneStringName(pressed), callable_mp(this, &NavigationObstacle3DEditorPlugin::set_mode).bind(NavigationObstacle3DEditorPlugin::MODE_CREATE));
  675. button_create->set_toggle_mode(true);
  676. button_create->set_button_group(bg);
  677. button_edit = memnew(Button);
  678. button_edit->set_theme_type_variation(SceneStringName(FlatButton));
  679. obstacle_editor->add_child(button_edit);
  680. button_edit->connect(SceneStringName(pressed), callable_mp(this, &NavigationObstacle3DEditorPlugin::set_mode).bind(NavigationObstacle3DEditorPlugin::MODE_EDIT));
  681. button_edit->set_toggle_mode(true);
  682. button_edit->set_button_group(bg);
  683. button_delete = memnew(Button);
  684. button_delete->set_theme_type_variation(SceneStringName(FlatButton));
  685. obstacle_editor->add_child(button_delete);
  686. button_delete->connect(SceneStringName(pressed), callable_mp(this, &NavigationObstacle3DEditorPlugin::set_mode).bind(NavigationObstacle3DEditorPlugin::MODE_DELETE));
  687. button_delete->set_toggle_mode(true);
  688. button_delete->set_button_group(bg);
  689. button_flip = memnew(Button);
  690. button_flip->set_theme_type_variation(SceneStringName(FlatButton));
  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. obstacle_editor->add_child(button_clear);
  697. button_clear->connect(SceneStringName(pressed), callable_mp(this, &NavigationObstacle3DEditorPlugin::set_mode).bind(NavigationObstacle3DEditorPlugin::ACTION_CLEAR));
  698. button_clear->set_toggle_mode(true);
  699. button_clear_dialog = memnew(ConfirmationDialog);
  700. button_clear_dialog->set_title(TTR("Please Confirm..."));
  701. button_clear_dialog->set_text(TTR("Remove all vertices?"));
  702. button_clear_dialog->connect(SceneStringName(confirmed), callable_mp(NavigationObstacle3DEditorPlugin::singleton, &NavigationObstacle3DEditorPlugin::action_clear_vertices));
  703. obstacle_editor->add_child(button_clear_dialog);
  704. Node3DEditor::get_singleton()->add_control_to_menu_panel(obstacle_editor);
  705. Ref<NavigationObstacle3DGizmoPlugin> gizmo_plugin = memnew(NavigationObstacle3DGizmoPlugin());
  706. obstacle_3d_gizmo_plugin = gizmo_plugin;
  707. Node3DEditor::get_singleton()->add_gizmo_plugin(gizmo_plugin);
  708. }
  709. NavigationObstacle3DEditorPlugin::~NavigationObstacle3DEditorPlugin() {
  710. RenderingServer *rs = RenderingServer::get_singleton();
  711. ERR_FAIL_NULL(rs);
  712. if (point_lines_instance_rid.is_valid()) {
  713. rs->free(point_lines_instance_rid);
  714. point_lines_instance_rid = RID();
  715. }
  716. if (point_lines_mesh_rid.is_valid()) {
  717. rs->free(point_lines_mesh_rid);
  718. point_lines_mesh_rid = RID();
  719. }
  720. if (point_handles_instance_rid.is_valid()) {
  721. rs->free(point_handles_instance_rid);
  722. point_handles_instance_rid = RID();
  723. }
  724. if (point_handle_mesh_rid.is_valid()) {
  725. rs->free(point_handle_mesh_rid);
  726. point_handle_mesh_rid = RID();
  727. }
  728. }