path_3d.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. /*************************************************************************/
  2. /* path_3d.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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 "path_3d.h"
  31. Path3D::Path3D() {
  32. SceneTree *st = SceneTree::get_singleton();
  33. if (st && st->is_debugging_paths_hint()) {
  34. debug_instance = RS::get_singleton()->instance_create();
  35. set_notify_transform(true);
  36. _update_debug_mesh();
  37. }
  38. }
  39. Path3D::~Path3D() {
  40. if (debug_instance.is_valid()) {
  41. RS::get_singleton()->free(debug_instance);
  42. }
  43. if (debug_mesh.is_valid()) {
  44. RS::get_singleton()->free(debug_mesh->get_rid());
  45. }
  46. }
  47. void Path3D::_notification(int p_what) {
  48. switch (p_what) {
  49. case NOTIFICATION_ENTER_TREE: {
  50. SceneTree *st = SceneTree::get_singleton();
  51. if (st && st->is_debugging_paths_hint()) {
  52. _update_debug_mesh();
  53. }
  54. } break;
  55. case NOTIFICATION_EXIT_TREE: {
  56. SceneTree *st = SceneTree::get_singleton();
  57. if (st && st->is_debugging_paths_hint()) {
  58. RS::get_singleton()->instance_set_visible(debug_instance, false);
  59. }
  60. } break;
  61. case NOTIFICATION_TRANSFORM_CHANGED: {
  62. if (is_inside_tree() && debug_instance.is_valid()) {
  63. RS::get_singleton()->instance_set_transform(debug_instance, get_global_transform());
  64. }
  65. } break;
  66. }
  67. }
  68. void Path3D::_update_debug_mesh() {
  69. SceneTree *st = SceneTree::get_singleton();
  70. if (!(st && st->is_debugging_paths_hint())) {
  71. return;
  72. }
  73. if (!debug_mesh.is_valid()) {
  74. debug_mesh = Ref<ArrayMesh>(memnew(ArrayMesh));
  75. }
  76. if (!(curve.is_valid())) {
  77. RS::get_singleton()->instance_set_visible(debug_instance, false);
  78. return;
  79. }
  80. if (curve->get_point_count() < 2) {
  81. RS::get_singleton()->instance_set_visible(debug_instance, false);
  82. return;
  83. }
  84. Vector<Vector3> vertex_array;
  85. for (int i = 1; i < curve->get_point_count(); i++) {
  86. Vector3 line_end = curve->get_point_position(i);
  87. Vector3 line_start = curve->get_point_position(i - 1);
  88. vertex_array.push_back(line_start);
  89. vertex_array.push_back(line_end);
  90. }
  91. Array mesh_array;
  92. mesh_array.resize(Mesh::ARRAY_MAX);
  93. mesh_array[Mesh::ARRAY_VERTEX] = vertex_array;
  94. debug_mesh->clear_surfaces();
  95. debug_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_LINES, mesh_array);
  96. RS::get_singleton()->instance_set_base(debug_instance, debug_mesh->get_rid());
  97. RS::get_singleton()->mesh_surface_set_material(debug_mesh->get_rid(), 0, st->get_debug_paths_material()->get_rid());
  98. if (is_inside_tree()) {
  99. RS::get_singleton()->instance_set_scenario(debug_instance, get_world_3d()->get_scenario());
  100. RS::get_singleton()->instance_set_transform(debug_instance, get_global_transform());
  101. RS::get_singleton()->instance_set_visible(debug_instance, is_visible_in_tree());
  102. }
  103. }
  104. void Path3D::_curve_changed() {
  105. if (is_inside_tree() && Engine::get_singleton()->is_editor_hint()) {
  106. update_gizmos();
  107. }
  108. if (is_inside_tree()) {
  109. emit_signal(SNAME("curve_changed"));
  110. }
  111. // update the configuration warnings of all children of type PathFollow
  112. // previously used for PathFollowOriented (now enforced orientation is done in PathFollow)
  113. if (is_inside_tree()) {
  114. for (int i = 0; i < get_child_count(); i++) {
  115. PathFollow3D *child = Object::cast_to<PathFollow3D>(get_child(i));
  116. if (child) {
  117. child->update_configuration_warnings();
  118. }
  119. }
  120. }
  121. SceneTree *st = SceneTree::get_singleton();
  122. if (st && st->is_debugging_paths_hint()) {
  123. _update_debug_mesh();
  124. }
  125. }
  126. void Path3D::set_curve(const Ref<Curve3D> &p_curve) {
  127. if (curve.is_valid()) {
  128. curve->disconnect("changed", callable_mp(this, &Path3D::_curve_changed));
  129. }
  130. curve = p_curve;
  131. if (curve.is_valid()) {
  132. curve->connect("changed", callable_mp(this, &Path3D::_curve_changed));
  133. }
  134. _curve_changed();
  135. }
  136. Ref<Curve3D> Path3D::get_curve() const {
  137. return curve;
  138. }
  139. void Path3D::_bind_methods() {
  140. ClassDB::bind_method(D_METHOD("set_curve", "curve"), &Path3D::set_curve);
  141. ClassDB::bind_method(D_METHOD("get_curve"), &Path3D::get_curve);
  142. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve3D", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_EDITOR_INSTANTIATE_OBJECT), "set_curve", "get_curve");
  143. ADD_SIGNAL(MethodInfo("curve_changed"));
  144. }
  145. //////////////
  146. void PathFollow3D::_update_transform(bool p_update_xyz_rot) {
  147. if (!path) {
  148. return;
  149. }
  150. Ref<Curve3D> c = path->get_curve();
  151. if (!c.is_valid()) {
  152. return;
  153. }
  154. real_t bl = c->get_baked_length();
  155. if (bl == 0.0) {
  156. return;
  157. }
  158. real_t bi = c->get_bake_interval();
  159. real_t o_next = progress + bi;
  160. real_t o_prev = progress - bi;
  161. if (loop) {
  162. o_next = Math::fposmod(o_next, bl);
  163. o_prev = Math::fposmod(o_prev, bl);
  164. } else if (rotation_mode == ROTATION_ORIENTED) {
  165. if (o_next >= bl) {
  166. o_next = bl;
  167. }
  168. if (o_prev <= 0) {
  169. o_prev = 0;
  170. }
  171. }
  172. Vector3 pos = c->sample_baked(progress, cubic);
  173. Transform3D t = get_transform();
  174. // Vector3 pos_offset = Vector3(h_offset, v_offset, 0); not used in all cases
  175. // will be replaced by "Vector3(h_offset, v_offset, 0)" where it was formerly used
  176. if (rotation_mode == ROTATION_ORIENTED) {
  177. Vector3 forward = c->sample_baked(o_next, cubic) - pos;
  178. // Try with the previous position
  179. if (forward.length_squared() < CMP_EPSILON2) {
  180. forward = pos - c->sample_baked(o_prev, cubic);
  181. }
  182. if (forward.length_squared() < CMP_EPSILON2) {
  183. forward = Vector3(0, 0, 1);
  184. } else {
  185. forward.normalize();
  186. }
  187. Vector3 up = c->sample_baked_up_vector(progress, true);
  188. if (o_next < progress) {
  189. Vector3 up1 = c->sample_baked_up_vector(o_next, true);
  190. Vector3 axis = up.cross(up1);
  191. if (axis.length_squared() < CMP_EPSILON2) {
  192. axis = forward;
  193. } else {
  194. axis.normalize();
  195. }
  196. up.rotate(axis, up.angle_to(up1) * 0.5f);
  197. }
  198. Vector3 scale = t.basis.get_scale();
  199. Vector3 sideways = up.cross(forward).normalized();
  200. up = forward.cross(sideways).normalized();
  201. t.basis.set_columns(sideways, up, forward);
  202. t.basis.scale_local(scale);
  203. t.origin = pos + sideways * h_offset + up * v_offset;
  204. } else if (rotation_mode != ROTATION_NONE) {
  205. // perform parallel transport
  206. //
  207. // see C. Dougan, The Parallel Transport Frame, Game Programming Gems 2 for example
  208. // for a discussion about why not Frenet frame.
  209. t.origin = pos;
  210. if (p_update_xyz_rot && prev_offset != progress) { // Only update rotation if some parameter has changed - i.e. not on addition to scene tree.
  211. real_t sample_distance = bi * 0.01;
  212. Vector3 t_prev_pos_a = c->sample_baked(prev_offset - sample_distance, cubic);
  213. Vector3 t_prev_pos_b = c->sample_baked(prev_offset + sample_distance, cubic);
  214. Vector3 t_cur_pos_a = c->sample_baked(progress - sample_distance, cubic);
  215. Vector3 t_cur_pos_b = c->sample_baked(progress + sample_distance, cubic);
  216. Vector3 t_prev = (t_prev_pos_a - t_prev_pos_b).normalized();
  217. Vector3 t_cur = (t_cur_pos_a - t_cur_pos_b).normalized();
  218. Vector3 axis = t_prev.cross(t_cur);
  219. real_t dot = t_prev.dot(t_cur);
  220. real_t angle = Math::acos(CLAMP(dot, -1, 1));
  221. if (likely(!Math::is_zero_approx(angle))) {
  222. if (rotation_mode == ROTATION_Y) {
  223. // assuming we're referring to global Y-axis. is this correct?
  224. axis.x = 0;
  225. axis.z = 0;
  226. } else if (rotation_mode == ROTATION_XY) {
  227. axis.z = 0;
  228. } else if (rotation_mode == ROTATION_XYZ) {
  229. // all components are allowed
  230. }
  231. if (likely(!Math::is_zero_approx(axis.length()))) {
  232. t.rotate_basis(axis.normalized(), angle);
  233. }
  234. }
  235. // do the additional tilting
  236. real_t tilt_angle = c->sample_baked_tilt(progress);
  237. Vector3 tilt_axis = t_cur; // not sure what tilt is supposed to do, is this correct??
  238. if (likely(!Math::is_zero_approx(Math::abs(tilt_angle)))) {
  239. if (rotation_mode == ROTATION_Y) {
  240. tilt_axis.x = 0;
  241. tilt_axis.z = 0;
  242. } else if (rotation_mode == ROTATION_XY) {
  243. tilt_axis.z = 0;
  244. } else if (rotation_mode == ROTATION_XYZ) {
  245. // all components are allowed
  246. }
  247. if (likely(!Math::is_zero_approx(tilt_axis.length()))) {
  248. t.rotate_basis(tilt_axis.normalized(), tilt_angle);
  249. }
  250. }
  251. }
  252. t.translate_local(Vector3(h_offset, v_offset, 0));
  253. } else {
  254. t.origin = pos + Vector3(h_offset, v_offset, 0);
  255. }
  256. set_transform(t);
  257. }
  258. void PathFollow3D::_notification(int p_what) {
  259. switch (p_what) {
  260. case NOTIFICATION_ENTER_TREE: {
  261. Node *parent = get_parent();
  262. if (parent) {
  263. path = Object::cast_to<Path3D>(parent);
  264. if (path) {
  265. _update_transform(false);
  266. }
  267. }
  268. } break;
  269. case NOTIFICATION_EXIT_TREE: {
  270. path = nullptr;
  271. } break;
  272. }
  273. }
  274. void PathFollow3D::set_cubic_interpolation(bool p_enable) {
  275. cubic = p_enable;
  276. }
  277. bool PathFollow3D::get_cubic_interpolation() const {
  278. return cubic;
  279. }
  280. void PathFollow3D::_validate_property(PropertyInfo &p_property) const {
  281. if (p_property.name == "offset") {
  282. real_t max = 10000;
  283. if (path && path->get_curve().is_valid()) {
  284. max = path->get_curve()->get_baked_length();
  285. }
  286. p_property.hint_string = "0," + rtos(max) + ",0.01,or_lesser,or_greater";
  287. }
  288. }
  289. TypedArray<String> PathFollow3D::get_configuration_warnings() const {
  290. TypedArray<String> warnings = Node::get_configuration_warnings();
  291. if (is_visible_in_tree() && is_inside_tree()) {
  292. if (!Object::cast_to<Path3D>(get_parent())) {
  293. warnings.push_back(RTR("PathFollow3D only works when set as a child of a Path3D node."));
  294. } else {
  295. Path3D *path = Object::cast_to<Path3D>(get_parent());
  296. if (path->get_curve().is_valid() && !path->get_curve()->is_up_vector_enabled() && rotation_mode == ROTATION_ORIENTED) {
  297. warnings.push_back(RTR("PathFollow3D's ROTATION_ORIENTED requires \"Up Vector\" to be enabled in its parent Path3D's Curve resource."));
  298. }
  299. }
  300. }
  301. return warnings;
  302. }
  303. void PathFollow3D::_bind_methods() {
  304. ClassDB::bind_method(D_METHOD("set_progress", "progress"), &PathFollow3D::set_progress);
  305. ClassDB::bind_method(D_METHOD("get_progress"), &PathFollow3D::get_progress);
  306. ClassDB::bind_method(D_METHOD("set_h_offset", "h_offset"), &PathFollow3D::set_h_offset);
  307. ClassDB::bind_method(D_METHOD("get_h_offset"), &PathFollow3D::get_h_offset);
  308. ClassDB::bind_method(D_METHOD("set_v_offset", "v_offset"), &PathFollow3D::set_v_offset);
  309. ClassDB::bind_method(D_METHOD("get_v_offset"), &PathFollow3D::get_v_offset);
  310. ClassDB::bind_method(D_METHOD("set_progress_ratio", "ratio"), &PathFollow3D::set_progress_ratio);
  311. ClassDB::bind_method(D_METHOD("get_progress_ratio"), &PathFollow3D::get_progress_ratio);
  312. ClassDB::bind_method(D_METHOD("set_rotation_mode", "rotation_mode"), &PathFollow3D::set_rotation_mode);
  313. ClassDB::bind_method(D_METHOD("get_rotation_mode"), &PathFollow3D::get_rotation_mode);
  314. ClassDB::bind_method(D_METHOD("set_cubic_interpolation", "enable"), &PathFollow3D::set_cubic_interpolation);
  315. ClassDB::bind_method(D_METHOD("get_cubic_interpolation"), &PathFollow3D::get_cubic_interpolation);
  316. ClassDB::bind_method(D_METHOD("set_loop", "loop"), &PathFollow3D::set_loop);
  317. ClassDB::bind_method(D_METHOD("has_loop"), &PathFollow3D::has_loop);
  318. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "progress", PROPERTY_HINT_RANGE, "0,10000,0.01,or_lesser,or_greater,suffix:m"), "set_progress", "get_progress");
  319. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "progress_ratio", PROPERTY_HINT_RANGE, "0,1,0.0001,or_lesser,or_greater", PROPERTY_USAGE_EDITOR), "set_progress_ratio", "get_progress_ratio");
  320. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "h_offset", PROPERTY_HINT_NONE, "suffix:m"), "set_h_offset", "get_h_offset");
  321. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "v_offset", PROPERTY_HINT_NONE, "suffix:m"), "set_v_offset", "get_v_offset");
  322. ADD_PROPERTY(PropertyInfo(Variant::INT, "rotation_mode", PROPERTY_HINT_ENUM, "None,Y,XY,XYZ,Oriented"), "set_rotation_mode", "get_rotation_mode");
  323. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "cubic_interp"), "set_cubic_interpolation", "get_cubic_interpolation");
  324. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "loop"), "set_loop", "has_loop");
  325. BIND_ENUM_CONSTANT(ROTATION_NONE);
  326. BIND_ENUM_CONSTANT(ROTATION_Y);
  327. BIND_ENUM_CONSTANT(ROTATION_XY);
  328. BIND_ENUM_CONSTANT(ROTATION_XYZ);
  329. BIND_ENUM_CONSTANT(ROTATION_ORIENTED);
  330. }
  331. void PathFollow3D::set_progress(real_t p_progress) {
  332. ERR_FAIL_COND(!isfinite(p_progress));
  333. prev_offset = progress;
  334. progress = p_progress;
  335. if (path) {
  336. if (path->get_curve().is_valid()) {
  337. real_t path_length = path->get_curve()->get_baked_length();
  338. if (loop && path_length) {
  339. progress = Math::fposmod(progress, path_length);
  340. if (!Math::is_zero_approx(p_progress) && Math::is_zero_approx(progress)) {
  341. progress = path_length;
  342. }
  343. } else {
  344. progress = CLAMP(progress, 0, path_length);
  345. }
  346. }
  347. _update_transform();
  348. }
  349. }
  350. void PathFollow3D::set_h_offset(real_t p_h_offset) {
  351. h_offset = p_h_offset;
  352. if (path) {
  353. _update_transform();
  354. }
  355. }
  356. real_t PathFollow3D::get_h_offset() const {
  357. return h_offset;
  358. }
  359. void PathFollow3D::set_v_offset(real_t p_v_offset) {
  360. v_offset = p_v_offset;
  361. if (path) {
  362. _update_transform();
  363. }
  364. }
  365. real_t PathFollow3D::get_v_offset() const {
  366. return v_offset;
  367. }
  368. real_t PathFollow3D::get_progress() const {
  369. return progress;
  370. }
  371. void PathFollow3D::set_progress_ratio(real_t p_ratio) {
  372. if (path && path->get_curve().is_valid() && path->get_curve()->get_baked_length()) {
  373. set_progress(p_ratio * path->get_curve()->get_baked_length());
  374. }
  375. }
  376. real_t PathFollow3D::get_progress_ratio() const {
  377. if (path && path->get_curve().is_valid() && path->get_curve()->get_baked_length()) {
  378. return get_progress() / path->get_curve()->get_baked_length();
  379. } else {
  380. return 0;
  381. }
  382. }
  383. void PathFollow3D::set_rotation_mode(RotationMode p_rotation_mode) {
  384. rotation_mode = p_rotation_mode;
  385. update_configuration_warnings();
  386. _update_transform();
  387. }
  388. PathFollow3D::RotationMode PathFollow3D::get_rotation_mode() const {
  389. return rotation_mode;
  390. }
  391. void PathFollow3D::set_loop(bool p_loop) {
  392. loop = p_loop;
  393. }
  394. bool PathFollow3D::has_loop() const {
  395. return loop;
  396. }