navigation_mesh_instance.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*************************************************************************/
  2. /* navigation_mesh_instance.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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. #include "navigation_mesh_instance.h"
  31. #include "core/os/thread.h"
  32. #include "mesh_instance.h"
  33. #include "navigation.h"
  34. #include "servers/navigation_server.h"
  35. void NavigationMeshInstance::set_enabled(bool p_enabled) {
  36. if (enabled == p_enabled) {
  37. return;
  38. }
  39. enabled = p_enabled;
  40. if (!is_inside_tree()) {
  41. return;
  42. }
  43. if (!enabled) {
  44. NavigationServer::get_singleton()->region_set_map(region, RID());
  45. } else {
  46. if (navigation) {
  47. NavigationServer::get_singleton()->region_set_map(region, navigation->get_rid());
  48. }
  49. }
  50. if (debug_view) {
  51. MeshInstance *dm = Object::cast_to<MeshInstance>(debug_view);
  52. if (is_enabled()) {
  53. dm->set_material_override(get_tree()->get_debug_navigation_material());
  54. } else {
  55. dm->set_material_override(get_tree()->get_debug_navigation_disabled_material());
  56. }
  57. }
  58. update_gizmo();
  59. }
  60. bool NavigationMeshInstance::is_enabled() const {
  61. return enabled;
  62. }
  63. /////////////////////////////
  64. void NavigationMeshInstance::_notification(int p_what) {
  65. switch (p_what) {
  66. case NOTIFICATION_ENTER_TREE: {
  67. Spatial *c = this;
  68. while (c) {
  69. navigation = Object::cast_to<Navigation>(c);
  70. if (navigation) {
  71. if (enabled) {
  72. NavigationServer::get_singleton()->region_set_map(region, navigation->get_rid());
  73. }
  74. break;
  75. }
  76. c = c->get_parent_spatial();
  77. }
  78. if (navmesh.is_valid() && get_tree()->is_debugging_navigation_hint()) {
  79. MeshInstance *dm = memnew(MeshInstance);
  80. dm->set_mesh(navmesh->get_debug_mesh());
  81. if (is_enabled()) {
  82. dm->set_material_override(get_tree()->get_debug_navigation_material());
  83. } else {
  84. dm->set_material_override(get_tree()->get_debug_navigation_disabled_material());
  85. }
  86. add_child(dm);
  87. debug_view = dm;
  88. }
  89. } break;
  90. case NOTIFICATION_TRANSFORM_CHANGED: {
  91. NavigationServer::get_singleton()->region_set_transform(region, get_global_transform());
  92. } break;
  93. case NOTIFICATION_EXIT_TREE: {
  94. if (navigation) {
  95. NavigationServer::get_singleton()->region_set_map(region, RID());
  96. }
  97. if (debug_view) {
  98. debug_view->queue_delete();
  99. debug_view = nullptr;
  100. }
  101. navigation = nullptr;
  102. } break;
  103. }
  104. }
  105. void NavigationMeshInstance::set_navigation_mesh(const Ref<NavigationMesh> &p_navmesh) {
  106. if (p_navmesh == navmesh)
  107. return;
  108. if (navmesh.is_valid()) {
  109. navmesh->remove_change_receptor(this);
  110. }
  111. navmesh = p_navmesh;
  112. if (navmesh.is_valid()) {
  113. navmesh->add_change_receptor(this);
  114. }
  115. NavigationServer::get_singleton()->region_set_navmesh(region, p_navmesh);
  116. if (debug_view && navmesh.is_valid()) {
  117. Object::cast_to<MeshInstance>(debug_view)->set_mesh(navmesh->get_debug_mesh());
  118. }
  119. emit_signal("navigation_mesh_changed");
  120. update_gizmo();
  121. update_configuration_warning();
  122. }
  123. Ref<NavigationMesh> NavigationMeshInstance::get_navigation_mesh() const {
  124. return navmesh;
  125. }
  126. struct BakeThreadsArgs {
  127. NavigationMeshInstance *nav_region;
  128. };
  129. void _bake_navigation_mesh(void *p_user_data) {
  130. BakeThreadsArgs *args = static_cast<BakeThreadsArgs *>(p_user_data);
  131. if (args->nav_region->get_navigation_mesh().is_valid()) {
  132. Ref<NavigationMesh> nav_mesh = args->nav_region->get_navigation_mesh()->duplicate();
  133. NavigationServer::get_singleton()->region_bake_navmesh(nav_mesh, args->nav_region);
  134. args->nav_region->call_deferred("_bake_finished", nav_mesh);
  135. memdelete(args);
  136. } else {
  137. ERR_PRINT("Can't bake the navigation mesh if the `NavigationMesh` resource doesn't exist");
  138. args->nav_region->call_deferred("_bake_finished", Ref<NavigationMesh>());
  139. memdelete(args);
  140. }
  141. }
  142. void NavigationMeshInstance::bake_navigation_mesh() {
  143. ERR_FAIL_COND_MSG(bake_thread.is_started(), "Navigation Mesh Bake thread is already baking a Navigation Mesh. Unable to start another bake request.");
  144. BakeThreadsArgs *args = memnew(BakeThreadsArgs);
  145. args->nav_region = this;
  146. bake_thread.start(_bake_navigation_mesh, args);
  147. }
  148. void NavigationMeshInstance::_bake_finished(Ref<NavigationMesh> p_nav_mesh) {
  149. set_navigation_mesh(p_nav_mesh);
  150. bake_thread.wait_to_finish();
  151. emit_signal("bake_finished");
  152. }
  153. String NavigationMeshInstance::get_configuration_warning() const {
  154. if (!is_visible_in_tree() || !is_inside_tree()) {
  155. return String();
  156. }
  157. if (!navmesh.is_valid()) {
  158. return TTR("A NavigationMesh resource must be set or created for this node to work.");
  159. }
  160. const Spatial *c = this;
  161. while (c) {
  162. if (Object::cast_to<Navigation>(c)) {
  163. return String();
  164. }
  165. c = Object::cast_to<Spatial>(c->get_parent());
  166. }
  167. return TTR("NavigationMeshInstance must be a child or grandchild to a Navigation node. It only provides navigation data.");
  168. }
  169. void NavigationMeshInstance::_bind_methods() {
  170. ClassDB::bind_method(D_METHOD("set_navigation_mesh", "navmesh"), &NavigationMeshInstance::set_navigation_mesh);
  171. ClassDB::bind_method(D_METHOD("get_navigation_mesh"), &NavigationMeshInstance::get_navigation_mesh);
  172. ClassDB::bind_method(D_METHOD("set_enabled", "enabled"), &NavigationMeshInstance::set_enabled);
  173. ClassDB::bind_method(D_METHOD("is_enabled"), &NavigationMeshInstance::is_enabled);
  174. ClassDB::bind_method(D_METHOD("bake_navigation_mesh"), &NavigationMeshInstance::bake_navigation_mesh);
  175. ClassDB::bind_method(D_METHOD("_bake_finished", "nav_mesh"), &NavigationMeshInstance::_bake_finished);
  176. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "navmesh", PROPERTY_HINT_RESOURCE_TYPE, "NavigationMesh"), "set_navigation_mesh", "get_navigation_mesh");
  177. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "enabled"), "set_enabled", "is_enabled");
  178. ADD_SIGNAL(MethodInfo("navigation_mesh_changed"));
  179. ADD_SIGNAL(MethodInfo("bake_finished"));
  180. }
  181. void NavigationMeshInstance::_changed_callback(Object *p_changed, const char *p_prop) {
  182. update_gizmo();
  183. update_configuration_warning();
  184. }
  185. NavigationMeshInstance::NavigationMeshInstance() {
  186. set_notify_transform(true);
  187. region = NavigationServer::get_singleton()->region_create();
  188. enabled = true;
  189. }
  190. NavigationMeshInstance::~NavigationMeshInstance() {
  191. if (navmesh.is_valid()) {
  192. navmesh->remove_change_receptor(this);
  193. }
  194. NavigationServer::get_singleton()->free(region);
  195. }