navigation_mesh_generator.cpp 19 KB

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