navigation_mesh_generator.cpp 19 KB

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