| 1 | /**************************************************************************/ |
| 2 | /* camera_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 "camera_3d_gizmo_plugin.h" |
| 32 | |
| 33 | #include "editor/editor_node.h" |
| 34 | #include "editor/editor_string_names.h" |
| 35 | #include "editor/editor_undo_redo_manager.h" |
| 36 | #include "editor/scene/3d/node_3d_editor_plugin.h" |
| 37 | #include "editor/settings/editor_settings.h" |
| 38 | #include "scene/3d/camera_3d.h" |
| 39 | |
| 40 | Camera3DGizmoPlugin::Camera3DGizmoPlugin() { |
| 41 | Color gizmo_color = EDITOR_GET("editors/3d_gizmos/gizmo_colors/camera"); |
| 42 | |
| 43 | create_material("camera_material", gizmo_color); |
| 44 | create_icon_material("camera_icon", EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("GizmoCamera3D"), EditorStringName(EditorIcons))); |
| 45 | create_handle_material("handles"); |
| 46 | } |
| 47 | |
| 48 | bool Camera3DGizmoPlugin::has_gizmo(Node3D *p_spatial) { |
| 49 | return Object::cast_to<Camera3D>(p_spatial) != nullptr; |
| 50 | } |
| 51 | |
| 52 | String Camera3DGizmoPlugin::get_gizmo_name() const { |
| 53 | return "Camera3D"; |
| 54 | } |
| 55 | |
| 56 | int Camera3DGizmoPlugin::get_priority() const { |
| 57 | return -1; |
| 58 | } |
| 59 | |
| 60 | String Camera3DGizmoPlugin::get_handle_name(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const { |
| 61 | Camera3D *camera = Object::cast_to<Camera3D>(p_gizmo->get_node_3d()); |
| 62 | |
| 63 | if (camera->get_projection() == Camera3D::PROJECTION_PERSPECTIVE) { |
| 64 | return "FOV"; |
| 65 | } else { |
| 66 | return "Size"; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | Variant Camera3DGizmoPlugin::get_handle_value(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const { |
| 71 | Camera3D *camera = Object::cast_to<Camera3D>(p_gizmo->get_node_3d()); |
| 72 | |
| 73 | if (camera->get_projection() == Camera3D::PROJECTION_PERSPECTIVE) { |
| 74 | return camera->get_fov(); |
| 75 | } else { |
| 76 | return camera->get_size(); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | void Camera3DGizmoPlugin::set_handle(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary, Camera3D *p_camera, const Point2 &p_point) { |
| 81 | Camera3D *camera = Object::cast_to<Camera3D>(p_gizmo->get_node_3d()); |
| 82 | |
| 83 | Transform3D gt = camera->get_global_transform(); |
| 84 | Transform3D gi = gt.affine_inverse(); |
| 85 | |
| 86 | Vector3 ray_from = p_camera->project_ray_origin(p_point); |
| 87 | Vector3 ray_dir = p_camera->project_ray_normal(p_point); |
| 88 | |
| 89 | Vector3 s[2] = { gi.xform(ray_from), gi.xform(ray_from + ray_dir * 4096) }; |
| 90 | |
| 91 | if (camera->get_projection() == Camera3D::PROJECTION_PERSPECTIVE) { |
| 92 | Transform3D gt2 = camera->get_global_transform(); |
| 93 | float a = _find_closest_angle_to_half_pi_arc(s[0], s[1], 1.0, gt2); |
| 94 | camera->set("fov", CLAMP(a * 2.0, 1, 179)); |
| 95 | } else { |
| 96 | Camera3D::KeepAspect aspect = camera->get_keep_aspect_mode(); |
| 97 | Vector3 camera_far = aspect == Camera3D::KeepAspect::KEEP_WIDTH ? Vector3(4096, 0, -1) : Vector3(0, 4096, -1); |
| 98 | |
| 99 | Vector3 ra, rb; |
| 100 | Geometry3D::get_closest_points_between_segments(Vector3(0, 0, -1), camera_far, s[0], s[1], ra, rb); |
| 101 | float d = aspect == Camera3D::KeepAspect::KEEP_WIDTH ? ra.x * 2 : ra.y * 2; |
| 102 | if (Node3DEditor::get_singleton()->is_snap_enabled()) { |
| 103 | d = Math::snapped(d, Node3DEditor::get_singleton()->get_translate_snap()); |
| 104 | } |
| 105 | |
| 106 | d = CLAMP(d, 0.1, 16384); |
| 107 | |
| 108 | camera->set("size", d); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | void Camera3DGizmoPlugin::commit_handle(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary, const Variant &p_restore, bool p_cancel) { |
| 113 | Camera3D *camera = Object::cast_to<Camera3D>(p_gizmo->get_node_3d()); |
| 114 | |
| 115 | if (camera->get_projection() == Camera3D::PROJECTION_PERSPECTIVE) { |
| 116 | if (p_cancel) { |
| 117 | camera->set("fov", p_restore); |
| 118 | } else { |
| 119 | EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton(); |
| 120 | ur->create_action(TTR("Change Camera FOV")); |
| 121 | ur->add_do_property(camera, "fov", camera->get_fov()); |
| 122 | ur->add_undo_property(camera, "fov", p_restore); |
| 123 | ur->commit_action(); |
| 124 | } |
| 125 | |
| 126 | } else { |
| 127 | if (p_cancel) { |
| 128 | camera->set("size", p_restore); |
| 129 | } else { |
| 130 | EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton(); |
| 131 | ur->create_action(TTR("Change Camera Size")); |
| 132 | ur->add_do_property(camera, "size", camera->get_size()); |
| 133 | ur->add_undo_property(camera, "size", p_restore); |
| 134 | ur->commit_action(); |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | void Camera3DGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) { |
| 140 | Camera3D *camera = Object::cast_to<Camera3D>(p_gizmo->get_node_3d()); |
| 141 | |
| 142 | p_gizmo->clear(); |
| 143 | |
| 144 | Vector<Vector3> lines; |
| 145 | Vector<Vector3> handles; |
| 146 | |
| 147 | Ref<Material> material = get_material("camera_material", p_gizmo); |
| 148 | Ref<Material> icon = get_material("camera_icon", p_gizmo); |
| 149 | |
| 150 | const Size2i viewport_size = Node3DEditor::get_camera_viewport_size(camera); |
| 151 | const real_t viewport_aspect = viewport_size.x > 0 && viewport_size.y > 0 ? viewport_size.aspect() : 1.0; |
| 152 | const Size2 size_factor = viewport_aspect > 1.0 ? Size2(1.0, 1.0 / viewport_aspect) : Size2(viewport_aspect, 1.0); |
| 153 | |
| 154 | #define ADD_TRIANGLE(m_a, m_b, m_c) \ |
| 155 | { \ |
| 156 | lines.push_back(m_a); \ |
| 157 | lines.push_back(m_b); \ |
| 158 | lines.push_back(m_b); \ |
| 159 | lines.push_back(m_c); \ |
| 160 | lines.push_back(m_c); \ |
| 161 | lines.push_back(m_a); \ |
| 162 | } |
| 163 | |
| 164 | #define ADD_QUAD(m_a, m_b, m_c, m_d) \ |
| 165 | { \ |
| 166 | lines.push_back(m_a); \ |
| 167 | lines.push_back(m_b); \ |
| 168 | lines.push_back(m_b); \ |
| 169 | lines.push_back(m_c); \ |
| 170 | lines.push_back(m_c); \ |
| 171 | lines.push_back(m_d); \ |
| 172 | lines.push_back(m_d); \ |
| 173 | lines.push_back(m_a); \ |
| 174 | } |
| 175 | |
| 176 | switch (camera->get_projection()) { |
| 177 | case Camera3D::PROJECTION_PERSPECTIVE: { |
| 178 | // The real FOV is halved for accurate representation |
| 179 | float fov = camera->get_fov() / 2.0; |
| 180 | |
| 181 | const float hsize = Math::sin(Math::deg_to_rad(fov)); |
| 182 | const float depth = -Math::cos(Math::deg_to_rad(fov)); |
| 183 | |
| 184 | Vector3 side; |
| 185 | if (camera->get_keep_aspect_mode() == Camera3D::KEEP_WIDTH) { |
| 186 | side = Vector3(hsize * size_factor.x, 0, depth * size_factor.x); |
| 187 | } else { |
| 188 | side = Vector3(hsize * size_factor.x, 0, depth * size_factor.y); |
| 189 | } |
| 190 | Vector3 nside = Vector3(-side.x, side.y, side.z); |
| 191 | Vector3 up = Vector3(0, hsize * size_factor.y, 0); |
| 192 | |
| 193 | ADD_TRIANGLE(Vector3(), side + up, side - up); |
| 194 | ADD_TRIANGLE(Vector3(), nside + up, nside - up); |
| 195 | ADD_TRIANGLE(Vector3(), side + up, nside + up); |
| 196 | ADD_TRIANGLE(Vector3(), side - up, nside - up); |
| 197 | |
| 198 | handles.push_back(side); |
| 199 | side.x = MIN(side.x, hsize * 0.25); |
| 200 | nside.x = -side.x; |
| 201 | Vector3 tup(0, up.y + hsize / 2, side.z); |
| 202 | ADD_TRIANGLE(tup, side + up, nside + up); |
| 203 | } break; |
| 204 | |
| 205 | case Camera3D::PROJECTION_ORTHOGONAL: { |
| 206 | Camera3D::KeepAspect aspect = camera->get_keep_aspect_mode(); |
| 207 | |
| 208 | float size = camera->get_size(); |
| 209 | float keep_size = size * 0.5; |
| 210 | |
| 211 | Vector3 right, up; |
| 212 | Vector3 back(0, 0, -1.0); |
| 213 | |
| 214 | if (aspect == Camera3D::KeepAspect::KEEP_WIDTH) { |
| 215 | right = Vector3(keep_size, 0, 0); |
| 216 | up = Vector3(0, keep_size / viewport_aspect, 0); |
| 217 | handles.push_back(right + back); |
| 218 | } else { |
| 219 | right = Vector3(keep_size * viewport_aspect, 0, 0); |
| 220 | up = Vector3(0, keep_size, 0); |
| 221 | handles.push_back(up + back); |
| 222 | } |
| 223 | |
| 224 | ADD_QUAD(-up - right, -up + right, up + right, up - right); |
| 225 | ADD_QUAD(-up - right + back, -up + right + back, up + right + back, up - right + back); |
| 226 | ADD_QUAD(up + right, up + right + back, up - right + back, up - right); |
| 227 | ADD_QUAD(-up + right, -up + right + back, -up - right + back, -up - right); |
| 228 | |
| 229 | right.x = MIN(right.x, keep_size * 0.25); |
| 230 | Vector3 tup(0, up.y + keep_size / 2, back.z); |
| 231 | ADD_TRIANGLE(tup, right + up + back, -right + up + back); |
| 232 | } break; |
| 233 | |
| 234 | case Camera3D::PROJECTION_FRUSTUM: { |
| 235 | float hsize = camera->get_size() / 2.0; |
| 236 | |
| 237 | Vector3 side = Vector3(hsize, 0, -camera->get_near()).normalized(); |
| 238 | side.x *= size_factor.x; |
| 239 | Vector3 nside = Vector3(-side.x, side.y, side.z); |
| 240 | Vector3 up = Vector3(0, hsize * size_factor.y, 0); |
| 241 | Vector3 offset = Vector3(camera->get_frustum_offset().x, camera->get_frustum_offset().y, 0.0); |
| 242 | |
| 243 | ADD_TRIANGLE(Vector3(), side + up + offset, side - up + offset); |
| 244 | ADD_TRIANGLE(Vector3(), nside + up + offset, nside - up + offset); |
| 245 | ADD_TRIANGLE(Vector3(), side + up + offset, nside + up + offset); |
| 246 | ADD_TRIANGLE(Vector3(), side - up + offset, nside - up + offset); |
| 247 | |
| 248 | side.x = MIN(side.x, hsize * 0.25); |
| 249 | nside.x = -side.x; |
| 250 | Vector3 tup(0, up.y + hsize / 2, side.z); |
| 251 | ADD_TRIANGLE(tup + offset, side + up + offset, nside + up + offset); |
| 252 | } break; |
| 253 | } |
| 254 | |
| 255 | #undef ADD_TRIANGLE |
| 256 | #undef ADD_QUAD |
| 257 | |
| 258 | p_gizmo->add_lines(lines, material); |
| 259 | p_gizmo->add_unscaled_billboard(icon, 0.05); |
| 260 | p_gizmo->add_collision_segments(lines); |
| 261 | |
| 262 | if (!handles.is_empty()) { |
| 263 | p_gizmo->add_handles(handles, get_material("handles")); |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | float Camera3DGizmoPlugin::_find_closest_angle_to_half_pi_arc(const Vector3 &p_from, const Vector3 &p_to, float p_arc_radius, const Transform3D &p_arc_xform) { |
| 268 | //bleh, discrete is simpler |
| 269 | static const int arc_test_points = 64; |
| 270 | float min_d = 1e20; |
| 271 | Vector3 min_p; |
| 272 | |
| 273 | for (int i = 0; i < arc_test_points; i++) { |
| 274 | float a = i * Math::PI * 0.5 / arc_test_points; |
| 275 | float an = (i + 1) * Math::PI * 0.5 / arc_test_points; |
| 276 | Vector3 p = Vector3(Math::cos(a), 0, -Math::sin(a)) * p_arc_radius; |
| 277 | Vector3 n = Vector3(Math::cos(an), 0, -Math::sin(an)) * p_arc_radius; |
| 278 | |
| 279 | Vector3 ra, rb; |
| 280 | Geometry3D::get_closest_points_between_segments(p, n, p_from, p_to, ra, rb); |
| 281 | |
| 282 | float d = ra.distance_to(rb); |
| 283 | if (d < min_d) { |
| 284 | min_d = d; |
| 285 | min_p = ra; |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | //min_p = p_arc_xform.affine_inverse().xform(min_p); |
| 290 | float a = (Math::PI * 0.5) - Vector2(min_p.x, -min_p.z).angle(); |
| 291 | return Math::rad_to_deg(a); |
| 292 | } |