nav_mesh_generator_3d.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /**************************************************************************/
  2. /* nav_mesh_generator_3d.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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 NAV_MESH_GENERATOR_3D_H
  31. #define NAV_MESH_GENERATOR_3D_H
  32. #ifndef _3D_DISABLED
  33. #include "core/object/class_db.h"
  34. #include "core/object/worker_thread_pool.h"
  35. #include "core/templates/rid_owner.h"
  36. #include "modules/modules_enabled.gen.h" // For csg, gridmap.
  37. class Node;
  38. class NavigationMesh;
  39. class NavigationMeshSourceGeometryData3D;
  40. class NavMeshGenerator3D : public Object {
  41. static NavMeshGenerator3D *singleton;
  42. static Mutex baking_navmesh_mutex;
  43. static Mutex generator_task_mutex;
  44. static RWLock generator_rid_rwlock;
  45. struct NavMeshGeometryParser3D {
  46. RID self;
  47. Callable callback;
  48. };
  49. static RID_Owner<NavMeshGeometryParser3D> generator_parser_owner;
  50. static LocalVector<NavMeshGeometryParser3D *> generator_parsers;
  51. static bool use_threads;
  52. static bool baking_use_multiple_threads;
  53. static bool baking_use_high_priority_threads;
  54. struct NavMeshGeneratorTask3D {
  55. enum TaskStatus {
  56. BAKING_STARTED,
  57. BAKING_FINISHED,
  58. BAKING_FAILED,
  59. CALLBACK_DISPATCHED,
  60. CALLBACK_FAILED,
  61. };
  62. Ref<NavigationMesh> navigation_mesh;
  63. Ref<NavigationMeshSourceGeometryData3D> source_geometry_data;
  64. Callable callback;
  65. WorkerThreadPool::TaskID thread_task_id = WorkerThreadPool::INVALID_TASK_ID;
  66. NavMeshGeneratorTask3D::TaskStatus status = NavMeshGeneratorTask3D::TaskStatus::BAKING_STARTED;
  67. };
  68. static HashMap<WorkerThreadPool::TaskID, NavMeshGeneratorTask3D *> generator_tasks;
  69. static void generator_thread_bake(void *p_arg);
  70. static HashSet<Ref<NavigationMesh>> baking_navmeshes;
  71. static void generator_parse_geometry_node(const Ref<NavigationMesh> &p_navigation_mesh, Ref<NavigationMeshSourceGeometryData3D> p_source_geometry_data, Node *p_node, bool p_recurse_children);
  72. static void generator_parse_source_geometry_data(const Ref<NavigationMesh> &p_navigation_mesh, Ref<NavigationMeshSourceGeometryData3D> p_source_geometry_data, Node *p_root_node);
  73. static void generator_bake_from_source_geometry_data(Ref<NavigationMesh> p_navigation_mesh, const Ref<NavigationMeshSourceGeometryData3D> &p_source_geometry_data);
  74. static void generator_parse_meshinstance3d_node(const Ref<NavigationMesh> &p_navigation_mesh, Ref<NavigationMeshSourceGeometryData3D> p_source_geometry_data, Node *p_node);
  75. static void generator_parse_multimeshinstance3d_node(const Ref<NavigationMesh> &p_navigation_mesh, Ref<NavigationMeshSourceGeometryData3D> p_source_geometry_data, Node *p_node);
  76. static void generator_parse_staticbody3d_node(const Ref<NavigationMesh> &p_navigation_mesh, Ref<NavigationMeshSourceGeometryData3D> p_source_geometry_data, Node *p_node);
  77. #ifdef MODULE_CSG_ENABLED
  78. static void generator_parse_csgshape3d_node(const Ref<NavigationMesh> &p_navigation_mesh, Ref<NavigationMeshSourceGeometryData3D> p_source_geometry_data, Node *p_node);
  79. #endif // MODULE_CSG_ENABLED
  80. #ifdef MODULE_GRIDMAP_ENABLED
  81. static void generator_parse_gridmap_node(const Ref<NavigationMesh> &p_navigation_mesh, Ref<NavigationMeshSourceGeometryData3D> p_source_geometry_data, Node *p_node);
  82. #endif // MODULE_GRIDMAP_ENABLED
  83. static void generator_parse_navigationobstacle_node(const Ref<NavigationMesh> &p_navigation_mesh, Ref<NavigationMeshSourceGeometryData3D> p_source_geometry_data, Node *p_node);
  84. static bool generator_emit_callback(const Callable &p_callback);
  85. public:
  86. static NavMeshGenerator3D *get_singleton();
  87. static void sync();
  88. static void cleanup();
  89. static void finish();
  90. static void parse_source_geometry_data(Ref<NavigationMesh> p_navigation_mesh, Ref<NavigationMeshSourceGeometryData3D> p_source_geometry_data, Node *p_root_node, const Callable &p_callback = Callable());
  91. static void bake_from_source_geometry_data(Ref<NavigationMesh> p_navigation_mesh, Ref<NavigationMeshSourceGeometryData3D> p_source_geometry_data, const Callable &p_callback = Callable());
  92. static void bake_from_source_geometry_data_async(Ref<NavigationMesh> p_navigation_mesh, Ref<NavigationMeshSourceGeometryData3D> p_source_geometry_data, const Callable &p_callback = Callable());
  93. static bool is_baking(Ref<NavigationMesh> p_navigation_mesh);
  94. static RID source_geometry_parser_create();
  95. static void source_geometry_parser_set_callback(RID p_parser, const Callable &p_callback);
  96. static bool owns(RID p_object);
  97. static void free(RID p_object);
  98. NavMeshGenerator3D();
  99. ~NavMeshGenerator3D();
  100. };
  101. #endif // _3D_DISABLED
  102. #endif // NAV_MESH_GENERATOR_3D_H