path_3d.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. /**************************************************************************/
  2. /* path_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 "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. ERR_FAIL_NULL(RenderingServer::get_singleton());
  41. if (debug_instance.is_valid()) {
  42. RS::get_singleton()->free(debug_instance);
  43. }
  44. if (debug_mesh.is_valid()) {
  45. RS::get_singleton()->free(debug_mesh->get_rid());
  46. }
  47. }
  48. void Path3D::_notification(int p_what) {
  49. switch (p_what) {
  50. case NOTIFICATION_ENTER_TREE: {
  51. SceneTree *st = SceneTree::get_singleton();
  52. if (st && st->is_debugging_paths_hint()) {
  53. _update_debug_mesh();
  54. }
  55. } break;
  56. case NOTIFICATION_EXIT_TREE: {
  57. SceneTree *st = SceneTree::get_singleton();
  58. if (st && st->is_debugging_paths_hint()) {
  59. RS::get_singleton()->instance_set_visible(debug_instance, false);
  60. }
  61. } break;
  62. case NOTIFICATION_TRANSFORM_CHANGED: {
  63. if (is_inside_tree() && debug_instance.is_valid()) {
  64. RS::get_singleton()->instance_set_transform(debug_instance, get_global_transform());
  65. }
  66. } break;
  67. }
  68. }
  69. void Path3D::_update_debug_mesh() {
  70. SceneTree *st = SceneTree::get_singleton();
  71. if (!(st && st->is_debugging_paths_hint())) {
  72. return;
  73. }
  74. if (!debug_mesh.is_valid()) {
  75. debug_mesh = Ref<ArrayMesh>(memnew(ArrayMesh));
  76. }
  77. if (!(curve.is_valid())) {
  78. RS::get_singleton()->instance_set_visible(debug_instance, false);
  79. return;
  80. }
  81. if (curve->get_point_count() < 2) {
  82. RS::get_singleton()->instance_set_visible(debug_instance, false);
  83. return;
  84. }
  85. Vector<Vector3> vertex_array;
  86. for (int i = 1; i < curve->get_point_count(); i++) {
  87. Vector3 line_end = curve->get_point_position(i);
  88. Vector3 line_start = curve->get_point_position(i - 1);
  89. vertex_array.push_back(line_start);
  90. vertex_array.push_back(line_end);
  91. }
  92. Array mesh_array;
  93. mesh_array.resize(Mesh::ARRAY_MAX);
  94. mesh_array[Mesh::ARRAY_VERTEX] = vertex_array;
  95. debug_mesh->clear_surfaces();
  96. debug_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_LINES, mesh_array);
  97. RS::get_singleton()->instance_set_base(debug_instance, debug_mesh->get_rid());
  98. RS::get_singleton()->mesh_surface_set_material(debug_mesh->get_rid(), 0, st->get_debug_paths_material()->get_rid());
  99. if (is_inside_tree()) {
  100. RS::get_singleton()->instance_set_scenario(debug_instance, get_world_3d()->get_scenario());
  101. RS::get_singleton()->instance_set_transform(debug_instance, get_global_transform());
  102. RS::get_singleton()->instance_set_visible(debug_instance, is_visible_in_tree());
  103. }
  104. }
  105. void Path3D::_curve_changed() {
  106. if (is_inside_tree() && Engine::get_singleton()->is_editor_hint()) {
  107. update_gizmos();
  108. }
  109. if (is_inside_tree()) {
  110. emit_signal(SNAME("curve_changed"));
  111. }
  112. // update the configuration warnings of all children of type PathFollow
  113. // previously used for PathFollowOriented (now enforced orientation is done in PathFollow)
  114. if (is_inside_tree()) {
  115. for (int i = 0; i < get_child_count(); i++) {
  116. PathFollow3D *child = Object::cast_to<PathFollow3D>(get_child(i));
  117. if (child) {
  118. child->update_configuration_warnings();
  119. }
  120. }
  121. }
  122. SceneTree *st = SceneTree::get_singleton();
  123. if (st && st->is_debugging_paths_hint()) {
  124. _update_debug_mesh();
  125. }
  126. }
  127. void Path3D::set_curve(const Ref<Curve3D> &p_curve) {
  128. if (curve.is_valid()) {
  129. curve->disconnect("changed", callable_mp(this, &Path3D::_curve_changed));
  130. }
  131. curve = p_curve;
  132. if (curve.is_valid()) {
  133. curve->connect("changed", callable_mp(this, &Path3D::_curve_changed));
  134. }
  135. _curve_changed();
  136. }
  137. Ref<Curve3D> Path3D::get_curve() const {
  138. return curve;
  139. }
  140. void Path3D::_bind_methods() {
  141. ClassDB::bind_method(D_METHOD("set_curve", "curve"), &Path3D::set_curve);
  142. ClassDB::bind_method(D_METHOD("get_curve"), &Path3D::get_curve);
  143. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve3D", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_EDITOR_INSTANTIATE_OBJECT), "set_curve", "get_curve");
  144. ADD_SIGNAL(MethodInfo("curve_changed"));
  145. }
  146. //////////////
  147. void PathFollow3D::_update_transform(bool p_update_xyz_rot) {
  148. if (!path) {
  149. return;
  150. }
  151. Ref<Curve3D> c = path->get_curve();
  152. if (!c.is_valid()) {
  153. return;
  154. }
  155. real_t bl = c->get_baked_length();
  156. if (bl == 0.0) {
  157. return;
  158. }
  159. Transform3D t;
  160. if (rotation_mode == ROTATION_NONE) {
  161. Vector3 pos = c->sample_baked(progress, cubic);
  162. t.origin = pos;
  163. } else {
  164. t = c->sample_baked_with_rotation(progress, cubic, false);
  165. Vector3 forward = t.basis.get_column(2); // Retain tangent for applying tilt
  166. t = PathFollow3D::correct_posture(t, rotation_mode);
  167. // Apply tilt *after* correct_posture
  168. if (tilt_enabled) {
  169. const real_t tilt = c->sample_baked_tilt(progress);
  170. const Basis twist(forward, tilt);
  171. t.basis = twist * t.basis;
  172. }
  173. }
  174. Vector3 scale = get_transform().basis.get_scale();
  175. t.translate_local(Vector3(h_offset, v_offset, 0));
  176. t.basis.scale_local(scale);
  177. set_transform(t);
  178. }
  179. void PathFollow3D::_notification(int p_what) {
  180. switch (p_what) {
  181. case NOTIFICATION_ENTER_TREE: {
  182. Node *parent = get_parent();
  183. if (parent) {
  184. path = Object::cast_to<Path3D>(parent);
  185. if (path) {
  186. _update_transform(false);
  187. }
  188. }
  189. } break;
  190. case NOTIFICATION_EXIT_TREE: {
  191. path = nullptr;
  192. } break;
  193. }
  194. }
  195. void PathFollow3D::set_cubic_interpolation(bool p_enable) {
  196. cubic = p_enable;
  197. }
  198. bool PathFollow3D::get_cubic_interpolation() const {
  199. return cubic;
  200. }
  201. void PathFollow3D::_validate_property(PropertyInfo &p_property) const {
  202. if (p_property.name == "offset") {
  203. real_t max = 10000;
  204. if (path && path->get_curve().is_valid()) {
  205. max = path->get_curve()->get_baked_length();
  206. }
  207. p_property.hint_string = "0," + rtos(max) + ",0.01,or_less,or_greater";
  208. }
  209. }
  210. PackedStringArray PathFollow3D::get_configuration_warnings() const {
  211. PackedStringArray warnings = Node::get_configuration_warnings();
  212. if (is_visible_in_tree() && is_inside_tree()) {
  213. if (!Object::cast_to<Path3D>(get_parent())) {
  214. warnings.push_back(RTR("PathFollow3D only works when set as a child of a Path3D node."));
  215. } else {
  216. Path3D *p = Object::cast_to<Path3D>(get_parent());
  217. if (p->get_curve().is_valid() && !p->get_curve()->is_up_vector_enabled() && rotation_mode == ROTATION_ORIENTED) {
  218. warnings.push_back(RTR("PathFollow3D's ROTATION_ORIENTED requires \"Up Vector\" to be enabled in its parent Path3D's Curve resource."));
  219. }
  220. }
  221. }
  222. return warnings;
  223. }
  224. Transform3D PathFollow3D::correct_posture(Transform3D p_transform, PathFollow3D::RotationMode p_rotation_mode) {
  225. Transform3D t = p_transform;
  226. // Modify frame according to rotation mode.
  227. if (p_rotation_mode == PathFollow3D::ROTATION_NONE) {
  228. // Clear rotation.
  229. t.basis = Basis();
  230. } else if (p_rotation_mode == PathFollow3D::ROTATION_ORIENTED) {
  231. // Y-axis always straight up.
  232. Vector3 up(0.0, 1.0, 0.0);
  233. Vector3 forward = t.basis.get_column(2);
  234. t.basis = Basis::looking_at(-forward, up);
  235. } else {
  236. // Lock some euler axes.
  237. Vector3 euler = t.basis.get_euler_normalized(EulerOrder::YXZ);
  238. if (p_rotation_mode == PathFollow3D::ROTATION_Y) {
  239. // Only Y-axis allowed.
  240. euler[0] = 0;
  241. euler[2] = 0;
  242. } else if (p_rotation_mode == PathFollow3D::ROTATION_XY) {
  243. // XY allowed.
  244. euler[2] = 0;
  245. }
  246. Basis locked = Basis::from_euler(euler, EulerOrder::YXZ);
  247. t.basis = locked;
  248. }
  249. return t;
  250. }
  251. void PathFollow3D::_bind_methods() {
  252. ClassDB::bind_method(D_METHOD("set_progress", "progress"), &PathFollow3D::set_progress);
  253. ClassDB::bind_method(D_METHOD("get_progress"), &PathFollow3D::get_progress);
  254. ClassDB::bind_method(D_METHOD("set_h_offset", "h_offset"), &PathFollow3D::set_h_offset);
  255. ClassDB::bind_method(D_METHOD("get_h_offset"), &PathFollow3D::get_h_offset);
  256. ClassDB::bind_method(D_METHOD("set_v_offset", "v_offset"), &PathFollow3D::set_v_offset);
  257. ClassDB::bind_method(D_METHOD("get_v_offset"), &PathFollow3D::get_v_offset);
  258. ClassDB::bind_method(D_METHOD("set_progress_ratio", "ratio"), &PathFollow3D::set_progress_ratio);
  259. ClassDB::bind_method(D_METHOD("get_progress_ratio"), &PathFollow3D::get_progress_ratio);
  260. ClassDB::bind_method(D_METHOD("set_rotation_mode", "rotation_mode"), &PathFollow3D::set_rotation_mode);
  261. ClassDB::bind_method(D_METHOD("get_rotation_mode"), &PathFollow3D::get_rotation_mode);
  262. ClassDB::bind_method(D_METHOD("set_cubic_interpolation", "enable"), &PathFollow3D::set_cubic_interpolation);
  263. ClassDB::bind_method(D_METHOD("get_cubic_interpolation"), &PathFollow3D::get_cubic_interpolation);
  264. ClassDB::bind_method(D_METHOD("set_loop", "loop"), &PathFollow3D::set_loop);
  265. ClassDB::bind_method(D_METHOD("has_loop"), &PathFollow3D::has_loop);
  266. ClassDB::bind_method(D_METHOD("set_tilt_enabled", "enabled"), &PathFollow3D::set_tilt_enabled);
  267. ClassDB::bind_method(D_METHOD("is_tilt_enabled"), &PathFollow3D::is_tilt_enabled);
  268. ClassDB::bind_static_method("PathFollow3D", D_METHOD("correct_posture", "transform", "rotation_mode"), &PathFollow3D::correct_posture);
  269. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "progress", PROPERTY_HINT_RANGE, "0,10000,0.01,or_less,or_greater,suffix:m"), "set_progress", "get_progress");
  270. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "progress_ratio", PROPERTY_HINT_RANGE, "0,1,0.0001,or_less,or_greater", PROPERTY_USAGE_EDITOR), "set_progress_ratio", "get_progress_ratio");
  271. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "h_offset", PROPERTY_HINT_NONE, "suffix:m"), "set_h_offset", "get_h_offset");
  272. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "v_offset", PROPERTY_HINT_NONE, "suffix:m"), "set_v_offset", "get_v_offset");
  273. ADD_PROPERTY(PropertyInfo(Variant::INT, "rotation_mode", PROPERTY_HINT_ENUM, "None,Y,XY,XYZ,Oriented"), "set_rotation_mode", "get_rotation_mode");
  274. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "cubic_interp"), "set_cubic_interpolation", "get_cubic_interpolation");
  275. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "loop"), "set_loop", "has_loop");
  276. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "tilt_enabled"), "set_tilt_enabled", "is_tilt_enabled");
  277. BIND_ENUM_CONSTANT(ROTATION_NONE);
  278. BIND_ENUM_CONSTANT(ROTATION_Y);
  279. BIND_ENUM_CONSTANT(ROTATION_XY);
  280. BIND_ENUM_CONSTANT(ROTATION_XYZ);
  281. BIND_ENUM_CONSTANT(ROTATION_ORIENTED);
  282. }
  283. void PathFollow3D::set_progress(real_t p_progress) {
  284. ERR_FAIL_COND(!isfinite(p_progress));
  285. progress = p_progress;
  286. if (path) {
  287. if (path->get_curve().is_valid()) {
  288. real_t path_length = path->get_curve()->get_baked_length();
  289. if (loop && path_length) {
  290. progress = Math::fposmod(progress, path_length);
  291. if (!Math::is_zero_approx(p_progress) && Math::is_zero_approx(progress)) {
  292. progress = path_length;
  293. }
  294. }
  295. }
  296. _update_transform();
  297. }
  298. }
  299. void PathFollow3D::set_h_offset(real_t p_h_offset) {
  300. h_offset = p_h_offset;
  301. if (path) {
  302. _update_transform();
  303. }
  304. }
  305. real_t PathFollow3D::get_h_offset() const {
  306. return h_offset;
  307. }
  308. void PathFollow3D::set_v_offset(real_t p_v_offset) {
  309. v_offset = p_v_offset;
  310. if (path) {
  311. _update_transform();
  312. }
  313. }
  314. real_t PathFollow3D::get_v_offset() const {
  315. return v_offset;
  316. }
  317. real_t PathFollow3D::get_progress() const {
  318. return progress;
  319. }
  320. void PathFollow3D::set_progress_ratio(real_t p_ratio) {
  321. if (path && path->get_curve().is_valid() && path->get_curve()->get_baked_length()) {
  322. set_progress(p_ratio * path->get_curve()->get_baked_length());
  323. }
  324. }
  325. real_t PathFollow3D::get_progress_ratio() const {
  326. if (path && path->get_curve().is_valid() && path->get_curve()->get_baked_length()) {
  327. return get_progress() / path->get_curve()->get_baked_length();
  328. } else {
  329. return 0;
  330. }
  331. }
  332. void PathFollow3D::set_rotation_mode(RotationMode p_rotation_mode) {
  333. rotation_mode = p_rotation_mode;
  334. update_configuration_warnings();
  335. _update_transform();
  336. }
  337. PathFollow3D::RotationMode PathFollow3D::get_rotation_mode() const {
  338. return rotation_mode;
  339. }
  340. void PathFollow3D::set_loop(bool p_loop) {
  341. loop = p_loop;
  342. }
  343. bool PathFollow3D::has_loop() const {
  344. return loop;
  345. }
  346. void PathFollow3D::set_tilt_enabled(bool p_enable) {
  347. tilt_enabled = p_enable;
  348. }
  349. bool PathFollow3D::is_tilt_enabled() const {
  350. return tilt_enabled;
  351. }