navigation_region_3d_gizmo_plugin.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /**************************************************************************/
  2. /* navigation_region_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 "navigation_region_3d_gizmo_plugin.h"
  31. #include "core/math/random_pcg.h"
  32. #include "scene/3d/navigation/navigation_region_3d.h"
  33. #include "servers/navigation_server_3d.h"
  34. NavigationRegion3DGizmoPlugin::NavigationRegion3DGizmoPlugin() {
  35. create_material("face_material", NavigationServer3D::get_singleton()->get_debug_navigation_geometry_face_color(), false, false, true);
  36. create_material("face_material_disabled", NavigationServer3D::get_singleton()->get_debug_navigation_geometry_face_disabled_color(), false, false, true);
  37. create_material("edge_material", NavigationServer3D::get_singleton()->get_debug_navigation_geometry_edge_color());
  38. create_material("edge_material_disabled", NavigationServer3D::get_singleton()->get_debug_navigation_geometry_edge_disabled_color());
  39. Color baking_aabb_material_color = Color(0.8, 0.5, 0.7);
  40. baking_aabb_material_color.a = 0.1;
  41. create_material("baking_aabb_material", baking_aabb_material_color);
  42. }
  43. bool NavigationRegion3DGizmoPlugin::has_gizmo(Node3D *p_spatial) {
  44. return Object::cast_to<NavigationRegion3D>(p_spatial) != nullptr;
  45. }
  46. String NavigationRegion3DGizmoPlugin::get_gizmo_name() const {
  47. return "NavigationRegion3D";
  48. }
  49. int NavigationRegion3DGizmoPlugin::get_priority() const {
  50. return -1;
  51. }
  52. void NavigationRegion3DGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {
  53. NavigationRegion3D *navigationregion = Object::cast_to<NavigationRegion3D>(p_gizmo->get_node_3d());
  54. p_gizmo->clear();
  55. Ref<NavigationMesh> navigationmesh = navigationregion->get_navigation_mesh();
  56. if (navigationmesh.is_null()) {
  57. return;
  58. }
  59. AABB baking_aabb = navigationmesh->get_filter_baking_aabb();
  60. if (baking_aabb.has_volume()) {
  61. Vector3 baking_aabb_offset = navigationmesh->get_filter_baking_aabb_offset();
  62. if (p_gizmo->is_selected()) {
  63. Ref<Material> material = get_material("baking_aabb_material", p_gizmo);
  64. p_gizmo->add_solid_box(material, baking_aabb.get_size(), baking_aabb.get_center() + baking_aabb_offset);
  65. }
  66. }
  67. Vector<Vector3> vertices = navigationmesh->get_vertices();
  68. const Vector3 *vr = vertices.ptr();
  69. List<Face3> faces;
  70. for (int i = 0; i < navigationmesh->get_polygon_count(); i++) {
  71. Vector<int> p = navigationmesh->get_polygon(i);
  72. for (int j = 2; j < p.size(); j++) {
  73. Face3 f;
  74. f.vertex[0] = vr[p[0]];
  75. f.vertex[1] = vr[p[j - 1]];
  76. f.vertex[2] = vr[p[j]];
  77. faces.push_back(f);
  78. }
  79. }
  80. if (faces.is_empty()) {
  81. return;
  82. }
  83. HashMap<_EdgeKey, bool, _EdgeKey> edge_map;
  84. Vector<Vector3> tmeshfaces;
  85. tmeshfaces.resize(faces.size() * 3);
  86. {
  87. Vector3 *tw = tmeshfaces.ptrw();
  88. int tidx = 0;
  89. for (const Face3 &f : faces) {
  90. for (int j = 0; j < 3; j++) {
  91. tw[tidx++] = f.vertex[j];
  92. _EdgeKey ek;
  93. ek.from = f.vertex[j].snappedf(CMP_EPSILON);
  94. ek.to = f.vertex[(j + 1) % 3].snappedf(CMP_EPSILON);
  95. if (ek.from < ek.to) {
  96. SWAP(ek.from, ek.to);
  97. }
  98. HashMap<_EdgeKey, bool, _EdgeKey>::Iterator F = edge_map.find(ek);
  99. if (F) {
  100. F->value = false;
  101. } else {
  102. edge_map[ek] = true;
  103. }
  104. }
  105. }
  106. }
  107. Vector<Vector3> lines;
  108. for (const KeyValue<_EdgeKey, bool> &E : edge_map) {
  109. if (E.value) {
  110. lines.push_back(E.key.from);
  111. lines.push_back(E.key.to);
  112. }
  113. }
  114. Ref<TriangleMesh> tmesh = memnew(TriangleMesh);
  115. tmesh->create(tmeshfaces);
  116. p_gizmo->add_collision_triangles(tmesh);
  117. p_gizmo->add_collision_segments(lines);
  118. Ref<ArrayMesh> debug_mesh = Ref<ArrayMesh>(memnew(ArrayMesh));
  119. int polygon_count = navigationmesh->get_polygon_count();
  120. // build geometry face surface
  121. Vector<Vector3> face_vertex_array;
  122. face_vertex_array.resize(polygon_count * 3);
  123. for (int i = 0; i < polygon_count; i++) {
  124. Vector<int> polygon = navigationmesh->get_polygon(i);
  125. face_vertex_array.push_back(vertices[polygon[0]]);
  126. face_vertex_array.push_back(vertices[polygon[1]]);
  127. face_vertex_array.push_back(vertices[polygon[2]]);
  128. }
  129. Array face_mesh_array;
  130. face_mesh_array.resize(Mesh::ARRAY_MAX);
  131. face_mesh_array[Mesh::ARRAY_VERTEX] = face_vertex_array;
  132. // if enabled add vertex colors to colorize each face individually
  133. RandomPCG rand;
  134. bool enabled_geometry_face_random_color = NavigationServer3D::get_singleton()->get_debug_navigation_enable_geometry_face_random_color();
  135. if (enabled_geometry_face_random_color) {
  136. Color debug_navigation_geometry_face_color = NavigationServer3D::get_singleton()->get_debug_navigation_geometry_face_color();
  137. Color polygon_color = debug_navigation_geometry_face_color;
  138. Vector<Color> face_color_array;
  139. face_color_array.resize(polygon_count * 3);
  140. for (int i = 0; i < polygon_count; i++) {
  141. // Generate the polygon color, slightly randomly modified from the settings one.
  142. polygon_color.set_hsv(debug_navigation_geometry_face_color.get_h() + rand.random(-1.0, 1.0) * 0.1, debug_navigation_geometry_face_color.get_s(), debug_navigation_geometry_face_color.get_v() + rand.random(-1.0, 1.0) * 0.2);
  143. polygon_color.a = debug_navigation_geometry_face_color.a;
  144. Vector<int> polygon = navigationmesh->get_polygon(i);
  145. face_color_array.push_back(polygon_color);
  146. face_color_array.push_back(polygon_color);
  147. face_color_array.push_back(polygon_color);
  148. }
  149. face_mesh_array[Mesh::ARRAY_COLOR] = face_color_array;
  150. }
  151. debug_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, face_mesh_array);
  152. p_gizmo->add_mesh(debug_mesh, navigationregion->is_enabled() ? get_material("face_material", p_gizmo) : get_material("face_material_disabled", p_gizmo));
  153. // if enabled build geometry edge line surface
  154. bool enabled_edge_lines = NavigationServer3D::get_singleton()->get_debug_navigation_enable_edge_lines();
  155. if (enabled_edge_lines) {
  156. Vector<Vector3> line_vertex_array;
  157. line_vertex_array.resize(polygon_count * 6);
  158. for (int i = 0; i < polygon_count; i++) {
  159. Vector<int> polygon = navigationmesh->get_polygon(i);
  160. line_vertex_array.push_back(vertices[polygon[0]]);
  161. line_vertex_array.push_back(vertices[polygon[1]]);
  162. line_vertex_array.push_back(vertices[polygon[1]]);
  163. line_vertex_array.push_back(vertices[polygon[2]]);
  164. line_vertex_array.push_back(vertices[polygon[2]]);
  165. line_vertex_array.push_back(vertices[polygon[0]]);
  166. }
  167. p_gizmo->add_lines(line_vertex_array, navigationregion->is_enabled() ? get_material("edge_material", p_gizmo) : get_material("edge_material_disabled", p_gizmo));
  168. }
  169. }