navigation_mesh_generator.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. /*************************************************************************/
  2. /* navigation_mesh_generator.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_generator.h"
  31. #include "core/math/convex_hull.h"
  32. #include "core/os/thread.h"
  33. #include "editor/editor_settings.h"
  34. #include "scene/3d/collision_shape.h"
  35. #include "scene/3d/mesh_instance.h"
  36. #include "scene/3d/physics_body.h"
  37. #include "scene/resources/box_shape.h"
  38. #include "scene/resources/capsule_shape.h"
  39. #include "scene/resources/concave_polygon_shape.h"
  40. #include "scene/resources/convex_polygon_shape.h"
  41. #include "scene/resources/cylinder_shape.h"
  42. #include "scene/resources/plane_shape.h"
  43. #include "scene/resources/primitive_meshes.h"
  44. #include "scene/resources/shape.h"
  45. #include "scene/resources/sphere_shape.h"
  46. #include "modules/modules_enabled.gen.h" // For csg, gridmap.
  47. #ifdef MODULE_CSG_ENABLED
  48. #include "modules/csg/csg_shape.h"
  49. #endif
  50. #ifdef MODULE_GRIDMAP_ENABLED
  51. #include "modules/gridmap/grid_map.h"
  52. #endif
  53. EditorNavigationMeshGenerator *EditorNavigationMeshGenerator::singleton = nullptr;
  54. void EditorNavigationMeshGenerator::_add_vertex(const Vector3 &p_vec3, Vector<float> &p_verticies) {
  55. p_verticies.push_back(p_vec3.x);
  56. p_verticies.push_back(p_vec3.y);
  57. p_verticies.push_back(p_vec3.z);
  58. }
  59. void EditorNavigationMeshGenerator::_add_mesh(const Ref<Mesh> &p_mesh, const Transform &p_xform, Vector<float> &p_verticies, Vector<int> &p_indices) {
  60. int current_vertex_count;
  61. for (int i = 0; i < p_mesh->get_surface_count(); i++) {
  62. current_vertex_count = p_verticies.size() / 3;
  63. if (p_mesh->surface_get_primitive_type(i) != Mesh::PRIMITIVE_TRIANGLES) {
  64. continue;
  65. }
  66. int index_count = 0;
  67. if (p_mesh->surface_get_format(i) & Mesh::ARRAY_FORMAT_INDEX) {
  68. index_count = p_mesh->surface_get_array_index_len(i);
  69. } else {
  70. index_count = p_mesh->surface_get_array_len(i);
  71. }
  72. ERR_CONTINUE((index_count == 0 || (index_count % 3) != 0));
  73. int face_count = index_count / 3;
  74. Array a = p_mesh->surface_get_arrays(i);
  75. PoolVector<Vector3> mesh_vertices = a[Mesh::ARRAY_VERTEX];
  76. PoolVector<Vector3>::Read vr = mesh_vertices.read();
  77. if (p_mesh->surface_get_format(i) & Mesh::ARRAY_FORMAT_INDEX) {
  78. PoolVector<int> mesh_indices = a[Mesh::ARRAY_INDEX];
  79. PoolVector<int>::Read ir = mesh_indices.read();
  80. for (int j = 0; j < mesh_vertices.size(); j++) {
  81. _add_vertex(p_xform.xform(vr[j]), p_verticies);
  82. }
  83. for (int j = 0; j < face_count; j++) {
  84. // CCW
  85. p_indices.push_back(current_vertex_count + (ir[j * 3 + 0]));
  86. p_indices.push_back(current_vertex_count + (ir[j * 3 + 2]));
  87. p_indices.push_back(current_vertex_count + (ir[j * 3 + 1]));
  88. }
  89. } else {
  90. face_count = mesh_vertices.size() / 3;
  91. for (int j = 0; j < face_count; j++) {
  92. _add_vertex(p_xform.xform(vr[j * 3 + 0]), p_verticies);
  93. _add_vertex(p_xform.xform(vr[j * 3 + 2]), p_verticies);
  94. _add_vertex(p_xform.xform(vr[j * 3 + 1]), p_verticies);
  95. p_indices.push_back(current_vertex_count + (j * 3 + 0));
  96. p_indices.push_back(current_vertex_count + (j * 3 + 1));
  97. p_indices.push_back(current_vertex_count + (j * 3 + 2));
  98. }
  99. }
  100. }
  101. }
  102. void EditorNavigationMeshGenerator::_add_faces(const PoolVector3Array &p_faces, const Transform &p_xform, Vector<float> &p_verticies, Vector<int> &p_indices) {
  103. int face_count = p_faces.size() / 3;
  104. int current_vertex_count = p_verticies.size() / 3;
  105. for (int j = 0; j < face_count; j++) {
  106. _add_vertex(p_xform.xform(p_faces[j * 3 + 0]), p_verticies);
  107. _add_vertex(p_xform.xform(p_faces[j * 3 + 1]), p_verticies);
  108. _add_vertex(p_xform.xform(p_faces[j * 3 + 2]), p_verticies);
  109. p_indices.push_back(current_vertex_count + (j * 3 + 0));
  110. p_indices.push_back(current_vertex_count + (j * 3 + 2));
  111. p_indices.push_back(current_vertex_count + (j * 3 + 1));
  112. }
  113. }
  114. void EditorNavigationMeshGenerator::_parse_geometry(Transform p_accumulated_transform, Node *p_node, Vector<float> &p_verticies, Vector<int> &p_indices, NavigationMesh::ParsedGeometryType p_generate_from, uint32_t p_collision_mask, bool p_recurse_children) {
  115. if (Object::cast_to<MeshInstance>(p_node) && p_generate_from != NavigationMesh::PARSED_GEOMETRY_STATIC_COLLIDERS) {
  116. MeshInstance *mesh_instance = Object::cast_to<MeshInstance>(p_node);
  117. Ref<Mesh> mesh = mesh_instance->get_mesh();
  118. if (mesh.is_valid()) {
  119. _add_mesh(mesh, p_accumulated_transform * mesh_instance->get_transform(), p_verticies, p_indices);
  120. }
  121. }
  122. #ifdef MODULE_CSG_ENABLED
  123. if (Object::cast_to<CSGShape>(p_node) && p_generate_from != NavigationMesh::PARSED_GEOMETRY_STATIC_COLLIDERS) {
  124. CSGShape *csg_shape = Object::cast_to<CSGShape>(p_node);
  125. Array meshes = csg_shape->get_meshes();
  126. if (!meshes.empty()) {
  127. Ref<Mesh> mesh = meshes[1];
  128. if (mesh.is_valid()) {
  129. _add_mesh(mesh, p_accumulated_transform * csg_shape->get_transform(), p_verticies, p_indices);
  130. }
  131. }
  132. }
  133. #endif
  134. if (Object::cast_to<StaticBody>(p_node) && p_generate_from != NavigationMesh::PARSED_GEOMETRY_MESH_INSTANCES) {
  135. StaticBody *static_body = Object::cast_to<StaticBody>(p_node);
  136. if (static_body->get_collision_layer() & p_collision_mask) {
  137. for (int i = 0; i < p_node->get_child_count(); ++i) {
  138. Node *child = p_node->get_child(i);
  139. if (Object::cast_to<CollisionShape>(child)) {
  140. CollisionShape *col_shape = Object::cast_to<CollisionShape>(child);
  141. Transform transform = p_accumulated_transform * static_body->get_transform() * col_shape->get_transform();
  142. Ref<Mesh> mesh;
  143. Ref<Shape> s = col_shape->get_shape();
  144. BoxShape *box = Object::cast_to<BoxShape>(*s);
  145. if (box) {
  146. Ref<CubeMesh> cube_mesh;
  147. cube_mesh.instance();
  148. cube_mesh->set_size(box->get_extents() * 2.0);
  149. mesh = cube_mesh;
  150. }
  151. CapsuleShape *capsule = Object::cast_to<CapsuleShape>(*s);
  152. if (capsule) {
  153. Ref<CapsuleMesh> capsule_mesh;
  154. capsule_mesh.instance();
  155. capsule_mesh->set_radius(capsule->get_radius());
  156. capsule_mesh->set_mid_height(capsule->get_height() / 2.0);
  157. mesh = capsule_mesh;
  158. }
  159. CylinderShape *cylinder = Object::cast_to<CylinderShape>(*s);
  160. if (cylinder) {
  161. Ref<CylinderMesh> cylinder_mesh;
  162. cylinder_mesh.instance();
  163. cylinder_mesh->set_height(cylinder->get_height());
  164. cylinder_mesh->set_bottom_radius(cylinder->get_radius());
  165. cylinder_mesh->set_top_radius(cylinder->get_radius());
  166. mesh = cylinder_mesh;
  167. }
  168. SphereShape *sphere = Object::cast_to<SphereShape>(*s);
  169. if (sphere) {
  170. Ref<SphereMesh> sphere_mesh;
  171. sphere_mesh.instance();
  172. sphere_mesh->set_radius(sphere->get_radius());
  173. sphere_mesh->set_height(sphere->get_radius() * 2.0);
  174. mesh = sphere_mesh;
  175. }
  176. ConcavePolygonShape *concave_polygon = Object::cast_to<ConcavePolygonShape>(*s);
  177. if (concave_polygon) {
  178. _add_faces(concave_polygon->get_faces(), transform, p_verticies, p_indices);
  179. }
  180. ConvexPolygonShape *convex_polygon = Object::cast_to<ConvexPolygonShape>(*s);
  181. if (convex_polygon) {
  182. Vector<Vector3> varr = Variant(convex_polygon->get_points());
  183. Geometry::MeshData md;
  184. Error err = ConvexHullComputer::convex_hull(varr, md);
  185. if (err == OK) {
  186. PoolVector3Array faces;
  187. for (int j = 0; j < md.faces.size(); ++j) {
  188. Geometry::MeshData::Face face = md.faces[j];
  189. for (int k = 2; k < face.indices.size(); ++k) {
  190. faces.push_back(md.vertices[face.indices[0]]);
  191. faces.push_back(md.vertices[face.indices[k - 1]]);
  192. faces.push_back(md.vertices[face.indices[k]]);
  193. }
  194. }
  195. _add_faces(faces, transform, p_verticies, p_indices);
  196. }
  197. }
  198. if (mesh.is_valid()) {
  199. _add_mesh(mesh, transform, p_verticies, p_indices);
  200. }
  201. }
  202. }
  203. }
  204. }
  205. #ifdef MODULE_GRIDMAP_ENABLED
  206. if (Object::cast_to<GridMap>(p_node) && p_generate_from != NavigationMesh::PARSED_GEOMETRY_STATIC_COLLIDERS) {
  207. GridMap *gridmap_instance = Object::cast_to<GridMap>(p_node);
  208. Array meshes = gridmap_instance->get_meshes();
  209. Transform xform = gridmap_instance->get_transform();
  210. for (int i = 0; i < meshes.size(); i += 2) {
  211. Ref<Mesh> mesh = meshes[i + 1];
  212. if (mesh.is_valid()) {
  213. _add_mesh(mesh, p_accumulated_transform * xform * meshes[i], p_verticies, p_indices);
  214. }
  215. }
  216. }
  217. #endif
  218. if (Object::cast_to<Spatial>(p_node)) {
  219. Spatial *spatial = Object::cast_to<Spatial>(p_node);
  220. p_accumulated_transform = p_accumulated_transform * spatial->get_transform();
  221. }
  222. if (p_recurse_children) {
  223. for (int i = 0; i < p_node->get_child_count(); i++) {
  224. _parse_geometry(p_accumulated_transform, p_node->get_child(i), p_verticies, p_indices, p_generate_from, p_collision_mask, p_recurse_children);
  225. }
  226. }
  227. }
  228. void EditorNavigationMeshGenerator::_convert_detail_mesh_to_native_navigation_mesh(const rcPolyMeshDetail *p_detail_mesh, Ref<NavigationMesh> p_nav_mesh) {
  229. PoolVector<Vector3> nav_vertices;
  230. for (int i = 0; i < p_detail_mesh->nverts; i++) {
  231. const float *v = &p_detail_mesh->verts[i * 3];
  232. nav_vertices.append(Vector3(v[0], v[1], v[2]));
  233. }
  234. p_nav_mesh->set_vertices(nav_vertices);
  235. for (int i = 0; i < p_detail_mesh->nmeshes; i++) {
  236. const unsigned int *m = &p_detail_mesh->meshes[i * 4];
  237. const unsigned int bverts = m[0];
  238. const unsigned int btris = m[2];
  239. const unsigned int ntris = m[3];
  240. const unsigned char *tris = &p_detail_mesh->tris[btris * 4];
  241. for (unsigned int j = 0; j < ntris; j++) {
  242. Vector<int> nav_indices;
  243. nav_indices.resize(3);
  244. // Polygon order in recast is opposite than godot's
  245. nav_indices.write[0] = ((int)(bverts + tris[j * 4 + 0]));
  246. nav_indices.write[1] = ((int)(bverts + tris[j * 4 + 2]));
  247. nav_indices.write[2] = ((int)(bverts + tris[j * 4 + 1]));
  248. p_nav_mesh->add_polygon(nav_indices);
  249. }
  250. }
  251. }
  252. void EditorNavigationMeshGenerator::_build_recast_navigation_mesh(Ref<NavigationMesh> p_nav_mesh, EditorProgress *ep,
  253. rcHeightfield *hf, rcCompactHeightfield *chf, rcContourSet *cset, rcPolyMesh *poly_mesh, rcPolyMeshDetail *detail_mesh,
  254. Vector<float> &vertices, Vector<int> &indices) {
  255. rcContext ctx;
  256. ep->step(TTR("Setting up Configuration..."), 1);
  257. const float *verts = vertices.ptr();
  258. const int nverts = vertices.size() / 3;
  259. const int *tris = indices.ptr();
  260. const int ntris = indices.size() / 3;
  261. float bmin[3], bmax[3];
  262. rcCalcBounds(verts, nverts, bmin, bmax);
  263. rcConfig cfg;
  264. memset(&cfg, 0, sizeof(cfg));
  265. cfg.cs = p_nav_mesh->get_cell_size();
  266. cfg.ch = p_nav_mesh->get_cell_height();
  267. cfg.walkableSlopeAngle = p_nav_mesh->get_agent_max_slope();
  268. cfg.walkableHeight = (int)Math::ceil(p_nav_mesh->get_agent_height() / cfg.ch);
  269. cfg.walkableClimb = (int)Math::floor(p_nav_mesh->get_agent_max_climb() / cfg.ch);
  270. cfg.walkableRadius = (int)Math::ceil(p_nav_mesh->get_agent_radius() / cfg.cs);
  271. cfg.maxEdgeLen = (int)(p_nav_mesh->get_edge_max_length() / p_nav_mesh->get_cell_size());
  272. cfg.maxSimplificationError = p_nav_mesh->get_edge_max_error();
  273. cfg.minRegionArea = (int)(p_nav_mesh->get_region_min_size() * p_nav_mesh->get_region_min_size());
  274. cfg.mergeRegionArea = (int)(p_nav_mesh->get_region_merge_size() * p_nav_mesh->get_region_merge_size());
  275. cfg.maxVertsPerPoly = (int)p_nav_mesh->get_verts_per_poly();
  276. cfg.detailSampleDist = p_nav_mesh->get_detail_sample_distance() < 0.9f ? 0 : p_nav_mesh->get_cell_size() * p_nav_mesh->get_detail_sample_distance();
  277. cfg.detailSampleMaxError = p_nav_mesh->get_cell_height() * p_nav_mesh->get_detail_sample_max_error();
  278. cfg.bmin[0] = bmin[0];
  279. cfg.bmin[1] = bmin[1];
  280. cfg.bmin[2] = bmin[2];
  281. cfg.bmax[0] = bmax[0];
  282. cfg.bmax[1] = bmax[1];
  283. cfg.bmax[2] = bmax[2];
  284. ep->step(TTR("Calculating grid size..."), 2);
  285. rcCalcGridSize(cfg.bmin, cfg.bmax, cfg.cs, &cfg.width, &cfg.height);
  286. ep->step(TTR("Creating heightfield..."), 3);
  287. hf = rcAllocHeightfield();
  288. ERR_FAIL_COND(!hf);
  289. ERR_FAIL_COND(!rcCreateHeightfield(&ctx, *hf, cfg.width, cfg.height, cfg.bmin, cfg.bmax, cfg.cs, cfg.ch));
  290. ep->step(TTR("Marking walkable triangles..."), 4);
  291. {
  292. Vector<unsigned char> tri_areas;
  293. tri_areas.resize(ntris);
  294. ERR_FAIL_COND(tri_areas.size() == 0);
  295. memset(tri_areas.ptrw(), 0, ntris * sizeof(unsigned char));
  296. rcMarkWalkableTriangles(&ctx, cfg.walkableSlopeAngle, verts, nverts, tris, ntris, tri_areas.ptrw());
  297. ERR_FAIL_COND(!rcRasterizeTriangles(&ctx, verts, nverts, tris, tri_areas.ptr(), ntris, *hf, cfg.walkableClimb));
  298. }
  299. if (p_nav_mesh->get_filter_low_hanging_obstacles()) {
  300. rcFilterLowHangingWalkableObstacles(&ctx, cfg.walkableClimb, *hf);
  301. }
  302. if (p_nav_mesh->get_filter_ledge_spans()) {
  303. rcFilterLedgeSpans(&ctx, cfg.walkableHeight, cfg.walkableClimb, *hf);
  304. }
  305. if (p_nav_mesh->get_filter_walkable_low_height_spans()) {
  306. rcFilterWalkableLowHeightSpans(&ctx, cfg.walkableHeight, *hf);
  307. }
  308. ep->step(TTR("Constructing compact heightfield..."), 5);
  309. chf = rcAllocCompactHeightfield();
  310. ERR_FAIL_COND(!chf);
  311. ERR_FAIL_COND(!rcBuildCompactHeightfield(&ctx, cfg.walkableHeight, cfg.walkableClimb, *hf, *chf));
  312. rcFreeHeightField(hf);
  313. hf = nullptr;
  314. ep->step(TTR("Eroding walkable area..."), 6);
  315. ERR_FAIL_COND(!rcErodeWalkableArea(&ctx, cfg.walkableRadius, *chf));
  316. ep->step(TTR("Partitioning..."), 7);
  317. if (p_nav_mesh->get_sample_partition_type() == NavigationMesh::SAMPLE_PARTITION_WATERSHED) {
  318. ERR_FAIL_COND(!rcBuildDistanceField(&ctx, *chf));
  319. ERR_FAIL_COND(!rcBuildRegions(&ctx, *chf, 0, cfg.minRegionArea, cfg.mergeRegionArea));
  320. } else if (p_nav_mesh->get_sample_partition_type() == NavigationMesh::SAMPLE_PARTITION_MONOTONE) {
  321. ERR_FAIL_COND(!rcBuildRegionsMonotone(&ctx, *chf, 0, cfg.minRegionArea, cfg.mergeRegionArea));
  322. } else {
  323. ERR_FAIL_COND(!rcBuildLayerRegions(&ctx, *chf, 0, cfg.minRegionArea));
  324. }
  325. ep->step(TTR("Creating contours..."), 8);
  326. cset = rcAllocContourSet();
  327. ERR_FAIL_COND(!cset);
  328. ERR_FAIL_COND(!rcBuildContours(&ctx, *chf, cfg.maxSimplificationError, cfg.maxEdgeLen, *cset));
  329. ep->step(TTR("Creating polymesh..."), 9);
  330. poly_mesh = rcAllocPolyMesh();
  331. ERR_FAIL_COND(!poly_mesh);
  332. ERR_FAIL_COND(!rcBuildPolyMesh(&ctx, *cset, cfg.maxVertsPerPoly, *poly_mesh));
  333. detail_mesh = rcAllocPolyMeshDetail();
  334. ERR_FAIL_COND(!detail_mesh);
  335. ERR_FAIL_COND(!rcBuildPolyMeshDetail(&ctx, *poly_mesh, *chf, cfg.detailSampleDist, cfg.detailSampleMaxError, *detail_mesh));
  336. rcFreeCompactHeightfield(chf);
  337. chf = nullptr;
  338. rcFreeContourSet(cset);
  339. cset = nullptr;
  340. ep->step(TTR("Converting to native navigation mesh..."), 10);
  341. _convert_detail_mesh_to_native_navigation_mesh(detail_mesh, p_nav_mesh);
  342. rcFreePolyMesh(poly_mesh);
  343. poly_mesh = nullptr;
  344. rcFreePolyMeshDetail(detail_mesh);
  345. detail_mesh = nullptr;
  346. }
  347. EditorNavigationMeshGenerator *EditorNavigationMeshGenerator::get_singleton() {
  348. return singleton;
  349. }
  350. EditorNavigationMeshGenerator::EditorNavigationMeshGenerator() {
  351. singleton = this;
  352. }
  353. EditorNavigationMeshGenerator::~EditorNavigationMeshGenerator() {
  354. }
  355. void EditorNavigationMeshGenerator::bake(Ref<NavigationMesh> p_nav_mesh, Node *p_node) {
  356. if (!Engine::get_singleton()->is_editor_hint()) {
  357. ERR_PRINT("Invoking EditorNavigationMeshGenerator::bake(...) in-game is not supported in Godot 3.2 or below. Aborting bake...");
  358. return;
  359. }
  360. ERR_FAIL_COND(!p_nav_mesh.is_valid());
  361. EditorProgress ep("bake", TTR("Navigation Mesh Generator Setup:"), 11);
  362. ep.step(TTR("Parsing Geometry..."), 0);
  363. Vector<float> vertices;
  364. Vector<int> indices;
  365. List<Node *> parse_nodes;
  366. if (p_nav_mesh->get_source_geometry_mode() == NavigationMesh::SOURCE_GEOMETRY_NAVMESH_CHILDREN) {
  367. parse_nodes.push_back(p_node);
  368. } else {
  369. p_node->get_tree()->get_nodes_in_group(p_nav_mesh->get_source_group_name(), &parse_nodes);
  370. }
  371. Transform navmesh_xform = Object::cast_to<Spatial>(p_node)->get_transform().affine_inverse();
  372. for (const List<Node *>::Element *E = parse_nodes.front(); E; E = E->next()) {
  373. NavigationMesh::ParsedGeometryType geometry_type = p_nav_mesh->get_parsed_geometry_type();
  374. uint32_t collision_mask = p_nav_mesh->get_collision_mask();
  375. bool recurse_children = p_nav_mesh->get_source_geometry_mode() != NavigationMesh::SOURCE_GEOMETRY_GROUPS_EXPLICIT;
  376. _parse_geometry(navmesh_xform, E->get(), vertices, indices, geometry_type, collision_mask, recurse_children);
  377. }
  378. if (vertices.size() > 0 && indices.size() > 0) {
  379. rcHeightfield *hf = nullptr;
  380. rcCompactHeightfield *chf = nullptr;
  381. rcContourSet *cset = nullptr;
  382. rcPolyMesh *poly_mesh = nullptr;
  383. rcPolyMeshDetail *detail_mesh = nullptr;
  384. _build_recast_navigation_mesh(p_nav_mesh, &ep, hf, chf, cset, poly_mesh, detail_mesh, vertices, indices);
  385. rcFreeHeightField(hf);
  386. hf = nullptr;
  387. rcFreeCompactHeightfield(chf);
  388. chf = nullptr;
  389. rcFreeContourSet(cset);
  390. cset = nullptr;
  391. rcFreePolyMesh(poly_mesh);
  392. poly_mesh = nullptr;
  393. rcFreePolyMeshDetail(detail_mesh);
  394. detail_mesh = nullptr;
  395. }
  396. ep.step(TTR("Done!"), 11);
  397. }
  398. void EditorNavigationMeshGenerator::clear(Ref<NavigationMesh> p_nav_mesh) {
  399. if (p_nav_mesh.is_valid()) {
  400. p_nav_mesh->clear_polygons();
  401. p_nav_mesh->set_vertices(PoolVector<Vector3>());
  402. }
  403. }
  404. void EditorNavigationMeshGenerator::_bind_methods() {
  405. ClassDB::bind_method(D_METHOD("bake", "nav_mesh", "root_node"), &EditorNavigationMeshGenerator::bake);
  406. ClassDB::bind_method(D_METHOD("clear", "nav_mesh"), &EditorNavigationMeshGenerator::clear);
  407. }