navigation_mesh_generator.cpp 20 KB

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