navigation_mesh.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. /*************************************************************************/
  2. /* navigation_mesh.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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 "navigation_mesh.h"
  31. #include "mesh_instance.h"
  32. #include "navigation.h"
  33. void NavigationMesh::create_from_mesh(const Ref<Mesh> &p_mesh) {
  34. vertices = DVector<Vector3>();
  35. clear_polygons();
  36. for (int i = 0; i < p_mesh->get_surface_count(); i++) {
  37. if (p_mesh->surface_get_primitive_type(i) != Mesh::PRIMITIVE_TRIANGLES)
  38. continue;
  39. Array arr = p_mesh->surface_get_arrays(i);
  40. DVector<Vector3> varr = arr[Mesh::ARRAY_VERTEX];
  41. DVector<int> iarr = arr[Mesh::ARRAY_INDEX];
  42. if (varr.size() == 0 || iarr.size() == 0)
  43. continue;
  44. int from = vertices.size();
  45. vertices.append_array(varr);
  46. int rlen = iarr.size();
  47. DVector<int>::Read r = iarr.read();
  48. for (int j = 0; j < rlen; j += 3) {
  49. Vector<int> vi;
  50. vi.resize(3);
  51. vi[0] = r[j + 0] + from;
  52. vi[1] = r[j + 1] + from;
  53. vi[2] = r[j + 2] + from;
  54. add_polygon(vi);
  55. }
  56. }
  57. }
  58. void NavigationMesh::set_vertices(const DVector<Vector3> &p_vertices) {
  59. vertices = p_vertices;
  60. }
  61. DVector<Vector3> NavigationMesh::get_vertices() const {
  62. return vertices;
  63. }
  64. void NavigationMesh::_set_polygons(const Array &p_array) {
  65. polygons.resize(p_array.size());
  66. for (int i = 0; i < p_array.size(); i++) {
  67. polygons[i].indices = p_array[i];
  68. }
  69. }
  70. Array NavigationMesh::_get_polygons() const {
  71. Array ret;
  72. ret.resize(polygons.size());
  73. for (int i = 0; i < ret.size(); i++) {
  74. ret[i] = polygons[i].indices;
  75. }
  76. return ret;
  77. }
  78. void NavigationMesh::add_polygon(const Vector<int> &p_polygon) {
  79. Polygon polygon;
  80. polygon.indices = p_polygon;
  81. polygons.push_back(polygon);
  82. }
  83. int NavigationMesh::get_polygon_count() const {
  84. return polygons.size();
  85. }
  86. Vector<int> NavigationMesh::get_polygon(int p_idx) {
  87. ERR_FAIL_INDEX_V(p_idx, polygons.size(), Vector<int>());
  88. return polygons[p_idx].indices;
  89. }
  90. void NavigationMesh::clear_polygons() {
  91. polygons.clear();
  92. }
  93. Ref<Mesh> NavigationMesh::get_debug_mesh() {
  94. if (debug_mesh.is_valid())
  95. return debug_mesh;
  96. DVector<Vector3> vertices = get_vertices();
  97. DVector<Vector3>::Read vr = vertices.read();
  98. List<Face3> faces;
  99. for (int i = 0; i < get_polygon_count(); i++) {
  100. Vector<int> p = get_polygon(i);
  101. for (int j = 2; j < p.size(); j++) {
  102. Face3 f;
  103. f.vertex[0] = vr[p[0]];
  104. f.vertex[1] = vr[p[j - 1]];
  105. f.vertex[2] = vr[p[j]];
  106. faces.push_back(f);
  107. }
  108. }
  109. Map<_EdgeKey, bool> edge_map;
  110. DVector<Vector3> tmeshfaces;
  111. tmeshfaces.resize(faces.size() * 3);
  112. {
  113. DVector<Vector3>::Write tw = tmeshfaces.write();
  114. int tidx = 0;
  115. for (List<Face3>::Element *E = faces.front(); E; E = E->next()) {
  116. const Face3 &f = E->get();
  117. for (int j = 0; j < 3; j++) {
  118. tw[tidx++] = f.vertex[j];
  119. _EdgeKey ek;
  120. ek.from = f.vertex[j].snapped(CMP_EPSILON);
  121. ek.to = f.vertex[(j + 1) % 3].snapped(CMP_EPSILON);
  122. if (ek.from < ek.to)
  123. SWAP(ek.from, ek.to);
  124. Map<_EdgeKey, bool>::Element *E = edge_map.find(ek);
  125. if (E) {
  126. E->get() = false;
  127. } else {
  128. edge_map[ek] = true;
  129. }
  130. }
  131. }
  132. }
  133. List<Vector3> lines;
  134. for (Map<_EdgeKey, bool>::Element *E = edge_map.front(); E; E = E->next()) {
  135. if (E->get()) {
  136. lines.push_back(E->key().from);
  137. lines.push_back(E->key().to);
  138. }
  139. }
  140. DVector<Vector3> varr;
  141. varr.resize(lines.size());
  142. {
  143. DVector<Vector3>::Write w = varr.write();
  144. int idx = 0;
  145. for (List<Vector3>::Element *E = lines.front(); E; E = E->next()) {
  146. w[idx++] = E->get();
  147. }
  148. }
  149. debug_mesh = Ref<Mesh>(memnew(Mesh));
  150. Array arr;
  151. arr.resize(Mesh::ARRAY_MAX);
  152. arr[Mesh::ARRAY_VERTEX] = varr;
  153. debug_mesh->add_surface(Mesh::PRIMITIVE_LINES, arr);
  154. return debug_mesh;
  155. }
  156. void NavigationMesh::_bind_methods() {
  157. ObjectTypeDB::bind_method(_MD("set_vertices", "vertices"), &NavigationMesh::set_vertices);
  158. ObjectTypeDB::bind_method(_MD("get_vertices"), &NavigationMesh::get_vertices);
  159. ObjectTypeDB::bind_method(_MD("add_polygon", "polygon"), &NavigationMesh::add_polygon);
  160. ObjectTypeDB::bind_method(_MD("get_polygon_count"), &NavigationMesh::get_polygon_count);
  161. ObjectTypeDB::bind_method(_MD("get_polygon", "idx"), &NavigationMesh::get_polygon);
  162. ObjectTypeDB::bind_method(_MD("clear_polygons"), &NavigationMesh::clear_polygons);
  163. ObjectTypeDB::bind_method(_MD("create_from_mesh", "mesh"), &NavigationMesh::create_from_mesh);
  164. ObjectTypeDB::bind_method(_MD("_set_polygons", "polygons"), &NavigationMesh::_set_polygons);
  165. ObjectTypeDB::bind_method(_MD("_get_polygons"), &NavigationMesh::_get_polygons);
  166. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3_ARRAY, "vertices", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), _SCS("set_vertices"), _SCS("get_vertices"));
  167. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "polygons", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), _SCS("_set_polygons"), _SCS("_get_polygons"));
  168. }
  169. NavigationMesh::NavigationMesh() {
  170. }
  171. void NavigationMeshInstance::set_enabled(bool p_enabled) {
  172. if (enabled == p_enabled)
  173. return;
  174. enabled = p_enabled;
  175. if (!is_inside_tree())
  176. return;
  177. if (!enabled) {
  178. if (nav_id != -1) {
  179. navigation->navmesh_remove(nav_id);
  180. nav_id = -1;
  181. }
  182. } else {
  183. if (navigation) {
  184. if (navmesh.is_valid()) {
  185. nav_id = navigation->navmesh_create(navmesh, get_relative_transform(navigation), this);
  186. }
  187. }
  188. }
  189. if (debug_view) {
  190. MeshInstance *dm = debug_view->cast_to<MeshInstance>();
  191. if (is_enabled()) {
  192. dm->set_material_override(get_tree()->get_debug_navigation_material());
  193. } else {
  194. dm->set_material_override(get_tree()->get_debug_navigation_disabled_material());
  195. }
  196. }
  197. update_gizmo();
  198. }
  199. bool NavigationMeshInstance::is_enabled() const {
  200. return enabled;
  201. }
  202. /////////////////////////////
  203. void NavigationMeshInstance::_notification(int p_what) {
  204. switch (p_what) {
  205. case NOTIFICATION_ENTER_TREE: {
  206. Spatial *c = this;
  207. while (c) {
  208. navigation = c->cast_to<Navigation>();
  209. if (navigation) {
  210. if (enabled && navmesh.is_valid()) {
  211. nav_id = navigation->navmesh_create(navmesh, get_relative_transform(navigation), this);
  212. }
  213. break;
  214. }
  215. c = c->get_parent_spatial();
  216. }
  217. if (navmesh.is_valid() && get_tree()->is_debugging_navigation_hint()) {
  218. MeshInstance *dm = memnew(MeshInstance);
  219. dm->set_mesh(navmesh->get_debug_mesh());
  220. if (is_enabled()) {
  221. dm->set_material_override(get_tree()->get_debug_navigation_material());
  222. } else {
  223. dm->set_material_override(get_tree()->get_debug_navigation_disabled_material());
  224. }
  225. add_child(dm);
  226. debug_view = dm;
  227. }
  228. } break;
  229. case NOTIFICATION_TRANSFORM_CHANGED: {
  230. if (navigation && nav_id != -1) {
  231. navigation->navmesh_set_transform(nav_id, get_relative_transform(navigation));
  232. }
  233. } break;
  234. case NOTIFICATION_EXIT_TREE: {
  235. if (navigation) {
  236. if (nav_id != -1) {
  237. navigation->navmesh_remove(nav_id);
  238. nav_id = -1;
  239. }
  240. }
  241. if (debug_view) {
  242. debug_view->queue_delete();
  243. debug_view = NULL;
  244. }
  245. navigation = NULL;
  246. } break;
  247. }
  248. }
  249. void NavigationMeshInstance::set_navigation_mesh(const Ref<NavigationMesh> &p_navmesh) {
  250. if (p_navmesh == navmesh)
  251. return;
  252. if (navigation && nav_id != -1) {
  253. navigation->navmesh_remove(nav_id);
  254. nav_id = -1;
  255. }
  256. navmesh = p_navmesh;
  257. if (navigation && navmesh.is_valid() && enabled) {
  258. nav_id = navigation->navmesh_create(navmesh, get_relative_transform(navigation), this);
  259. }
  260. if (debug_view && navmesh.is_valid()) {
  261. debug_view->cast_to<MeshInstance>()->set_mesh(navmesh->get_debug_mesh());
  262. }
  263. update_gizmo();
  264. update_configuration_warning();
  265. }
  266. Ref<NavigationMesh> NavigationMeshInstance::get_navigation_mesh() const {
  267. return navmesh;
  268. }
  269. String NavigationMeshInstance::get_configuration_warning() const {
  270. if (!is_visible() || !is_inside_tree())
  271. return String();
  272. if (!navmesh.is_valid()) {
  273. return TTR("A NavigationMesh resource must be set or created for this node to work.");
  274. }
  275. const Spatial *c = this;
  276. while (c) {
  277. if (c->cast_to<Navigation>())
  278. return String();
  279. c = c->get_parent()->cast_to<Spatial>();
  280. }
  281. return TTR("NavigationMeshInstance must be a child or grandchild to a Navigation node. It only provides navigation data.");
  282. }
  283. void NavigationMeshInstance::_bind_methods() {
  284. ObjectTypeDB::bind_method(_MD("set_navigation_mesh", "navmesh"), &NavigationMeshInstance::set_navigation_mesh);
  285. ObjectTypeDB::bind_method(_MD("get_navigation_mesh"), &NavigationMeshInstance::get_navigation_mesh);
  286. ObjectTypeDB::bind_method(_MD("set_enabled", "enabled"), &NavigationMeshInstance::set_enabled);
  287. ObjectTypeDB::bind_method(_MD("is_enabled"), &NavigationMeshInstance::is_enabled);
  288. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "navmesh", PROPERTY_HINT_RESOURCE_TYPE, "NavigationMesh"), _SCS("set_navigation_mesh"), _SCS("get_navigation_mesh"));
  289. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "enabled"), _SCS("set_enabled"), _SCS("is_enabled"));
  290. }
  291. NavigationMeshInstance::NavigationMeshInstance() {
  292. debug_view = NULL;
  293. navigation = NULL;
  294. nav_id = -1;
  295. enabled = true;
  296. }