path_3d.cpp 16 KB

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