Debian Codegraph @

modules/navigation_3d/editor/navigation_obstacle_3d_editor_plugin.cpp

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