lightmap_gi_gizmo_plugin.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /**************************************************************************/
  2. /* lightmap_gi_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_gi_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_gi.h"
  35. LightmapGIGizmoPlugin::LightmapGIGizmoPlugin() {
  36. Color gizmo_color = EDITOR_GET("editors/3d_gizmos/gizmo_colors/lightmap_lines");
  37. gizmo_color.a = 0.1;
  38. create_material("lightmap_lines", gizmo_color);
  39. Ref<StandardMaterial3D> mat = memnew(StandardMaterial3D);
  40. mat->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
  41. // Fade out probes when camera gets too close to them.
  42. mat->set_distance_fade(StandardMaterial3D::DISTANCE_FADE_PIXEL_DITHER);
  43. mat->set_distance_fade_min_distance(0.5);
  44. mat->set_distance_fade_max_distance(1.5);
  45. mat->set_flag(StandardMaterial3D::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
  46. mat->set_flag(StandardMaterial3D::FLAG_SRGB_VERTEX_COLOR, false);
  47. mat->set_flag(StandardMaterial3D::FLAG_DISABLE_FOG, true);
  48. add_material("lightmap_probe_material", mat);
  49. create_icon_material("baked_indirect_light_icon", EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("GizmoLightmapGI"), EditorStringName(EditorIcons)));
  50. }
  51. bool LightmapGIGizmoPlugin::has_gizmo(Node3D *p_spatial) {
  52. return Object::cast_to<LightmapGI>(p_spatial) != nullptr;
  53. }
  54. String LightmapGIGizmoPlugin::get_gizmo_name() const {
  55. return "LightmapGI";
  56. }
  57. int LightmapGIGizmoPlugin::get_priority() const {
  58. return -1;
  59. }
  60. void LightmapGIGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {
  61. Ref<Material> icon = get_material("baked_indirect_light_icon", p_gizmo);
  62. LightmapGI *baker = Object::cast_to<LightmapGI>(p_gizmo->get_node_3d());
  63. Ref<LightmapGIData> data = baker->get_light_data();
  64. p_gizmo->clear();
  65. p_gizmo->add_unscaled_billboard(icon, 0.05);
  66. if (data.is_null() || !p_gizmo->is_selected()) {
  67. return;
  68. }
  69. Ref<Material> material_lines = get_material("lightmap_lines", p_gizmo);
  70. Ref<Material> material_probes = get_material("lightmap_probe_material", p_gizmo);
  71. Vector<Vector3> lines;
  72. HashSet<Vector2i> lines_found;
  73. Vector<Vector3> points = data->get_capture_points();
  74. if (points.is_empty()) {
  75. return;
  76. }
  77. Vector<Color> sh = data->get_capture_sh();
  78. if (sh.size() != points.size() * 9) {
  79. return;
  80. }
  81. Vector<int> tetrahedrons = data->get_capture_tetrahedra();
  82. for (int i = 0; i < tetrahedrons.size(); i += 4) {
  83. for (int j = 0; j < 4; j++) {
  84. for (int k = j + 1; k < 4; k++) {
  85. Vector2i pair;
  86. pair.x = tetrahedrons[i + j];
  87. pair.y = tetrahedrons[i + k];
  88. if (pair.y < pair.x) {
  89. SWAP(pair.x, pair.y);
  90. }
  91. if (lines_found.has(pair)) {
  92. continue;
  93. }
  94. lines_found.insert(pair);
  95. lines.push_back(points[pair.x]);
  96. lines.push_back(points[pair.y]);
  97. }
  98. }
  99. }
  100. p_gizmo->add_lines(lines, material_lines);
  101. int stack_count = 8;
  102. int sector_count = 16;
  103. float sector_step = (Math_PI * 2.0) / sector_count;
  104. float stack_step = Math_PI / stack_count;
  105. Vector<Vector3> vertices;
  106. Vector<Color> colors;
  107. Vector<int> indices;
  108. float radius = 0.3;
  109. for (int p = 0; p < points.size(); p++) {
  110. int vertex_base = vertices.size();
  111. Vector3 sh_col[9];
  112. for (int i = 0; i < 9; i++) {
  113. sh_col[i].x = sh[p * 9 + i].r;
  114. sh_col[i].y = sh[p * 9 + i].g;
  115. sh_col[i].z = sh[p * 9 + i].b;
  116. }
  117. for (int i = 0; i <= stack_count; ++i) {
  118. float stack_angle = Math_PI / 2 - i * stack_step; // starting from pi/2 to -pi/2
  119. float xy = radius * Math::cos(stack_angle); // r * cos(u)
  120. float z = radius * Math::sin(stack_angle); // r * sin(u)
  121. // add (sector_count+1) vertices per stack
  122. // the first and last vertices have same position and normal, but different tex coords
  123. for (int j = 0; j <= sector_count; ++j) {
  124. float sector_angle = j * sector_step; // starting from 0 to 2pi
  125. // vertex position (x, y, z)
  126. float x = xy * Math::cos(sector_angle); // r * cos(u) * cos(v)
  127. float y = xy * Math::sin(sector_angle); // r * cos(u) * sin(v)
  128. Vector3 n = Vector3(x, z, y);
  129. vertices.push_back(points[p] + n);
  130. n.normalize();
  131. const float c1 = 0.429043;
  132. const float c2 = 0.511664;
  133. const float c3 = 0.743125;
  134. const float c4 = 0.886227;
  135. const float c5 = 0.247708;
  136. Vector3 light = (c1 * sh_col[8] * (n.x * n.x - n.y * n.y) +
  137. c3 * sh_col[6] * n.z * n.z +
  138. c4 * sh_col[0] -
  139. c5 * sh_col[6] +
  140. 2.0 * c1 * sh_col[4] * n.x * n.y +
  141. 2.0 * c1 * sh_col[7] * n.x * n.z +
  142. 2.0 * c1 * sh_col[5] * n.y * n.z +
  143. 2.0 * c2 * sh_col[3] * n.x +
  144. 2.0 * c2 * sh_col[1] * n.y +
  145. 2.0 * c2 * sh_col[2] * n.z);
  146. colors.push_back(Color(light.x, light.y, light.z, 1));
  147. }
  148. }
  149. for (int i = 0; i < stack_count; ++i) {
  150. int k1 = i * (sector_count + 1); // beginning of current stack
  151. int k2 = k1 + sector_count + 1; // beginning of next stack
  152. for (int j = 0; j < sector_count; ++j, ++k1, ++k2) {
  153. // 2 triangles per sector excluding first and last stacks
  154. // k1 => k2 => k1+1
  155. if (i != 0) {
  156. indices.push_back(vertex_base + k1);
  157. indices.push_back(vertex_base + k2);
  158. indices.push_back(vertex_base + k1 + 1);
  159. }
  160. // k1+1 => k2 => k2+1
  161. if (i != (stack_count - 1)) {
  162. indices.push_back(vertex_base + k1 + 1);
  163. indices.push_back(vertex_base + k2);
  164. indices.push_back(vertex_base + k2 + 1);
  165. }
  166. }
  167. }
  168. }
  169. Array array;
  170. array.resize(RS::ARRAY_MAX);
  171. array[RS::ARRAY_VERTEX] = vertices;
  172. array[RS::ARRAY_INDEX] = indices;
  173. array[RS::ARRAY_COLOR] = colors;
  174. Ref<ArrayMesh> mesh;
  175. mesh.instantiate();
  176. mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, array, Array(), Dictionary(), 0); //no compression
  177. mesh->surface_set_material(0, material_probes);
  178. p_gizmo->add_mesh(mesh);
  179. }