nav_region.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. void NavRegion::set_map(NavMap *p_map) {
  33. if (map == p_map) {
  34. return;
  35. }
  36. if (map) {
  37. map->remove_region(this);
  38. }
  39. map = p_map;
  40. polygons_dirty = true;
  41. connections.clear();
  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. }
  67. void NavRegion::set_mesh(Ref<NavigationMesh> p_mesh) {
  68. mesh = p_mesh;
  69. polygons_dirty = true;
  70. }
  71. int NavRegion::get_connections_count() const {
  72. if (!map) {
  73. return 0;
  74. }
  75. return connections.size();
  76. }
  77. Vector3 NavRegion::get_connection_pathway_start(int p_connection_id) const {
  78. ERR_FAIL_COND_V(!map, Vector3());
  79. ERR_FAIL_INDEX_V(p_connection_id, connections.size(), Vector3());
  80. return connections[p_connection_id].pathway_start;
  81. }
  82. Vector3 NavRegion::get_connection_pathway_end(int p_connection_id) const {
  83. ERR_FAIL_COND_V(!map, Vector3());
  84. ERR_FAIL_INDEX_V(p_connection_id, connections.size(), Vector3());
  85. return connections[p_connection_id].pathway_end;
  86. }
  87. bool NavRegion::sync() {
  88. bool something_changed = polygons_dirty /* || something_dirty? */;
  89. update_polygons();
  90. return something_changed;
  91. }
  92. void NavRegion::update_polygons() {
  93. if (!polygons_dirty) {
  94. return;
  95. }
  96. polygons.clear();
  97. polygons_dirty = false;
  98. if (map == nullptr) {
  99. return;
  100. }
  101. if (mesh.is_null()) {
  102. return;
  103. }
  104. #ifdef DEBUG_ENABLED
  105. if (!Math::is_equal_approx(double(map->get_cell_size()), double(mesh->get_cell_size()))) {
  106. ERR_PRINT_ONCE(vformat("Navigation map synchronization error. 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(map->get_cell_size()), double(mesh->get_cell_size())));
  107. }
  108. if (!Math::is_equal_approx(double(map->get_cell_height()), double(mesh->get_cell_height()))) {
  109. ERR_PRINT_ONCE(vformat("Navigation map synchronization error. 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(map->get_cell_height()), double(mesh->get_cell_height())));
  110. }
  111. if (map && Math::rad_to_deg(map->get_up().angle_to(transform.basis.get_column(1))) >= 90.0f) {
  112. ERR_PRINT_ONCE("Navigation map synchronization error. Attempted to update a navigation region transform rotated 90 degrees or more away from the current navigation map UP orientation.");
  113. }
  114. #endif // DEBUG_ENABLED
  115. Vector<Vector3> vertices = mesh->get_vertices();
  116. int len = vertices.size();
  117. if (len == 0) {
  118. return;
  119. }
  120. const Vector3 *vertices_r = vertices.ptr();
  121. polygons.resize(mesh->get_polygon_count());
  122. // Build
  123. for (size_t i(0); i < polygons.size(); i++) {
  124. gd::Polygon &p = polygons[i];
  125. p.owner = this;
  126. Vector<int> mesh_poly = mesh->get_polygon(i);
  127. const int *indices = mesh_poly.ptr();
  128. bool valid(true);
  129. p.points.resize(mesh_poly.size());
  130. p.edges.resize(mesh_poly.size());
  131. Vector3 center;
  132. real_t sum(0);
  133. for (int j(0); j < mesh_poly.size(); j++) {
  134. int idx = indices[j];
  135. if (idx < 0 || idx >= len) {
  136. valid = false;
  137. break;
  138. }
  139. Vector3 point_position = transform.xform(vertices_r[idx]);
  140. p.points[j].pos = point_position;
  141. p.points[j].key = map->get_point_key(point_position);
  142. center += point_position; // Composing the center of the polygon
  143. if (j >= 2) {
  144. Vector3 epa = transform.xform(vertices_r[indices[j - 2]]);
  145. Vector3 epb = transform.xform(vertices_r[indices[j - 1]]);
  146. sum += map->get_up().dot((epb - epa).cross(point_position - epa));
  147. }
  148. }
  149. if (!valid) {
  150. ERR_BREAK_MSG(!valid, "The navigation mesh set in this region is not valid!");
  151. }
  152. p.clockwise = sum > 0;
  153. if (mesh_poly.size() != 0) {
  154. p.center = center / real_t(mesh_poly.size());
  155. }
  156. }
  157. }