light_3d_gizmo_plugin.cpp 12 KB

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