lightmap_probe_gizmo_plugin.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /**************************************************************************/
  2. /* lightmap_probe_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 "lightmap_probe_gizmo_plugin.h"
  31. #include "editor/editor_node.h"
  32. #include "editor/editor_settings.h"
  33. #include "editor/editor_string_names.h"
  34. #include "scene/3d/lightmap_probe.h"
  35. LightmapProbeGizmoPlugin::LightmapProbeGizmoPlugin() {
  36. create_icon_material("lightmap_probe_icon", EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("GizmoLightmapProbe"), EditorStringName(EditorIcons)));
  37. Color gizmo_color = EDITOR_GET("editors/3d_gizmos/gizmo_colors/lightprobe_lines");
  38. gizmo_color.a = 0.3;
  39. create_material("lightprobe_lines", gizmo_color);
  40. }
  41. bool LightmapProbeGizmoPlugin::has_gizmo(Node3D *p_spatial) {
  42. return Object::cast_to<LightmapProbe>(p_spatial) != nullptr;
  43. }
  44. String LightmapProbeGizmoPlugin::get_gizmo_name() const {
  45. return "LightmapProbe";
  46. }
  47. int LightmapProbeGizmoPlugin::get_priority() const {
  48. return -1;
  49. }
  50. void LightmapProbeGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {
  51. Ref<Material> material_lines = get_material("lightprobe_lines", p_gizmo);
  52. p_gizmo->clear();
  53. Vector<Vector3> lines;
  54. int stack_count = 8;
  55. int sector_count = 16;
  56. float sector_step = (Math::PI * 2.0) / sector_count;
  57. float stack_step = Math::PI / stack_count;
  58. Vector<Vector3> vertices;
  59. float radius = 0.2;
  60. for (int i = 0; i <= stack_count; ++i) {
  61. float stack_angle = Math::PI / 2 - i * stack_step; // starting from pi/2 to -pi/2
  62. float xy = radius * Math::cos(stack_angle); // r * cos(u)
  63. float z = radius * Math::sin(stack_angle); // r * sin(u)
  64. // add (sector_count+1) vertices per stack
  65. // the first and last vertices have same position and normal, but different tex coords
  66. for (int j = 0; j <= sector_count; ++j) {
  67. float sector_angle = j * sector_step; // starting from 0 to 2pi
  68. // vertex position (x, y, z)
  69. float x = xy * Math::cos(sector_angle); // r * cos(u) * cos(v)
  70. float y = xy * Math::sin(sector_angle); // r * cos(u) * sin(v)
  71. Vector3 n = Vector3(x, z, y);
  72. vertices.push_back(n);
  73. }
  74. }
  75. for (int i = 0; i < stack_count; ++i) {
  76. int k1 = i * (sector_count + 1); // beginning of current stack
  77. int k2 = k1 + sector_count + 1; // beginning of next stack
  78. for (int j = 0; j < sector_count; ++j, ++k1, ++k2) {
  79. // 2 triangles per sector excluding first and last stacks
  80. // k1 => k2 => k1+1
  81. if (i != 0) {
  82. lines.push_back(vertices[k1]);
  83. lines.push_back(vertices[k2]);
  84. lines.push_back(vertices[k1]);
  85. lines.push_back(vertices[k1 + 1]);
  86. }
  87. if (i != (stack_count - 1)) {
  88. lines.push_back(vertices[k1 + 1]);
  89. lines.push_back(vertices[k2]);
  90. lines.push_back(vertices[k2]);
  91. lines.push_back(vertices[k2 + 1]);
  92. }
  93. }
  94. }
  95. const Ref<Material> icon = get_material("lightmap_probe_icon", p_gizmo);
  96. p_gizmo->add_lines(lines, material_lines);
  97. p_gizmo->add_unscaled_billboard(icon, 0.05);
  98. }