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