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