nav_region.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*************************************************************************/
  2. /* nav_region.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  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. /**
  33. @author AndreaCatania
  34. */
  35. void NavRegion::set_map(NavMap *p_map) {
  36. map = p_map;
  37. polygons_dirty = true;
  38. if (!map) {
  39. connections.clear();
  40. }
  41. }
  42. void NavRegion::set_layers(uint32_t p_layers) {
  43. layers = p_layers;
  44. }
  45. uint32_t NavRegion::get_layers() const {
  46. return layers;
  47. }
  48. void NavRegion::set_transform(Transform p_transform) {
  49. transform = p_transform;
  50. polygons_dirty = true;
  51. }
  52. void NavRegion::set_mesh(Ref<NavigationMesh> p_mesh) {
  53. mesh = p_mesh;
  54. polygons_dirty = true;
  55. }
  56. int NavRegion::get_connections_count() const {
  57. if (!map) {
  58. return 0;
  59. }
  60. return connections.size();
  61. }
  62. Vector3 NavRegion::get_connection_pathway_start(int p_connection_id) const {
  63. ERR_FAIL_COND_V(!map, Vector3());
  64. ERR_FAIL_INDEX_V(p_connection_id, connections.size(), Vector3());
  65. return connections[p_connection_id].pathway_start;
  66. }
  67. Vector3 NavRegion::get_connection_pathway_end(int p_connection_id) const {
  68. ERR_FAIL_COND_V(!map, Vector3());
  69. ERR_FAIL_INDEX_V(p_connection_id, connections.size(), Vector3());
  70. return connections[p_connection_id].pathway_end;
  71. }
  72. bool NavRegion::sync() {
  73. bool something_changed = polygons_dirty /* || something_dirty? */;
  74. update_polygons();
  75. return something_changed;
  76. }
  77. void NavRegion::update_polygons() {
  78. if (!polygons_dirty) {
  79. return;
  80. }
  81. polygons.clear();
  82. polygons_dirty = false;
  83. if (map == nullptr) {
  84. return;
  85. }
  86. if (mesh.is_null()) {
  87. return;
  88. }
  89. Vector<Vector3> vertices = mesh->get_vertices();
  90. int len = vertices.size();
  91. if (len == 0) {
  92. return;
  93. }
  94. const Vector3 *vertices_r = vertices.ptr();
  95. polygons.resize(mesh->get_polygon_count());
  96. // Build
  97. for (size_t i(0); i < polygons.size(); i++) {
  98. gd::Polygon &p = polygons[i];
  99. p.owner = this;
  100. Vector<int> mesh_poly = mesh->get_polygon(i);
  101. const int *indices = mesh_poly.ptr();
  102. bool valid(true);
  103. p.points.resize(mesh_poly.size());
  104. p.edges.resize(mesh_poly.size());
  105. Vector3 center;
  106. float sum(0);
  107. for (int j(0); j < mesh_poly.size(); j++) {
  108. int idx = indices[j];
  109. if (idx < 0 || idx >= len) {
  110. valid = false;
  111. break;
  112. }
  113. Vector3 point_position = transform.xform(vertices_r[idx]);
  114. p.points[j].pos = point_position;
  115. p.points[j].key = map->get_point_key(point_position);
  116. center += point_position; // Composing the center of the polygon
  117. if (j >= 2) {
  118. Vector3 epa = transform.xform(vertices_r[indices[j - 2]]);
  119. Vector3 epb = transform.xform(vertices_r[indices[j - 1]]);
  120. sum += map->get_up().dot((epb - epa).cross(point_position - epa));
  121. }
  122. }
  123. if (!valid) {
  124. ERR_BREAK_MSG(!valid, "The navigation mesh set in this region is not valid!");
  125. }
  126. p.clockwise = sum > 0;
  127. if (mesh_poly.size() != 0) {
  128. p.center = center / float(mesh_poly.size());
  129. }
  130. }
  131. }