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