Debian Codegraph @

editor/scene/3d/gizmos/light_3d_gizmo_plugin.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* light_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 "light_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 "scene/3d/light_3d.h"
38
39 Light3DGizmoPlugin::Light3DGizmoPlugin() {
40 // Enable vertex colors for the materials below as the gizmo color depends on the light color.
41 create_material("lines_primary", Color(1, 1, 1), false, false, true);
42 create_material("lines_secondary", Color(1, 1, 1, 0.35), false, false, true);
43 create_material("lines_billboard", Color(1, 1, 1), true, false, true);
44
45 create_icon_material("light_directional_icon", EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("GizmoDirectionalLight"), EditorStringName(EditorIcons)));
46 create_icon_material("light_omni_icon", EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("GizmoLight"), EditorStringName(EditorIcons)));
47 create_icon_material("light_spot_icon", EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("GizmoSpotLight"), EditorStringName(EditorIcons)));
48
49 create_handle_material("handles");
50 create_handle_material("handles_billboard", true);
51 }
52
53 bool Light3DGizmoPlugin::has_gizmo(Node3D *p_spatial) {
54 return Object::cast_to<Light3D>(p_spatial) != nullptr;
55 }
56
57 String Light3DGizmoPlugin::get_gizmo_name() const {
58 return "Light3D";
59 }
60
61 int Light3DGizmoPlugin::get_priority() const {
62 return -1;
63 }
64
65 String Light3DGizmoPlugin::get_handle_name(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const {
66 if (p_id == 0) {
67 return "Radius";
68 } else {
69 return "Aperture";
70 }
71 }
72
73 Variant Light3DGizmoPlugin::get_handle_value(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const {
74 Light3D *light = Object::cast_to<Light3D>(p_gizmo->get_node_3d());
75 if (p_id == 0) {
76 return light->get_param(Light3D::PARAM_RANGE);
77 }
78 if (p_id == 1) {
79 return light->get_param(Light3D::PARAM_SPOT_ANGLE);
80 }
81
82 return Variant();
83 }
84
85 void Light3DGizmoPlugin::set_handle(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary, Camera3D *p_camera, const Point2 &p_point) {
86 Light3D *light = Object::cast_to<Light3D>(p_gizmo->get_node_3d());
87 Transform3D gt = light->get_global_transform();
88 Transform3D gi = gt.affine_inverse();
89
90 Vector3 ray_from = p_camera->project_ray_origin(p_point);
91 Vector3 ray_dir = p_camera->project_ray_normal(p_point);
92
93 Vector3 s[2] = { gi.xform(ray_from), gi.xform(ray_from + ray_dir * 4096) };
94 if (p_id == 0) {
95 if (Object::cast_to<SpotLight3D>(light)) {
96 Vector3 ra, rb;
97 Geometry3D::get_closest_points_between_segments(Vector3(), Vector3(0, 0, -4096), s[0], s[1], ra, rb);
98
99 float d = -ra.z;
100 if (Node3DEditor::get_singleton()->is_snap_enabled()) {
101 d = Math::snapped(d, Node3DEditor::get_singleton()->get_translate_snap());
102 }
103
104 if (d <= 0) { // Equal is here for negative zero.
105 d = 0;
106 }
107
108 light->set_param(Light3D::PARAM_RANGE, d);
109 } else if (Object::cast_to<OmniLight3D>(light)) {
110 Plane cp = Plane(p_camera->get_transform().basis.get_column(2), gt.origin);
111
112 Vector3 inters;
113 if (cp.intersects_ray(ray_from, ray_dir, &inters)) {
114 float r = inters.distance_to(gt.origin);
115 if (Node3DEditor::get_singleton()->is_snap_enabled()) {
116 r = Math::snapped(r, Node3DEditor::get_singleton()->get_translate_snap());
117 }
118
119 light->set_param(Light3D::PARAM_RANGE, r);
120 }
121 }
122
123 } else if (p_id == 1) {
124 float a = _find_closest_angle_to_half_pi_arc(s[0], s[1], light->get_param(Light3D::PARAM_RANGE), gt);
125 light->set_param(Light3D::PARAM_SPOT_ANGLE, CLAMP(a, 0.01, 89.99));
126 }
127 }
128
129 void Light3DGizmoPlugin::commit_handle(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary, const Variant &p_restore, bool p_cancel) {
130 Light3D *light = Object::cast_to<Light3D>(p_gizmo->get_node_3d());
131 if (p_cancel) {
132 light->set_param(p_id == 0 ? Light3D::PARAM_RANGE : Light3D::PARAM_SPOT_ANGLE, p_restore);
133
134 } else if (p_id == 0) {
135 EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
136 ur->create_action(TTR("Change Light Radius"));
137 ur->add_do_method(light, "set_param", Light3D::PARAM_RANGE, light->get_param(Light3D::PARAM_RANGE));
138 ur->add_undo_method(light, "set_param", Light3D::PARAM_RANGE, p_restore);
139 ur->commit_action();
140 } else if (p_id == 1) {
141 EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
142 ur->create_action(TTR("Change Light Radius"));
143 ur->add_do_method(light, "set_param", Light3D::PARAM_SPOT_ANGLE, light->get_param(Light3D::PARAM_SPOT_ANGLE));
144 ur->add_undo_method(light, "set_param", Light3D::PARAM_SPOT_ANGLE, p_restore);
145 ur->commit_action();
146 }
147 }
148
149 void Light3DGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {
150 Light3D *light = Object::cast_to<Light3D>(p_gizmo->get_node_3d());
151
152 Color color = light->get_color().srgb_to_linear() * light->get_correlated_color().srgb_to_linear();
153 color = color.linear_to_srgb();
154 // Make the gizmo color as bright as possible for better visibility
155 color.set_hsv(color.get_h(), color.get_s(), 1);
156
157 p_gizmo->clear();
158
159 if (Object::cast_to<DirectionalLight3D>(light)) {
160 if (p_gizmo->is_selected()) {
161 Ref<Material> material = get_material("lines_primary", p_gizmo);
162
163 const int arrow_points = 7;
164 const float arrow_length = 1.5;
165
166 Vector3 arrow[arrow_points] = {
167 Vector3(0, 0, -1),
168 Vector3(0, 0.8, 0),
169 Vector3(0, 0.3, 0),
170 Vector3(0, 0.3, arrow_length),
171 Vector3(0, -0.3, arrow_length),
172 Vector3(0, -0.3, 0),
173 Vector3(0, -0.8, 0)
174 };
175
176 int arrow_sides = 2;
177
178 Vector<Vector3> lines;
179
180 for (int i = 0; i < arrow_sides; i++) {
181 for (int j = 0; j < arrow_points; j++) {
182 Basis ma(Vector3(0, 0, 1), Math::PI * i / arrow_sides);
183
184 Vector3 v1 = arrow[j] - Vector3(0, 0, arrow_length);
185 Vector3 v2 = arrow[(j + 1) % arrow_points] - Vector3(0, 0, arrow_length);
186
187 lines.push_back(ma.xform(v1));
188 lines.push_back(ma.xform(v2));
189 }
190 }
191
192 p_gizmo->add_lines(lines, material, false, color);
193 }
194
195 Ref<Material> icon = get_material("light_directional_icon", p_gizmo);
196 p_gizmo->add_unscaled_billboard(icon, 0.05, color);
197 }
198
199 if (Object::cast_to<OmniLight3D>(light)) {
200 if (p_gizmo->is_selected()) {
201 // Use both a billboard circle and 3 non-billboard circles for a better sphere-like representation
202 const Ref<Material> lines_material = get_material("lines_secondary", p_gizmo);
203 const Ref<Material> lines_billboard_material = get_material("lines_billboard", p_gizmo);
204
205 OmniLight3D *on = Object::cast_to<OmniLight3D>(light);
206 const float r = on->get_param(Light3D::PARAM_RANGE);
207 Vector<Vector3> points;
208 Vector<Vector3> points_billboard;
209
210 for (int i = 0; i < 120; i++) {
211 // Create a circle
212 const float ra = Math::deg_to_rad((float)(i * 3));
213 const float rb = Math::deg_to_rad((float)((i + 1) * 3));
214 const Point2 a = Vector2(Math::sin(ra), Math::cos(ra)) * r;
215 const Point2 b = Vector2(Math::sin(rb), Math::cos(rb)) * r;
216
217 // Draw axis-aligned circles
218 points.push_back(Vector3(a.x, 0, a.y));
219 points.push_back(Vector3(b.x, 0, b.y));
220 points.push_back(Vector3(0, a.x, a.y));
221 points.push_back(Vector3(0, b.x, b.y));
222 points.push_back(Vector3(a.x, a.y, 0));
223 points.push_back(Vector3(b.x, b.y, 0));
224
225 // Draw a billboarded circle
226 points_billboard.push_back(Vector3(a.x, a.y, 0));
227 points_billboard.push_back(Vector3(b.x, b.y, 0));
228 }
229
230 p_gizmo->add_lines(points, lines_material, true, color);
231 p_gizmo->add_lines(points_billboard, lines_billboard_material, true, color);
232
233 Vector<Vector3> handles;
234 handles.push_back(Vector3(r, 0, 0));
235 p_gizmo->add_handles(handles, get_material("handles_billboard"), Vector<int>(), true);
236 }
237
238 const Ref<Material> icon = get_material("light_omni_icon", p_gizmo);
239 p_gizmo->add_unscaled_billboard(icon, 0.05, color);
240 }
241
242 if (Object::cast_to<SpotLight3D>(light)) {
243 if (p_gizmo->is_selected()) {
244 const Ref<Material> material_primary = get_material("lines_primary", p_gizmo);
245 const Ref<Material> material_secondary = get_material("lines_secondary", p_gizmo);
246
247 Vector<Vector3> points_primary;
248 Vector<Vector3> points_secondary;
249 SpotLight3D *sl = Object::cast_to<SpotLight3D>(light);
250
251 float r = sl->get_param(Light3D::PARAM_RANGE);
252 float w = r * Math::sin(Math::deg_to_rad(sl->get_param(Light3D::PARAM_SPOT_ANGLE)));
253 float d = r * Math::cos(Math::deg_to_rad(sl->get_param(Light3D::PARAM_SPOT_ANGLE)));
254
255 for (int i = 0; i < 120; i++) {
256 // Draw a circle
257 const float ra = Math::deg_to_rad((float)(i * 3));
258 const float rb = Math::deg_to_rad((float)((i + 1) * 3));
259 const Point2 a = Vector2(Math::sin(ra), Math::cos(ra)) * w;
260 const Point2 b = Vector2(Math::sin(rb), Math::cos(rb)) * w;
261
262 points_primary.push_back(Vector3(a.x, a.y, -d));
263 points_primary.push_back(Vector3(b.x, b.y, -d));
264
265 if (i % 15 == 0) {
266 // Draw 8 lines from the cone origin to the sides of the circle
267 points_secondary.push_back(Vector3(a.x, a.y, -d));
268 points_secondary.push_back(Vector3());
269 }
270 }
271
272 points_primary.push_back(Vector3(0, 0, -r));
273 points_primary.push_back(Vector3());
274
275 p_gizmo->add_lines(points_primary, material_primary, false, color);
276 p_gizmo->add_lines(points_secondary, material_secondary, false, color);
277
278 Vector<Vector3> handles = {
279 Vector3(0, 0, -r),
280 Vector3(w, 0, -d)
281 };
282
283 p_gizmo->add_handles(handles, get_material("handles"));
284 }
285
286 const Ref<Material> icon = get_material("light_spot_icon", p_gizmo);
287 p_gizmo->add_unscaled_billboard(icon, 0.05, color);
288 }
289 }
290
291 float Light3DGizmoPlugin::_find_closest_angle_to_half_pi_arc(const Vector3 &p_from, const Vector3 &p_to, float p_arc_radius, const Transform3D &p_arc_xform) {
292 //bleh, discrete is simpler
293 static const int arc_test_points = 64;
294 float min_d = 1e20;
295 Vector3 min_p;
296
297 for (int i = 0; i < arc_test_points; i++) {
298 float a = i * Math::PI * 0.5 / arc_test_points;
299 float an = (i + 1) * Math::PI * 0.5 / arc_test_points;
300 Vector3 p = Vector3(Math::cos(a), 0, -Math::sin(a)) * p_arc_radius;
301 Vector3 n = Vector3(Math::cos(an), 0, -Math::sin(an)) * p_arc_radius;
302
303 Vector3 ra, rb;
304 Geometry3D::get_closest_points_between_segments(p, n, p_from, p_to, ra, rb);
305
306 float d = ra.distance_to(rb);
307 if (d < min_d) {
308 min_d = d;
309 min_p = ra;
310 }
311 }
312
313 //min_p = p_arc_xform.affine_inverse().xform(min_p);
314 float a = (Math::PI * 0.5) - Vector2(min_p.x, -min_p.z).angle();
315 return Math::rad_to_deg(a);
316 }