navigation_link_3d.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. /**************************************************************************/
  2. /* navigation_link_3d.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_link_3d.h"
  31. #include "mesh_instance_3d.h"
  32. #include "servers/navigation_server_3d.h"
  33. #ifdef DEBUG_ENABLED
  34. void NavigationLink3D::_update_debug_mesh() {
  35. if (!is_inside_tree()) {
  36. return;
  37. }
  38. if (Engine::get_singleton()->is_editor_hint()) {
  39. // don't update inside Editor as node 3d gizmo takes care of this
  40. // as collisions and selections for Editor Viewport need to be updated
  41. return;
  42. }
  43. if (!NavigationServer3D::get_singleton()->get_debug_navigation_enabled()) {
  44. if (debug_instance.is_valid()) {
  45. RS::get_singleton()->instance_set_visible(debug_instance, false);
  46. }
  47. return;
  48. }
  49. if (!debug_instance.is_valid()) {
  50. debug_instance = RenderingServer::get_singleton()->instance_create();
  51. }
  52. if (!debug_mesh.is_valid()) {
  53. debug_mesh = Ref<ArrayMesh>(memnew(ArrayMesh));
  54. }
  55. RID nav_map = get_world_3d()->get_navigation_map();
  56. real_t search_radius = NavigationServer3D::get_singleton()->map_get_link_connection_radius(nav_map);
  57. Vector3 up_vector = NavigationServer3D::get_singleton()->map_get_up(nav_map);
  58. Vector3::Axis up_axis = up_vector.max_axis_index();
  59. debug_mesh->clear_surfaces();
  60. Vector<Vector3> lines;
  61. // Draw line between the points.
  62. lines.push_back(start_position);
  63. lines.push_back(end_position);
  64. // Draw start position search radius
  65. for (int i = 0; i < 30; i++) {
  66. // Create a circle
  67. const float ra = Math::deg_to_rad((float)(i * 12));
  68. const float rb = Math::deg_to_rad((float)((i + 1) * 12));
  69. const Point2 a = Vector2(Math::sin(ra), Math::cos(ra)) * search_radius;
  70. const Point2 b = Vector2(Math::sin(rb), Math::cos(rb)) * search_radius;
  71. // Draw axis-aligned circle
  72. switch (up_axis) {
  73. case Vector3::AXIS_X:
  74. lines.append(start_position + Vector3(0, a.x, a.y));
  75. lines.append(start_position + Vector3(0, b.x, b.y));
  76. break;
  77. case Vector3::AXIS_Y:
  78. lines.append(start_position + Vector3(a.x, 0, a.y));
  79. lines.append(start_position + Vector3(b.x, 0, b.y));
  80. break;
  81. case Vector3::AXIS_Z:
  82. lines.append(start_position + Vector3(a.x, a.y, 0));
  83. lines.append(start_position + Vector3(b.x, b.y, 0));
  84. break;
  85. }
  86. }
  87. // Draw end position search radius
  88. for (int i = 0; i < 30; i++) {
  89. // Create a circle
  90. const float ra = Math::deg_to_rad((float)(i * 12));
  91. const float rb = Math::deg_to_rad((float)((i + 1) * 12));
  92. const Point2 a = Vector2(Math::sin(ra), Math::cos(ra)) * search_radius;
  93. const Point2 b = Vector2(Math::sin(rb), Math::cos(rb)) * search_radius;
  94. // Draw axis-aligned circle
  95. switch (up_axis) {
  96. case Vector3::AXIS_X:
  97. lines.append(end_position + Vector3(0, a.x, a.y));
  98. lines.append(end_position + Vector3(0, b.x, b.y));
  99. break;
  100. case Vector3::AXIS_Y:
  101. lines.append(end_position + Vector3(a.x, 0, a.y));
  102. lines.append(end_position + Vector3(b.x, 0, b.y));
  103. break;
  104. case Vector3::AXIS_Z:
  105. lines.append(end_position + Vector3(a.x, a.y, 0));
  106. lines.append(end_position + Vector3(b.x, b.y, 0));
  107. break;
  108. }
  109. }
  110. Array mesh_array;
  111. mesh_array.resize(Mesh::ARRAY_MAX);
  112. mesh_array[Mesh::ARRAY_VERTEX] = lines;
  113. debug_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_LINES, mesh_array);
  114. RS::get_singleton()->instance_set_base(debug_instance, debug_mesh->get_rid());
  115. RS::get_singleton()->instance_set_scenario(debug_instance, get_world_3d()->get_scenario());
  116. RS::get_singleton()->instance_set_visible(debug_instance, is_visible_in_tree());
  117. Ref<StandardMaterial3D> link_material = NavigationServer3D::get_singleton()->get_debug_navigation_link_connections_material();
  118. Ref<StandardMaterial3D> disabled_link_material = NavigationServer3D::get_singleton()->get_debug_navigation_link_connections_disabled_material();
  119. if (enabled) {
  120. RS::get_singleton()->instance_set_surface_override_material(debug_instance, 0, link_material->get_rid());
  121. } else {
  122. RS::get_singleton()->instance_set_surface_override_material(debug_instance, 0, disabled_link_material->get_rid());
  123. }
  124. RS::get_singleton()->instance_set_transform(debug_instance, current_global_transform);
  125. }
  126. #endif // DEBUG_ENABLED
  127. void NavigationLink3D::_bind_methods() {
  128. ClassDB::bind_method(D_METHOD("get_rid"), &NavigationLink3D::get_rid);
  129. ClassDB::bind_method(D_METHOD("set_enabled", "enabled"), &NavigationLink3D::set_enabled);
  130. ClassDB::bind_method(D_METHOD("is_enabled"), &NavigationLink3D::is_enabled);
  131. ClassDB::bind_method(D_METHOD("set_bidirectional", "bidirectional"), &NavigationLink3D::set_bidirectional);
  132. ClassDB::bind_method(D_METHOD("is_bidirectional"), &NavigationLink3D::is_bidirectional);
  133. ClassDB::bind_method(D_METHOD("set_navigation_layers", "navigation_layers"), &NavigationLink3D::set_navigation_layers);
  134. ClassDB::bind_method(D_METHOD("get_navigation_layers"), &NavigationLink3D::get_navigation_layers);
  135. ClassDB::bind_method(D_METHOD("set_navigation_layer_value", "layer_number", "value"), &NavigationLink3D::set_navigation_layer_value);
  136. ClassDB::bind_method(D_METHOD("get_navigation_layer_value", "layer_number"), &NavigationLink3D::get_navigation_layer_value);
  137. ClassDB::bind_method(D_METHOD("set_start_position", "position"), &NavigationLink3D::set_start_position);
  138. ClassDB::bind_method(D_METHOD("get_start_position"), &NavigationLink3D::get_start_position);
  139. ClassDB::bind_method(D_METHOD("set_end_position", "position"), &NavigationLink3D::set_end_position);
  140. ClassDB::bind_method(D_METHOD("get_end_position"), &NavigationLink3D::get_end_position);
  141. ClassDB::bind_method(D_METHOD("set_global_start_position", "position"), &NavigationLink3D::set_global_start_position);
  142. ClassDB::bind_method(D_METHOD("get_global_start_position"), &NavigationLink3D::get_global_start_position);
  143. ClassDB::bind_method(D_METHOD("set_global_end_position", "position"), &NavigationLink3D::set_global_end_position);
  144. ClassDB::bind_method(D_METHOD("get_global_end_position"), &NavigationLink3D::get_global_end_position);
  145. ClassDB::bind_method(D_METHOD("set_enter_cost", "enter_cost"), &NavigationLink3D::set_enter_cost);
  146. ClassDB::bind_method(D_METHOD("get_enter_cost"), &NavigationLink3D::get_enter_cost);
  147. ClassDB::bind_method(D_METHOD("set_travel_cost", "travel_cost"), &NavigationLink3D::set_travel_cost);
  148. ClassDB::bind_method(D_METHOD("get_travel_cost"), &NavigationLink3D::get_travel_cost);
  149. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "enabled"), "set_enabled", "is_enabled");
  150. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "bidirectional"), "set_bidirectional", "is_bidirectional");
  151. ADD_PROPERTY(PropertyInfo(Variant::INT, "navigation_layers", PROPERTY_HINT_LAYERS_3D_NAVIGATION), "set_navigation_layers", "get_navigation_layers");
  152. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "start_position"), "set_start_position", "get_start_position");
  153. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "end_position"), "set_end_position", "get_end_position");
  154. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "enter_cost"), "set_enter_cost", "get_enter_cost");
  155. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "travel_cost"), "set_travel_cost", "get_travel_cost");
  156. }
  157. #ifndef DISABLE_DEPRECATED
  158. bool NavigationLink3D::_set(const StringName &p_name, const Variant &p_value) {
  159. if (p_name == "start_location") {
  160. set_start_position(p_value);
  161. return true;
  162. }
  163. if (p_name == "end_location") {
  164. set_end_position(p_value);
  165. return true;
  166. }
  167. return false;
  168. }
  169. bool NavigationLink3D::_get(const StringName &p_name, Variant &r_ret) const {
  170. if (p_name == "start_location") {
  171. r_ret = get_start_position();
  172. return true;
  173. }
  174. if (p_name == "end_location") {
  175. r_ret = get_end_position();
  176. return true;
  177. }
  178. return false;
  179. }
  180. #endif // DISABLE_DEPRECATED
  181. void NavigationLink3D::_notification(int p_what) {
  182. switch (p_what) {
  183. case NOTIFICATION_ENTER_TREE: {
  184. if (enabled) {
  185. NavigationServer3D::get_singleton()->link_set_map(link, get_world_3d()->get_navigation_map());
  186. }
  187. current_global_transform = get_global_transform();
  188. NavigationServer3D::get_singleton()->link_set_start_position(link, current_global_transform.xform(start_position));
  189. NavigationServer3D::get_singleton()->link_set_end_position(link, current_global_transform.xform(end_position));
  190. #ifdef DEBUG_ENABLED
  191. _update_debug_mesh();
  192. #endif // DEBUG_ENABLED
  193. } break;
  194. case NOTIFICATION_TRANSFORM_CHANGED: {
  195. set_physics_process_internal(true);
  196. } break;
  197. case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
  198. set_physics_process_internal(false);
  199. if (is_inside_tree()) {
  200. Transform3D new_global_transform = get_global_transform();
  201. if (current_global_transform != new_global_transform) {
  202. current_global_transform = new_global_transform;
  203. NavigationServer3D::get_singleton()->link_set_start_position(link, current_global_transform.xform(start_position));
  204. NavigationServer3D::get_singleton()->link_set_end_position(link, current_global_transform.xform(end_position));
  205. #ifdef DEBUG_ENABLED
  206. if (debug_instance.is_valid()) {
  207. RS::get_singleton()->instance_set_transform(debug_instance, current_global_transform);
  208. }
  209. #endif // DEBUG_ENABLED
  210. }
  211. }
  212. } break;
  213. case NOTIFICATION_EXIT_TREE: {
  214. NavigationServer3D::get_singleton()->link_set_map(link, RID());
  215. #ifdef DEBUG_ENABLED
  216. if (debug_instance.is_valid()) {
  217. RS::get_singleton()->instance_set_scenario(debug_instance, RID());
  218. RS::get_singleton()->instance_set_visible(debug_instance, false);
  219. }
  220. #endif // DEBUG_ENABLED
  221. } break;
  222. }
  223. }
  224. NavigationLink3D::NavigationLink3D() {
  225. link = NavigationServer3D::get_singleton()->link_create();
  226. NavigationServer3D::get_singleton()->link_set_owner_id(link, get_instance_id());
  227. NavigationServer3D::get_singleton()->link_set_enter_cost(link, enter_cost);
  228. NavigationServer3D::get_singleton()->link_set_travel_cost(link, travel_cost);
  229. NavigationServer3D::get_singleton()->link_set_navigation_layers(link, navigation_layers);
  230. NavigationServer3D::get_singleton()->link_set_bidirectional(link, bidirectional);
  231. NavigationServer3D::get_singleton()->link_set_enabled(link, enabled);
  232. set_notify_transform(true);
  233. }
  234. NavigationLink3D::~NavigationLink3D() {
  235. ERR_FAIL_NULL(NavigationServer3D::get_singleton());
  236. NavigationServer3D::get_singleton()->free(link);
  237. link = RID();
  238. #ifdef DEBUG_ENABLED
  239. ERR_FAIL_NULL(RenderingServer::get_singleton());
  240. if (debug_instance.is_valid()) {
  241. RenderingServer::get_singleton()->free(debug_instance);
  242. }
  243. if (debug_mesh.is_valid()) {
  244. RenderingServer::get_singleton()->free(debug_mesh->get_rid());
  245. }
  246. #endif // DEBUG_ENABLED
  247. }
  248. RID NavigationLink3D::get_rid() const {
  249. return link;
  250. }
  251. void NavigationLink3D::set_enabled(bool p_enabled) {
  252. if (enabled == p_enabled) {
  253. return;
  254. }
  255. enabled = p_enabled;
  256. NavigationServer3D::get_singleton()->link_set_enabled(link, enabled);
  257. #ifdef DEBUG_ENABLED
  258. if (debug_instance.is_valid() && debug_mesh.is_valid()) {
  259. if (enabled) {
  260. Ref<StandardMaterial3D> link_material = NavigationServer3D::get_singleton()->get_debug_navigation_link_connections_material();
  261. RS::get_singleton()->instance_set_surface_override_material(debug_instance, 0, link_material->get_rid());
  262. } else {
  263. Ref<StandardMaterial3D> disabled_link_material = NavigationServer3D::get_singleton()->get_debug_navigation_link_connections_disabled_material();
  264. RS::get_singleton()->instance_set_surface_override_material(debug_instance, 0, disabled_link_material->get_rid());
  265. }
  266. }
  267. #endif // DEBUG_ENABLED
  268. update_gizmos();
  269. }
  270. void NavigationLink3D::set_bidirectional(bool p_bidirectional) {
  271. if (bidirectional == p_bidirectional) {
  272. return;
  273. }
  274. bidirectional = p_bidirectional;
  275. NavigationServer3D::get_singleton()->link_set_bidirectional(link, bidirectional);
  276. }
  277. void NavigationLink3D::set_navigation_layers(uint32_t p_navigation_layers) {
  278. if (navigation_layers == p_navigation_layers) {
  279. return;
  280. }
  281. navigation_layers = p_navigation_layers;
  282. NavigationServer3D::get_singleton()->link_set_navigation_layers(link, navigation_layers);
  283. }
  284. void NavigationLink3D::set_navigation_layer_value(int p_layer_number, bool p_value) {
  285. ERR_FAIL_COND_MSG(p_layer_number < 1, "Navigation layer number must be between 1 and 32 inclusive.");
  286. ERR_FAIL_COND_MSG(p_layer_number > 32, "Navigation layer number must be between 1 and 32 inclusive.");
  287. uint32_t _navigation_layers = get_navigation_layers();
  288. if (p_value) {
  289. _navigation_layers |= 1 << (p_layer_number - 1);
  290. } else {
  291. _navigation_layers &= ~(1 << (p_layer_number - 1));
  292. }
  293. set_navigation_layers(_navigation_layers);
  294. }
  295. bool NavigationLink3D::get_navigation_layer_value(int p_layer_number) const {
  296. ERR_FAIL_COND_V_MSG(p_layer_number < 1, false, "Navigation layer number must be between 1 and 32 inclusive.");
  297. ERR_FAIL_COND_V_MSG(p_layer_number > 32, false, "Navigation layer number must be between 1 and 32 inclusive.");
  298. return get_navigation_layers() & (1 << (p_layer_number - 1));
  299. }
  300. void NavigationLink3D::set_start_position(Vector3 p_position) {
  301. if (start_position.is_equal_approx(p_position)) {
  302. return;
  303. }
  304. start_position = p_position;
  305. if (!is_inside_tree()) {
  306. return;
  307. }
  308. NavigationServer3D::get_singleton()->link_set_start_position(link, current_global_transform.xform(start_position));
  309. #ifdef DEBUG_ENABLED
  310. _update_debug_mesh();
  311. #endif // DEBUG_ENABLED
  312. update_gizmos();
  313. update_configuration_warnings();
  314. }
  315. void NavigationLink3D::set_end_position(Vector3 p_position) {
  316. if (end_position.is_equal_approx(p_position)) {
  317. return;
  318. }
  319. end_position = p_position;
  320. if (!is_inside_tree()) {
  321. return;
  322. }
  323. NavigationServer3D::get_singleton()->link_set_end_position(link, current_global_transform.xform(end_position));
  324. #ifdef DEBUG_ENABLED
  325. _update_debug_mesh();
  326. #endif // DEBUG_ENABLED
  327. update_gizmos();
  328. update_configuration_warnings();
  329. }
  330. void NavigationLink3D::set_global_start_position(Vector3 p_position) {
  331. if (is_inside_tree()) {
  332. set_start_position(to_local(p_position));
  333. } else {
  334. set_start_position(p_position);
  335. }
  336. }
  337. Vector3 NavigationLink3D::get_global_start_position() const {
  338. if (is_inside_tree()) {
  339. return to_global(start_position);
  340. } else {
  341. return start_position;
  342. }
  343. }
  344. void NavigationLink3D::set_global_end_position(Vector3 p_position) {
  345. if (is_inside_tree()) {
  346. set_end_position(to_local(p_position));
  347. } else {
  348. set_end_position(p_position);
  349. }
  350. }
  351. Vector3 NavigationLink3D::get_global_end_position() const {
  352. if (is_inside_tree()) {
  353. return to_global(end_position);
  354. } else {
  355. return end_position;
  356. }
  357. }
  358. void NavigationLink3D::set_enter_cost(real_t p_enter_cost) {
  359. ERR_FAIL_COND_MSG(p_enter_cost < 0.0, "The enter_cost must be positive.");
  360. if (Math::is_equal_approx(enter_cost, p_enter_cost)) {
  361. return;
  362. }
  363. enter_cost = p_enter_cost;
  364. NavigationServer3D::get_singleton()->link_set_enter_cost(link, enter_cost);
  365. }
  366. void NavigationLink3D::set_travel_cost(real_t p_travel_cost) {
  367. ERR_FAIL_COND_MSG(p_travel_cost < 0.0, "The travel_cost must be positive.");
  368. if (Math::is_equal_approx(travel_cost, p_travel_cost)) {
  369. return;
  370. }
  371. travel_cost = p_travel_cost;
  372. NavigationServer3D::get_singleton()->link_set_travel_cost(link, travel_cost);
  373. }
  374. PackedStringArray NavigationLink3D::get_configuration_warnings() const {
  375. PackedStringArray warnings = Node::get_configuration_warnings();
  376. if (start_position.is_equal_approx(end_position)) {
  377. warnings.push_back(RTR("NavigationLink3D start position should be different than the end position to be useful."));
  378. }
  379. return warnings;
  380. }