navigation_link_3d.cpp 18 KB

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