navigation_mesh.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  1. /*************************************************************************/
  2. /* navigation_mesh.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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 = PoolVector<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. PoolVector<Vector3> varr = arr[Mesh::ARRAY_VERTEX];
  41. PoolVector<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. PoolVector<int>::Read r = iarr.read();
  48. for (int j = 0; j < rlen; j += 3) {
  49. Vector<int> vi;
  50. vi.resize(3);
  51. vi.write[0] = r[j + 0] + from;
  52. vi.write[1] = r[j + 1] + from;
  53. vi.write[2] = r[j + 2] + from;
  54. add_polygon(vi);
  55. }
  56. }
  57. }
  58. void NavigationMesh::set_sample_partition_type(int p_value) {
  59. ERR_FAIL_COND(p_value >= SAMPLE_PARTITION_MAX);
  60. partition_type = static_cast<SamplePartitionType>(p_value);
  61. }
  62. int NavigationMesh::get_sample_partition_type() const {
  63. return static_cast<int>(partition_type);
  64. }
  65. void NavigationMesh::set_cell_size(float p_value) {
  66. cell_size = p_value;
  67. }
  68. float NavigationMesh::get_cell_size() const {
  69. return cell_size;
  70. }
  71. void NavigationMesh::set_cell_height(float p_value) {
  72. cell_height = p_value;
  73. }
  74. float NavigationMesh::get_cell_height() const {
  75. return cell_height;
  76. }
  77. void NavigationMesh::set_agent_height(float p_value) {
  78. agent_height = p_value;
  79. }
  80. float NavigationMesh::get_agent_height() const {
  81. return agent_height;
  82. }
  83. void NavigationMesh::set_agent_radius(float p_value) {
  84. agent_radius = p_value;
  85. }
  86. float NavigationMesh::get_agent_radius() {
  87. return agent_radius;
  88. }
  89. void NavigationMesh::set_agent_max_climb(float p_value) {
  90. agent_max_climb = p_value;
  91. }
  92. float NavigationMesh::get_agent_max_climb() const {
  93. return agent_max_climb;
  94. }
  95. void NavigationMesh::set_agent_max_slope(float p_value) {
  96. agent_max_slope = p_value;
  97. }
  98. float NavigationMesh::get_agent_max_slope() const {
  99. return agent_max_slope;
  100. }
  101. void NavigationMesh::set_region_min_size(float p_value) {
  102. region_min_size = p_value;
  103. }
  104. float NavigationMesh::get_region_min_size() const {
  105. return region_min_size;
  106. }
  107. void NavigationMesh::set_region_merge_size(float p_value) {
  108. region_merge_size = p_value;
  109. }
  110. float NavigationMesh::get_region_merge_size() const {
  111. return region_merge_size;
  112. }
  113. void NavigationMesh::set_edge_max_length(float p_value) {
  114. edge_max_length = p_value;
  115. }
  116. float NavigationMesh::get_edge_max_length() const {
  117. return edge_max_length;
  118. }
  119. void NavigationMesh::set_edge_max_error(float p_value) {
  120. edge_max_error = p_value;
  121. }
  122. float NavigationMesh::get_edge_max_error() const {
  123. return edge_max_error;
  124. }
  125. void NavigationMesh::set_verts_per_poly(float p_value) {
  126. verts_per_poly = p_value;
  127. }
  128. float NavigationMesh::get_verts_per_poly() const {
  129. return verts_per_poly;
  130. }
  131. void NavigationMesh::set_detail_sample_distance(float p_value) {
  132. detail_sample_distance = p_value;
  133. }
  134. float NavigationMesh::get_detail_sample_distance() const {
  135. return detail_sample_distance;
  136. }
  137. void NavigationMesh::set_detail_sample_max_error(float p_value) {
  138. detail_sample_max_error = p_value;
  139. }
  140. float NavigationMesh::get_detail_sample_max_error() const {
  141. return detail_sample_max_error;
  142. }
  143. void NavigationMesh::set_filter_low_hanging_obstacles(bool p_value) {
  144. filter_low_hanging_obstacles = p_value;
  145. }
  146. bool NavigationMesh::get_filter_low_hanging_obstacles() const {
  147. return filter_low_hanging_obstacles;
  148. }
  149. void NavigationMesh::set_filter_ledge_spans(bool p_value) {
  150. filter_ledge_spans = p_value;
  151. }
  152. bool NavigationMesh::get_filter_ledge_spans() const {
  153. return filter_ledge_spans;
  154. }
  155. void NavigationMesh::set_filter_walkable_low_height_spans(bool p_value) {
  156. filter_walkable_low_height_spans = p_value;
  157. }
  158. bool NavigationMesh::get_filter_walkable_low_height_spans() const {
  159. return filter_walkable_low_height_spans;
  160. }
  161. void NavigationMesh::set_vertices(const PoolVector<Vector3> &p_vertices) {
  162. vertices = p_vertices;
  163. }
  164. PoolVector<Vector3> NavigationMesh::get_vertices() const {
  165. return vertices;
  166. }
  167. void NavigationMesh::_set_polygons(const Array &p_array) {
  168. polygons.resize(p_array.size());
  169. for (int i = 0; i < p_array.size(); i++) {
  170. polygons.write[i].indices = p_array[i];
  171. }
  172. }
  173. Array NavigationMesh::_get_polygons() const {
  174. Array ret;
  175. ret.resize(polygons.size());
  176. for (int i = 0; i < ret.size(); i++) {
  177. ret[i] = polygons[i].indices;
  178. }
  179. return ret;
  180. }
  181. void NavigationMesh::add_polygon(const Vector<int> &p_polygon) {
  182. Polygon polygon;
  183. polygon.indices = p_polygon;
  184. polygons.push_back(polygon);
  185. }
  186. int NavigationMesh::get_polygon_count() const {
  187. return polygons.size();
  188. }
  189. Vector<int> NavigationMesh::get_polygon(int p_idx) {
  190. ERR_FAIL_INDEX_V(p_idx, polygons.size(), Vector<int>());
  191. return polygons[p_idx].indices;
  192. }
  193. void NavigationMesh::clear_polygons() {
  194. polygons.clear();
  195. }
  196. Ref<Mesh> NavigationMesh::get_debug_mesh() {
  197. if (debug_mesh.is_valid())
  198. return debug_mesh;
  199. PoolVector<Vector3> vertices = get_vertices();
  200. PoolVector<Vector3>::Read vr = vertices.read();
  201. List<Face3> faces;
  202. for (int i = 0; i < get_polygon_count(); i++) {
  203. Vector<int> p = get_polygon(i);
  204. for (int j = 2; j < p.size(); j++) {
  205. Face3 f;
  206. f.vertex[0] = vr[p[0]];
  207. f.vertex[1] = vr[p[j - 1]];
  208. f.vertex[2] = vr[p[j]];
  209. faces.push_back(f);
  210. }
  211. }
  212. Map<_EdgeKey, bool> edge_map;
  213. PoolVector<Vector3> tmeshfaces;
  214. tmeshfaces.resize(faces.size() * 3);
  215. {
  216. PoolVector<Vector3>::Write tw = tmeshfaces.write();
  217. int tidx = 0;
  218. for (List<Face3>::Element *E = faces.front(); E; E = E->next()) {
  219. const Face3 &f = E->get();
  220. for (int j = 0; j < 3; j++) {
  221. tw[tidx++] = f.vertex[j];
  222. _EdgeKey ek;
  223. ek.from = f.vertex[j].snapped(Vector3(CMP_EPSILON, CMP_EPSILON, CMP_EPSILON));
  224. ek.to = f.vertex[(j + 1) % 3].snapped(Vector3(CMP_EPSILON, CMP_EPSILON, CMP_EPSILON));
  225. if (ek.from < ek.to)
  226. SWAP(ek.from, ek.to);
  227. Map<_EdgeKey, bool>::Element *E = edge_map.find(ek);
  228. if (E) {
  229. E->get() = false;
  230. } else {
  231. edge_map[ek] = true;
  232. }
  233. }
  234. }
  235. }
  236. List<Vector3> lines;
  237. for (Map<_EdgeKey, bool>::Element *E = edge_map.front(); E; E = E->next()) {
  238. if (E->get()) {
  239. lines.push_back(E->key().from);
  240. lines.push_back(E->key().to);
  241. }
  242. }
  243. PoolVector<Vector3> varr;
  244. varr.resize(lines.size());
  245. {
  246. PoolVector<Vector3>::Write w = varr.write();
  247. int idx = 0;
  248. for (List<Vector3>::Element *E = lines.front(); E; E = E->next()) {
  249. w[idx++] = E->get();
  250. }
  251. }
  252. debug_mesh = Ref<ArrayMesh>(memnew(ArrayMesh));
  253. Array arr;
  254. arr.resize(Mesh::ARRAY_MAX);
  255. arr[Mesh::ARRAY_VERTEX] = varr;
  256. debug_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_LINES, arr);
  257. return debug_mesh;
  258. }
  259. void NavigationMesh::_bind_methods() {
  260. ClassDB::bind_method(D_METHOD("set_sample_partition_type", "sample_partition_type"), &NavigationMesh::set_sample_partition_type);
  261. ClassDB::bind_method(D_METHOD("get_sample_partition_type"), &NavigationMesh::get_sample_partition_type);
  262. ClassDB::bind_method(D_METHOD("set_cell_size", "cell_size"), &NavigationMesh::set_cell_size);
  263. ClassDB::bind_method(D_METHOD("get_cell_size"), &NavigationMesh::get_cell_size);
  264. ClassDB::bind_method(D_METHOD("set_cell_height", "cell_height"), &NavigationMesh::set_cell_height);
  265. ClassDB::bind_method(D_METHOD("get_cell_height"), &NavigationMesh::get_cell_height);
  266. ClassDB::bind_method(D_METHOD("set_agent_height", "agent_height"), &NavigationMesh::set_agent_height);
  267. ClassDB::bind_method(D_METHOD("get_agent_height"), &NavigationMesh::get_agent_height);
  268. ClassDB::bind_method(D_METHOD("set_agent_radius", "agent_radius"), &NavigationMesh::set_agent_radius);
  269. ClassDB::bind_method(D_METHOD("get_agent_radius"), &NavigationMesh::get_agent_radius);
  270. ClassDB::bind_method(D_METHOD("set_agent_max_climb", "agent_max_climb"), &NavigationMesh::set_agent_max_climb);
  271. ClassDB::bind_method(D_METHOD("get_agent_max_climb"), &NavigationMesh::get_agent_max_climb);
  272. ClassDB::bind_method(D_METHOD("set_agent_max_slope", "agent_max_slope"), &NavigationMesh::set_agent_max_slope);
  273. ClassDB::bind_method(D_METHOD("get_agent_max_slope"), &NavigationMesh::get_agent_max_slope);
  274. ClassDB::bind_method(D_METHOD("set_region_min_size", "region_min_size"), &NavigationMesh::set_region_min_size);
  275. ClassDB::bind_method(D_METHOD("get_region_min_size"), &NavigationMesh::get_region_min_size);
  276. ClassDB::bind_method(D_METHOD("set_region_merge_size", "region_merge_size"), &NavigationMesh::set_region_merge_size);
  277. ClassDB::bind_method(D_METHOD("get_region_merge_size"), &NavigationMesh::get_region_merge_size);
  278. ClassDB::bind_method(D_METHOD("set_edge_max_length", "edge_max_length"), &NavigationMesh::set_edge_max_length);
  279. ClassDB::bind_method(D_METHOD("get_edge_max_length"), &NavigationMesh::get_edge_max_length);
  280. ClassDB::bind_method(D_METHOD("set_edge_max_error", "edge_max_error"), &NavigationMesh::set_edge_max_error);
  281. ClassDB::bind_method(D_METHOD("get_edge_max_error"), &NavigationMesh::get_edge_max_error);
  282. ClassDB::bind_method(D_METHOD("set_verts_per_poly", "verts_per_poly"), &NavigationMesh::set_verts_per_poly);
  283. ClassDB::bind_method(D_METHOD("get_verts_per_poly"), &NavigationMesh::get_verts_per_poly);
  284. ClassDB::bind_method(D_METHOD("set_detail_sample_distance", "detail_sample_dist"), &NavigationMesh::set_detail_sample_distance);
  285. ClassDB::bind_method(D_METHOD("get_detail_sample_distance"), &NavigationMesh::get_detail_sample_distance);
  286. ClassDB::bind_method(D_METHOD("set_detail_sample_max_error", "detail_sample_max_error"), &NavigationMesh::set_detail_sample_max_error);
  287. ClassDB::bind_method(D_METHOD("get_detail_sample_max_error"), &NavigationMesh::get_detail_sample_max_error);
  288. ClassDB::bind_method(D_METHOD("set_filter_low_hanging_obstacles", "filter_low_hanging_obstacles"), &NavigationMesh::set_filter_low_hanging_obstacles);
  289. ClassDB::bind_method(D_METHOD("get_filter_low_hanging_obstacles"), &NavigationMesh::get_filter_low_hanging_obstacles);
  290. ClassDB::bind_method(D_METHOD("set_filter_ledge_spans", "filter_ledge_spans"), &NavigationMesh::set_filter_ledge_spans);
  291. ClassDB::bind_method(D_METHOD("get_filter_ledge_spans"), &NavigationMesh::get_filter_ledge_spans);
  292. ClassDB::bind_method(D_METHOD("set_filter_walkable_low_height_spans", "filter_walkable_low_height_spans"), &NavigationMesh::set_filter_walkable_low_height_spans);
  293. ClassDB::bind_method(D_METHOD("get_filter_walkable_low_height_spans"), &NavigationMesh::get_filter_walkable_low_height_spans);
  294. ClassDB::bind_method(D_METHOD("set_vertices", "vertices"), &NavigationMesh::set_vertices);
  295. ClassDB::bind_method(D_METHOD("get_vertices"), &NavigationMesh::get_vertices);
  296. ClassDB::bind_method(D_METHOD("add_polygon", "polygon"), &NavigationMesh::add_polygon);
  297. ClassDB::bind_method(D_METHOD("get_polygon_count"), &NavigationMesh::get_polygon_count);
  298. ClassDB::bind_method(D_METHOD("get_polygon", "idx"), &NavigationMesh::get_polygon);
  299. ClassDB::bind_method(D_METHOD("clear_polygons"), &NavigationMesh::clear_polygons);
  300. ClassDB::bind_method(D_METHOD("create_from_mesh", "mesh"), &NavigationMesh::create_from_mesh);
  301. ClassDB::bind_method(D_METHOD("_set_polygons", "polygons"), &NavigationMesh::_set_polygons);
  302. ClassDB::bind_method(D_METHOD("_get_polygons"), &NavigationMesh::_get_polygons);
  303. BIND_CONSTANT(SAMPLE_PARTITION_WATERSHED);
  304. BIND_CONSTANT(SAMPLE_PARTITION_MONOTONE);
  305. BIND_CONSTANT(SAMPLE_PARTITION_LAYERS);
  306. ADD_PROPERTY(PropertyInfo(Variant::POOL_VECTOR3_ARRAY, "vertices", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL), "set_vertices", "get_vertices");
  307. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "polygons", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL), "_set_polygons", "_get_polygons");
  308. ADD_PROPERTY(PropertyInfo(Variant::INT, "sample_partition_type/sample_partition_type", PROPERTY_HINT_ENUM, "Watershed,Monotone,Layers"), "set_sample_partition_type", "get_sample_partition_type");
  309. ADD_PROPERTY(PropertyInfo(Variant::REAL, "cell/size", PROPERTY_HINT_RANGE, "0.1,1.0,0.01"), "set_cell_size", "get_cell_size");
  310. ADD_PROPERTY(PropertyInfo(Variant::REAL, "cell/height", PROPERTY_HINT_RANGE, "0.1,1.0,0.01"), "set_cell_height", "get_cell_height");
  311. ADD_PROPERTY(PropertyInfo(Variant::REAL, "agent/height", PROPERTY_HINT_RANGE, "0.1,5.0,0.01"), "set_agent_height", "get_agent_height");
  312. ADD_PROPERTY(PropertyInfo(Variant::REAL, "agent/radius", PROPERTY_HINT_RANGE, "0.1,5.0,0.01"), "set_agent_radius", "get_agent_radius");
  313. ADD_PROPERTY(PropertyInfo(Variant::REAL, "agent/max_climb", PROPERTY_HINT_RANGE, "0.1,5.0,0.01"), "set_agent_max_climb", "get_agent_max_climb");
  314. ADD_PROPERTY(PropertyInfo(Variant::REAL, "agent/max_slope", PROPERTY_HINT_RANGE, "0.0,90.0,0.1"), "set_agent_max_slope", "get_agent_max_slope");
  315. ADD_PROPERTY(PropertyInfo(Variant::REAL, "region/min_size", PROPERTY_HINT_RANGE, "0.0,150.0,0.01"), "set_region_min_size", "get_region_min_size");
  316. ADD_PROPERTY(PropertyInfo(Variant::REAL, "region/merge_size", PROPERTY_HINT_RANGE, "0.0,150.0,0.01"), "set_region_merge_size", "get_region_merge_size");
  317. ADD_PROPERTY(PropertyInfo(Variant::REAL, "edge/max_length", PROPERTY_HINT_RANGE, "0.0,50.0,0.01"), "set_edge_max_length", "get_edge_max_length");
  318. ADD_PROPERTY(PropertyInfo(Variant::REAL, "edge/max_error", PROPERTY_HINT_RANGE, "0.1,3.0,0.01"), "set_edge_max_error", "get_edge_max_error");
  319. ADD_PROPERTY(PropertyInfo(Variant::REAL, "polygon/verts_per_poly", PROPERTY_HINT_RANGE, "3.0,12.0,1.0"), "set_verts_per_poly", "get_verts_per_poly");
  320. ADD_PROPERTY(PropertyInfo(Variant::REAL, "detail/sample_distance", PROPERTY_HINT_RANGE, "0.0,16.0,0.01"), "set_detail_sample_distance", "get_detail_sample_distance");
  321. ADD_PROPERTY(PropertyInfo(Variant::REAL, "detail/sample_max_error", PROPERTY_HINT_RANGE, "0.0,16.0,0.01"), "set_detail_sample_max_error", "get_detail_sample_max_error");
  322. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "filter/low_hanging_obstacles"), "set_filter_low_hanging_obstacles", "get_filter_low_hanging_obstacles");
  323. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "filter/ledge_spans"), "set_filter_ledge_spans", "get_filter_ledge_spans");
  324. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "filter/filter_walkable_low_height_spans"), "set_filter_walkable_low_height_spans", "get_filter_walkable_low_height_spans");
  325. }
  326. NavigationMesh::NavigationMesh() {
  327. cell_size = 0.3f;
  328. cell_height = 0.2f;
  329. agent_height = 2.0f;
  330. agent_radius = 0.6f;
  331. agent_max_climb = 0.9f;
  332. agent_max_slope = 45.0f;
  333. region_min_size = 8.0f;
  334. region_merge_size = 20.0f;
  335. edge_max_length = 12.0f;
  336. edge_max_error = 1.3f;
  337. verts_per_poly = 6.0f;
  338. detail_sample_distance = 6.0f;
  339. detail_sample_max_error = 1.0f;
  340. partition_type = SAMPLE_PARTITION_WATERSHED;
  341. filter_low_hanging_obstacles = false;
  342. filter_ledge_spans = false;
  343. filter_walkable_low_height_spans = false;
  344. }
  345. void NavigationMeshInstance::set_enabled(bool p_enabled) {
  346. if (enabled == p_enabled)
  347. return;
  348. enabled = p_enabled;
  349. if (!is_inside_tree())
  350. return;
  351. if (!enabled) {
  352. if (nav_id != -1) {
  353. navigation->navmesh_remove(nav_id);
  354. nav_id = -1;
  355. }
  356. } else {
  357. if (navigation) {
  358. if (navmesh.is_valid()) {
  359. nav_id = navigation->navmesh_add(navmesh, get_relative_transform(navigation), this);
  360. }
  361. }
  362. }
  363. if (debug_view) {
  364. MeshInstance *dm = Object::cast_to<MeshInstance>(debug_view);
  365. if (is_enabled()) {
  366. dm->set_material_override(get_tree()->get_debug_navigation_material());
  367. } else {
  368. dm->set_material_override(get_tree()->get_debug_navigation_disabled_material());
  369. }
  370. }
  371. update_gizmo();
  372. }
  373. bool NavigationMeshInstance::is_enabled() const {
  374. return enabled;
  375. }
  376. /////////////////////////////
  377. void NavigationMeshInstance::_notification(int p_what) {
  378. switch (p_what) {
  379. case NOTIFICATION_ENTER_TREE: {
  380. Spatial *c = this;
  381. while (c) {
  382. navigation = Object::cast_to<Navigation>(c);
  383. if (navigation) {
  384. if (enabled && navmesh.is_valid()) {
  385. nav_id = navigation->navmesh_add(navmesh, get_relative_transform(navigation), this);
  386. }
  387. break;
  388. }
  389. c = c->get_parent_spatial();
  390. }
  391. if (navmesh.is_valid() && get_tree()->is_debugging_navigation_hint()) {
  392. MeshInstance *dm = memnew(MeshInstance);
  393. dm->set_mesh(navmesh->get_debug_mesh());
  394. if (is_enabled()) {
  395. dm->set_material_override(get_tree()->get_debug_navigation_material());
  396. } else {
  397. dm->set_material_override(get_tree()->get_debug_navigation_disabled_material());
  398. }
  399. add_child(dm);
  400. debug_view = dm;
  401. }
  402. } break;
  403. case NOTIFICATION_TRANSFORM_CHANGED: {
  404. if (navigation && nav_id != -1) {
  405. navigation->navmesh_set_transform(nav_id, get_relative_transform(navigation));
  406. }
  407. } break;
  408. case NOTIFICATION_EXIT_TREE: {
  409. if (navigation) {
  410. if (nav_id != -1) {
  411. navigation->navmesh_remove(nav_id);
  412. nav_id = -1;
  413. }
  414. }
  415. if (debug_view) {
  416. debug_view->queue_delete();
  417. debug_view = NULL;
  418. }
  419. navigation = NULL;
  420. } break;
  421. }
  422. }
  423. void NavigationMeshInstance::set_navigation_mesh(const Ref<NavigationMesh> &p_navmesh) {
  424. if (p_navmesh == navmesh)
  425. return;
  426. if (navigation && nav_id != -1) {
  427. navigation->navmesh_remove(nav_id);
  428. nav_id = -1;
  429. }
  430. navmesh = p_navmesh;
  431. if (navigation && navmesh.is_valid() && enabled) {
  432. nav_id = navigation->navmesh_add(navmesh, get_relative_transform(navigation), this);
  433. }
  434. if (debug_view && navmesh.is_valid()) {
  435. Object::cast_to<MeshInstance>(debug_view)->set_mesh(navmesh->get_debug_mesh());
  436. }
  437. update_gizmo();
  438. update_configuration_warning();
  439. }
  440. Ref<NavigationMesh> NavigationMeshInstance::get_navigation_mesh() const {
  441. return navmesh;
  442. }
  443. String NavigationMeshInstance::get_configuration_warning() const {
  444. if (!is_visible_in_tree() || !is_inside_tree())
  445. return String();
  446. if (!navmesh.is_valid()) {
  447. return TTR("A NavigationMesh resource must be set or created for this node to work.");
  448. }
  449. const Spatial *c = this;
  450. while (c) {
  451. if (Object::cast_to<Navigation>(c))
  452. return String();
  453. c = Object::cast_to<Spatial>(c->get_parent());
  454. }
  455. return TTR("NavigationMeshInstance must be a child or grandchild to a Navigation node. It only provides navigation data.");
  456. }
  457. void NavigationMeshInstance::_bind_methods() {
  458. ClassDB::bind_method(D_METHOD("set_navigation_mesh", "navmesh"), &NavigationMeshInstance::set_navigation_mesh);
  459. ClassDB::bind_method(D_METHOD("get_navigation_mesh"), &NavigationMeshInstance::get_navigation_mesh);
  460. ClassDB::bind_method(D_METHOD("set_enabled", "enabled"), &NavigationMeshInstance::set_enabled);
  461. ClassDB::bind_method(D_METHOD("is_enabled"), &NavigationMeshInstance::is_enabled);
  462. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "navmesh", PROPERTY_HINT_RESOURCE_TYPE, "NavigationMesh"), "set_navigation_mesh", "get_navigation_mesh");
  463. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "enabled"), "set_enabled", "is_enabled");
  464. }
  465. NavigationMeshInstance::NavigationMeshInstance() {
  466. debug_view = NULL;
  467. navigation = NULL;
  468. nav_id = -1;
  469. enabled = true;
  470. set_notify_transform(true);
  471. }