| 1 | /**************************************************************************/ |
| 2 | /* navigation_link_3d_gizmo_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_link_3d_gizmo_plugin.h" |
| 32 | |
| 33 | #include "editor/editor_undo_redo_manager.h" |
| 34 | #include "editor/scene/3d/node_3d_editor_plugin.h" |
| 35 | #include "scene/3d/navigation/navigation_link_3d.h" |
| 36 | #include "servers/navigation_3d/navigation_server_3d.h" |
| 37 | |
| 38 | NavigationLink3DGizmoPlugin::NavigationLink3DGizmoPlugin() { |
| 39 | create_material("navigation_link_material", NavigationServer3D::get_singleton()->get_debug_navigation_link_connection_color()); |
| 40 | create_material("navigation_link_material_disabled", NavigationServer3D::get_singleton()->get_debug_navigation_link_connection_disabled_color()); |
| 41 | create_handle_material("handles"); |
| 42 | } |
| 43 | |
| 44 | bool NavigationLink3DGizmoPlugin::has_gizmo(Node3D *p_spatial) { |
| 45 | return Object::cast_to<NavigationLink3D>(p_spatial) != nullptr; |
| 46 | } |
| 47 | |
| 48 | String NavigationLink3DGizmoPlugin::get_gizmo_name() const { |
| 49 | return "NavigationLink3D"; |
| 50 | } |
| 51 | |
| 52 | int NavigationLink3DGizmoPlugin::get_priority() const { |
| 53 | return -1; |
| 54 | } |
| 55 | |
| 56 | void NavigationLink3DGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) { |
| 57 | NavigationLink3D *link = Object::cast_to<NavigationLink3D>(p_gizmo->get_node_3d()); |
| 58 | |
| 59 | RID nav_map = link->get_world_3d()->get_navigation_map(); |
| 60 | real_t search_radius = NavigationServer3D::get_singleton()->map_get_link_connection_radius(nav_map); |
| 61 | Vector3 up_vector = NavigationServer3D::get_singleton()->map_get_up(nav_map); |
| 62 | Vector3::Axis up_axis = up_vector.max_axis_index(); |
| 63 | |
| 64 | Vector3 start_position = link->get_start_position(); |
| 65 | Vector3 end_position = link->get_end_position(); |
| 66 | |
| 67 | Ref<Material> link_material = get_material("navigation_link_material", p_gizmo); |
| 68 | Ref<Material> link_material_disabled = get_material("navigation_link_material_disabled", p_gizmo); |
| 69 | Ref<Material> handles_material = get_material("handles"); |
| 70 | |
| 71 | p_gizmo->clear(); |
| 72 | |
| 73 | // Number of points in an octant. So there will be 8 * points_in_octant points in total. |
| 74 | // Correspond to the smoothness of the circle. |
| 75 | const uint32_t points_in_octant = 8; |
| 76 | real_t inc = (Math::PI / (4 * points_in_octant)); |
| 77 | |
| 78 | Vector<Vector3> lines; |
| 79 | // points_in_octant * 8 * 2 per circle * 2 circles. 2 for the start-end. 4 for the arrow, and another 4 if bidirectionnal. |
| 80 | lines.resize(points_in_octant * 8 * 2 * 2 + 2 + 4 + (link->is_bidirectional() ? 4 : 0)); |
| 81 | uint32_t index = 0; |
| 82 | Vector3 *lines_ptrw = lines.ptrw(); |
| 83 | // Draw line between the points. |
| 84 | lines_ptrw[index++] = start_position; |
| 85 | lines_ptrw[index++] = end_position; |
| 86 | real_t search_radius_squared = search_radius * search_radius; |
| 87 | |
| 88 | // Draw circles at start and end positions in one go. |
| 89 | real_t r = 0; |
| 90 | Vector2 a = Vector2(search_radius, 0); |
| 91 | for (uint32_t i = 0; i < points_in_octant; i++) { |
| 92 | r += inc; |
| 93 | real_t x = Math::cos(r) * search_radius; |
| 94 | real_t y = Math::sqrt(search_radius_squared - (x * x)); |
| 95 | |
| 96 | // Draw axis-aligned circle. |
| 97 | switch (up_axis) { |
| 98 | case Vector3::AXIS_X: |
| 99 | #define PUSH_OCTANT(_position, a, b) \ |
| 100 | lines_ptrw[index++] = _position + Vector3(0, a.x, a.y); \ |
| 101 | lines_ptrw[index++] = _position + Vector3(0, x, y); \ |
| 102 | lines_ptrw[index++] = _position + Vector3(0, -a.x, a.y); \ |
| 103 | lines_ptrw[index++] = _position + Vector3(0, x, y); \ |
| 104 | lines_ptrw[index++] = _position + Vector3(0, a.x, -a.y); \ |
| 105 | lines_ptrw[index++] = _position + Vector3(0, x, -y); \ |
| 106 | lines_ptrw[index++] = _position + Vector3(0, -a.x, -a.y); \ |
| 107 | lines_ptrw[index++] = _position + Vector3(0, x, y); \ |
| 108 | lines_ptrw[index++] = _position + Vector3(0, a.y, a.x); \ |
| 109 | lines_ptrw[index++] = _position + Vector3(0, y, x); \ |
| 110 | lines_ptrw[index++] = _position + Vector3(0, -a.y, a.x); \ |
| 111 | lines_ptrw[index++] = _position + Vector3(0, -y, x); \ |
| 112 | lines_ptrw[index++] = _position + Vector3(0, a.y, -a.x); \ |
| 113 | lines_ptrw[index++] = _position + Vector3(0, y, -x); \ |
| 114 | lines_ptrw[index++] = _position + Vector3(0, -a.y, -a.x); \ |
| 115 | lines_ptrw[index++] = _position + Vector3(0, -y, -x); |
| 116 | |
| 117 | PUSH_OCTANT(start_position, a, b) |
| 118 | PUSH_OCTANT(end_position, a, b) |
| 119 | #undef PUSH_OCTANT |
| 120 | break; |
| 121 | case Vector3::AXIS_Y: |
| 122 | #define PUSH_OCTANT(_position, a, b) \ |
| 123 | lines_ptrw[index++] = _position + Vector3(a.x, 0, a.y); \ |
| 124 | lines_ptrw[index++] = _position + Vector3(x, 0, y); \ |
| 125 | lines_ptrw[index++] = _position + Vector3(-a.x, 0, a.y); \ |
| 126 | lines_ptrw[index++] = _position + Vector3(-x, 0, y); \ |
| 127 | lines_ptrw[index++] = _position + Vector3(a.x, 0, -a.y); \ |
| 128 | lines_ptrw[index++] = _position + Vector3(x, 0, -y); \ |
| 129 | lines_ptrw[index++] = _position + Vector3(-a.x, 0, -a.y); \ |
| 130 | lines_ptrw[index++] = _position + Vector3(-x, 0, -y); \ |
| 131 | lines_ptrw[index++] = _position + Vector3(a.y, 0, a.x); \ |
| 132 | lines_ptrw[index++] = _position + Vector3(y, 0, x); \ |
| 133 | lines_ptrw[index++] = _position + Vector3(-a.y, 0, a.x); \ |
| 134 | lines_ptrw[index++] = _position + Vector3(-y, 0, x); \ |
| 135 | lines_ptrw[index++] = _position + Vector3(a.y, 0, -a.x); \ |
| 136 | lines_ptrw[index++] = _position + Vector3(y, 0, -x); \ |
| 137 | lines_ptrw[index++] = _position + Vector3(-a.y, 0, -a.x); \ |
| 138 | lines_ptrw[index++] = _position + Vector3(-y, 0, -x); |
| 139 | |
| 140 | PUSH_OCTANT(start_position, a, b) |
| 141 | PUSH_OCTANT(end_position, a, b) |
| 142 | #undef PUSH_OCTANT |
| 143 | break; |
| 144 | case Vector3::AXIS_Z: |
| 145 | #define PUSH_OCTANT(_position, a, b) \ |
| 146 | lines_ptrw[index++] = _position + Vector3(a.x, a.y, 0); \ |
| 147 | lines_ptrw[index++] = _position + Vector3(x, y, 0); \ |
| 148 | lines_ptrw[index++] = _position + Vector3(-a.x, a.y, 0); \ |
| 149 | lines_ptrw[index++] = _position + Vector3(-x, y, 0); \ |
| 150 | lines_ptrw[index++] = _position + Vector3(a.x, -a.y, 0); \ |
| 151 | lines_ptrw[index++] = _position + Vector3(x, -y, 0); \ |
| 152 | lines_ptrw[index++] = _position + Vector3(-a.x, -a.y, 0); \ |
| 153 | lines_ptrw[index++] = _position + Vector3(-x, -y, 0); \ |
| 154 | lines_ptrw[index++] = _position + Vector3(a.y, a.x, 0); \ |
| 155 | lines_ptrw[index++] = _position + Vector3(y, x, 0); \ |
| 156 | lines_ptrw[index++] = _position + Vector3(-a.y, a.x, 0); \ |
| 157 | lines_ptrw[index++] = _position + Vector3(-y, x, 0); \ |
| 158 | lines_ptrw[index++] = _position + Vector3(a.y, -a.x, 0); \ |
| 159 | lines_ptrw[index++] = _position + Vector3(y, -x, 0); \ |
| 160 | lines_ptrw[index++] = _position + Vector3(-a.y, -a.x, 0); \ |
| 161 | lines_ptrw[index++] = _position + Vector3(-y, -x, 0); |
| 162 | |
| 163 | PUSH_OCTANT(start_position, a, b) |
| 164 | PUSH_OCTANT(end_position, a, b) |
| 165 | #undef PUSH_OCTANT |
| 166 | break; |
| 167 | } |
| 168 | a.x = x; |
| 169 | a.y = y; |
| 170 | } |
| 171 | |
| 172 | const Vector3 link_segment = end_position - start_position; |
| 173 | const Vector3 up = Vector3(0.0, 1.0, 0.0); |
| 174 | const float arror_len = 0.5; |
| 175 | |
| 176 | { |
| 177 | Vector3 anchor = start_position + (link_segment * 0.75); |
| 178 | Vector3 direction = start_position.direction_to(end_position); |
| 179 | Vector3 arrow_dir = direction.cross(up); |
| 180 | lines_ptrw[index++] = anchor; |
| 181 | lines_ptrw[index++] = anchor + (arrow_dir - direction) * arror_len; |
| 182 | |
| 183 | arrow_dir = -direction.cross(up); |
| 184 | lines_ptrw[index++] = anchor; |
| 185 | lines_ptrw[index++] = anchor + (arrow_dir - direction) * arror_len; |
| 186 | } |
| 187 | |
| 188 | if (link->is_bidirectional()) { |
| 189 | Vector3 anchor = start_position + (link_segment * 0.25); |
| 190 | Vector3 direction = end_position.direction_to(start_position); |
| 191 | Vector3 arrow_dir = direction.cross(up); |
| 192 | lines_ptrw[index++] = anchor; |
| 193 | lines_ptrw[index++] = anchor + (arrow_dir - direction) * arror_len; |
| 194 | |
| 195 | arrow_dir = -direction.cross(up); |
| 196 | lines_ptrw[index++] = anchor; |
| 197 | lines_ptrw[index++] = anchor + (arrow_dir - direction) * arror_len; |
| 198 | } |
| 199 | |
| 200 | p_gizmo->add_lines(lines, link->is_enabled() ? link_material : link_material_disabled); |
| 201 | p_gizmo->add_collision_segments(lines); |
| 202 | |
| 203 | p_gizmo->add_handles(Vector({ start_position, end_position }), handles_material); |
| 204 | } |
| 205 | |
| 206 | String NavigationLink3DGizmoPlugin::get_handle_name(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const { |
| 207 | return p_id == 0 ? TTR("Start Position") : TTR("End Position"); |
| 208 | } |
| 209 | |
| 210 | Variant NavigationLink3DGizmoPlugin::get_handle_value(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const { |
| 211 | NavigationLink3D *link = Object::cast_to<NavigationLink3D>(p_gizmo->get_node_3d()); |
| 212 | return p_id == 0 ? link->get_start_position() : link->get_end_position(); |
| 213 | } |
| 214 | |
| 215 | void NavigationLink3DGizmoPlugin::set_handle(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary, Camera3D *p_camera, const Point2 &p_point) { |
| 216 | NavigationLink3D *link = Object::cast_to<NavigationLink3D>(p_gizmo->get_node_3d()); |
| 217 | |
| 218 | Transform3D gt = link->get_global_transform(); |
| 219 | Transform3D gi = gt.affine_inverse(); |
| 220 | |
| 221 | Transform3D ct = p_camera->get_global_transform(); |
| 222 | Vector3 cam_dir = ct.basis.get_column(Vector3::AXIS_Z); |
| 223 | |
| 224 | Vector3 ray_from = p_camera->project_ray_origin(p_point); |
| 225 | Vector3 ray_dir = p_camera->project_ray_normal(p_point); |
| 226 | |
| 227 | Vector3 position = p_id == 0 ? link->get_start_position() : link->get_end_position(); |
| 228 | Plane move_plane = Plane(cam_dir, gt.xform(position)); |
| 229 | |
| 230 | Vector3 intersection; |
| 231 | if (!move_plane.intersects_ray(ray_from, ray_dir, &intersection)) { |
| 232 | return; |
| 233 | } |
| 234 | |
| 235 | if (Node3DEditor::get_singleton()->is_snap_enabled()) { |
| 236 | double snap = Node3DEditor::get_singleton()->get_translate_snap(); |
| 237 | intersection.snapf(snap); |
| 238 | } |
| 239 | |
| 240 | position = gi.xform(intersection); |
| 241 | if (p_id == 0) { |
| 242 | link->set_start_position(position); |
| 243 | } else if (p_id == 1) { |
| 244 | link->set_end_position(position); |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | void NavigationLink3DGizmoPlugin::commit_handle(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary, const Variant &p_restore, bool p_cancel) { |
| 249 | NavigationLink3D *link = Object::cast_to<NavigationLink3D>(p_gizmo->get_node_3d()); |
| 250 | |
| 251 | if (p_cancel) { |
| 252 | if (p_id == 0) { |
| 253 | link->set_start_position(p_restore); |
| 254 | } else { |
| 255 | link->set_end_position(p_restore); |
| 256 | } |
| 257 | return; |
| 258 | } |
| 259 | |
| 260 | EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton(); |
| 261 | if (p_id == 0) { |
| 262 | ur->create_action(TTR("Change Start Position")); |
| 263 | ur->add_do_method(link, "set_start_position", link->get_start_position()); |
| 264 | ur->add_undo_method(link, "set_start_position", p_restore); |
| 265 | } else { |
| 266 | ur->create_action(TTR("Change End Position")); |
| 267 | ur->add_do_method(link, "set_end_position", link->get_end_position()); |
| 268 | ur->add_undo_method(link, "set_end_position", p_restore); |
| 269 | } |
| 270 | |
| 271 | ur->commit_action(); |
| 272 | } |