navigation_mesh_instance.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /**************************************************************************/
  2. /* navigation_mesh_instance.cpp */
  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. #include "navigation_mesh_instance.h"
  31. #include "core/os/os.h"
  32. #include "core/os/thread.h"
  33. #include "mesh_instance.h"
  34. #include "navigation.h"
  35. #include "servers/navigation_server.h"
  36. void NavigationMeshInstance::set_enabled(bool p_enabled) {
  37. if (enabled == p_enabled) {
  38. return;
  39. }
  40. enabled = p_enabled;
  41. if (!is_inside_tree()) {
  42. return;
  43. }
  44. if (!enabled) {
  45. NavigationServer::get_singleton()->region_set_map(region, RID());
  46. } else {
  47. if (navigation) {
  48. NavigationServer::get_singleton()->region_set_map(region, navigation->get_rid());
  49. } else {
  50. NavigationServer::get_singleton()->region_set_map(region, get_world()->get_navigation_map());
  51. }
  52. }
  53. if (debug_view) {
  54. MeshInstance *dm = Object::cast_to<MeshInstance>(debug_view);
  55. if (is_enabled()) {
  56. dm->set_material_override(get_tree()->get_debug_navigation_material());
  57. } else {
  58. dm->set_material_override(get_tree()->get_debug_navigation_disabled_material());
  59. }
  60. }
  61. update_gizmo();
  62. }
  63. bool NavigationMeshInstance::is_enabled() const {
  64. return enabled;
  65. }
  66. void NavigationMeshInstance::set_navigation_layers(uint32_t p_navigation_layers) {
  67. navigation_layers = p_navigation_layers;
  68. NavigationServer::get_singleton()->region_set_navigation_layers(region, navigation_layers);
  69. }
  70. uint32_t NavigationMeshInstance::get_navigation_layers() const {
  71. return navigation_layers;
  72. }
  73. void NavigationMeshInstance::set_enter_cost(real_t p_enter_cost) {
  74. ERR_FAIL_COND_MSG(p_enter_cost < 0.0, "The enter_cost must be positive.");
  75. enter_cost = MAX(p_enter_cost, 0.0);
  76. NavigationServer::get_singleton()->region_set_enter_cost(region, p_enter_cost);
  77. }
  78. real_t NavigationMeshInstance::get_enter_cost() const {
  79. return enter_cost;
  80. }
  81. void NavigationMeshInstance::set_travel_cost(real_t p_travel_cost) {
  82. ERR_FAIL_COND_MSG(p_travel_cost < 0.0, "The travel_cost must be positive.");
  83. travel_cost = MAX(p_travel_cost, 0.0);
  84. NavigationServer::get_singleton()->region_set_travel_cost(region, travel_cost);
  85. }
  86. real_t NavigationMeshInstance::get_travel_cost() const {
  87. return travel_cost;
  88. }
  89. RID NavigationMeshInstance::get_region_rid() const {
  90. return region;
  91. }
  92. /////////////////////////////
  93. void NavigationMeshInstance::_notification(int p_what) {
  94. switch (p_what) {
  95. case NOTIFICATION_ENTER_TREE: {
  96. Spatial *c = this;
  97. while (c) {
  98. navigation = Object::cast_to<Navigation>(c);
  99. if (navigation) {
  100. if (enabled) {
  101. NavigationServer::get_singleton()->region_set_map(region, navigation->get_rid());
  102. }
  103. break;
  104. }
  105. c = c->get_parent_spatial();
  106. }
  107. if (enabled && navigation == nullptr) {
  108. // did not find a valid navigation node parent, fallback to default navigation map on world resource
  109. NavigationServer::get_singleton()->region_set_map(region, get_world()->get_navigation_map());
  110. }
  111. if (navmesh.is_valid() && get_tree()->is_debugging_navigation_hint()) {
  112. MeshInstance *dm = memnew(MeshInstance);
  113. dm->set_mesh(navmesh->get_debug_mesh());
  114. if (is_enabled()) {
  115. dm->set_material_override(get_tree()->get_debug_navigation_material());
  116. } else {
  117. dm->set_material_override(get_tree()->get_debug_navigation_disabled_material());
  118. }
  119. add_child(dm);
  120. debug_view = dm;
  121. }
  122. } break;
  123. case NOTIFICATION_TRANSFORM_CHANGED: {
  124. NavigationServer::get_singleton()->region_set_transform(region, get_global_transform());
  125. } break;
  126. case NOTIFICATION_EXIT_TREE: {
  127. if (navigation) {
  128. NavigationServer::get_singleton()->region_set_map(region, RID());
  129. }
  130. if (debug_view) {
  131. debug_view->queue_delete();
  132. debug_view = nullptr;
  133. }
  134. navigation = nullptr;
  135. } break;
  136. }
  137. }
  138. void NavigationMeshInstance::set_navigation_mesh(const Ref<NavigationMesh> &p_navmesh) {
  139. if (p_navmesh == navmesh)
  140. return;
  141. if (navmesh.is_valid()) {
  142. navmesh->remove_change_receptor(this);
  143. }
  144. navmesh = p_navmesh;
  145. if (navmesh.is_valid()) {
  146. navmesh->add_change_receptor(this);
  147. }
  148. NavigationServer::get_singleton()->region_set_navmesh(region, p_navmesh);
  149. if (debug_view == nullptr && is_inside_tree() && navmesh.is_valid() && get_tree()->is_debugging_navigation_hint()) {
  150. MeshInstance *dm = memnew(MeshInstance);
  151. dm->set_mesh(navmesh->get_debug_mesh());
  152. if (is_enabled()) {
  153. dm->set_material_override(get_tree()->get_debug_navigation_material());
  154. } else {
  155. dm->set_material_override(get_tree()->get_debug_navigation_disabled_material());
  156. }
  157. add_child(dm);
  158. debug_view = dm;
  159. }
  160. if (debug_view && navmesh.is_valid()) {
  161. Object::cast_to<MeshInstance>(debug_view)->set_mesh(navmesh->get_debug_mesh());
  162. }
  163. emit_signal("navigation_mesh_changed");
  164. update_gizmo();
  165. update_configuration_warning();
  166. }
  167. Ref<NavigationMesh> NavigationMeshInstance::get_navigation_mesh() const {
  168. return navmesh;
  169. }
  170. struct BakeThreadsArgs {
  171. NavigationMeshInstance *nav_region = nullptr;
  172. };
  173. void _bake_navigation_mesh(void *p_user_data) {
  174. BakeThreadsArgs *args = static_cast<BakeThreadsArgs *>(p_user_data);
  175. if (args->nav_region->get_navigation_mesh().is_valid()) {
  176. Ref<NavigationMesh> nav_mesh = args->nav_region->get_navigation_mesh()->duplicate();
  177. NavigationServer::get_singleton()->region_bake_navmesh(nav_mesh, args->nav_region);
  178. args->nav_region->call_deferred("_bake_finished", nav_mesh);
  179. memdelete(args);
  180. } else {
  181. ERR_PRINT("Can't bake the navigation mesh if the `NavigationMesh` resource doesn't exist");
  182. args->nav_region->call_deferred("_bake_finished", Ref<NavigationMesh>());
  183. memdelete(args);
  184. }
  185. }
  186. void NavigationMeshInstance::bake_navigation_mesh(bool p_on_thread) {
  187. ERR_FAIL_COND_MSG(bake_thread.is_started(), "Navigation Mesh Bake thread is already baking a Navigation Mesh. Unable to start another bake request.");
  188. BakeThreadsArgs *args = memnew(BakeThreadsArgs);
  189. args->nav_region = this;
  190. if (p_on_thread && !OS::get_singleton()->can_use_threads()) {
  191. WARN_PRINT("NavigationMesh bake 'on_thread' will be disabled as the current OS does not support multiple threads."
  192. "\nAs a fallback the navigation mesh will bake on the main thread which can cause framerate issues.");
  193. }
  194. if (p_on_thread && OS::get_singleton()->can_use_threads()) {
  195. bake_thread.start(_bake_navigation_mesh, args);
  196. } else {
  197. _bake_navigation_mesh(args);
  198. }
  199. }
  200. void NavigationMeshInstance::_bake_finished(Ref<NavigationMesh> p_nav_mesh) {
  201. set_navigation_mesh(p_nav_mesh);
  202. bake_thread.wait_to_finish();
  203. emit_signal("bake_finished");
  204. }
  205. String NavigationMeshInstance::get_configuration_warning() const {
  206. String warning = Spatial::get_configuration_warning();
  207. if (!navmesh.is_valid()) {
  208. if (warning != String()) {
  209. warning += "\n\n";
  210. }
  211. warning += TTR("A NavigationMesh resource must be set or created for this node to work.");
  212. }
  213. return warning;
  214. }
  215. void NavigationMeshInstance::_bind_methods() {
  216. ClassDB::bind_method(D_METHOD("set_navigation_mesh", "navmesh"), &NavigationMeshInstance::set_navigation_mesh);
  217. ClassDB::bind_method(D_METHOD("get_navigation_mesh"), &NavigationMeshInstance::get_navigation_mesh);
  218. ClassDB::bind_method(D_METHOD("set_enabled", "enabled"), &NavigationMeshInstance::set_enabled);
  219. ClassDB::bind_method(D_METHOD("is_enabled"), &NavigationMeshInstance::is_enabled);
  220. ClassDB::bind_method(D_METHOD("set_navigation_layers", "navigation_layers"), &NavigationMeshInstance::set_navigation_layers);
  221. ClassDB::bind_method(D_METHOD("get_navigation_layers"), &NavigationMeshInstance::get_navigation_layers);
  222. ClassDB::bind_method(D_METHOD("get_region_rid"), &NavigationMeshInstance::get_region_rid);
  223. ClassDB::bind_method(D_METHOD("set_enter_cost", "enter_cost"), &NavigationMeshInstance::set_enter_cost);
  224. ClassDB::bind_method(D_METHOD("get_enter_cost"), &NavigationMeshInstance::get_enter_cost);
  225. ClassDB::bind_method(D_METHOD("set_travel_cost", "travel_cost"), &NavigationMeshInstance::set_travel_cost);
  226. ClassDB::bind_method(D_METHOD("get_travel_cost"), &NavigationMeshInstance::get_travel_cost);
  227. ClassDB::bind_method(D_METHOD("bake_navigation_mesh", "on_thread"), &NavigationMeshInstance::bake_navigation_mesh, DEFVAL(true));
  228. ClassDB::bind_method(D_METHOD("_bake_finished", "nav_mesh"), &NavigationMeshInstance::_bake_finished);
  229. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "navmesh", PROPERTY_HINT_RESOURCE_TYPE, "NavigationMesh"), "set_navigation_mesh", "get_navigation_mesh");
  230. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "enabled"), "set_enabled", "is_enabled");
  231. ADD_PROPERTY(PropertyInfo(Variant::INT, "navigation_layers", PROPERTY_HINT_LAYERS_3D_NAVIGATION), "set_navigation_layers", "get_navigation_layers");
  232. ADD_PROPERTY(PropertyInfo(Variant::REAL, "enter_cost"), "set_enter_cost", "get_enter_cost");
  233. ADD_PROPERTY(PropertyInfo(Variant::REAL, "travel_cost"), "set_travel_cost", "get_travel_cost");
  234. ADD_SIGNAL(MethodInfo("navigation_mesh_changed"));
  235. ADD_SIGNAL(MethodInfo("bake_finished"));
  236. }
  237. void NavigationMeshInstance::_changed_callback(Object *p_changed, const char *p_prop) {
  238. update_gizmo();
  239. update_configuration_warning();
  240. }
  241. NavigationMeshInstance::NavigationMeshInstance() {
  242. set_notify_transform(true);
  243. region = NavigationServer::get_singleton()->region_create();
  244. NavigationServer::get_singleton()->region_set_enter_cost(region, get_enter_cost());
  245. NavigationServer::get_singleton()->region_set_travel_cost(region, get_travel_cost());
  246. enabled = true;
  247. }
  248. NavigationMeshInstance::~NavigationMeshInstance() {
  249. if (navmesh.is_valid()) {
  250. navmesh->remove_change_receptor(this);
  251. }
  252. NavigationServer::get_singleton()->free(region);
  253. }