nav_region.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. }
  39. void NavRegion::set_transform(Transform p_transform) {
  40. transform = p_transform;
  41. polygons_dirty = true;
  42. }
  43. void NavRegion::set_mesh(Ref<NavigationMesh> p_mesh) {
  44. mesh = p_mesh;
  45. polygons_dirty = true;
  46. }
  47. bool NavRegion::sync() {
  48. bool something_changed = polygons_dirty /* || something_dirty? */;
  49. update_polygons();
  50. return something_changed;
  51. }
  52. void NavRegion::update_polygons() {
  53. if (!polygons_dirty) {
  54. return;
  55. }
  56. polygons.clear();
  57. polygons_dirty = false;
  58. if (map == nullptr) {
  59. return;
  60. }
  61. if (mesh.is_null()) {
  62. return;
  63. }
  64. Vector<Vector3> vertices = mesh->get_vertices();
  65. int len = vertices.size();
  66. if (len == 0) {
  67. return;
  68. }
  69. const Vector3 *vertices_r = vertices.ptr();
  70. polygons.resize(mesh->get_polygon_count());
  71. // Build
  72. for (size_t i(0); i < polygons.size(); i++) {
  73. gd::Polygon &p = polygons[i];
  74. p.owner = this;
  75. Vector<int> mesh_poly = mesh->get_polygon(i);
  76. const int *indices = mesh_poly.ptr();
  77. bool valid(true);
  78. p.points.resize(mesh_poly.size());
  79. p.edges.resize(mesh_poly.size());
  80. Vector3 center;
  81. float sum(0);
  82. for (int j(0); j < mesh_poly.size(); j++) {
  83. int idx = indices[j];
  84. if (idx < 0 || idx >= len) {
  85. valid = false;
  86. break;
  87. }
  88. Vector3 point_position = transform.xform(vertices_r[idx]);
  89. p.points[j].pos = point_position;
  90. p.points[j].key = map->get_point_key(point_position);
  91. center += point_position; // Composing the center of the polygon
  92. if (j >= 2) {
  93. Vector3 epa = transform.xform(vertices_r[indices[j - 2]]);
  94. Vector3 epb = transform.xform(vertices_r[indices[j - 1]]);
  95. sum += map->get_up().dot((epb - epa).cross(point_position - epa));
  96. }
  97. }
  98. if (!valid) {
  99. ERR_BREAK_MSG(!valid, "The navigation mesh set in this region is not valid!");
  100. }
  101. p.clockwise = sum > 0;
  102. if (mesh_poly.size() != 0) {
  103. p.center = center / float(mesh_poly.size());
  104. }
  105. }
  106. }