navigation_link_3d.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  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_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. }
  125. #endif // DEBUG_ENABLED
  126. void NavigationLink3D::_bind_methods() {
  127. ClassDB::bind_method(D_METHOD("set_enabled", "enabled"), &NavigationLink3D::set_enabled);
  128. ClassDB::bind_method(D_METHOD("is_enabled"), &NavigationLink3D::is_enabled);
  129. ClassDB::bind_method(D_METHOD("set_bidirectional", "bidirectional"), &NavigationLink3D::set_bidirectional);
  130. ClassDB::bind_method(D_METHOD("is_bidirectional"), &NavigationLink3D::is_bidirectional);
  131. ClassDB::bind_method(D_METHOD("set_navigation_layers", "navigation_layers"), &NavigationLink3D::set_navigation_layers);
  132. ClassDB::bind_method(D_METHOD("get_navigation_layers"), &NavigationLink3D::get_navigation_layers);
  133. ClassDB::bind_method(D_METHOD("set_navigation_layer_value", "layer_number", "value"), &NavigationLink3D::set_navigation_layer_value);
  134. ClassDB::bind_method(D_METHOD("get_navigation_layer_value", "layer_number"), &NavigationLink3D::get_navigation_layer_value);
  135. ClassDB::bind_method(D_METHOD("set_start_position", "position"), &NavigationLink3D::set_start_position);
  136. ClassDB::bind_method(D_METHOD("get_start_position"), &NavigationLink3D::get_start_position);
  137. ClassDB::bind_method(D_METHOD("set_end_position", "position"), &NavigationLink3D::set_end_position);
  138. ClassDB::bind_method(D_METHOD("get_end_position"), &NavigationLink3D::get_end_position);
  139. ClassDB::bind_method(D_METHOD("set_global_start_position", "position"), &NavigationLink3D::set_global_start_position);
  140. ClassDB::bind_method(D_METHOD("get_global_start_position"), &NavigationLink3D::get_global_start_position);
  141. ClassDB::bind_method(D_METHOD("set_global_end_position", "position"), &NavigationLink3D::set_global_end_position);
  142. ClassDB::bind_method(D_METHOD("get_global_end_position"), &NavigationLink3D::get_global_end_position);
  143. ClassDB::bind_method(D_METHOD("set_enter_cost", "enter_cost"), &NavigationLink3D::set_enter_cost);
  144. ClassDB::bind_method(D_METHOD("get_enter_cost"), &NavigationLink3D::get_enter_cost);
  145. ClassDB::bind_method(D_METHOD("set_travel_cost", "travel_cost"), &NavigationLink3D::set_travel_cost);
  146. ClassDB::bind_method(D_METHOD("get_travel_cost"), &NavigationLink3D::get_travel_cost);
  147. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "enabled"), "set_enabled", "is_enabled");
  148. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "bidirectional"), "set_bidirectional", "is_bidirectional");
  149. ADD_PROPERTY(PropertyInfo(Variant::INT, "navigation_layers", PROPERTY_HINT_LAYERS_3D_NAVIGATION), "set_navigation_layers", "get_navigation_layers");
  150. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "start_position"), "set_start_position", "get_start_position");
  151. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "end_position"), "set_end_position", "get_end_position");
  152. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "enter_cost"), "set_enter_cost", "get_enter_cost");
  153. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "travel_cost"), "set_travel_cost", "get_travel_cost");
  154. }
  155. #ifndef DISABLE_DEPRECATED
  156. bool NavigationLink3D::_set(const StringName &p_name, const Variant &p_value) {
  157. if (p_name == "start_location") {
  158. set_start_position(p_value);
  159. return true;
  160. }
  161. if (p_name == "end_location") {
  162. set_end_position(p_value);
  163. return true;
  164. }
  165. return false;
  166. }
  167. bool NavigationLink3D::_get(const StringName &p_name, Variant &r_ret) const {
  168. if (p_name == "start_location") {
  169. r_ret = get_start_position();
  170. return true;
  171. }
  172. if (p_name == "end_location") {
  173. r_ret = get_end_position();
  174. return true;
  175. }
  176. return false;
  177. }
  178. #endif // DISABLE_DEPRECATED
  179. void NavigationLink3D::_notification(int p_what) {
  180. switch (p_what) {
  181. case NOTIFICATION_ENTER_TREE: {
  182. if (enabled) {
  183. NavigationServer3D::get_singleton()->link_set_map(link, get_world_3d()->get_navigation_map());
  184. // Update global positions for the link.
  185. Transform3D gt = get_global_transform();
  186. NavigationServer3D::get_singleton()->link_set_start_position(link, gt.xform(start_position));
  187. NavigationServer3D::get_singleton()->link_set_end_position(link, gt.xform(end_position));
  188. }
  189. #ifdef DEBUG_ENABLED
  190. _update_debug_mesh();
  191. #endif // DEBUG_ENABLED
  192. } break;
  193. case NOTIFICATION_TRANSFORM_CHANGED: {
  194. // Update global positions for the link.
  195. Transform3D gt = get_global_transform();
  196. NavigationServer3D::get_singleton()->link_set_start_position(link, gt.xform(start_position));
  197. NavigationServer3D::get_singleton()->link_set_end_position(link, gt.xform(end_position));
  198. #ifdef DEBUG_ENABLED
  199. if (is_inside_tree() && debug_instance.is_valid()) {
  200. RS::get_singleton()->instance_set_transform(debug_instance, get_global_transform());
  201. }
  202. #endif // DEBUG_ENABLED
  203. } break;
  204. case NOTIFICATION_EXIT_TREE: {
  205. NavigationServer3D::get_singleton()->link_set_map(link, RID());
  206. #ifdef DEBUG_ENABLED
  207. if (debug_instance.is_valid()) {
  208. RS::get_singleton()->instance_set_scenario(debug_instance, RID());
  209. RS::get_singleton()->instance_set_visible(debug_instance, false);
  210. }
  211. #endif // DEBUG_ENABLED
  212. } break;
  213. }
  214. }
  215. NavigationLink3D::NavigationLink3D() {
  216. link = NavigationServer3D::get_singleton()->link_create();
  217. NavigationServer3D::get_singleton()->link_set_owner_id(link, get_instance_id());
  218. set_notify_transform(true);
  219. }
  220. NavigationLink3D::~NavigationLink3D() {
  221. ERR_FAIL_NULL(NavigationServer3D::get_singleton());
  222. NavigationServer3D::get_singleton()->free(link);
  223. link = RID();
  224. #ifdef DEBUG_ENABLED
  225. ERR_FAIL_NULL(RenderingServer::get_singleton());
  226. if (debug_instance.is_valid()) {
  227. RenderingServer::get_singleton()->free(debug_instance);
  228. }
  229. if (debug_mesh.is_valid()) {
  230. RenderingServer::get_singleton()->free(debug_mesh->get_rid());
  231. }
  232. #endif // DEBUG_ENABLED
  233. }
  234. void NavigationLink3D::set_enabled(bool p_enabled) {
  235. if (enabled == p_enabled) {
  236. return;
  237. }
  238. enabled = p_enabled;
  239. if (!is_inside_tree()) {
  240. return;
  241. }
  242. if (enabled) {
  243. NavigationServer3D::get_singleton()->link_set_map(link, get_world_3d()->get_navigation_map());
  244. } else {
  245. NavigationServer3D::get_singleton()->link_set_map(link, RID());
  246. }
  247. #ifdef DEBUG_ENABLED
  248. if (debug_instance.is_valid() && debug_mesh.is_valid()) {
  249. if (enabled) {
  250. Ref<StandardMaterial3D> link_material = NavigationServer3D::get_singleton()->get_debug_navigation_link_connections_material();
  251. RS::get_singleton()->instance_set_surface_override_material(debug_instance, 0, link_material->get_rid());
  252. } else {
  253. Ref<StandardMaterial3D> disabled_link_material = NavigationServer3D::get_singleton()->get_debug_navigation_link_connections_disabled_material();
  254. RS::get_singleton()->instance_set_surface_override_material(debug_instance, 0, disabled_link_material->get_rid());
  255. }
  256. }
  257. #endif // DEBUG_ENABLED
  258. update_gizmos();
  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. Transform3D gt = get_global_transform();
  299. NavigationServer3D::get_singleton()->link_set_start_position(link, gt.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. Transform3D gt = get_global_transform();
  315. NavigationServer3D::get_singleton()->link_set_end_position(link, gt.xform(end_position));
  316. #ifdef DEBUG_ENABLED
  317. _update_debug_mesh();
  318. #endif // DEBUG_ENABLED
  319. update_gizmos();
  320. update_configuration_warnings();
  321. }
  322. void NavigationLink3D::set_global_start_position(Vector3 p_position) {
  323. if (is_inside_tree()) {
  324. set_start_position(to_local(p_position));
  325. } else {
  326. set_start_position(p_position);
  327. }
  328. }
  329. Vector3 NavigationLink3D::get_global_start_position() const {
  330. if (is_inside_tree()) {
  331. return to_global(start_position);
  332. } else {
  333. return start_position;
  334. }
  335. }
  336. void NavigationLink3D::set_global_end_position(Vector3 p_position) {
  337. if (is_inside_tree()) {
  338. set_end_position(to_local(p_position));
  339. } else {
  340. set_end_position(p_position);
  341. }
  342. }
  343. Vector3 NavigationLink3D::get_global_end_position() const {
  344. if (is_inside_tree()) {
  345. return to_global(end_position);
  346. } else {
  347. return end_position;
  348. }
  349. }
  350. void NavigationLink3D::set_enter_cost(real_t p_enter_cost) {
  351. ERR_FAIL_COND_MSG(p_enter_cost < 0.0, "The enter_cost must be positive.");
  352. if (Math::is_equal_approx(enter_cost, p_enter_cost)) {
  353. return;
  354. }
  355. enter_cost = p_enter_cost;
  356. NavigationServer3D::get_singleton()->link_set_enter_cost(link, enter_cost);
  357. }
  358. void NavigationLink3D::set_travel_cost(real_t p_travel_cost) {
  359. ERR_FAIL_COND_MSG(p_travel_cost < 0.0, "The travel_cost must be positive.");
  360. if (Math::is_equal_approx(travel_cost, p_travel_cost)) {
  361. return;
  362. }
  363. travel_cost = p_travel_cost;
  364. NavigationServer3D::get_singleton()->link_set_travel_cost(link, travel_cost);
  365. }
  366. PackedStringArray NavigationLink3D::get_configuration_warnings() const {
  367. PackedStringArray warnings = Node::get_configuration_warnings();
  368. if (start_position.is_equal_approx(end_position)) {
  369. warnings.push_back(RTR("NavigationLink3D start position should be different than the end position to be useful."));
  370. }
  371. return warnings;
  372. }