nav_mesh_generator_3d.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 "modules/modules_enabled.gen.h" // For csg, gridmap.
  36. class Node;
  37. class NavigationMesh;
  38. class NavigationMeshSourceGeometryData3D;
  39. class NavMeshGenerator3D : public Object {
  40. static NavMeshGenerator3D *singleton;
  41. static Mutex baking_navmesh_mutex;
  42. static Mutex generator_task_mutex;
  43. static bool use_threads;
  44. static bool baking_use_multiple_threads;
  45. static bool baking_use_high_priority_threads;
  46. struct NavMeshGeneratorTask3D {
  47. enum TaskStatus {
  48. BAKING_STARTED,
  49. BAKING_FINISHED,
  50. BAKING_FAILED,
  51. CALLBACK_DISPATCHED,
  52. CALLBACK_FAILED,
  53. };
  54. Ref<NavigationMesh> navigation_mesh;
  55. Ref<NavigationMeshSourceGeometryData3D> source_geometry_data;
  56. Callable callback;
  57. WorkerThreadPool::TaskID thread_task_id = WorkerThreadPool::INVALID_TASK_ID;
  58. NavMeshGeneratorTask3D::TaskStatus status = NavMeshGeneratorTask3D::TaskStatus::BAKING_STARTED;
  59. };
  60. static HashMap<WorkerThreadPool::TaskID, NavMeshGeneratorTask3D *> generator_tasks;
  61. static void generator_thread_bake(void *p_arg);
  62. static HashSet<Ref<NavigationMesh>> baking_navmeshes;
  63. 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);
  64. static void generator_parse_source_geometry_data(const Ref<NavigationMesh> &p_navigation_mesh, Ref<NavigationMeshSourceGeometryData3D> p_source_geometry_data, Node *p_root_node);
  65. static void generator_bake_from_source_geometry_data(Ref<NavigationMesh> p_navigation_mesh, const Ref<NavigationMeshSourceGeometryData3D> &p_source_geometry_data);
  66. static void generator_parse_meshinstance3d_node(const Ref<NavigationMesh> &p_navigation_mesh, Ref<NavigationMeshSourceGeometryData3D> p_source_geometry_data, Node *p_node);
  67. static void generator_parse_multimeshinstance3d_node(const Ref<NavigationMesh> &p_navigation_mesh, Ref<NavigationMeshSourceGeometryData3D> p_source_geometry_data, Node *p_node);
  68. static void generator_parse_staticbody3d_node(const Ref<NavigationMesh> &p_navigation_mesh, Ref<NavigationMeshSourceGeometryData3D> p_source_geometry_data, Node *p_node);
  69. #ifdef MODULE_CSG_ENABLED
  70. static void generator_parse_csgshape3d_node(const Ref<NavigationMesh> &p_navigation_mesh, Ref<NavigationMeshSourceGeometryData3D> p_source_geometry_data, Node *p_node);
  71. #endif // MODULE_CSG_ENABLED
  72. #ifdef MODULE_GRIDMAP_ENABLED
  73. static void generator_parse_gridmap_node(const Ref<NavigationMesh> &p_navigation_mesh, Ref<NavigationMeshSourceGeometryData3D> p_source_geometry_data, Node *p_node);
  74. #endif // MODULE_GRIDMAP_ENABLED
  75. static bool generator_emit_callback(const Callable &p_callback);
  76. public:
  77. static NavMeshGenerator3D *get_singleton();
  78. static void sync();
  79. static void cleanup();
  80. static void finish();
  81. 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());
  82. static void bake_from_source_geometry_data(Ref<NavigationMesh> p_navigation_mesh, Ref<NavigationMeshSourceGeometryData3D> p_source_geometry_data, const Callable &p_callback = Callable());
  83. static void bake_from_source_geometry_data_async(Ref<NavigationMesh> p_navigation_mesh, Ref<NavigationMeshSourceGeometryData3D> p_source_geometry_data, const Callable &p_callback = Callable());
  84. NavMeshGenerator3D();
  85. ~NavMeshGenerator3D();
  86. };
  87. #endif // _3D_DISABLED
  88. #endif // NAV_MESH_GENERATOR_3D_H