navigation_mesh.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  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. void NavigationMesh::create_from_mesh(const Ref<Mesh> &p_mesh) {
  32. ERR_FAIL_COND(p_mesh.is_null());
  33. vertices = Vector<Vector3>();
  34. clear_polygons();
  35. for (int i = 0; i < p_mesh->get_surface_count(); i++) {
  36. if (p_mesh->surface_get_primitive_type(i) != Mesh::PRIMITIVE_TRIANGLES) {
  37. 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.");
  38. continue;
  39. }
  40. Array arr = p_mesh->surface_get_arrays(i);
  41. ERR_CONTINUE(arr.size() != Mesh::ARRAY_MAX);
  42. Vector<Vector3> varr = arr[Mesh::ARRAY_VERTEX];
  43. Vector<int> iarr = arr[Mesh::ARRAY_INDEX];
  44. if (varr.size() == 0 || iarr.size() == 0) {
  45. WARN_PRINT("A mesh surface was skipped when creating a NavigationMesh due to an empty vertex or index array.");
  46. continue;
  47. }
  48. int from = vertices.size();
  49. vertices.append_array(varr);
  50. int rlen = iarr.size();
  51. const int *r = iarr.ptr();
  52. for (int j = 0; j < rlen; j += 3) {
  53. Vector<int> vi;
  54. vi.resize(3);
  55. vi.write[0] = r[j + 0] + from;
  56. vi.write[1] = r[j + 1] + from;
  57. vi.write[2] = r[j + 2] + from;
  58. add_polygon(vi);
  59. }
  60. }
  61. }
  62. void NavigationMesh::set_sample_partition_type(SamplePartitionType p_value) {
  63. ERR_FAIL_INDEX(p_value, SAMPLE_PARTITION_MAX);
  64. partition_type = p_value;
  65. }
  66. NavigationMesh::SamplePartitionType NavigationMesh::get_sample_partition_type() const {
  67. return partition_type;
  68. }
  69. void NavigationMesh::set_parsed_geometry_type(ParsedGeometryType p_value) {
  70. ERR_FAIL_INDEX(p_value, PARSED_GEOMETRY_MAX);
  71. parsed_geometry_type = p_value;
  72. notify_property_list_changed();
  73. }
  74. NavigationMesh::ParsedGeometryType NavigationMesh::get_parsed_geometry_type() const {
  75. return parsed_geometry_type;
  76. }
  77. void NavigationMesh::set_collision_mask(uint32_t p_mask) {
  78. collision_mask = p_mask;
  79. }
  80. uint32_t NavigationMesh::get_collision_mask() const {
  81. return collision_mask;
  82. }
  83. void NavigationMesh::set_collision_mask_value(int p_layer_number, bool p_value) {
  84. ERR_FAIL_COND_MSG(p_layer_number < 1, "Collision layer number must be between 1 and 32 inclusive.");
  85. ERR_FAIL_COND_MSG(p_layer_number > 32, "Collision layer number must be between 1 and 32 inclusive.");
  86. uint32_t mask = get_collision_mask();
  87. if (p_value) {
  88. mask |= 1 << (p_layer_number - 1);
  89. } else {
  90. mask &= ~(1 << (p_layer_number - 1));
  91. }
  92. set_collision_mask(mask);
  93. }
  94. bool NavigationMesh::get_collision_mask_value(int p_layer_number) const {
  95. ERR_FAIL_COND_V_MSG(p_layer_number < 1, false, "Collision layer number must be between 1 and 32 inclusive.");
  96. ERR_FAIL_COND_V_MSG(p_layer_number > 32, false, "Collision layer number must be between 1 and 32 inclusive.");
  97. return get_collision_mask() & (1 << (p_layer_number - 1));
  98. }
  99. void NavigationMesh::set_source_geometry_mode(SourceGeometryMode p_geometry_mode) {
  100. ERR_FAIL_INDEX(p_geometry_mode, SOURCE_GEOMETRY_MAX);
  101. source_geometry_mode = p_geometry_mode;
  102. notify_property_list_changed();
  103. }
  104. NavigationMesh::SourceGeometryMode NavigationMesh::get_source_geometry_mode() const {
  105. return source_geometry_mode;
  106. }
  107. void NavigationMesh::set_source_group_name(StringName p_group_name) {
  108. source_group_name = p_group_name;
  109. }
  110. StringName NavigationMesh::get_source_group_name() const {
  111. return source_group_name;
  112. }
  113. void NavigationMesh::set_cell_size(float p_value) {
  114. ERR_FAIL_COND(p_value <= 0);
  115. cell_size = p_value;
  116. }
  117. float NavigationMesh::get_cell_size() const {
  118. return cell_size;
  119. }
  120. void NavigationMesh::set_cell_height(float p_value) {
  121. ERR_FAIL_COND(p_value <= 0);
  122. cell_height = p_value;
  123. }
  124. float NavigationMesh::get_cell_height() const {
  125. return cell_height;
  126. }
  127. void NavigationMesh::set_agent_height(float p_value) {
  128. ERR_FAIL_COND(p_value < 0);
  129. agent_height = p_value;
  130. }
  131. float NavigationMesh::get_agent_height() const {
  132. return agent_height;
  133. }
  134. void NavigationMesh::set_agent_radius(float p_value) {
  135. ERR_FAIL_COND(p_value < 0);
  136. agent_radius = p_value;
  137. }
  138. float NavigationMesh::get_agent_radius() {
  139. return agent_radius;
  140. }
  141. void NavigationMesh::set_agent_max_climb(float p_value) {
  142. ERR_FAIL_COND(p_value < 0);
  143. agent_max_climb = p_value;
  144. }
  145. float NavigationMesh::get_agent_max_climb() const {
  146. return agent_max_climb;
  147. }
  148. void NavigationMesh::set_agent_max_slope(float p_value) {
  149. ERR_FAIL_COND(p_value < 0 || p_value > 90);
  150. agent_max_slope = p_value;
  151. }
  152. float NavigationMesh::get_agent_max_slope() const {
  153. return agent_max_slope;
  154. }
  155. void NavigationMesh::set_region_min_size(float p_value) {
  156. ERR_FAIL_COND(p_value < 0);
  157. region_min_size = p_value;
  158. }
  159. float NavigationMesh::get_region_min_size() const {
  160. return region_min_size;
  161. }
  162. void NavigationMesh::set_region_merge_size(float p_value) {
  163. ERR_FAIL_COND(p_value < 0);
  164. region_merge_size = p_value;
  165. }
  166. float NavigationMesh::get_region_merge_size() const {
  167. return region_merge_size;
  168. }
  169. void NavigationMesh::set_edge_max_length(float p_value) {
  170. ERR_FAIL_COND(p_value < 0);
  171. edge_max_length = p_value;
  172. }
  173. float NavigationMesh::get_edge_max_length() const {
  174. return edge_max_length;
  175. }
  176. void NavigationMesh::set_edge_max_error(float p_value) {
  177. ERR_FAIL_COND(p_value < 0);
  178. edge_max_error = p_value;
  179. }
  180. float NavigationMesh::get_edge_max_error() const {
  181. return edge_max_error;
  182. }
  183. void NavigationMesh::set_verts_per_poly(float p_value) {
  184. ERR_FAIL_COND(p_value < 3);
  185. verts_per_poly = p_value;
  186. }
  187. float NavigationMesh::get_verts_per_poly() const {
  188. return verts_per_poly;
  189. }
  190. void NavigationMesh::set_detail_sample_distance(float p_value) {
  191. ERR_FAIL_COND(p_value < 0.1);
  192. detail_sample_distance = p_value;
  193. }
  194. float NavigationMesh::get_detail_sample_distance() const {
  195. return detail_sample_distance;
  196. }
  197. void NavigationMesh::set_detail_sample_max_error(float p_value) {
  198. ERR_FAIL_COND(p_value < 0);
  199. detail_sample_max_error = p_value;
  200. }
  201. float NavigationMesh::get_detail_sample_max_error() const {
  202. return detail_sample_max_error;
  203. }
  204. void NavigationMesh::set_filter_low_hanging_obstacles(bool p_value) {
  205. filter_low_hanging_obstacles = p_value;
  206. }
  207. bool NavigationMesh::get_filter_low_hanging_obstacles() const {
  208. return filter_low_hanging_obstacles;
  209. }
  210. void NavigationMesh::set_filter_ledge_spans(bool p_value) {
  211. filter_ledge_spans = p_value;
  212. }
  213. bool NavigationMesh::get_filter_ledge_spans() const {
  214. return filter_ledge_spans;
  215. }
  216. void NavigationMesh::set_filter_walkable_low_height_spans(bool p_value) {
  217. filter_walkable_low_height_spans = p_value;
  218. }
  219. bool NavigationMesh::get_filter_walkable_low_height_spans() const {
  220. return filter_walkable_low_height_spans;
  221. }
  222. void NavigationMesh::set_vertices(const Vector<Vector3> &p_vertices) {
  223. vertices = p_vertices;
  224. notify_property_list_changed();
  225. }
  226. Vector<Vector3> NavigationMesh::get_vertices() const {
  227. return vertices;
  228. }
  229. void NavigationMesh::_set_polygons(const Array &p_array) {
  230. polygons.resize(p_array.size());
  231. for (int i = 0; i < p_array.size(); i++) {
  232. polygons.write[i].indices = p_array[i];
  233. }
  234. notify_property_list_changed();
  235. }
  236. Array NavigationMesh::_get_polygons() const {
  237. Array ret;
  238. ret.resize(polygons.size());
  239. for (int i = 0; i < ret.size(); i++) {
  240. ret[i] = polygons[i].indices;
  241. }
  242. return ret;
  243. }
  244. void NavigationMesh::add_polygon(const Vector<int> &p_polygon) {
  245. Polygon polygon;
  246. polygon.indices = p_polygon;
  247. polygons.push_back(polygon);
  248. notify_property_list_changed();
  249. }
  250. int NavigationMesh::get_polygon_count() const {
  251. return polygons.size();
  252. }
  253. Vector<int> NavigationMesh::get_polygon(int p_idx) {
  254. ERR_FAIL_INDEX_V(p_idx, polygons.size(), Vector<int>());
  255. return polygons[p_idx].indices;
  256. }
  257. void NavigationMesh::clear_polygons() {
  258. polygons.clear();
  259. }
  260. Ref<Mesh> NavigationMesh::get_debug_mesh() {
  261. if (debug_mesh.is_valid()) {
  262. return debug_mesh;
  263. }
  264. Vector<Vector3> vertices = get_vertices();
  265. const Vector3 *vr = vertices.ptr();
  266. List<Face3> faces;
  267. for (int i = 0; i < get_polygon_count(); i++) {
  268. Vector<int> p = get_polygon(i);
  269. for (int j = 2; j < p.size(); j++) {
  270. Face3 f;
  271. f.vertex[0] = vr[p[0]];
  272. f.vertex[1] = vr[p[j - 1]];
  273. f.vertex[2] = vr[p[j]];
  274. faces.push_back(f);
  275. }
  276. }
  277. HashMap<_EdgeKey, bool, _EdgeKey> edge_map;
  278. Vector<Vector3> tmeshfaces;
  279. tmeshfaces.resize(faces.size() * 3);
  280. {
  281. Vector3 *tw = tmeshfaces.ptrw();
  282. int tidx = 0;
  283. for (const Face3 &f : faces) {
  284. for (int j = 0; j < 3; j++) {
  285. tw[tidx++] = f.vertex[j];
  286. _EdgeKey ek;
  287. ek.from = f.vertex[j].snapped(Vector3(CMP_EPSILON, CMP_EPSILON, CMP_EPSILON));
  288. ek.to = f.vertex[(j + 1) % 3].snapped(Vector3(CMP_EPSILON, CMP_EPSILON, CMP_EPSILON));
  289. if (ek.from < ek.to) {
  290. SWAP(ek.from, ek.to);
  291. }
  292. HashMap<_EdgeKey, bool, _EdgeKey>::Iterator F = edge_map.find(ek);
  293. if (F) {
  294. F->value = false;
  295. } else {
  296. edge_map[ek] = true;
  297. }
  298. }
  299. }
  300. }
  301. List<Vector3> lines;
  302. for (const KeyValue<_EdgeKey, bool> &E : edge_map) {
  303. if (E.value) {
  304. lines.push_back(E.key.from);
  305. lines.push_back(E.key.to);
  306. }
  307. }
  308. Vector<Vector3> varr;
  309. varr.resize(lines.size());
  310. {
  311. Vector3 *w = varr.ptrw();
  312. int idx = 0;
  313. for (const Vector3 &E : lines) {
  314. w[idx++] = E;
  315. }
  316. }
  317. debug_mesh = Ref<ArrayMesh>(memnew(ArrayMesh));
  318. if (!lines.size()) {
  319. return debug_mesh;
  320. }
  321. Array arr;
  322. arr.resize(Mesh::ARRAY_MAX);
  323. arr[Mesh::ARRAY_VERTEX] = varr;
  324. debug_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_LINES, arr);
  325. return debug_mesh;
  326. }
  327. void NavigationMesh::_bind_methods() {
  328. ClassDB::bind_method(D_METHOD("set_sample_partition_type", "sample_partition_type"), &NavigationMesh::set_sample_partition_type);
  329. ClassDB::bind_method(D_METHOD("get_sample_partition_type"), &NavigationMesh::get_sample_partition_type);
  330. ClassDB::bind_method(D_METHOD("set_parsed_geometry_type", "geometry_type"), &NavigationMesh::set_parsed_geometry_type);
  331. ClassDB::bind_method(D_METHOD("get_parsed_geometry_type"), &NavigationMesh::get_parsed_geometry_type);
  332. ClassDB::bind_method(D_METHOD("set_collision_mask", "mask"), &NavigationMesh::set_collision_mask);
  333. ClassDB::bind_method(D_METHOD("get_collision_mask"), &NavigationMesh::get_collision_mask);
  334. ClassDB::bind_method(D_METHOD("set_collision_mask_value", "layer_number", "value"), &NavigationMesh::set_collision_mask_value);
  335. ClassDB::bind_method(D_METHOD("get_collision_mask_value", "layer_number"), &NavigationMesh::get_collision_mask_value);
  336. ClassDB::bind_method(D_METHOD("set_source_geometry_mode", "mask"), &NavigationMesh::set_source_geometry_mode);
  337. ClassDB::bind_method(D_METHOD("get_source_geometry_mode"), &NavigationMesh::get_source_geometry_mode);
  338. ClassDB::bind_method(D_METHOD("set_source_group_name", "mask"), &NavigationMesh::set_source_group_name);
  339. ClassDB::bind_method(D_METHOD("get_source_group_name"), &NavigationMesh::get_source_group_name);
  340. ClassDB::bind_method(D_METHOD("set_cell_size", "cell_size"), &NavigationMesh::set_cell_size);
  341. ClassDB::bind_method(D_METHOD("get_cell_size"), &NavigationMesh::get_cell_size);
  342. ClassDB::bind_method(D_METHOD("set_cell_height", "cell_height"), &NavigationMesh::set_cell_height);
  343. ClassDB::bind_method(D_METHOD("get_cell_height"), &NavigationMesh::get_cell_height);
  344. ClassDB::bind_method(D_METHOD("set_agent_height", "agent_height"), &NavigationMesh::set_agent_height);
  345. ClassDB::bind_method(D_METHOD("get_agent_height"), &NavigationMesh::get_agent_height);
  346. ClassDB::bind_method(D_METHOD("set_agent_radius", "agent_radius"), &NavigationMesh::set_agent_radius);
  347. ClassDB::bind_method(D_METHOD("get_agent_radius"), &NavigationMesh::get_agent_radius);
  348. ClassDB::bind_method(D_METHOD("set_agent_max_climb", "agent_max_climb"), &NavigationMesh::set_agent_max_climb);
  349. ClassDB::bind_method(D_METHOD("get_agent_max_climb"), &NavigationMesh::get_agent_max_climb);
  350. ClassDB::bind_method(D_METHOD("set_agent_max_slope", "agent_max_slope"), &NavigationMesh::set_agent_max_slope);
  351. ClassDB::bind_method(D_METHOD("get_agent_max_slope"), &NavigationMesh::get_agent_max_slope);
  352. ClassDB::bind_method(D_METHOD("set_region_min_size", "region_min_size"), &NavigationMesh::set_region_min_size);
  353. ClassDB::bind_method(D_METHOD("get_region_min_size"), &NavigationMesh::get_region_min_size);
  354. ClassDB::bind_method(D_METHOD("set_region_merge_size", "region_merge_size"), &NavigationMesh::set_region_merge_size);
  355. ClassDB::bind_method(D_METHOD("get_region_merge_size"), &NavigationMesh::get_region_merge_size);
  356. ClassDB::bind_method(D_METHOD("set_edge_max_length", "edge_max_length"), &NavigationMesh::set_edge_max_length);
  357. ClassDB::bind_method(D_METHOD("get_edge_max_length"), &NavigationMesh::get_edge_max_length);
  358. ClassDB::bind_method(D_METHOD("set_edge_max_error", "edge_max_error"), &NavigationMesh::set_edge_max_error);
  359. ClassDB::bind_method(D_METHOD("get_edge_max_error"), &NavigationMesh::get_edge_max_error);
  360. ClassDB::bind_method(D_METHOD("set_verts_per_poly", "verts_per_poly"), &NavigationMesh::set_verts_per_poly);
  361. ClassDB::bind_method(D_METHOD("get_verts_per_poly"), &NavigationMesh::get_verts_per_poly);
  362. ClassDB::bind_method(D_METHOD("set_detail_sample_distance", "detail_sample_dist"), &NavigationMesh::set_detail_sample_distance);
  363. ClassDB::bind_method(D_METHOD("get_detail_sample_distance"), &NavigationMesh::get_detail_sample_distance);
  364. ClassDB::bind_method(D_METHOD("set_detail_sample_max_error", "detail_sample_max_error"), &NavigationMesh::set_detail_sample_max_error);
  365. ClassDB::bind_method(D_METHOD("get_detail_sample_max_error"), &NavigationMesh::get_detail_sample_max_error);
  366. ClassDB::bind_method(D_METHOD("set_filter_low_hanging_obstacles", "filter_low_hanging_obstacles"), &NavigationMesh::set_filter_low_hanging_obstacles);
  367. ClassDB::bind_method(D_METHOD("get_filter_low_hanging_obstacles"), &NavigationMesh::get_filter_low_hanging_obstacles);
  368. ClassDB::bind_method(D_METHOD("set_filter_ledge_spans", "filter_ledge_spans"), &NavigationMesh::set_filter_ledge_spans);
  369. ClassDB::bind_method(D_METHOD("get_filter_ledge_spans"), &NavigationMesh::get_filter_ledge_spans);
  370. ClassDB::bind_method(D_METHOD("set_filter_walkable_low_height_spans", "filter_walkable_low_height_spans"), &NavigationMesh::set_filter_walkable_low_height_spans);
  371. ClassDB::bind_method(D_METHOD("get_filter_walkable_low_height_spans"), &NavigationMesh::get_filter_walkable_low_height_spans);
  372. ClassDB::bind_method(D_METHOD("set_vertices", "vertices"), &NavigationMesh::set_vertices);
  373. ClassDB::bind_method(D_METHOD("get_vertices"), &NavigationMesh::get_vertices);
  374. ClassDB::bind_method(D_METHOD("add_polygon", "polygon"), &NavigationMesh::add_polygon);
  375. ClassDB::bind_method(D_METHOD("get_polygon_count"), &NavigationMesh::get_polygon_count);
  376. ClassDB::bind_method(D_METHOD("get_polygon", "idx"), &NavigationMesh::get_polygon);
  377. ClassDB::bind_method(D_METHOD("clear_polygons"), &NavigationMesh::clear_polygons);
  378. ClassDB::bind_method(D_METHOD("create_from_mesh", "mesh"), &NavigationMesh::create_from_mesh);
  379. ClassDB::bind_method(D_METHOD("_set_polygons", "polygons"), &NavigationMesh::_set_polygons);
  380. ClassDB::bind_method(D_METHOD("_get_polygons"), &NavigationMesh::_get_polygons);
  381. ADD_PROPERTY(PropertyInfo(Variant::PACKED_VECTOR3_ARRAY, "vertices", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "set_vertices", "get_vertices");
  382. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "polygons", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "_set_polygons", "_get_polygons");
  383. ADD_GROUP("Sampling", "sample_");
  384. ADD_PROPERTY(PropertyInfo(Variant::INT, "sample_partition_type", PROPERTY_HINT_ENUM, "Watershed,Monotone,Layers"), "set_sample_partition_type", "get_sample_partition_type");
  385. ADD_GROUP("Geometry", "geometry_");
  386. 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");
  387. ADD_PROPERTY(PropertyInfo(Variant::INT, "geometry_collision_mask", PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_collision_mask", "get_collision_mask");
  388. 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");
  389. ADD_PROPERTY(PropertyInfo(Variant::STRING, "geometry_source_group_name"), "set_source_group_name", "get_source_group_name");
  390. ADD_GROUP("Cells", "cell_");
  391. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "cell_size", PROPERTY_HINT_RANGE, "0.01,500.0,0.01,or_greater"), "set_cell_size", "get_cell_size");
  392. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "cell_height", PROPERTY_HINT_RANGE, "0.01,500.0,0.01,or_greater"), "set_cell_height", "get_cell_height");
  393. ADD_GROUP("Agents", "agent_");
  394. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "agent_height", PROPERTY_HINT_RANGE, "0.0,500.0,0.01,or_greater"), "set_agent_height", "get_agent_height");
  395. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "agent_radius", PROPERTY_HINT_RANGE, "0.0,500.0,0.01,or_greater"), "set_agent_radius", "get_agent_radius");
  396. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "agent_max_climb", PROPERTY_HINT_RANGE, "0.0,500.0,0.01,or_greater"), "set_agent_max_climb", "get_agent_max_climb");
  397. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "agent_max_slope", PROPERTY_HINT_RANGE, "0.02,90.0,0.01"), "set_agent_max_slope", "get_agent_max_slope");
  398. ADD_GROUP("Regions", "region_");
  399. 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");
  400. 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");
  401. ADD_GROUP("Edges", "edge_");
  402. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "edge_max_length", PROPERTY_HINT_RANGE, "0.0,50.0,0.01,or_greater"), "set_edge_max_length", "get_edge_max_length");
  403. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "edge_max_error", PROPERTY_HINT_RANGE, "0.1,3.0,0.01,or_greater"), "set_edge_max_error", "get_edge_max_error");
  404. ADD_GROUP("Polygons", "polygon_");
  405. 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");
  406. ADD_GROUP("Details", "detail_");
  407. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "detail_sample_distance", PROPERTY_HINT_RANGE, "0.1,16.0,0.01,or_greater"), "set_detail_sample_distance", "get_detail_sample_distance");
  408. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "detail_sample_max_error", PROPERTY_HINT_RANGE, "0.0,16.0,0.01,or_greater"), "set_detail_sample_max_error", "get_detail_sample_max_error");
  409. ADD_GROUP("Filters", "filter_");
  410. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "filter_low_hanging_obstacles"), "set_filter_low_hanging_obstacles", "get_filter_low_hanging_obstacles");
  411. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "filter_ledge_spans"), "set_filter_ledge_spans", "get_filter_ledge_spans");
  412. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "filter_walkable_low_height_spans"), "set_filter_walkable_low_height_spans", "get_filter_walkable_low_height_spans");
  413. BIND_ENUM_CONSTANT(SAMPLE_PARTITION_WATERSHED);
  414. BIND_ENUM_CONSTANT(SAMPLE_PARTITION_MONOTONE);
  415. BIND_ENUM_CONSTANT(SAMPLE_PARTITION_LAYERS);
  416. BIND_ENUM_CONSTANT(SAMPLE_PARTITION_MAX);
  417. BIND_ENUM_CONSTANT(PARSED_GEOMETRY_MESH_INSTANCES);
  418. BIND_ENUM_CONSTANT(PARSED_GEOMETRY_STATIC_COLLIDERS);
  419. BIND_ENUM_CONSTANT(PARSED_GEOMETRY_BOTH);
  420. BIND_ENUM_CONSTANT(PARSED_GEOMETRY_MAX);
  421. BIND_ENUM_CONSTANT(SOURCE_GEOMETRY_NAVMESH_CHILDREN);
  422. BIND_ENUM_CONSTANT(SOURCE_GEOMETRY_GROUPS_WITH_CHILDREN);
  423. BIND_ENUM_CONSTANT(SOURCE_GEOMETRY_GROUPS_EXPLICIT);
  424. BIND_ENUM_CONSTANT(SOURCE_GEOMETRY_MAX);
  425. }
  426. void NavigationMesh::_validate_property(PropertyInfo &property) const {
  427. if (property.name == "geometry/collision_mask") {
  428. if (parsed_geometry_type == PARSED_GEOMETRY_MESH_INSTANCES) {
  429. property.usage = PROPERTY_USAGE_NONE;
  430. return;
  431. }
  432. }
  433. if (property.name == "geometry/source_group_name") {
  434. if (source_geometry_mode == SOURCE_GEOMETRY_NAVMESH_CHILDREN) {
  435. property.usage = PROPERTY_USAGE_NONE;
  436. return;
  437. }
  438. }
  439. }
  440. #ifndef DISABLE_DEPRECATED
  441. bool NavigationMesh::_set(const StringName &p_name, const Variant &p_value) {
  442. String name = p_name;
  443. if (name.find("/") != -1) {
  444. // Compatibility with pre-3.5 "category/path" property names.
  445. name = name.replace("/", "_");
  446. if (name == "sample_partition_type_sample_partition_type") {
  447. set("sample_partition_type", p_value);
  448. } else if (name == "filter_filter_walkable_low_height_spans") {
  449. set("filter_walkable_low_height_spans", p_value);
  450. } else {
  451. set(name, p_value);
  452. }
  453. return true;
  454. }
  455. return false;
  456. }
  457. bool NavigationMesh::_get(const StringName &p_name, Variant &r_ret) const {
  458. String name = p_name;
  459. if (name.find("/") != -1) {
  460. // Compatibility with pre-3.5 "category/path" property names.
  461. name = name.replace("/", "_");
  462. if (name == "sample_partition_type_sample_partition_type") {
  463. r_ret = get("sample_partition_type");
  464. } else if (name == "filter_filter_walkable_low_height_spans") {
  465. r_ret = get("filter_walkable_low_height_spans");
  466. } else {
  467. r_ret = get(name);
  468. }
  469. return true;
  470. }
  471. return false;
  472. }
  473. #endif // DISABLE_DEPRECATED
  474. NavigationMesh::NavigationMesh() {}