navigation_link_3d.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  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_null()) {
  53. debug_mesh.instantiate();
  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_navigation_map", "navigation_map"), &NavigationLink3D::set_navigation_map);
  132. ClassDB::bind_method(D_METHOD("get_navigation_map"), &NavigationLink3D::get_navigation_map);
  133. ClassDB::bind_method(D_METHOD("set_bidirectional", "bidirectional"), &NavigationLink3D::set_bidirectional);
  134. ClassDB::bind_method(D_METHOD("is_bidirectional"), &NavigationLink3D::is_bidirectional);
  135. ClassDB::bind_method(D_METHOD("set_navigation_layers", "navigation_layers"), &NavigationLink3D::set_navigation_layers);
  136. ClassDB::bind_method(D_METHOD("get_navigation_layers"), &NavigationLink3D::get_navigation_layers);
  137. ClassDB::bind_method(D_METHOD("set_navigation_layer_value", "layer_number", "value"), &NavigationLink3D::set_navigation_layer_value);
  138. ClassDB::bind_method(D_METHOD("get_navigation_layer_value", "layer_number"), &NavigationLink3D::get_navigation_layer_value);
  139. ClassDB::bind_method(D_METHOD("set_start_position", "position"), &NavigationLink3D::set_start_position);
  140. ClassDB::bind_method(D_METHOD("get_start_position"), &NavigationLink3D::get_start_position);
  141. ClassDB::bind_method(D_METHOD("set_end_position", "position"), &NavigationLink3D::set_end_position);
  142. ClassDB::bind_method(D_METHOD("get_end_position"), &NavigationLink3D::get_end_position);
  143. ClassDB::bind_method(D_METHOD("set_global_start_position", "position"), &NavigationLink3D::set_global_start_position);
  144. ClassDB::bind_method(D_METHOD("get_global_start_position"), &NavigationLink3D::get_global_start_position);
  145. ClassDB::bind_method(D_METHOD("set_global_end_position", "position"), &NavigationLink3D::set_global_end_position);
  146. ClassDB::bind_method(D_METHOD("get_global_end_position"), &NavigationLink3D::get_global_end_position);
  147. ClassDB::bind_method(D_METHOD("set_enter_cost", "enter_cost"), &NavigationLink3D::set_enter_cost);
  148. ClassDB::bind_method(D_METHOD("get_enter_cost"), &NavigationLink3D::get_enter_cost);
  149. ClassDB::bind_method(D_METHOD("set_travel_cost", "travel_cost"), &NavigationLink3D::set_travel_cost);
  150. ClassDB::bind_method(D_METHOD("get_travel_cost"), &NavigationLink3D::get_travel_cost);
  151. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "enabled"), "set_enabled", "is_enabled");
  152. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "bidirectional"), "set_bidirectional", "is_bidirectional");
  153. ADD_PROPERTY(PropertyInfo(Variant::INT, "navigation_layers", PROPERTY_HINT_LAYERS_3D_NAVIGATION), "set_navigation_layers", "get_navigation_layers");
  154. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "start_position"), "set_start_position", "get_start_position");
  155. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "end_position"), "set_end_position", "get_end_position");
  156. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "enter_cost"), "set_enter_cost", "get_enter_cost");
  157. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "travel_cost"), "set_travel_cost", "get_travel_cost");
  158. }
  159. #ifndef DISABLE_DEPRECATED
  160. bool NavigationLink3D::_set(const StringName &p_name, const Variant &p_value) {
  161. if (p_name == "start_location") {
  162. set_start_position(p_value);
  163. return true;
  164. }
  165. if (p_name == "end_location") {
  166. set_end_position(p_value);
  167. return true;
  168. }
  169. return false;
  170. }
  171. bool NavigationLink3D::_get(const StringName &p_name, Variant &r_ret) const {
  172. if (p_name == "start_location") {
  173. r_ret = get_start_position();
  174. return true;
  175. }
  176. if (p_name == "end_location") {
  177. r_ret = get_end_position();
  178. return true;
  179. }
  180. return false;
  181. }
  182. #endif // DISABLE_DEPRECATED
  183. void NavigationLink3D::_notification(int p_what) {
  184. switch (p_what) {
  185. case NOTIFICATION_ENTER_TREE: {
  186. _link_enter_navigation_map();
  187. } break;
  188. case NOTIFICATION_TRANSFORM_CHANGED: {
  189. set_physics_process_internal(true);
  190. } break;
  191. case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
  192. set_physics_process_internal(false);
  193. _link_update_transform();
  194. } break;
  195. case NOTIFICATION_EXIT_TREE: {
  196. _link_exit_navigation_map();
  197. } break;
  198. }
  199. }
  200. NavigationLink3D::NavigationLink3D() {
  201. link = NavigationServer3D::get_singleton()->link_create();
  202. NavigationServer3D::get_singleton()->link_set_owner_id(link, get_instance_id());
  203. NavigationServer3D::get_singleton()->link_set_enter_cost(link, enter_cost);
  204. NavigationServer3D::get_singleton()->link_set_travel_cost(link, travel_cost);
  205. NavigationServer3D::get_singleton()->link_set_navigation_layers(link, navigation_layers);
  206. NavigationServer3D::get_singleton()->link_set_bidirectional(link, bidirectional);
  207. NavigationServer3D::get_singleton()->link_set_enabled(link, enabled);
  208. set_notify_transform(true);
  209. }
  210. NavigationLink3D::~NavigationLink3D() {
  211. ERR_FAIL_NULL(NavigationServer3D::get_singleton());
  212. NavigationServer3D::get_singleton()->free(link);
  213. link = RID();
  214. #ifdef DEBUG_ENABLED
  215. ERR_FAIL_NULL(RenderingServer::get_singleton());
  216. if (debug_instance.is_valid()) {
  217. RenderingServer::get_singleton()->free(debug_instance);
  218. }
  219. if (debug_mesh.is_valid()) {
  220. RenderingServer::get_singleton()->free(debug_mesh->get_rid());
  221. }
  222. #endif // DEBUG_ENABLED
  223. }
  224. RID NavigationLink3D::get_rid() const {
  225. return link;
  226. }
  227. void NavigationLink3D::set_enabled(bool p_enabled) {
  228. if (enabled == p_enabled) {
  229. return;
  230. }
  231. enabled = p_enabled;
  232. NavigationServer3D::get_singleton()->link_set_enabled(link, enabled);
  233. #ifdef DEBUG_ENABLED
  234. if (debug_instance.is_valid() && debug_mesh.is_valid()) {
  235. if (enabled) {
  236. Ref<StandardMaterial3D> link_material = NavigationServer3D::get_singleton()->get_debug_navigation_link_connections_material();
  237. RS::get_singleton()->instance_set_surface_override_material(debug_instance, 0, link_material->get_rid());
  238. } else {
  239. Ref<StandardMaterial3D> disabled_link_material = NavigationServer3D::get_singleton()->get_debug_navigation_link_connections_disabled_material();
  240. RS::get_singleton()->instance_set_surface_override_material(debug_instance, 0, disabled_link_material->get_rid());
  241. }
  242. }
  243. #endif // DEBUG_ENABLED
  244. update_gizmos();
  245. }
  246. void NavigationLink3D::set_navigation_map(RID p_navigation_map) {
  247. if (map_override == p_navigation_map) {
  248. return;
  249. }
  250. map_override = p_navigation_map;
  251. NavigationServer3D::get_singleton()->link_set_map(link, map_override);
  252. }
  253. RID NavigationLink3D::get_navigation_map() const {
  254. if (map_override.is_valid()) {
  255. return map_override;
  256. } else if (is_inside_tree()) {
  257. return get_world_3d()->get_navigation_map();
  258. }
  259. return RID();
  260. }
  261. void NavigationLink3D::set_bidirectional(bool p_bidirectional) {
  262. if (bidirectional == p_bidirectional) {
  263. return;
  264. }
  265. bidirectional = p_bidirectional;
  266. NavigationServer3D::get_singleton()->link_set_bidirectional(link, bidirectional);
  267. }
  268. void NavigationLink3D::set_navigation_layers(uint32_t p_navigation_layers) {
  269. if (navigation_layers == p_navigation_layers) {
  270. return;
  271. }
  272. navigation_layers = p_navigation_layers;
  273. NavigationServer3D::get_singleton()->link_set_navigation_layers(link, navigation_layers);
  274. }
  275. void NavigationLink3D::set_navigation_layer_value(int p_layer_number, bool p_value) {
  276. ERR_FAIL_COND_MSG(p_layer_number < 1, "Navigation layer number must be between 1 and 32 inclusive.");
  277. ERR_FAIL_COND_MSG(p_layer_number > 32, "Navigation layer number must be between 1 and 32 inclusive.");
  278. uint32_t _navigation_layers = get_navigation_layers();
  279. if (p_value) {
  280. _navigation_layers |= 1 << (p_layer_number - 1);
  281. } else {
  282. _navigation_layers &= ~(1 << (p_layer_number - 1));
  283. }
  284. set_navigation_layers(_navigation_layers);
  285. }
  286. bool NavigationLink3D::get_navigation_layer_value(int p_layer_number) const {
  287. ERR_FAIL_COND_V_MSG(p_layer_number < 1, false, "Navigation layer number must be between 1 and 32 inclusive.");
  288. ERR_FAIL_COND_V_MSG(p_layer_number > 32, false, "Navigation layer number must be between 1 and 32 inclusive.");
  289. return get_navigation_layers() & (1 << (p_layer_number - 1));
  290. }
  291. void NavigationLink3D::set_start_position(Vector3 p_position) {
  292. if (start_position.is_equal_approx(p_position)) {
  293. return;
  294. }
  295. start_position = p_position;
  296. if (!is_inside_tree()) {
  297. return;
  298. }
  299. NavigationServer3D::get_singleton()->link_set_start_position(link, current_global_transform.xform(start_position));
  300. #ifdef DEBUG_ENABLED
  301. _update_debug_mesh();
  302. #endif // DEBUG_ENABLED
  303. update_gizmos();
  304. update_configuration_warnings();
  305. }
  306. void NavigationLink3D::set_end_position(Vector3 p_position) {
  307. if (end_position.is_equal_approx(p_position)) {
  308. return;
  309. }
  310. end_position = p_position;
  311. if (!is_inside_tree()) {
  312. return;
  313. }
  314. NavigationServer3D::get_singleton()->link_set_end_position(link, current_global_transform.xform(end_position));
  315. #ifdef DEBUG_ENABLED
  316. _update_debug_mesh();
  317. #endif // DEBUG_ENABLED
  318. update_gizmos();
  319. update_configuration_warnings();
  320. }
  321. void NavigationLink3D::set_global_start_position(Vector3 p_position) {
  322. if (is_inside_tree()) {
  323. set_start_position(to_local(p_position));
  324. } else {
  325. set_start_position(p_position);
  326. }
  327. }
  328. Vector3 NavigationLink3D::get_global_start_position() const {
  329. if (is_inside_tree()) {
  330. return to_global(start_position);
  331. } else {
  332. return start_position;
  333. }
  334. }
  335. void NavigationLink3D::set_global_end_position(Vector3 p_position) {
  336. if (is_inside_tree()) {
  337. set_end_position(to_local(p_position));
  338. } else {
  339. set_end_position(p_position);
  340. }
  341. }
  342. Vector3 NavigationLink3D::get_global_end_position() const {
  343. if (is_inside_tree()) {
  344. return to_global(end_position);
  345. } else {
  346. return end_position;
  347. }
  348. }
  349. void NavigationLink3D::set_enter_cost(real_t p_enter_cost) {
  350. ERR_FAIL_COND_MSG(p_enter_cost < 0.0, "The enter_cost must be positive.");
  351. if (Math::is_equal_approx(enter_cost, p_enter_cost)) {
  352. return;
  353. }
  354. enter_cost = p_enter_cost;
  355. NavigationServer3D::get_singleton()->link_set_enter_cost(link, enter_cost);
  356. }
  357. void NavigationLink3D::set_travel_cost(real_t p_travel_cost) {
  358. ERR_FAIL_COND_MSG(p_travel_cost < 0.0, "The travel_cost must be positive.");
  359. if (Math::is_equal_approx(travel_cost, p_travel_cost)) {
  360. return;
  361. }
  362. travel_cost = p_travel_cost;
  363. NavigationServer3D::get_singleton()->link_set_travel_cost(link, travel_cost);
  364. }
  365. PackedStringArray NavigationLink3D::get_configuration_warnings() const {
  366. PackedStringArray warnings = Node3D::get_configuration_warnings();
  367. if (start_position.is_equal_approx(end_position)) {
  368. warnings.push_back(RTR("NavigationLink3D start position should be different than the end position to be useful."));
  369. }
  370. return warnings;
  371. }
  372. void NavigationLink3D::_link_enter_navigation_map() {
  373. if (!is_inside_tree()) {
  374. return;
  375. }
  376. if (map_override.is_valid()) {
  377. NavigationServer3D::get_singleton()->link_set_map(link, map_override);
  378. } else {
  379. NavigationServer3D::get_singleton()->link_set_map(link, get_world_3d()->get_navigation_map());
  380. }
  381. current_global_transform = get_global_transform();
  382. NavigationServer3D::get_singleton()->link_set_start_position(link, current_global_transform.xform(start_position));
  383. NavigationServer3D::get_singleton()->link_set_end_position(link, current_global_transform.xform(end_position));
  384. NavigationServer3D::get_singleton()->link_set_enabled(link, enabled);
  385. #ifdef DEBUG_ENABLED
  386. if (NavigationServer3D::get_singleton()->get_debug_navigation_enabled()) {
  387. _update_debug_mesh();
  388. }
  389. #endif // DEBUG_ENABLED
  390. }
  391. void NavigationLink3D::_link_exit_navigation_map() {
  392. NavigationServer3D::get_singleton()->link_set_map(link, RID());
  393. #ifdef DEBUG_ENABLED
  394. if (debug_instance.is_valid()) {
  395. RS::get_singleton()->instance_set_visible(debug_instance, false);
  396. }
  397. #endif // DEBUG_ENABLED
  398. }
  399. void NavigationLink3D::_link_update_transform() {
  400. if (!is_inside_tree()) {
  401. return;
  402. }
  403. Transform3D new_global_transform = get_global_transform();
  404. if (current_global_transform != new_global_transform) {
  405. current_global_transform = new_global_transform;
  406. NavigationServer3D::get_singleton()->link_set_start_position(link, current_global_transform.xform(start_position));
  407. NavigationServer3D::get_singleton()->link_set_end_position(link, current_global_transform.xform(end_position));
  408. #ifdef DEBUG_ENABLED
  409. if (NavigationServer3D::get_singleton()->get_debug_navigation_enabled()) {
  410. _update_debug_mesh();
  411. }
  412. #endif // DEBUG_ENABLED
  413. }
  414. }