nav_region.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /**************************************************************************/
  2. /* nav_region.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 "nav_region.h"
  31. #include "nav_map.h"
  32. #include "3d/nav_mesh_queries_3d.h"
  33. void NavRegion::set_map(NavMap *p_map) {
  34. if (map == p_map) {
  35. return;
  36. }
  37. if (map) {
  38. map->remove_region(this);
  39. }
  40. map = p_map;
  41. polygons_dirty = true;
  42. if (map) {
  43. map->add_region(this);
  44. }
  45. }
  46. void NavRegion::set_enabled(bool p_enabled) {
  47. if (enabled == p_enabled) {
  48. return;
  49. }
  50. enabled = p_enabled;
  51. // TODO: This should not require a full rebuild as the region has not really changed.
  52. polygons_dirty = true;
  53. }
  54. void NavRegion::set_use_edge_connections(bool p_enabled) {
  55. if (use_edge_connections != p_enabled) {
  56. use_edge_connections = p_enabled;
  57. polygons_dirty = true;
  58. }
  59. }
  60. void NavRegion::set_transform(Transform3D p_transform) {
  61. if (transform == p_transform) {
  62. return;
  63. }
  64. transform = p_transform;
  65. polygons_dirty = true;
  66. #ifdef DEBUG_ENABLED
  67. if (map && Math::rad_to_deg(map->get_up().angle_to(transform.basis.get_column(1))) >= 90.0f) {
  68. ERR_PRINT_ONCE("Attempted to update a navigation region transform rotated 90 degrees or more away from the current navigation map UP orientation.");
  69. }
  70. #endif // DEBUG_ENABLED
  71. }
  72. void NavRegion::set_navigation_mesh(Ref<NavigationMesh> p_navigation_mesh) {
  73. #ifdef DEBUG_ENABLED
  74. if (map && p_navigation_mesh.is_valid() && !Math::is_equal_approx(double(map->get_cell_size()), double(p_navigation_mesh->get_cell_size()))) {
  75. ERR_PRINT_ONCE(vformat("Attempted to update a navigation region with a navigation mesh that uses a `cell_size` of %s while assigned to a navigation map set to a `cell_size` of %s. The cell size for navigation maps can be changed by using the NavigationServer map_set_cell_size() function. The cell size for default navigation maps can also be changed in the ProjectSettings.", double(p_navigation_mesh->get_cell_size()), double(map->get_cell_size())));
  76. }
  77. if (map && p_navigation_mesh.is_valid() && !Math::is_equal_approx(double(map->get_cell_height()), double(p_navigation_mesh->get_cell_height()))) {
  78. ERR_PRINT_ONCE(vformat("Attempted to update a navigation region with a navigation mesh that uses a `cell_height` of %s while assigned to a navigation map set to a `cell_height` of %s. The cell height for navigation maps can be changed by using the NavigationServer map_set_cell_height() function. The cell height for default navigation maps can also be changed in the ProjectSettings.", double(p_navigation_mesh->get_cell_height()), double(map->get_cell_height())));
  79. }
  80. #endif // DEBUG_ENABLED
  81. RWLockWrite write_lock(navmesh_rwlock);
  82. pending_navmesh_vertices.clear();
  83. pending_navmesh_polygons.clear();
  84. if (p_navigation_mesh.is_valid()) {
  85. p_navigation_mesh->get_data(pending_navmesh_vertices, pending_navmesh_polygons);
  86. }
  87. polygons_dirty = true;
  88. }
  89. Vector3 NavRegion::get_closest_point_to_segment(const Vector3 &p_from, const Vector3 &p_to, bool p_use_collision) const {
  90. RWLockRead read_lock(region_rwlock);
  91. return NavMeshQueries3D::polygons_get_closest_point_to_segment(
  92. get_polygons(), p_from, p_to, p_use_collision);
  93. }
  94. gd::ClosestPointQueryResult NavRegion::get_closest_point_info(const Vector3 &p_point) const {
  95. RWLockRead read_lock(region_rwlock);
  96. return NavMeshQueries3D::polygons_get_closest_point_info(get_polygons(), p_point);
  97. }
  98. Vector3 NavRegion::get_random_point(uint32_t p_navigation_layers, bool p_uniformly) const {
  99. RWLockRead read_lock(region_rwlock);
  100. if (!get_enabled()) {
  101. return Vector3();
  102. }
  103. return NavMeshQueries3D::polygons_get_random_point(get_polygons(), p_navigation_layers, p_uniformly);
  104. }
  105. bool NavRegion::sync() {
  106. RWLockWrite write_lock(region_rwlock);
  107. bool something_changed = polygons_dirty /* || something_dirty? */;
  108. update_polygons();
  109. return something_changed;
  110. }
  111. void NavRegion::update_polygons() {
  112. if (!polygons_dirty) {
  113. return;
  114. }
  115. polygons.clear();
  116. surface_area = 0.0;
  117. polygons_dirty = false;
  118. if (map == nullptr) {
  119. return;
  120. }
  121. RWLockRead read_lock(navmesh_rwlock);
  122. if (pending_navmesh_vertices.is_empty() || pending_navmesh_polygons.is_empty()) {
  123. return;
  124. }
  125. int len = pending_navmesh_vertices.size();
  126. if (len == 0) {
  127. return;
  128. }
  129. const Vector3 *vertices_r = pending_navmesh_vertices.ptr();
  130. polygons.resize(pending_navmesh_polygons.size());
  131. real_t _new_region_surface_area = 0.0;
  132. // Build
  133. int navigation_mesh_polygon_index = 0;
  134. for (gd::Polygon &polygon : polygons) {
  135. polygon.owner = this;
  136. polygon.surface_area = 0.0;
  137. Vector<int> navigation_mesh_polygon = pending_navmesh_polygons[navigation_mesh_polygon_index];
  138. navigation_mesh_polygon_index += 1;
  139. int navigation_mesh_polygon_size = navigation_mesh_polygon.size();
  140. if (navigation_mesh_polygon_size < 3) {
  141. continue;
  142. }
  143. const int *indices = navigation_mesh_polygon.ptr();
  144. bool valid(true);
  145. polygon.points.resize(navigation_mesh_polygon_size);
  146. polygon.edges.resize(navigation_mesh_polygon_size);
  147. real_t _new_polygon_surface_area = 0.0;
  148. for (int j(2); j < navigation_mesh_polygon_size; j++) {
  149. const Face3 face = Face3(
  150. transform.xform(vertices_r[indices[0]]),
  151. transform.xform(vertices_r[indices[j - 1]]),
  152. transform.xform(vertices_r[indices[j]]));
  153. _new_polygon_surface_area += face.get_area();
  154. }
  155. polygon.surface_area = _new_polygon_surface_area;
  156. _new_region_surface_area += _new_polygon_surface_area;
  157. for (int j(0); j < navigation_mesh_polygon_size; j++) {
  158. int idx = indices[j];
  159. if (idx < 0 || idx >= len) {
  160. valid = false;
  161. break;
  162. }
  163. Vector3 point_position = transform.xform(vertices_r[idx]);
  164. polygon.points[j].pos = point_position;
  165. polygon.points[j].key = map->get_point_key(point_position);
  166. }
  167. if (!valid) {
  168. ERR_BREAK_MSG(!valid, "The navigation mesh set in this region is not valid!");
  169. }
  170. }
  171. surface_area = _new_region_surface_area;
  172. }