navigation_mesh.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  1. /*************************************************************************/
  2. /* navigation_mesh.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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. #ifdef DEBUG_ENABLED
  32. #include "servers/navigation_server_3d.h"
  33. #endif
  34. void NavigationMesh::create_from_mesh(const Ref<Mesh> &p_mesh) {
  35. ERR_FAIL_COND(p_mesh.is_null());
  36. vertices = Vector<Vector3>();
  37. clear_polygons();
  38. for (int i = 0; i < p_mesh->get_surface_count(); i++) {
  39. if (p_mesh->surface_get_primitive_type(i) != Mesh::PRIMITIVE_TRIANGLES) {
  40. WARN_PRINT("A mesh surface was skipped when creating a NavigationMesh due to wrong primitive type in the source mesh. Mesh surface must be made out of triangles.");
  41. continue;
  42. }
  43. Array arr = p_mesh->surface_get_arrays(i);
  44. ERR_CONTINUE(arr.size() != Mesh::ARRAY_MAX);
  45. Vector<Vector3> varr = arr[Mesh::ARRAY_VERTEX];
  46. Vector<int> iarr = arr[Mesh::ARRAY_INDEX];
  47. if (varr.size() == 0 || iarr.size() == 0) {
  48. WARN_PRINT("A mesh surface was skipped when creating a NavigationMesh due to an empty vertex or index array.");
  49. continue;
  50. }
  51. int from = vertices.size();
  52. vertices.append_array(varr);
  53. int rlen = iarr.size();
  54. const int *r = iarr.ptr();
  55. for (int j = 0; j < rlen; j += 3) {
  56. Vector<int> vi;
  57. vi.resize(3);
  58. vi.write[0] = r[j + 0] + from;
  59. vi.write[1] = r[j + 1] + from;
  60. vi.write[2] = r[j + 2] + from;
  61. add_polygon(vi);
  62. }
  63. }
  64. }
  65. void NavigationMesh::set_sample_partition_type(SamplePartitionType p_value) {
  66. ERR_FAIL_INDEX(p_value, SAMPLE_PARTITION_MAX);
  67. partition_type = p_value;
  68. }
  69. NavigationMesh::SamplePartitionType NavigationMesh::get_sample_partition_type() const {
  70. return partition_type;
  71. }
  72. void NavigationMesh::set_parsed_geometry_type(ParsedGeometryType p_value) {
  73. ERR_FAIL_INDEX(p_value, PARSED_GEOMETRY_MAX);
  74. parsed_geometry_type = p_value;
  75. notify_property_list_changed();
  76. }
  77. NavigationMesh::ParsedGeometryType NavigationMesh::get_parsed_geometry_type() const {
  78. return parsed_geometry_type;
  79. }
  80. void NavigationMesh::set_collision_mask(uint32_t p_mask) {
  81. collision_mask = p_mask;
  82. }
  83. uint32_t NavigationMesh::get_collision_mask() const {
  84. return collision_mask;
  85. }
  86. void NavigationMesh::set_collision_mask_value(int p_layer_number, bool p_value) {
  87. ERR_FAIL_COND_MSG(p_layer_number < 1, "Collision layer number must be between 1 and 32 inclusive.");
  88. ERR_FAIL_COND_MSG(p_layer_number > 32, "Collision layer number must be between 1 and 32 inclusive.");
  89. uint32_t mask = get_collision_mask();
  90. if (p_value) {
  91. mask |= 1 << (p_layer_number - 1);
  92. } else {
  93. mask &= ~(1 << (p_layer_number - 1));
  94. }
  95. set_collision_mask(mask);
  96. }
  97. bool NavigationMesh::get_collision_mask_value(int p_layer_number) const {
  98. ERR_FAIL_COND_V_MSG(p_layer_number < 1, false, "Collision layer number must be between 1 and 32 inclusive.");
  99. ERR_FAIL_COND_V_MSG(p_layer_number > 32, false, "Collision layer number must be between 1 and 32 inclusive.");
  100. return get_collision_mask() & (1 << (p_layer_number - 1));
  101. }
  102. void NavigationMesh::set_source_geometry_mode(SourceGeometryMode p_geometry_mode) {
  103. ERR_FAIL_INDEX(p_geometry_mode, SOURCE_GEOMETRY_MAX);
  104. source_geometry_mode = p_geometry_mode;
  105. notify_property_list_changed();
  106. }
  107. NavigationMesh::SourceGeometryMode NavigationMesh::get_source_geometry_mode() const {
  108. return source_geometry_mode;
  109. }
  110. void NavigationMesh::set_source_group_name(StringName p_group_name) {
  111. source_group_name = p_group_name;
  112. }
  113. StringName NavigationMesh::get_source_group_name() const {
  114. return source_group_name;
  115. }
  116. void NavigationMesh::set_cell_size(float p_value) {
  117. ERR_FAIL_COND(p_value <= 0);
  118. cell_size = p_value;
  119. }
  120. float NavigationMesh::get_cell_size() const {
  121. return cell_size;
  122. }
  123. void NavigationMesh::set_cell_height(float p_value) {
  124. ERR_FAIL_COND(p_value <= 0);
  125. cell_height = p_value;
  126. }
  127. float NavigationMesh::get_cell_height() const {
  128. return cell_height;
  129. }
  130. void NavigationMesh::set_agent_height(float p_value) {
  131. ERR_FAIL_COND(p_value < 0);
  132. agent_height = p_value;
  133. }
  134. float NavigationMesh::get_agent_height() const {
  135. return agent_height;
  136. }
  137. void NavigationMesh::set_agent_radius(float p_value) {
  138. ERR_FAIL_COND(p_value < 0);
  139. agent_radius = p_value;
  140. }
  141. float NavigationMesh::get_agent_radius() {
  142. return agent_radius;
  143. }
  144. void NavigationMesh::set_agent_max_climb(float p_value) {
  145. ERR_FAIL_COND(p_value < 0);
  146. agent_max_climb = p_value;
  147. }
  148. float NavigationMesh::get_agent_max_climb() const {
  149. return agent_max_climb;
  150. }
  151. void NavigationMesh::set_agent_max_slope(float p_value) {
  152. ERR_FAIL_COND(p_value < 0 || p_value > 90);
  153. agent_max_slope = p_value;
  154. }
  155. float NavigationMesh::get_agent_max_slope() const {
  156. return agent_max_slope;
  157. }
  158. void NavigationMesh::set_region_min_size(float p_value) {
  159. ERR_FAIL_COND(p_value < 0);
  160. region_min_size = p_value;
  161. }
  162. float NavigationMesh::get_region_min_size() const {
  163. return region_min_size;
  164. }
  165. void NavigationMesh::set_region_merge_size(float p_value) {
  166. ERR_FAIL_COND(p_value < 0);
  167. region_merge_size = p_value;
  168. }
  169. float NavigationMesh::get_region_merge_size() const {
  170. return region_merge_size;
  171. }
  172. void NavigationMesh::set_edge_max_length(float p_value) {
  173. ERR_FAIL_COND(p_value < 0);
  174. edge_max_length = p_value;
  175. }
  176. float NavigationMesh::get_edge_max_length() const {
  177. return edge_max_length;
  178. }
  179. void NavigationMesh::set_edge_max_error(float p_value) {
  180. ERR_FAIL_COND(p_value < 0);
  181. edge_max_error = p_value;
  182. }
  183. float NavigationMesh::get_edge_max_error() const {
  184. return edge_max_error;
  185. }
  186. void NavigationMesh::set_verts_per_poly(float p_value) {
  187. ERR_FAIL_COND(p_value < 3);
  188. verts_per_poly = p_value;
  189. }
  190. float NavigationMesh::get_verts_per_poly() const {
  191. return verts_per_poly;
  192. }
  193. void NavigationMesh::set_detail_sample_distance(float p_value) {
  194. ERR_FAIL_COND(p_value < 0.1);
  195. detail_sample_distance = p_value;
  196. }
  197. float NavigationMesh::get_detail_sample_distance() const {
  198. return detail_sample_distance;
  199. }
  200. void NavigationMesh::set_detail_sample_max_error(float p_value) {
  201. ERR_FAIL_COND(p_value < 0);
  202. detail_sample_max_error = p_value;
  203. }
  204. float NavigationMesh::get_detail_sample_max_error() const {
  205. return detail_sample_max_error;
  206. }
  207. void NavigationMesh::set_filter_low_hanging_obstacles(bool p_value) {
  208. filter_low_hanging_obstacles = p_value;
  209. }
  210. bool NavigationMesh::get_filter_low_hanging_obstacles() const {
  211. return filter_low_hanging_obstacles;
  212. }
  213. void NavigationMesh::set_filter_ledge_spans(bool p_value) {
  214. filter_ledge_spans = p_value;
  215. }
  216. bool NavigationMesh::get_filter_ledge_spans() const {
  217. return filter_ledge_spans;
  218. }
  219. void NavigationMesh::set_filter_walkable_low_height_spans(bool p_value) {
  220. filter_walkable_low_height_spans = p_value;
  221. }
  222. bool NavigationMesh::get_filter_walkable_low_height_spans() const {
  223. return filter_walkable_low_height_spans;
  224. }
  225. void NavigationMesh::set_filter_baking_aabb(const AABB &p_aabb) {
  226. filter_baking_aabb = p_aabb;
  227. notify_property_list_changed();
  228. }
  229. AABB NavigationMesh::get_filter_baking_aabb() const {
  230. return filter_baking_aabb;
  231. }
  232. void NavigationMesh::set_filter_baking_aabb_offset(const Vector3 &p_aabb_offset) {
  233. filter_baking_aabb_offset = p_aabb_offset;
  234. notify_property_list_changed();
  235. }
  236. Vector3 NavigationMesh::get_filter_baking_aabb_offset() const {
  237. return filter_baking_aabb_offset;
  238. }
  239. void NavigationMesh::set_vertices(const Vector<Vector3> &p_vertices) {
  240. vertices = p_vertices;
  241. notify_property_list_changed();
  242. }
  243. Vector<Vector3> NavigationMesh::get_vertices() const {
  244. return vertices;
  245. }
  246. void NavigationMesh::_set_polygons(const Array &p_array) {
  247. polygons.resize(p_array.size());
  248. for (int i = 0; i < p_array.size(); i++) {
  249. polygons.write[i].indices = p_array[i];
  250. }
  251. notify_property_list_changed();
  252. }
  253. Array NavigationMesh::_get_polygons() const {
  254. Array ret;
  255. ret.resize(polygons.size());
  256. for (int i = 0; i < ret.size(); i++) {
  257. ret[i] = polygons[i].indices;
  258. }
  259. return ret;
  260. }
  261. void NavigationMesh::add_polygon(const Vector<int> &p_polygon) {
  262. Polygon polygon;
  263. polygon.indices = p_polygon;
  264. polygons.push_back(polygon);
  265. notify_property_list_changed();
  266. }
  267. int NavigationMesh::get_polygon_count() const {
  268. return polygons.size();
  269. }
  270. Vector<int> NavigationMesh::get_polygon(int p_idx) {
  271. ERR_FAIL_INDEX_V(p_idx, polygons.size(), Vector<int>());
  272. return polygons[p_idx].indices;
  273. }
  274. void NavigationMesh::clear_polygons() {
  275. polygons.clear();
  276. }
  277. #ifndef DISABLE_DEPRECATED
  278. Ref<Mesh> NavigationMesh::get_debug_mesh() {
  279. if (debug_mesh.is_valid()) {
  280. return debug_mesh;
  281. }
  282. Vector<Vector3> vertices = get_vertices();
  283. const Vector3 *vr = vertices.ptr();
  284. List<Face3> faces;
  285. for (int i = 0; i < get_polygon_count(); i++) {
  286. Vector<int> p = get_polygon(i);
  287. for (int j = 2; j < p.size(); j++) {
  288. Face3 f;
  289. f.vertex[0] = vr[p[0]];
  290. f.vertex[1] = vr[p[j - 1]];
  291. f.vertex[2] = vr[p[j]];
  292. faces.push_back(f);
  293. }
  294. }
  295. HashMap<_EdgeKey, bool, _EdgeKey> edge_map;
  296. Vector<Vector3> tmeshfaces;
  297. tmeshfaces.resize(faces.size() * 3);
  298. {
  299. Vector3 *tw = tmeshfaces.ptrw();
  300. int tidx = 0;
  301. for (const Face3 &f : faces) {
  302. for (int j = 0; j < 3; j++) {
  303. tw[tidx++] = f.vertex[j];
  304. _EdgeKey ek;
  305. ek.from = f.vertex[j].snapped(Vector3(CMP_EPSILON, CMP_EPSILON, CMP_EPSILON));
  306. ek.to = f.vertex[(j + 1) % 3].snapped(Vector3(CMP_EPSILON, CMP_EPSILON, CMP_EPSILON));
  307. if (ek.from < ek.to) {
  308. SWAP(ek.from, ek.to);
  309. }
  310. HashMap<_EdgeKey, bool, _EdgeKey>::Iterator F = edge_map.find(ek);
  311. if (F) {
  312. F->value = false;
  313. } else {
  314. edge_map[ek] = true;
  315. }
  316. }
  317. }
  318. }
  319. List<Vector3> lines;
  320. for (const KeyValue<_EdgeKey, bool> &E : edge_map) {
  321. if (E.value) {
  322. lines.push_back(E.key.from);
  323. lines.push_back(E.key.to);
  324. }
  325. }
  326. Vector<Vector3> varr;
  327. varr.resize(lines.size());
  328. {
  329. Vector3 *w = varr.ptrw();
  330. int idx = 0;
  331. for (const Vector3 &E : lines) {
  332. w[idx++] = E;
  333. }
  334. }
  335. debug_mesh = Ref<ArrayMesh>(memnew(ArrayMesh));
  336. if (!lines.size()) {
  337. return debug_mesh;
  338. }
  339. Array arr;
  340. arr.resize(Mesh::ARRAY_MAX);
  341. arr[Mesh::ARRAY_VERTEX] = varr;
  342. debug_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_LINES, arr);
  343. return debug_mesh;
  344. }
  345. #endif // DISABLE_DEPRECATED
  346. #ifdef DEBUG_ENABLED
  347. Ref<ArrayMesh> NavigationMesh::_get_debug_mesh() {
  348. if (debug_mesh.is_valid()) {
  349. // Blocks further updates for now, code below is intended for dynamic updates e.g. when settings change.
  350. return debug_mesh;
  351. }
  352. if (!debug_mesh.is_valid()) {
  353. debug_mesh = Ref<ArrayMesh>(memnew(ArrayMesh));
  354. } else {
  355. debug_mesh->clear_surfaces();
  356. }
  357. if (vertices.size() == 0) {
  358. return debug_mesh;
  359. }
  360. int polygon_count = get_polygon_count();
  361. if (polygon_count < 1) {
  362. // no face, no play
  363. return debug_mesh;
  364. }
  365. // build geometry face surface
  366. Vector<Vector3> face_vertex_array;
  367. face_vertex_array.resize(polygon_count * 3);
  368. for (int i = 0; i < polygon_count; i++) {
  369. Vector<int> polygon = get_polygon(i);
  370. face_vertex_array.push_back(vertices[polygon[0]]);
  371. face_vertex_array.push_back(vertices[polygon[1]]);
  372. face_vertex_array.push_back(vertices[polygon[2]]);
  373. }
  374. Array face_mesh_array;
  375. face_mesh_array.resize(Mesh::ARRAY_MAX);
  376. face_mesh_array[Mesh::ARRAY_VERTEX] = face_vertex_array;
  377. // if enabled add vertex colors to colorize each face individually
  378. bool enabled_geometry_face_random_color = NavigationServer3D::get_singleton()->get_debug_navigation_enable_geometry_face_random_color();
  379. if (enabled_geometry_face_random_color) {
  380. Color debug_navigation_geometry_face_color = NavigationServer3D::get_singleton()->get_debug_navigation_geometry_face_color();
  381. Color polygon_color = debug_navigation_geometry_face_color;
  382. Vector<Color> face_color_array;
  383. face_color_array.resize(polygon_count * 3);
  384. for (int i = 0; i < polygon_count; i++) {
  385. polygon_color = debug_navigation_geometry_face_color * (Color(Math::randf(), Math::randf(), Math::randf()));
  386. Vector<int> polygon = get_polygon(i);
  387. face_color_array.push_back(polygon_color);
  388. face_color_array.push_back(polygon_color);
  389. face_color_array.push_back(polygon_color);
  390. }
  391. face_mesh_array[Mesh::ARRAY_COLOR] = face_color_array;
  392. }
  393. debug_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, face_mesh_array);
  394. Ref<StandardMaterial3D> debug_geometry_face_material = NavigationServer3D::get_singleton_mut()->get_debug_navigation_geometry_face_material();
  395. debug_mesh->surface_set_material(debug_mesh->get_surface_count(), debug_geometry_face_material);
  396. // if enabled build geometry edge line surface
  397. bool enabled_edge_lines = NavigationServer3D::get_singleton()->get_debug_navigation_enable_edge_lines();
  398. if (enabled_edge_lines) {
  399. Vector<Vector3> line_vertex_array;
  400. line_vertex_array.resize(polygon_count * 6);
  401. for (int i = 0; i < polygon_count; i++) {
  402. Vector<int> polygon = get_polygon(i);
  403. line_vertex_array.push_back(vertices[polygon[0]]);
  404. line_vertex_array.push_back(vertices[polygon[1]]);
  405. line_vertex_array.push_back(vertices[polygon[1]]);
  406. line_vertex_array.push_back(vertices[polygon[2]]);
  407. line_vertex_array.push_back(vertices[polygon[2]]);
  408. line_vertex_array.push_back(vertices[polygon[0]]);
  409. }
  410. Array line_mesh_array;
  411. line_mesh_array.resize(Mesh::ARRAY_MAX);
  412. line_mesh_array[Mesh::ARRAY_VERTEX] = line_vertex_array;
  413. debug_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_LINES, line_mesh_array);
  414. Ref<StandardMaterial3D> debug_geometry_edge_material = NavigationServer3D::get_singleton_mut()->get_debug_navigation_geometry_edge_material();
  415. debug_mesh->surface_set_material(debug_mesh->get_surface_count(), debug_geometry_edge_material);
  416. }
  417. return debug_mesh;
  418. }
  419. #endif
  420. void NavigationMesh::_bind_methods() {
  421. ClassDB::bind_method(D_METHOD("set_sample_partition_type", "sample_partition_type"), &NavigationMesh::set_sample_partition_type);
  422. ClassDB::bind_method(D_METHOD("get_sample_partition_type"), &NavigationMesh::get_sample_partition_type);
  423. ClassDB::bind_method(D_METHOD("set_parsed_geometry_type", "geometry_type"), &NavigationMesh::set_parsed_geometry_type);
  424. ClassDB::bind_method(D_METHOD("get_parsed_geometry_type"), &NavigationMesh::get_parsed_geometry_type);
  425. ClassDB::bind_method(D_METHOD("set_collision_mask", "mask"), &NavigationMesh::set_collision_mask);
  426. ClassDB::bind_method(D_METHOD("get_collision_mask"), &NavigationMesh::get_collision_mask);
  427. ClassDB::bind_method(D_METHOD("set_collision_mask_value", "layer_number", "value"), &NavigationMesh::set_collision_mask_value);
  428. ClassDB::bind_method(D_METHOD("get_collision_mask_value", "layer_number"), &NavigationMesh::get_collision_mask_value);
  429. ClassDB::bind_method(D_METHOD("set_source_geometry_mode", "mask"), &NavigationMesh::set_source_geometry_mode);
  430. ClassDB::bind_method(D_METHOD("get_source_geometry_mode"), &NavigationMesh::get_source_geometry_mode);
  431. ClassDB::bind_method(D_METHOD("set_source_group_name", "mask"), &NavigationMesh::set_source_group_name);
  432. ClassDB::bind_method(D_METHOD("get_source_group_name"), &NavigationMesh::get_source_group_name);
  433. ClassDB::bind_method(D_METHOD("set_cell_size", "cell_size"), &NavigationMesh::set_cell_size);
  434. ClassDB::bind_method(D_METHOD("get_cell_size"), &NavigationMesh::get_cell_size);
  435. ClassDB::bind_method(D_METHOD("set_cell_height", "cell_height"), &NavigationMesh::set_cell_height);
  436. ClassDB::bind_method(D_METHOD("get_cell_height"), &NavigationMesh::get_cell_height);
  437. ClassDB::bind_method(D_METHOD("set_agent_height", "agent_height"), &NavigationMesh::set_agent_height);
  438. ClassDB::bind_method(D_METHOD("get_agent_height"), &NavigationMesh::get_agent_height);
  439. ClassDB::bind_method(D_METHOD("set_agent_radius", "agent_radius"), &NavigationMesh::set_agent_radius);
  440. ClassDB::bind_method(D_METHOD("get_agent_radius"), &NavigationMesh::get_agent_radius);
  441. ClassDB::bind_method(D_METHOD("set_agent_max_climb", "agent_max_climb"), &NavigationMesh::set_agent_max_climb);
  442. ClassDB::bind_method(D_METHOD("get_agent_max_climb"), &NavigationMesh::get_agent_max_climb);
  443. ClassDB::bind_method(D_METHOD("set_agent_max_slope", "agent_max_slope"), &NavigationMesh::set_agent_max_slope);
  444. ClassDB::bind_method(D_METHOD("get_agent_max_slope"), &NavigationMesh::get_agent_max_slope);
  445. ClassDB::bind_method(D_METHOD("set_region_min_size", "region_min_size"), &NavigationMesh::set_region_min_size);
  446. ClassDB::bind_method(D_METHOD("get_region_min_size"), &NavigationMesh::get_region_min_size);
  447. ClassDB::bind_method(D_METHOD("set_region_merge_size", "region_merge_size"), &NavigationMesh::set_region_merge_size);
  448. ClassDB::bind_method(D_METHOD("get_region_merge_size"), &NavigationMesh::get_region_merge_size);
  449. ClassDB::bind_method(D_METHOD("set_edge_max_length", "edge_max_length"), &NavigationMesh::set_edge_max_length);
  450. ClassDB::bind_method(D_METHOD("get_edge_max_length"), &NavigationMesh::get_edge_max_length);
  451. ClassDB::bind_method(D_METHOD("set_edge_max_error", "edge_max_error"), &NavigationMesh::set_edge_max_error);
  452. ClassDB::bind_method(D_METHOD("get_edge_max_error"), &NavigationMesh::get_edge_max_error);
  453. ClassDB::bind_method(D_METHOD("set_verts_per_poly", "verts_per_poly"), &NavigationMesh::set_verts_per_poly);
  454. ClassDB::bind_method(D_METHOD("get_verts_per_poly"), &NavigationMesh::get_verts_per_poly);
  455. ClassDB::bind_method(D_METHOD("set_detail_sample_distance", "detail_sample_dist"), &NavigationMesh::set_detail_sample_distance);
  456. ClassDB::bind_method(D_METHOD("get_detail_sample_distance"), &NavigationMesh::get_detail_sample_distance);
  457. ClassDB::bind_method(D_METHOD("set_detail_sample_max_error", "detail_sample_max_error"), &NavigationMesh::set_detail_sample_max_error);
  458. ClassDB::bind_method(D_METHOD("get_detail_sample_max_error"), &NavigationMesh::get_detail_sample_max_error);
  459. ClassDB::bind_method(D_METHOD("set_filter_low_hanging_obstacles", "filter_low_hanging_obstacles"), &NavigationMesh::set_filter_low_hanging_obstacles);
  460. ClassDB::bind_method(D_METHOD("get_filter_low_hanging_obstacles"), &NavigationMesh::get_filter_low_hanging_obstacles);
  461. ClassDB::bind_method(D_METHOD("set_filter_ledge_spans", "filter_ledge_spans"), &NavigationMesh::set_filter_ledge_spans);
  462. ClassDB::bind_method(D_METHOD("get_filter_ledge_spans"), &NavigationMesh::get_filter_ledge_spans);
  463. ClassDB::bind_method(D_METHOD("set_filter_walkable_low_height_spans", "filter_walkable_low_height_spans"), &NavigationMesh::set_filter_walkable_low_height_spans);
  464. ClassDB::bind_method(D_METHOD("get_filter_walkable_low_height_spans"), &NavigationMesh::get_filter_walkable_low_height_spans);
  465. ClassDB::bind_method(D_METHOD("set_filter_baking_aabb", "baking_aabb"), &NavigationMesh::set_filter_baking_aabb);
  466. ClassDB::bind_method(D_METHOD("get_filter_baking_aabb"), &NavigationMesh::get_filter_baking_aabb);
  467. ClassDB::bind_method(D_METHOD("set_filter_baking_aabb_offset", "baking_aabb_offset"), &NavigationMesh::set_filter_baking_aabb_offset);
  468. ClassDB::bind_method(D_METHOD("get_filter_baking_aabb_offset"), &NavigationMesh::get_filter_baking_aabb_offset);
  469. ClassDB::bind_method(D_METHOD("set_vertices", "vertices"), &NavigationMesh::set_vertices);
  470. ClassDB::bind_method(D_METHOD("get_vertices"), &NavigationMesh::get_vertices);
  471. ClassDB::bind_method(D_METHOD("add_polygon", "polygon"), &NavigationMesh::add_polygon);
  472. ClassDB::bind_method(D_METHOD("get_polygon_count"), &NavigationMesh::get_polygon_count);
  473. ClassDB::bind_method(D_METHOD("get_polygon", "idx"), &NavigationMesh::get_polygon);
  474. ClassDB::bind_method(D_METHOD("clear_polygons"), &NavigationMesh::clear_polygons);
  475. ClassDB::bind_method(D_METHOD("create_from_mesh", "mesh"), &NavigationMesh::create_from_mesh);
  476. ClassDB::bind_method(D_METHOD("_set_polygons", "polygons"), &NavigationMesh::_set_polygons);
  477. ClassDB::bind_method(D_METHOD("_get_polygons"), &NavigationMesh::_get_polygons);
  478. ADD_PROPERTY(PropertyInfo(Variant::PACKED_VECTOR3_ARRAY, "vertices", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "set_vertices", "get_vertices");
  479. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "polygons", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "_set_polygons", "_get_polygons");
  480. ADD_GROUP("Sampling", "sample_");
  481. ADD_PROPERTY(PropertyInfo(Variant::INT, "sample_partition_type", PROPERTY_HINT_ENUM, "Watershed,Monotone,Layers"), "set_sample_partition_type", "get_sample_partition_type");
  482. ADD_GROUP("Geometry", "geometry_");
  483. ADD_PROPERTY(PropertyInfo(Variant::INT, "geometry_parsed_geometry_type", PROPERTY_HINT_ENUM, "Mesh Instances,Static Colliders,Both"), "set_parsed_geometry_type", "get_parsed_geometry_type");
  484. ADD_PROPERTY(PropertyInfo(Variant::INT, "geometry_collision_mask", PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_collision_mask", "get_collision_mask");
  485. ADD_PROPERTY_DEFAULT("geometry_collision_mask", 0xFFFFFFFF);
  486. ADD_PROPERTY(PropertyInfo(Variant::INT, "geometry_source_geometry_mode", PROPERTY_HINT_ENUM, "NavMesh Children, Group With Children, Group Explicit"), "set_source_geometry_mode", "get_source_geometry_mode");
  487. ADD_PROPERTY(PropertyInfo(Variant::STRING, "geometry_source_group_name"), "set_source_group_name", "get_source_group_name");
  488. ADD_PROPERTY_DEFAULT("geometry_source_group_name", StringName("navmesh"));
  489. ADD_GROUP("Cells", "cell_");
  490. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "cell_size", PROPERTY_HINT_RANGE, "0.01,500.0,0.01,or_greater,suffix:m"), "set_cell_size", "get_cell_size");
  491. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "cell_height", PROPERTY_HINT_RANGE, "0.01,500.0,0.01,or_greater,suffix:m"), "set_cell_height", "get_cell_height");
  492. ADD_GROUP("Agents", "agent_");
  493. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "agent_height", PROPERTY_HINT_RANGE, "0.0,500.0,0.01,or_greater,suffix:m"), "set_agent_height", "get_agent_height");
  494. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "agent_radius", PROPERTY_HINT_RANGE, "0.0,500.0,0.01,or_greater,suffix:m"), "set_agent_radius", "get_agent_radius");
  495. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "agent_max_climb", PROPERTY_HINT_RANGE, "0.0,500.0,0.01,or_greater,suffix:m"), "set_agent_max_climb", "get_agent_max_climb");
  496. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "agent_max_slope", PROPERTY_HINT_RANGE, "0.02,90.0,0.01,degrees"), "set_agent_max_slope", "get_agent_max_slope");
  497. ADD_GROUP("Regions", "region_");
  498. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "region_min_size", PROPERTY_HINT_RANGE, "0.0,150.0,0.01,or_greater"), "set_region_min_size", "get_region_min_size");
  499. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "region_merge_size", PROPERTY_HINT_RANGE, "0.0,150.0,0.01,or_greater"), "set_region_merge_size", "get_region_merge_size");
  500. ADD_GROUP("Edges", "edge_");
  501. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "edge_max_length", PROPERTY_HINT_RANGE, "0.0,50.0,0.01,or_greater,suffix:m"), "set_edge_max_length", "get_edge_max_length");
  502. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "edge_max_error", PROPERTY_HINT_RANGE, "0.1,3.0,0.01,or_greater,suffix:m"), "set_edge_max_error", "get_edge_max_error");
  503. ADD_GROUP("Polygons", "polygon_");
  504. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "polygon_verts_per_poly", PROPERTY_HINT_RANGE, "3.0,12.0,1.0,or_greater"), "set_verts_per_poly", "get_verts_per_poly");
  505. ADD_GROUP("Details", "detail_");
  506. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "detail_sample_distance", PROPERTY_HINT_RANGE, "0.1,16.0,0.01,or_greater,suffix:m"), "set_detail_sample_distance", "get_detail_sample_distance");
  507. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "detail_sample_max_error", PROPERTY_HINT_RANGE, "0.0,16.0,0.01,or_greater,suffix:m"), "set_detail_sample_max_error", "get_detail_sample_max_error");
  508. ADD_GROUP("Filters", "filter_");
  509. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "filter_low_hanging_obstacles"), "set_filter_low_hanging_obstacles", "get_filter_low_hanging_obstacles");
  510. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "filter_ledge_spans"), "set_filter_ledge_spans", "get_filter_ledge_spans");
  511. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "filter_walkable_low_height_spans"), "set_filter_walkable_low_height_spans", "get_filter_walkable_low_height_spans");
  512. ADD_PROPERTY(PropertyInfo(Variant::AABB, "filter_baking_aabb"), "set_filter_baking_aabb", "get_filter_baking_aabb");
  513. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "filter_baking_aabb_offset"), "set_filter_baking_aabb_offset", "get_filter_baking_aabb_offset");
  514. BIND_ENUM_CONSTANT(SAMPLE_PARTITION_WATERSHED);
  515. BIND_ENUM_CONSTANT(SAMPLE_PARTITION_MONOTONE);
  516. BIND_ENUM_CONSTANT(SAMPLE_PARTITION_LAYERS);
  517. BIND_ENUM_CONSTANT(SAMPLE_PARTITION_MAX);
  518. BIND_ENUM_CONSTANT(PARSED_GEOMETRY_MESH_INSTANCES);
  519. BIND_ENUM_CONSTANT(PARSED_GEOMETRY_STATIC_COLLIDERS);
  520. BIND_ENUM_CONSTANT(PARSED_GEOMETRY_BOTH);
  521. BIND_ENUM_CONSTANT(PARSED_GEOMETRY_MAX);
  522. BIND_ENUM_CONSTANT(SOURCE_GEOMETRY_NAVMESH_CHILDREN);
  523. BIND_ENUM_CONSTANT(SOURCE_GEOMETRY_GROUPS_WITH_CHILDREN);
  524. BIND_ENUM_CONSTANT(SOURCE_GEOMETRY_GROUPS_EXPLICIT);
  525. BIND_ENUM_CONSTANT(SOURCE_GEOMETRY_MAX);
  526. }
  527. void NavigationMesh::_validate_property(PropertyInfo &p_property) const {
  528. if (p_property.name == "geometry_collision_mask") {
  529. if (parsed_geometry_type == PARSED_GEOMETRY_MESH_INSTANCES) {
  530. p_property.usage = PROPERTY_USAGE_NONE;
  531. return;
  532. }
  533. }
  534. if (p_property.name == "geometry_source_group_name") {
  535. if (source_geometry_mode == SOURCE_GEOMETRY_NAVMESH_CHILDREN) {
  536. p_property.usage = PROPERTY_USAGE_NONE;
  537. return;
  538. }
  539. }
  540. }
  541. #ifndef DISABLE_DEPRECATED
  542. bool NavigationMesh::_set(const StringName &p_name, const Variant &p_value) {
  543. String name = p_name;
  544. if (name.find("/") != -1) {
  545. // Compatibility with pre-3.5 "category/path" property names.
  546. name = name.replace("/", "_");
  547. if (name == "sample_partition_type_sample_partition_type") {
  548. set("sample_partition_type", p_value);
  549. } else if (name == "filter_filter_walkable_low_height_spans") {
  550. set("filter_walkable_low_height_spans", p_value);
  551. } else {
  552. set(name, p_value);
  553. }
  554. return true;
  555. }
  556. return false;
  557. }
  558. bool NavigationMesh::_get(const StringName &p_name, Variant &r_ret) const {
  559. String name = p_name;
  560. if (name.find("/") != -1) {
  561. // Compatibility with pre-3.5 "category/path" property names.
  562. name = name.replace("/", "_");
  563. if (name == "sample_partition_type_sample_partition_type") {
  564. r_ret = get("sample_partition_type");
  565. } else if (name == "filter_filter_walkable_low_height_spans") {
  566. r_ret = get("filter_walkable_low_height_spans");
  567. } else {
  568. r_ret = get(name);
  569. }
  570. return true;
  571. }
  572. return false;
  573. }
  574. #endif // DISABLE_DEPRECATED
  575. NavigationMesh::NavigationMesh() {}