path_3d.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  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. if (debug_instance.is_valid()) {
  41. ERR_FAIL_NULL(RenderingServer::get_singleton());
  42. RS::get_singleton()->free(debug_instance);
  43. }
  44. if (debug_mesh.is_valid()) {
  45. ERR_FAIL_NULL(RenderingServer::get_singleton());
  46. RS::get_singleton()->free(debug_mesh->get_rid());
  47. }
  48. }
  49. void Path3D::set_update_callback(Callable p_callback) {
  50. update_callback = p_callback;
  51. }
  52. void Path3D::_notification(int p_what) {
  53. switch (p_what) {
  54. case NOTIFICATION_ENTER_TREE: {
  55. SceneTree *st = SceneTree::get_singleton();
  56. if (st && st->is_debugging_paths_hint()) {
  57. _update_debug_mesh();
  58. }
  59. } break;
  60. case NOTIFICATION_EXIT_TREE: {
  61. SceneTree *st = SceneTree::get_singleton();
  62. if (st && st->is_debugging_paths_hint()) {
  63. RS::get_singleton()->instance_set_visible(debug_instance, false);
  64. }
  65. } break;
  66. case NOTIFICATION_TRANSFORM_CHANGED: {
  67. if (is_inside_tree()) {
  68. if (debug_instance.is_valid()) {
  69. RS::get_singleton()->instance_set_transform(debug_instance, get_global_transform());
  70. }
  71. update_callback.call();
  72. }
  73. } break;
  74. }
  75. }
  76. void Path3D::_update_debug_mesh() {
  77. SceneTree *st = SceneTree::get_singleton();
  78. if (!(st && st->is_debugging_paths_hint())) {
  79. return;
  80. }
  81. if (debug_mesh.is_null()) {
  82. debug_mesh.instantiate();
  83. }
  84. if (!(curve.is_valid())) {
  85. RS::get_singleton()->instance_set_visible(debug_instance, false);
  86. return;
  87. }
  88. if (curve->get_point_count() < 2) {
  89. RS::get_singleton()->instance_set_visible(debug_instance, false);
  90. return;
  91. }
  92. real_t interval = 0.1;
  93. const real_t length = curve->get_baked_length();
  94. if (length <= CMP_EPSILON) {
  95. RS::get_singleton()->instance_set_visible(debug_instance, false);
  96. return;
  97. }
  98. const int sample_count = int(length / interval) + 2;
  99. interval = length / (sample_count - 1);
  100. Vector<Vector3> ribbon;
  101. ribbon.resize(sample_count);
  102. Vector3 *ribbon_ptr = ribbon.ptrw();
  103. Vector<Vector3> bones;
  104. bones.resize(sample_count * 4);
  105. Vector3 *bones_ptr = bones.ptrw();
  106. for (int i = 0; i < sample_count; i++) {
  107. const Transform3D r = curve->sample_baked_with_rotation(i * interval, true, true);
  108. const Vector3 p1 = r.origin;
  109. const Vector3 side = r.basis.get_column(0);
  110. const Vector3 up = r.basis.get_column(1);
  111. const Vector3 forward = r.basis.get_column(2);
  112. // Path3D as a ribbon.
  113. ribbon_ptr[i] = p1;
  114. // Fish Bone.
  115. const Vector3 p_left = p1 + (side + forward - up * 0.3) * 0.06;
  116. const Vector3 p_right = p1 + (-side + forward - up * 0.3) * 0.06;
  117. const int bone_idx = i * 4;
  118. bones_ptr[bone_idx] = p1;
  119. bones_ptr[bone_idx + 1] = p_left;
  120. bones_ptr[bone_idx + 2] = p1;
  121. bones_ptr[bone_idx + 3] = p_right;
  122. }
  123. Array ribbon_array;
  124. ribbon_array.resize(Mesh::ARRAY_MAX);
  125. ribbon_array[Mesh::ARRAY_VERTEX] = ribbon;
  126. Array bone_array;
  127. bone_array.resize(Mesh::ARRAY_MAX);
  128. bone_array[Mesh::ARRAY_VERTEX] = bones;
  129. debug_mesh->clear_surfaces();
  130. debug_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_LINE_STRIP, ribbon_array);
  131. debug_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_LINES, bone_array);
  132. RS::get_singleton()->instance_set_base(debug_instance, debug_mesh->get_rid());
  133. RS::get_singleton()->mesh_surface_set_material(debug_mesh->get_rid(), 0, st->get_debug_paths_material()->get_rid());
  134. RS::get_singleton()->mesh_surface_set_material(debug_mesh->get_rid(), 1, st->get_debug_paths_material()->get_rid());
  135. if (is_inside_tree()) {
  136. RS::get_singleton()->instance_set_scenario(debug_instance, get_world_3d()->get_scenario());
  137. RS::get_singleton()->instance_set_transform(debug_instance, get_global_transform());
  138. RS::get_singleton()->instance_set_visible(debug_instance, is_visible_in_tree());
  139. }
  140. }
  141. void Path3D::_curve_changed() {
  142. if (is_inside_tree() && Engine::get_singleton()->is_editor_hint()) {
  143. update_gizmos();
  144. }
  145. if (is_inside_tree()) {
  146. emit_signal(SNAME("curve_changed"));
  147. }
  148. // Update the configuration warnings of all children of type PathFollow
  149. // previously used for PathFollowOriented (now enforced orientation is done in PathFollow). Also trigger transform update on PathFollow3Ds in deferred mode.
  150. if (is_inside_tree()) {
  151. for (int i = 0; i < get_child_count(); i++) {
  152. PathFollow3D *child = Object::cast_to<PathFollow3D>(get_child(i));
  153. if (child) {
  154. child->update_configuration_warnings();
  155. child->update_transform();
  156. }
  157. }
  158. }
  159. SceneTree *st = SceneTree::get_singleton();
  160. if (st && st->is_debugging_paths_hint()) {
  161. _update_debug_mesh();
  162. }
  163. }
  164. void Path3D::set_curve(const Ref<Curve3D> &p_curve) {
  165. if (curve.is_valid()) {
  166. curve->disconnect_changed(callable_mp(this, &Path3D::_curve_changed));
  167. }
  168. curve = p_curve;
  169. if (curve.is_valid()) {
  170. curve->connect_changed(callable_mp(this, &Path3D::_curve_changed));
  171. }
  172. _curve_changed();
  173. }
  174. Ref<Curve3D> Path3D::get_curve() const {
  175. return curve;
  176. }
  177. void Path3D::_bind_methods() {
  178. ClassDB::bind_method(D_METHOD("set_curve", "curve"), &Path3D::set_curve);
  179. ClassDB::bind_method(D_METHOD("get_curve"), &Path3D::get_curve);
  180. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve3D", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_EDITOR_INSTANTIATE_OBJECT), "set_curve", "get_curve");
  181. ADD_SIGNAL(MethodInfo("curve_changed"));
  182. }
  183. void PathFollow3D::update_transform() {
  184. if (!path) {
  185. return;
  186. }
  187. Ref<Curve3D> c = path->get_curve();
  188. if (!c.is_valid()) {
  189. return;
  190. }
  191. real_t bl = c->get_baked_length();
  192. if (bl == 0.0) {
  193. return;
  194. }
  195. Transform3D t;
  196. if (rotation_mode == ROTATION_NONE) {
  197. Vector3 pos = c->sample_baked(progress, cubic);
  198. t.origin = pos;
  199. } else {
  200. t = c->sample_baked_with_rotation(progress, cubic, false);
  201. Vector3 tangent = -t.basis.get_column(2); // Retain tangent for applying tilt.
  202. t = PathFollow3D::correct_posture(t, rotation_mode);
  203. // Switch Z+ and Z- if necessary.
  204. if (use_model_front) {
  205. t.basis *= Basis::from_scale(Vector3(-1.0, 1.0, -1.0));
  206. }
  207. // Apply tilt *after* correct_posture().
  208. if (tilt_enabled) {
  209. const real_t tilt = c->sample_baked_tilt(progress);
  210. const Basis twist(tangent, tilt);
  211. t.basis = twist * t.basis;
  212. }
  213. }
  214. // Apply offset and scale.
  215. Vector3 scale = get_transform().basis.get_scale();
  216. t.translate_local(Vector3(h_offset, v_offset, 0));
  217. t.basis.scale_local(scale);
  218. set_transform(t);
  219. }
  220. void PathFollow3D::_notification(int p_what) {
  221. switch (p_what) {
  222. case NOTIFICATION_ENTER_TREE: {
  223. Node *parent = get_parent();
  224. if (parent) {
  225. path = Object::cast_to<Path3D>(parent);
  226. update_transform();
  227. }
  228. } break;
  229. case NOTIFICATION_EXIT_TREE: {
  230. path = nullptr;
  231. } break;
  232. }
  233. }
  234. void PathFollow3D::set_cubic_interpolation_enabled(bool p_enabled) {
  235. cubic = p_enabled;
  236. }
  237. bool PathFollow3D::is_cubic_interpolation_enabled() const {
  238. return cubic;
  239. }
  240. void PathFollow3D::_validate_property(PropertyInfo &p_property) const {
  241. if (p_property.name == "offset") {
  242. real_t max = 10000;
  243. if (path && path->get_curve().is_valid()) {
  244. max = path->get_curve()->get_baked_length();
  245. }
  246. p_property.hint_string = "0," + rtos(max) + ",0.01,or_less,or_greater";
  247. }
  248. }
  249. PackedStringArray PathFollow3D::get_configuration_warnings() const {
  250. PackedStringArray warnings = Node3D::get_configuration_warnings();
  251. if (is_visible_in_tree() && is_inside_tree()) {
  252. if (!Object::cast_to<Path3D>(get_parent())) {
  253. warnings.push_back(RTR("PathFollow3D only works when set as a child of a Path3D node."));
  254. } else {
  255. Path3D *p = Object::cast_to<Path3D>(get_parent());
  256. if (p->get_curve().is_valid() && !p->get_curve()->is_up_vector_enabled() && rotation_mode == ROTATION_ORIENTED) {
  257. warnings.push_back(RTR("PathFollow3D's ROTATION_ORIENTED requires \"Up Vector\" to be enabled in its parent Path3D's Curve resource."));
  258. }
  259. }
  260. }
  261. return warnings;
  262. }
  263. Transform3D PathFollow3D::correct_posture(Transform3D p_transform, PathFollow3D::RotationMode p_rotation_mode) {
  264. Transform3D t = p_transform;
  265. // Modify frame according to rotation mode.
  266. if (p_rotation_mode == PathFollow3D::ROTATION_NONE) {
  267. // Clear rotation.
  268. t.basis = Basis();
  269. } else if (p_rotation_mode == PathFollow3D::ROTATION_ORIENTED) {
  270. Vector3 tangent = -t.basis.get_column(2);
  271. // Y-axis points up by default.
  272. t.basis = Basis::looking_at(tangent);
  273. } else {
  274. // Lock some euler axes.
  275. Vector3 euler = t.basis.get_euler_normalized(EulerOrder::YXZ);
  276. if (p_rotation_mode == PathFollow3D::ROTATION_Y) {
  277. // Only Y-axis allowed.
  278. euler[0] = 0;
  279. euler[2] = 0;
  280. } else if (p_rotation_mode == PathFollow3D::ROTATION_XY) {
  281. // XY allowed.
  282. euler[2] = 0;
  283. }
  284. Basis locked = Basis::from_euler(euler, EulerOrder::YXZ);
  285. t.basis = locked;
  286. }
  287. return t;
  288. }
  289. void PathFollow3D::_bind_methods() {
  290. ClassDB::bind_method(D_METHOD("set_progress", "progress"), &PathFollow3D::set_progress);
  291. ClassDB::bind_method(D_METHOD("get_progress"), &PathFollow3D::get_progress);
  292. ClassDB::bind_method(D_METHOD("set_h_offset", "h_offset"), &PathFollow3D::set_h_offset);
  293. ClassDB::bind_method(D_METHOD("get_h_offset"), &PathFollow3D::get_h_offset);
  294. ClassDB::bind_method(D_METHOD("set_v_offset", "v_offset"), &PathFollow3D::set_v_offset);
  295. ClassDB::bind_method(D_METHOD("get_v_offset"), &PathFollow3D::get_v_offset);
  296. ClassDB::bind_method(D_METHOD("set_progress_ratio", "ratio"), &PathFollow3D::set_progress_ratio);
  297. ClassDB::bind_method(D_METHOD("get_progress_ratio"), &PathFollow3D::get_progress_ratio);
  298. ClassDB::bind_method(D_METHOD("set_rotation_mode", "rotation_mode"), &PathFollow3D::set_rotation_mode);
  299. ClassDB::bind_method(D_METHOD("get_rotation_mode"), &PathFollow3D::get_rotation_mode);
  300. ClassDB::bind_method(D_METHOD("set_cubic_interpolation", "enabled"), &PathFollow3D::set_cubic_interpolation_enabled);
  301. ClassDB::bind_method(D_METHOD("get_cubic_interpolation"), &PathFollow3D::is_cubic_interpolation_enabled);
  302. ClassDB::bind_method(D_METHOD("set_use_model_front", "enabled"), &PathFollow3D::set_use_model_front);
  303. ClassDB::bind_method(D_METHOD("is_using_model_front"), &PathFollow3D::is_using_model_front);
  304. ClassDB::bind_method(D_METHOD("set_loop", "loop"), &PathFollow3D::set_loop);
  305. ClassDB::bind_method(D_METHOD("has_loop"), &PathFollow3D::has_loop);
  306. ClassDB::bind_method(D_METHOD("set_tilt_enabled", "enabled"), &PathFollow3D::set_tilt_enabled);
  307. ClassDB::bind_method(D_METHOD("is_tilt_enabled"), &PathFollow3D::is_tilt_enabled);
  308. ClassDB::bind_static_method("PathFollow3D", D_METHOD("correct_posture", "transform", "rotation_mode"), &PathFollow3D::correct_posture);
  309. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "progress", PROPERTY_HINT_RANGE, "0,10000,0.01,or_less,or_greater,suffix:m"), "set_progress", "get_progress");
  310. 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");
  311. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "h_offset", PROPERTY_HINT_NONE, "suffix:m"), "set_h_offset", "get_h_offset");
  312. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "v_offset", PROPERTY_HINT_NONE, "suffix:m"), "set_v_offset", "get_v_offset");
  313. ADD_PROPERTY(PropertyInfo(Variant::INT, "rotation_mode", PROPERTY_HINT_ENUM, "None,Y,XY,XYZ,Oriented"), "set_rotation_mode", "get_rotation_mode");
  314. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_model_front"), "set_use_model_front", "is_using_model_front");
  315. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "cubic_interp"), "set_cubic_interpolation", "get_cubic_interpolation");
  316. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "loop"), "set_loop", "has_loop");
  317. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "tilt_enabled"), "set_tilt_enabled", "is_tilt_enabled");
  318. BIND_ENUM_CONSTANT(ROTATION_NONE);
  319. BIND_ENUM_CONSTANT(ROTATION_Y);
  320. BIND_ENUM_CONSTANT(ROTATION_XY);
  321. BIND_ENUM_CONSTANT(ROTATION_XYZ);
  322. BIND_ENUM_CONSTANT(ROTATION_ORIENTED);
  323. }
  324. void PathFollow3D::set_progress(real_t p_progress) {
  325. ERR_FAIL_COND(!isfinite(p_progress));
  326. if (progress == p_progress) {
  327. return;
  328. }
  329. progress = p_progress;
  330. if (path) {
  331. if (path->get_curve().is_valid()) {
  332. real_t path_length = path->get_curve()->get_baked_length();
  333. if (loop && path_length) {
  334. progress = Math::fposmod(progress, path_length);
  335. if (!Math::is_zero_approx(p_progress) && Math::is_zero_approx(progress)) {
  336. progress = path_length;
  337. }
  338. } else {
  339. progress = CLAMP(progress, 0, path_length);
  340. }
  341. }
  342. update_transform();
  343. }
  344. }
  345. void PathFollow3D::set_h_offset(real_t p_h_offset) {
  346. if (h_offset == p_h_offset) {
  347. return;
  348. }
  349. h_offset = p_h_offset;
  350. update_transform();
  351. }
  352. real_t PathFollow3D::get_h_offset() const {
  353. return h_offset;
  354. }
  355. void PathFollow3D::set_v_offset(real_t p_v_offset) {
  356. if (v_offset == p_v_offset) {
  357. return;
  358. }
  359. v_offset = p_v_offset;
  360. update_transform();
  361. }
  362. real_t PathFollow3D::get_v_offset() const {
  363. return v_offset;
  364. }
  365. real_t PathFollow3D::get_progress() const {
  366. return progress;
  367. }
  368. void PathFollow3D::set_progress_ratio(real_t p_ratio) {
  369. ERR_FAIL_NULL_MSG(path, "Can only set progress ratio on a PathFollow3D that is the child of a Path3D which is itself part of the scene tree.");
  370. ERR_FAIL_COND_MSG(path->get_curve().is_null(), "Can't set progress ratio on a PathFollow3D that does not have a Curve.");
  371. ERR_FAIL_COND_MSG(!path->get_curve()->get_baked_length(), "Can't set progress ratio on a PathFollow3D that has a 0 length curve.");
  372. set_progress(p_ratio * path->get_curve()->get_baked_length());
  373. }
  374. real_t PathFollow3D::get_progress_ratio() const {
  375. if (path && path->get_curve().is_valid() && path->get_curve()->get_baked_length()) {
  376. return get_progress() / path->get_curve()->get_baked_length();
  377. } else {
  378. return 0;
  379. }
  380. }
  381. void PathFollow3D::set_rotation_mode(RotationMode p_rotation_mode) {
  382. if (rotation_mode == p_rotation_mode) {
  383. return;
  384. }
  385. rotation_mode = p_rotation_mode;
  386. update_configuration_warnings();
  387. update_transform();
  388. }
  389. PathFollow3D::RotationMode PathFollow3D::get_rotation_mode() const {
  390. return rotation_mode;
  391. }
  392. void PathFollow3D::set_use_model_front(bool p_use_model_front) {
  393. if (use_model_front == p_use_model_front) {
  394. return;
  395. }
  396. use_model_front = p_use_model_front;
  397. update_transform();
  398. }
  399. bool PathFollow3D::is_using_model_front() const {
  400. return use_model_front;
  401. }
  402. void PathFollow3D::set_loop(bool p_loop) {
  403. if (loop == p_loop) {
  404. return;
  405. }
  406. loop = p_loop;
  407. update_transform();
  408. }
  409. bool PathFollow3D::has_loop() const {
  410. return loop;
  411. }
  412. void PathFollow3D::set_tilt_enabled(bool p_enabled) {
  413. if (tilt_enabled == p_enabled) {
  414. return;
  415. }
  416. tilt_enabled = p_enabled;
  417. update_transform();
  418. }
  419. bool PathFollow3D::is_tilt_enabled() const {
  420. return tilt_enabled;
  421. }