path_3d.cpp 17 KB

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