curve.cpp 74 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428
  1. /**************************************************************************/
  2. /* curve.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 "curve.h"
  31. #include "core/math/math_funcs.h"
  32. const char *Curve::SIGNAL_RANGE_CHANGED = "range_changed";
  33. Curve::Curve() {
  34. }
  35. void Curve::set_point_count(int p_count) {
  36. ERR_FAIL_COND(p_count < 0);
  37. int old_size = _points.size();
  38. if (old_size == p_count) {
  39. return;
  40. }
  41. if (old_size > p_count) {
  42. _points.resize(p_count);
  43. mark_dirty();
  44. } else {
  45. for (int i = p_count - old_size; i > 0; i--) {
  46. _add_point(Vector2());
  47. }
  48. }
  49. notify_property_list_changed();
  50. }
  51. int Curve::_add_point(Vector2 p_position, real_t p_left_tangent, real_t p_right_tangent, TangentMode p_left_mode, TangentMode p_right_mode) {
  52. // Add a point and preserve order
  53. // Curve bounds is in 0..1
  54. if (p_position.x > MAX_X) {
  55. p_position.x = MAX_X;
  56. } else if (p_position.x < MIN_X) {
  57. p_position.x = MIN_X;
  58. }
  59. int ret = -1;
  60. if (_points.size() == 0) {
  61. _points.push_back(Point(p_position, p_left_tangent, p_right_tangent, p_left_mode, p_right_mode));
  62. ret = 0;
  63. } else if (_points.size() == 1) {
  64. // TODO Is the `else` able to handle this block already?
  65. real_t diff = p_position.x - _points[0].position.x;
  66. if (diff > 0) {
  67. _points.push_back(Point(p_position, p_left_tangent, p_right_tangent, p_left_mode, p_right_mode));
  68. ret = 1;
  69. } else {
  70. _points.insert(0, Point(p_position, p_left_tangent, p_right_tangent, p_left_mode, p_right_mode));
  71. ret = 0;
  72. }
  73. } else {
  74. int i = get_index(p_position.x);
  75. if (i == 0 && p_position.x < _points[0].position.x) {
  76. // Insert before anything else
  77. _points.insert(0, Point(p_position, p_left_tangent, p_right_tangent, p_left_mode, p_right_mode));
  78. ret = 0;
  79. } else {
  80. // Insert between i and i+1
  81. ++i;
  82. _points.insert(i, Point(p_position, p_left_tangent, p_right_tangent, p_left_mode, p_right_mode));
  83. ret = i;
  84. }
  85. }
  86. update_auto_tangents(ret);
  87. mark_dirty();
  88. return ret;
  89. }
  90. int Curve::add_point(Vector2 p_position, real_t p_left_tangent, real_t p_right_tangent, TangentMode p_left_mode, TangentMode p_right_mode) {
  91. int ret = _add_point(p_position, p_left_tangent, p_right_tangent, p_left_mode, p_right_mode);
  92. notify_property_list_changed();
  93. return ret;
  94. }
  95. // TODO: Needed to make the curve editor function properly until https://github.com/godotengine/godot/issues/76985 is fixed.
  96. int Curve::add_point_no_update(Vector2 p_position, real_t p_left_tangent, real_t p_right_tangent, TangentMode p_left_mode, TangentMode p_right_mode) {
  97. int ret = _add_point(p_position, p_left_tangent, p_right_tangent, p_left_mode, p_right_mode);
  98. return ret;
  99. }
  100. int Curve::get_index(real_t p_offset) const {
  101. // Lower-bound float binary search
  102. int imin = 0;
  103. int imax = _points.size() - 1;
  104. while (imax - imin > 1) {
  105. int m = (imin + imax) / 2;
  106. real_t a = _points[m].position.x;
  107. real_t b = _points[m + 1].position.x;
  108. if (a < p_offset && b < p_offset) {
  109. imin = m;
  110. } else if (a > p_offset) {
  111. imax = m;
  112. } else {
  113. return m;
  114. }
  115. }
  116. // Will happen if the offset is out of bounds
  117. if (p_offset > _points[imax].position.x) {
  118. return imax;
  119. }
  120. return imin;
  121. }
  122. void Curve::clean_dupes() {
  123. bool dirty = false;
  124. for (int i = 1; i < _points.size(); ++i) {
  125. real_t diff = _points[i - 1].position.x - _points[i].position.x;
  126. if (diff <= CMP_EPSILON) {
  127. _points.remove_at(i);
  128. --i;
  129. dirty = true;
  130. }
  131. }
  132. if (dirty) {
  133. mark_dirty();
  134. }
  135. }
  136. void Curve::set_point_left_tangent(int p_index, real_t p_tangent) {
  137. ERR_FAIL_INDEX(p_index, _points.size());
  138. _points.write[p_index].left_tangent = p_tangent;
  139. _points.write[p_index].left_mode = TANGENT_FREE;
  140. mark_dirty();
  141. }
  142. void Curve::set_point_right_tangent(int p_index, real_t p_tangent) {
  143. ERR_FAIL_INDEX(p_index, _points.size());
  144. _points.write[p_index].right_tangent = p_tangent;
  145. _points.write[p_index].right_mode = TANGENT_FREE;
  146. mark_dirty();
  147. }
  148. void Curve::set_point_left_mode(int p_index, TangentMode p_mode) {
  149. ERR_FAIL_INDEX(p_index, _points.size());
  150. _points.write[p_index].left_mode = p_mode;
  151. if (p_index > 0) {
  152. if (p_mode == TANGENT_LINEAR) {
  153. Vector2 v = (_points[p_index - 1].position - _points[p_index].position).normalized();
  154. _points.write[p_index].left_tangent = v.y / v.x;
  155. }
  156. }
  157. mark_dirty();
  158. }
  159. void Curve::set_point_right_mode(int p_index, TangentMode p_mode) {
  160. ERR_FAIL_INDEX(p_index, _points.size());
  161. _points.write[p_index].right_mode = p_mode;
  162. if (p_index + 1 < _points.size()) {
  163. if (p_mode == TANGENT_LINEAR) {
  164. Vector2 v = (_points[p_index + 1].position - _points[p_index].position).normalized();
  165. _points.write[p_index].right_tangent = v.y / v.x;
  166. }
  167. }
  168. mark_dirty();
  169. }
  170. real_t Curve::get_point_left_tangent(int p_index) const {
  171. ERR_FAIL_INDEX_V(p_index, _points.size(), 0);
  172. return _points[p_index].left_tangent;
  173. }
  174. real_t Curve::get_point_right_tangent(int p_index) const {
  175. ERR_FAIL_INDEX_V(p_index, _points.size(), 0);
  176. return _points[p_index].right_tangent;
  177. }
  178. Curve::TangentMode Curve::get_point_left_mode(int p_index) const {
  179. ERR_FAIL_INDEX_V(p_index, _points.size(), TANGENT_FREE);
  180. return _points[p_index].left_mode;
  181. }
  182. Curve::TangentMode Curve::get_point_right_mode(int p_index) const {
  183. ERR_FAIL_INDEX_V(p_index, _points.size(), TANGENT_FREE);
  184. return _points[p_index].right_mode;
  185. }
  186. void Curve::_remove_point(int p_index) {
  187. ERR_FAIL_INDEX(p_index, _points.size());
  188. _points.remove_at(p_index);
  189. mark_dirty();
  190. }
  191. void Curve::remove_point(int p_index) {
  192. _remove_point(p_index);
  193. notify_property_list_changed();
  194. }
  195. void Curve::clear_points() {
  196. if (_points.is_empty()) {
  197. return;
  198. }
  199. _points.clear();
  200. mark_dirty();
  201. notify_property_list_changed();
  202. }
  203. void Curve::set_point_value(int p_index, real_t p_position) {
  204. ERR_FAIL_INDEX(p_index, _points.size());
  205. _points.write[p_index].position.y = p_position;
  206. update_auto_tangents(p_index);
  207. mark_dirty();
  208. }
  209. int Curve::set_point_offset(int p_index, real_t p_offset) {
  210. ERR_FAIL_INDEX_V(p_index, _points.size(), -1);
  211. Point p = _points[p_index];
  212. _remove_point(p_index);
  213. int i = _add_point(Vector2(p_offset, p.position.y));
  214. _points.write[i].left_tangent = p.left_tangent;
  215. _points.write[i].right_tangent = p.right_tangent;
  216. _points.write[i].left_mode = p.left_mode;
  217. _points.write[i].right_mode = p.right_mode;
  218. if (p_index != i) {
  219. update_auto_tangents(p_index);
  220. }
  221. update_auto_tangents(i);
  222. return i;
  223. }
  224. Vector2 Curve::get_point_position(int p_index) const {
  225. ERR_FAIL_INDEX_V(p_index, _points.size(), Vector2(0, 0));
  226. return _points[p_index].position;
  227. }
  228. Curve::Point Curve::get_point(int p_index) const {
  229. ERR_FAIL_INDEX_V(p_index, _points.size(), Point());
  230. return _points[p_index];
  231. }
  232. void Curve::update_auto_tangents(int p_index) {
  233. Point &p = _points.write[p_index];
  234. if (p_index > 0) {
  235. if (p.left_mode == TANGENT_LINEAR) {
  236. Vector2 v = (_points[p_index - 1].position - p.position).normalized();
  237. p.left_tangent = v.y / v.x;
  238. }
  239. if (_points[p_index - 1].right_mode == TANGENT_LINEAR) {
  240. Vector2 v = (_points[p_index - 1].position - p.position).normalized();
  241. _points.write[p_index - 1].right_tangent = v.y / v.x;
  242. }
  243. }
  244. if (p_index + 1 < _points.size()) {
  245. if (p.right_mode == TANGENT_LINEAR) {
  246. Vector2 v = (_points[p_index + 1].position - p.position).normalized();
  247. p.right_tangent = v.y / v.x;
  248. }
  249. if (_points[p_index + 1].left_mode == TANGENT_LINEAR) {
  250. Vector2 v = (_points[p_index + 1].position - p.position).normalized();
  251. _points.write[p_index + 1].left_tangent = v.y / v.x;
  252. }
  253. }
  254. }
  255. #define MIN_Y_RANGE 0.01
  256. void Curve::set_min_value(real_t p_min) {
  257. if (_minmax_set_once & 0b11 && p_min > _max_value - MIN_Y_RANGE) {
  258. _min_value = _max_value - MIN_Y_RANGE;
  259. } else {
  260. _minmax_set_once |= 0b10; // first bit is "min set"
  261. _min_value = p_min;
  262. }
  263. // Note: min and max are indicative values,
  264. // it's still possible that existing points are out of range at this point.
  265. emit_signal(SNAME(SIGNAL_RANGE_CHANGED));
  266. }
  267. void Curve::set_max_value(real_t p_max) {
  268. if (_minmax_set_once & 0b11 && p_max < _min_value + MIN_Y_RANGE) {
  269. _max_value = _min_value + MIN_Y_RANGE;
  270. } else {
  271. _minmax_set_once |= 0b01; // second bit is "max set"
  272. _max_value = p_max;
  273. }
  274. emit_signal(SNAME(SIGNAL_RANGE_CHANGED));
  275. }
  276. real_t Curve::sample(real_t p_offset) const {
  277. if (_points.size() == 0) {
  278. return 0;
  279. }
  280. if (_points.size() == 1) {
  281. return _points[0].position.y;
  282. }
  283. int i = get_index(p_offset);
  284. if (i == _points.size() - 1) {
  285. return _points[i].position.y;
  286. }
  287. real_t local = p_offset - _points[i].position.x;
  288. if (i == 0 && local <= 0) {
  289. return _points[0].position.y;
  290. }
  291. return sample_local_nocheck(i, local);
  292. }
  293. real_t Curve::sample_local_nocheck(int p_index, real_t p_local_offset) const {
  294. const Point a = _points[p_index];
  295. const Point b = _points[p_index + 1];
  296. /* Cubic bézier
  297. *
  298. * ac-----bc
  299. * / \
  300. * / \ Here with a.right_tangent > 0
  301. * / \ and b.left_tangent < 0
  302. * / \
  303. * a b
  304. *
  305. * |-d1--|-d2--|-d3--|
  306. *
  307. * d1 == d2 == d3 == d / 3
  308. */
  309. // Control points are chosen at equal distances
  310. real_t d = b.position.x - a.position.x;
  311. if (Math::is_zero_approx(d)) {
  312. return b.position.y;
  313. }
  314. p_local_offset /= d;
  315. d /= 3.0;
  316. real_t yac = a.position.y + d * a.right_tangent;
  317. real_t ybc = b.position.y - d * b.left_tangent;
  318. real_t y = Math::bezier_interpolate(a.position.y, yac, ybc, b.position.y, p_local_offset);
  319. return y;
  320. }
  321. void Curve::mark_dirty() {
  322. _baked_cache_dirty = true;
  323. emit_changed();
  324. }
  325. Array Curve::get_data() const {
  326. Array output;
  327. const unsigned int ELEMS = 5;
  328. output.resize(_points.size() * ELEMS);
  329. for (int j = 0; j < _points.size(); ++j) {
  330. const Point p = _points[j];
  331. int i = j * ELEMS;
  332. output[i] = p.position;
  333. output[i + 1] = p.left_tangent;
  334. output[i + 2] = p.right_tangent;
  335. output[i + 3] = p.left_mode;
  336. output[i + 4] = p.right_mode;
  337. }
  338. return output;
  339. }
  340. void Curve::set_data(const Array p_input) {
  341. const unsigned int ELEMS = 5;
  342. ERR_FAIL_COND(p_input.size() % ELEMS != 0);
  343. // Validate input
  344. for (int i = 0; i < p_input.size(); i += ELEMS) {
  345. ERR_FAIL_COND(p_input[i].get_type() != Variant::VECTOR2);
  346. ERR_FAIL_COND(!p_input[i + 1].is_num());
  347. ERR_FAIL_COND(p_input[i + 2].get_type() != Variant::FLOAT);
  348. ERR_FAIL_COND(p_input[i + 3].get_type() != Variant::INT);
  349. int left_mode = p_input[i + 3];
  350. ERR_FAIL_COND(left_mode < 0 || left_mode >= TANGENT_MODE_COUNT);
  351. ERR_FAIL_COND(p_input[i + 4].get_type() != Variant::INT);
  352. int right_mode = p_input[i + 4];
  353. ERR_FAIL_COND(right_mode < 0 || right_mode >= TANGENT_MODE_COUNT);
  354. }
  355. int old_size = _points.size();
  356. int new_size = p_input.size() / ELEMS;
  357. if (old_size != new_size) {
  358. _points.resize(new_size);
  359. }
  360. for (int j = 0; j < _points.size(); ++j) {
  361. Point &p = _points.write[j];
  362. int i = j * ELEMS;
  363. p.position = p_input[i];
  364. p.left_tangent = p_input[i + 1];
  365. p.right_tangent = p_input[i + 2];
  366. int left_mode = p_input[i + 3];
  367. int right_mode = p_input[i + 4];
  368. p.left_mode = (TangentMode)left_mode;
  369. p.right_mode = (TangentMode)right_mode;
  370. }
  371. mark_dirty();
  372. if (old_size != new_size) {
  373. notify_property_list_changed();
  374. }
  375. }
  376. void Curve::bake() {
  377. _baked_cache.clear();
  378. _baked_cache.resize(_bake_resolution);
  379. for (int i = 1; i < _bake_resolution - 1; ++i) {
  380. real_t x = i / static_cast<real_t>(_bake_resolution - 1);
  381. real_t y = sample(x);
  382. _baked_cache.write[i] = y;
  383. }
  384. if (_points.size() != 0) {
  385. _baked_cache.write[0] = _points[0].position.y;
  386. _baked_cache.write[_baked_cache.size() - 1] = _points[_points.size() - 1].position.y;
  387. }
  388. _baked_cache_dirty = false;
  389. }
  390. void Curve::set_bake_resolution(int p_resolution) {
  391. ERR_FAIL_COND(p_resolution < 1);
  392. ERR_FAIL_COND(p_resolution > 1000);
  393. _bake_resolution = p_resolution;
  394. _baked_cache_dirty = true;
  395. }
  396. real_t Curve::sample_baked(real_t p_offset) const {
  397. // Make sure that p_offset is finite.
  398. ERR_FAIL_COND_V_MSG(!Math::is_finite(p_offset), 0, "Offset is non-finite");
  399. if (_baked_cache_dirty) {
  400. // Last-second bake if not done already
  401. const_cast<Curve *>(this)->bake();
  402. }
  403. // Special cases if the cache is too small
  404. if (_baked_cache.size() == 0) {
  405. if (_points.size() == 0) {
  406. return 0;
  407. }
  408. return _points[0].position.y;
  409. } else if (_baked_cache.size() == 1) {
  410. return _baked_cache[0];
  411. }
  412. // Get interpolation index
  413. real_t fi = p_offset * (_baked_cache.size() - 1);
  414. int i = Math::floor(fi);
  415. if (i < 0) {
  416. i = 0;
  417. fi = 0;
  418. } else if (i >= _baked_cache.size()) {
  419. i = _baked_cache.size() - 1;
  420. fi = 0;
  421. }
  422. // Sample
  423. if (i + 1 < _baked_cache.size()) {
  424. real_t t = fi - i;
  425. return Math::lerp(_baked_cache[i], _baked_cache[i + 1], t);
  426. } else {
  427. return _baked_cache[_baked_cache.size() - 1];
  428. }
  429. }
  430. void Curve::ensure_default_setup(real_t p_min, real_t p_max) {
  431. if (_points.size() == 0 && _min_value == 0 && _max_value == 1) {
  432. add_point(Vector2(0, 1));
  433. add_point(Vector2(1, 1));
  434. set_min_value(p_min);
  435. set_max_value(p_max);
  436. }
  437. }
  438. bool Curve::_set(const StringName &p_name, const Variant &p_value) {
  439. Vector<String> components = String(p_name).split("/", true, 2);
  440. if (components.size() >= 2 && components[0].begins_with("point_") && components[0].trim_prefix("point_").is_valid_int()) {
  441. int point_index = components[0].trim_prefix("point_").to_int();
  442. const String &property = components[1];
  443. if (property == "position") {
  444. Vector2 position = p_value.operator Vector2();
  445. set_point_offset(point_index, position.x);
  446. set_point_value(point_index, position.y);
  447. return true;
  448. } else if (property == "left_tangent") {
  449. set_point_left_tangent(point_index, p_value);
  450. return true;
  451. } else if (property == "left_mode") {
  452. int mode = p_value;
  453. set_point_left_mode(point_index, (TangentMode)mode);
  454. return true;
  455. } else if (property == "right_tangent") {
  456. set_point_right_tangent(point_index, p_value);
  457. return true;
  458. } else if (property == "right_mode") {
  459. int mode = p_value;
  460. set_point_right_mode(point_index, (TangentMode)mode);
  461. return true;
  462. }
  463. }
  464. return false;
  465. }
  466. bool Curve::_get(const StringName &p_name, Variant &r_ret) const {
  467. Vector<String> components = String(p_name).split("/", true, 2);
  468. if (components.size() >= 2 && components[0].begins_with("point_") && components[0].trim_prefix("point_").is_valid_int()) {
  469. int point_index = components[0].trim_prefix("point_").to_int();
  470. const String &property = components[1];
  471. if (property == "position") {
  472. r_ret = get_point_position(point_index);
  473. return true;
  474. } else if (property == "left_tangent") {
  475. r_ret = get_point_left_tangent(point_index);
  476. return true;
  477. } else if (property == "left_mode") {
  478. r_ret = get_point_left_mode(point_index);
  479. return true;
  480. } else if (property == "right_tangent") {
  481. r_ret = get_point_right_tangent(point_index);
  482. return true;
  483. } else if (property == "right_mode") {
  484. r_ret = get_point_right_mode(point_index);
  485. return true;
  486. }
  487. }
  488. return false;
  489. }
  490. void Curve::_get_property_list(List<PropertyInfo> *p_list) const {
  491. for (int i = 0; i < _points.size(); i++) {
  492. PropertyInfo pi = PropertyInfo(Variant::VECTOR2, vformat("point_%d/position", i));
  493. pi.usage &= ~PROPERTY_USAGE_STORAGE;
  494. p_list->push_back(pi);
  495. if (i != 0) {
  496. pi = PropertyInfo(Variant::FLOAT, vformat("point_%d/left_tangent", i));
  497. pi.usage &= ~PROPERTY_USAGE_STORAGE;
  498. p_list->push_back(pi);
  499. pi = PropertyInfo(Variant::INT, vformat("point_%d/left_mode", i), PROPERTY_HINT_ENUM, "Free,Linear");
  500. pi.usage &= ~PROPERTY_USAGE_STORAGE;
  501. p_list->push_back(pi);
  502. }
  503. if (i != _points.size() - 1) {
  504. pi = PropertyInfo(Variant::FLOAT, vformat("point_%d/right_tangent", i));
  505. pi.usage &= ~PROPERTY_USAGE_STORAGE;
  506. p_list->push_back(pi);
  507. pi = PropertyInfo(Variant::INT, vformat("point_%d/right_mode", i), PROPERTY_HINT_ENUM, "Free,Linear");
  508. pi.usage &= ~PROPERTY_USAGE_STORAGE;
  509. p_list->push_back(pi);
  510. }
  511. }
  512. }
  513. void Curve::_bind_methods() {
  514. ClassDB::bind_method(D_METHOD("get_point_count"), &Curve::get_point_count);
  515. ClassDB::bind_method(D_METHOD("set_point_count", "count"), &Curve::set_point_count);
  516. ClassDB::bind_method(D_METHOD("add_point", "position", "left_tangent", "right_tangent", "left_mode", "right_mode"), &Curve::add_point, DEFVAL(0), DEFVAL(0), DEFVAL(TANGENT_FREE), DEFVAL(TANGENT_FREE));
  517. ClassDB::bind_method(D_METHOD("remove_point", "index"), &Curve::remove_point);
  518. ClassDB::bind_method(D_METHOD("clear_points"), &Curve::clear_points);
  519. ClassDB::bind_method(D_METHOD("get_point_position", "index"), &Curve::get_point_position);
  520. ClassDB::bind_method(D_METHOD("set_point_value", "index", "y"), &Curve::set_point_value);
  521. ClassDB::bind_method(D_METHOD("set_point_offset", "index", "offset"), &Curve::set_point_offset);
  522. ClassDB::bind_method(D_METHOD("sample", "offset"), &Curve::sample);
  523. ClassDB::bind_method(D_METHOD("sample_baked", "offset"), &Curve::sample_baked);
  524. ClassDB::bind_method(D_METHOD("get_point_left_tangent", "index"), &Curve::get_point_left_tangent);
  525. ClassDB::bind_method(D_METHOD("get_point_right_tangent", "index"), &Curve::get_point_right_tangent);
  526. ClassDB::bind_method(D_METHOD("get_point_left_mode", "index"), &Curve::get_point_left_mode);
  527. ClassDB::bind_method(D_METHOD("get_point_right_mode", "index"), &Curve::get_point_right_mode);
  528. ClassDB::bind_method(D_METHOD("set_point_left_tangent", "index", "tangent"), &Curve::set_point_left_tangent);
  529. ClassDB::bind_method(D_METHOD("set_point_right_tangent", "index", "tangent"), &Curve::set_point_right_tangent);
  530. ClassDB::bind_method(D_METHOD("set_point_left_mode", "index", "mode"), &Curve::set_point_left_mode);
  531. ClassDB::bind_method(D_METHOD("set_point_right_mode", "index", "mode"), &Curve::set_point_right_mode);
  532. ClassDB::bind_method(D_METHOD("get_min_value"), &Curve::get_min_value);
  533. ClassDB::bind_method(D_METHOD("set_min_value", "min"), &Curve::set_min_value);
  534. ClassDB::bind_method(D_METHOD("get_max_value"), &Curve::get_max_value);
  535. ClassDB::bind_method(D_METHOD("set_max_value", "max"), &Curve::set_max_value);
  536. ClassDB::bind_method(D_METHOD("clean_dupes"), &Curve::clean_dupes);
  537. ClassDB::bind_method(D_METHOD("bake"), &Curve::bake);
  538. ClassDB::bind_method(D_METHOD("get_bake_resolution"), &Curve::get_bake_resolution);
  539. ClassDB::bind_method(D_METHOD("set_bake_resolution", "resolution"), &Curve::set_bake_resolution);
  540. ClassDB::bind_method(D_METHOD("_get_data"), &Curve::get_data);
  541. ClassDB::bind_method(D_METHOD("_set_data", "data"), &Curve::set_data);
  542. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "min_value", PROPERTY_HINT_RANGE, "-1024,1024,0.01"), "set_min_value", "get_min_value");
  543. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "max_value", PROPERTY_HINT_RANGE, "-1024,1024,0.01"), "set_max_value", "get_max_value");
  544. ADD_PROPERTY(PropertyInfo(Variant::INT, "bake_resolution", PROPERTY_HINT_RANGE, "1,1000,1"), "set_bake_resolution", "get_bake_resolution");
  545. ADD_PROPERTY(PropertyInfo(Variant::INT, "_data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "_set_data", "_get_data");
  546. ADD_ARRAY_COUNT("Points", "point_count", "set_point_count", "get_point_count", "point_");
  547. ADD_SIGNAL(MethodInfo(SIGNAL_RANGE_CHANGED));
  548. BIND_ENUM_CONSTANT(TANGENT_FREE);
  549. BIND_ENUM_CONSTANT(TANGENT_LINEAR);
  550. BIND_ENUM_CONSTANT(TANGENT_MODE_COUNT);
  551. }
  552. int Curve2D::get_point_count() const {
  553. return points.size();
  554. }
  555. void Curve2D::set_point_count(int p_count) {
  556. ERR_FAIL_COND(p_count < 0);
  557. int old_size = points.size();
  558. if (old_size == p_count) {
  559. return;
  560. }
  561. if (old_size > p_count) {
  562. points.resize(p_count);
  563. mark_dirty();
  564. } else {
  565. for (int i = p_count - old_size; i > 0; i--) {
  566. _add_point(Vector2());
  567. }
  568. }
  569. notify_property_list_changed();
  570. }
  571. void Curve2D::_add_point(const Vector2 &p_position, const Vector2 &p_in, const Vector2 &p_out, int p_atpos) {
  572. Point n;
  573. n.position = p_position;
  574. n.in = p_in;
  575. n.out = p_out;
  576. if (p_atpos >= 0 && p_atpos < points.size()) {
  577. points.insert(p_atpos, n);
  578. } else {
  579. points.push_back(n);
  580. }
  581. mark_dirty();
  582. }
  583. void Curve2D::add_point(const Vector2 &p_position, const Vector2 &p_in, const Vector2 &p_out, int p_atpos) {
  584. _add_point(p_position, p_in, p_out, p_atpos);
  585. notify_property_list_changed();
  586. }
  587. void Curve2D::set_point_position(int p_index, const Vector2 &p_position) {
  588. ERR_FAIL_INDEX(p_index, points.size());
  589. points.write[p_index].position = p_position;
  590. mark_dirty();
  591. }
  592. Vector2 Curve2D::get_point_position(int p_index) const {
  593. ERR_FAIL_INDEX_V(p_index, points.size(), Vector2());
  594. return points[p_index].position;
  595. }
  596. void Curve2D::set_point_in(int p_index, const Vector2 &p_in) {
  597. ERR_FAIL_INDEX(p_index, points.size());
  598. points.write[p_index].in = p_in;
  599. mark_dirty();
  600. }
  601. Vector2 Curve2D::get_point_in(int p_index) const {
  602. ERR_FAIL_INDEX_V(p_index, points.size(), Vector2());
  603. return points[p_index].in;
  604. }
  605. void Curve2D::set_point_out(int p_index, const Vector2 &p_out) {
  606. ERR_FAIL_INDEX(p_index, points.size());
  607. points.write[p_index].out = p_out;
  608. mark_dirty();
  609. }
  610. Vector2 Curve2D::get_point_out(int p_index) const {
  611. ERR_FAIL_INDEX_V(p_index, points.size(), Vector2());
  612. return points[p_index].out;
  613. }
  614. void Curve2D::_remove_point(int p_index) {
  615. ERR_FAIL_INDEX(p_index, points.size());
  616. points.remove_at(p_index);
  617. mark_dirty();
  618. }
  619. void Curve2D::remove_point(int p_index) {
  620. _remove_point(p_index);
  621. notify_property_list_changed();
  622. }
  623. void Curve2D::clear_points() {
  624. if (!points.is_empty()) {
  625. points.clear();
  626. mark_dirty();
  627. notify_property_list_changed();
  628. }
  629. }
  630. Vector2 Curve2D::sample(int p_index, const real_t p_offset) const {
  631. int pc = points.size();
  632. ERR_FAIL_COND_V(pc == 0, Vector2());
  633. if (p_index >= pc - 1) {
  634. return points[pc - 1].position;
  635. } else if (p_index < 0) {
  636. return points[0].position;
  637. }
  638. Vector2 p0 = points[p_index].position;
  639. Vector2 p1 = p0 + points[p_index].out;
  640. Vector2 p3 = points[p_index + 1].position;
  641. Vector2 p2 = p3 + points[p_index + 1].in;
  642. return p0.bezier_interpolate(p1, p2, p3, p_offset);
  643. }
  644. Vector2 Curve2D::samplef(real_t p_findex) const {
  645. if (p_findex < 0) {
  646. p_findex = 0;
  647. } else if (p_findex >= points.size()) {
  648. p_findex = points.size();
  649. }
  650. return sample((int)p_findex, Math::fmod(p_findex, (real_t)1.0));
  651. }
  652. void Curve2D::mark_dirty() {
  653. baked_cache_dirty = true;
  654. emit_changed();
  655. }
  656. void Curve2D::_bake_segment2d(RBMap<real_t, Vector2> &r_bake, real_t p_begin, real_t p_end, const Vector2 &p_a, const Vector2 &p_out, const Vector2 &p_b, const Vector2 &p_in, int p_depth, int p_max_depth, real_t p_tol) const {
  657. real_t mp = p_begin + (p_end - p_begin) * 0.5;
  658. Vector2 beg = p_a.bezier_interpolate(p_a + p_out, p_b + p_in, p_b, p_begin);
  659. Vector2 mid = p_a.bezier_interpolate(p_a + p_out, p_b + p_in, p_b, mp);
  660. Vector2 end = p_a.bezier_interpolate(p_a + p_out, p_b + p_in, p_b, p_end);
  661. Vector2 na = (mid - beg).normalized();
  662. Vector2 nb = (end - mid).normalized();
  663. real_t dp = na.dot(nb);
  664. if (dp < Math::cos(Math::deg_to_rad(p_tol))) {
  665. r_bake[mp] = mid;
  666. }
  667. if (p_depth < p_max_depth) {
  668. _bake_segment2d(r_bake, p_begin, mp, p_a, p_out, p_b, p_in, p_depth + 1, p_max_depth, p_tol);
  669. _bake_segment2d(r_bake, mp, p_end, p_a, p_out, p_b, p_in, p_depth + 1, p_max_depth, p_tol);
  670. }
  671. }
  672. void Curve2D::_bake_segment2d_even_length(RBMap<real_t, Vector2> &r_bake, real_t p_begin, real_t p_end, const Vector2 &p_a, const Vector2 &p_out, const Vector2 &p_b, const Vector2 &p_in, int p_depth, int p_max_depth, real_t p_length) const {
  673. Vector2 beg = p_a.bezier_interpolate(p_a + p_out, p_b + p_in, p_b, p_begin);
  674. Vector2 end = p_a.bezier_interpolate(p_a + p_out, p_b + p_in, p_b, p_end);
  675. real_t length = beg.distance_to(end);
  676. if (length > p_length && p_depth < p_max_depth) {
  677. real_t mp = (p_begin + p_end) * 0.5;
  678. Vector2 mid = p_a.bezier_interpolate(p_a + p_out, p_b + p_in, p_b, mp);
  679. r_bake[mp] = mid;
  680. _bake_segment2d_even_length(r_bake, p_begin, mp, p_a, p_out, p_b, p_in, p_depth + 1, p_max_depth, p_length);
  681. _bake_segment2d_even_length(r_bake, mp, p_end, p_a, p_out, p_b, p_in, p_depth + 1, p_max_depth, p_length);
  682. }
  683. }
  684. Vector2 Curve2D::_calculate_tangent(const Vector2 &p_begin, const Vector2 &p_control_1, const Vector2 &p_control_2, const Vector2 &p_end, const real_t p_t) {
  685. // Handle corner cases.
  686. if (Math::is_zero_approx(p_t - 0.0f) && p_control_1.is_equal_approx(p_begin)) {
  687. return (p_end - p_begin).normalized();
  688. }
  689. if (Math::is_zero_approx(p_t - 1.0f) && p_control_2.is_equal_approx(p_end)) {
  690. return (p_end - p_begin).normalized();
  691. }
  692. return p_begin.bezier_derivative(p_control_1, p_control_2, p_end, p_t).normalized();
  693. }
  694. void Curve2D::_bake() const {
  695. if (!baked_cache_dirty) {
  696. return;
  697. }
  698. baked_max_ofs = 0;
  699. baked_cache_dirty = false;
  700. if (points.size() == 0) {
  701. baked_point_cache.clear();
  702. baked_dist_cache.clear();
  703. baked_forward_vector_cache.clear();
  704. return;
  705. }
  706. if (points.size() == 1) {
  707. baked_point_cache.resize(1);
  708. baked_point_cache.set(0, points[0].position);
  709. baked_dist_cache.resize(1);
  710. baked_dist_cache.set(0, 0.0);
  711. baked_forward_vector_cache.resize(1);
  712. baked_forward_vector_cache.set(0, Vector2(0.0, 0.1));
  713. return;
  714. }
  715. // Tessellate curve to (almost) even length segments
  716. {
  717. Vector<RBMap<real_t, Vector2>> midpoints = _tessellate_even_length(10, bake_interval);
  718. int pc = 1;
  719. for (int i = 0; i < points.size() - 1; i++) {
  720. pc++;
  721. pc += midpoints[i].size();
  722. }
  723. baked_point_cache.resize(pc);
  724. baked_dist_cache.resize(pc);
  725. baked_forward_vector_cache.resize(pc);
  726. Vector2 *bpw = baked_point_cache.ptrw();
  727. Vector2 *bfw = baked_forward_vector_cache.ptrw();
  728. // Collect positions and sample tilts and tangents for each baked points.
  729. bpw[0] = points[0].position;
  730. bfw[0] = _calculate_tangent(points[0].position, points[0].position + points[0].out, points[1].position + points[1].in, points[1].position, 0.0);
  731. int pidx = 0;
  732. for (int i = 0; i < points.size() - 1; i++) {
  733. for (const KeyValue<real_t, Vector2> &E : midpoints[i]) {
  734. pidx++;
  735. bpw[pidx] = E.value;
  736. bfw[pidx] = _calculate_tangent(points[i].position, points[i].position + points[i].out, points[i + 1].position + points[i + 1].in, points[i + 1].position, E.key);
  737. }
  738. pidx++;
  739. bpw[pidx] = points[i + 1].position;
  740. bfw[pidx] = _calculate_tangent(points[i].position, points[i].position + points[i].out, points[i + 1].position + points[i + 1].in, points[i + 1].position, 1.0);
  741. }
  742. // Recalculate the baked distances.
  743. real_t *bdw = baked_dist_cache.ptrw();
  744. bdw[0] = 0.0;
  745. for (int i = 0; i < pc - 1; i++) {
  746. bdw[i + 1] = bdw[i] + bpw[i].distance_to(bpw[i + 1]);
  747. }
  748. baked_max_ofs = bdw[pc - 1];
  749. }
  750. }
  751. real_t Curve2D::get_baked_length() const {
  752. if (baked_cache_dirty) {
  753. _bake();
  754. }
  755. return baked_max_ofs;
  756. }
  757. Curve2D::Interval Curve2D::_find_interval(real_t p_offset) const {
  758. Interval interval = {
  759. -1,
  760. 0.0
  761. };
  762. ERR_FAIL_COND_V_MSG(baked_cache_dirty, interval, "Backed cache is dirty");
  763. int pc = baked_point_cache.size();
  764. ERR_FAIL_COND_V_MSG(pc < 2, interval, "Less than two points in cache");
  765. int start = 0;
  766. int end = pc;
  767. int idx = (end + start) / 2;
  768. // Binary search to find baked points.
  769. while (start < idx) {
  770. real_t offset = baked_dist_cache[idx];
  771. if (p_offset <= offset) {
  772. end = idx;
  773. } else {
  774. start = idx;
  775. }
  776. idx = (end + start) / 2;
  777. }
  778. real_t offset_begin = baked_dist_cache[idx];
  779. real_t offset_end = baked_dist_cache[idx + 1];
  780. real_t idx_interval = offset_end - offset_begin;
  781. ERR_FAIL_COND_V_MSG(p_offset < offset_begin || p_offset > offset_end, interval, "Offset out of range.");
  782. interval.idx = idx;
  783. if (idx_interval < FLT_EPSILON) {
  784. interval.frac = 0.5; // For a very short interval, 0.5 is a reasonable choice.
  785. ERR_FAIL_V_MSG(interval, "Zero length interval.");
  786. }
  787. interval.frac = (p_offset - offset_begin) / idx_interval;
  788. return interval;
  789. }
  790. Vector2 Curve2D::_sample_baked(Interval p_interval, bool p_cubic) const {
  791. // Assuming p_interval is valid.
  792. ERR_FAIL_INDEX_V_MSG(p_interval.idx, baked_point_cache.size(), Vector2(), "Invalid interval");
  793. int idx = p_interval.idx;
  794. real_t frac = p_interval.frac;
  795. const Vector2 *r = baked_point_cache.ptr();
  796. int pc = baked_point_cache.size();
  797. if (p_cubic) {
  798. Vector2 pre = idx > 0 ? r[idx - 1] : r[idx];
  799. Vector2 post = (idx < (pc - 2)) ? r[idx + 2] : r[idx + 1];
  800. return r[idx].cubic_interpolate(r[idx + 1], pre, post, frac);
  801. } else {
  802. return r[idx].lerp(r[idx + 1], frac);
  803. }
  804. }
  805. Transform2D Curve2D::_sample_posture(Interval p_interval) const {
  806. // Assuming that p_interval is valid.
  807. ERR_FAIL_INDEX_V_MSG(p_interval.idx, baked_point_cache.size(), Transform2D(), "Invalid interval");
  808. int idx = p_interval.idx;
  809. real_t frac = p_interval.frac;
  810. Vector2 forward_begin = baked_forward_vector_cache[idx];
  811. Vector2 forward_end = baked_forward_vector_cache[idx + 1];
  812. // Build frames at both ends of the interval, then interpolate.
  813. const Vector2 forward = forward_begin.slerp(forward_end, frac).normalized();
  814. const Vector2 side = Vector2(-forward.y, forward.x);
  815. return Transform2D(forward, side, Vector2(0.0, 0.0));
  816. }
  817. Vector2 Curve2D::sample_baked(real_t p_offset, bool p_cubic) const {
  818. // Make sure that p_offset is finite.
  819. ERR_FAIL_COND_V_MSG(!Math::is_finite(p_offset), Vector2(), "Offset is non-finite");
  820. if (baked_cache_dirty) {
  821. _bake();
  822. }
  823. // Validate: Curve may not have baked points.
  824. int pc = baked_point_cache.size();
  825. ERR_FAIL_COND_V_MSG(pc == 0, Vector2(), "No points in Curve2D.");
  826. if (pc == 1) {
  827. return baked_point_cache[0];
  828. }
  829. p_offset = CLAMP(p_offset, 0.0, get_baked_length()); // PathFollower implement wrapping logic.
  830. Curve2D::Interval interval = _find_interval(p_offset);
  831. return _sample_baked(interval, p_cubic);
  832. }
  833. Transform2D Curve2D::sample_baked_with_rotation(real_t p_offset, bool p_cubic) const {
  834. // Make sure that p_offset is finite.
  835. ERR_FAIL_COND_V_MSG(!Math::is_finite(p_offset), Transform2D(), "Offset is non-finite");
  836. if (baked_cache_dirty) {
  837. _bake();
  838. }
  839. // Validate: Curve may not have baked points.
  840. const int point_count = baked_point_cache.size();
  841. ERR_FAIL_COND_V_MSG(point_count == 0, Transform2D(), "No points in Curve3D.");
  842. if (point_count == 1) {
  843. Transform2D t;
  844. t.set_origin(baked_point_cache.get(0));
  845. ERR_FAIL_V_MSG(t, "Only 1 point in Curve2D.");
  846. }
  847. p_offset = CLAMP(p_offset, 0.0, get_baked_length()); // PathFollower implement wrapping logic.
  848. // 0. Find interval for all sampling steps.
  849. Curve2D::Interval interval = _find_interval(p_offset);
  850. // 1. Sample position.
  851. Vector2 pos = _sample_baked(interval, p_cubic);
  852. // 2. Sample rotation frame.
  853. Transform2D frame = _sample_posture(interval);
  854. frame.set_origin(pos);
  855. return frame;
  856. }
  857. PackedVector2Array Curve2D::get_baked_points() const {
  858. if (baked_cache_dirty) {
  859. _bake();
  860. }
  861. return baked_point_cache;
  862. }
  863. void Curve2D::set_bake_interval(real_t p_tolerance) {
  864. bake_interval = p_tolerance;
  865. mark_dirty();
  866. }
  867. real_t Curve2D::get_bake_interval() const {
  868. return bake_interval;
  869. }
  870. PackedVector2Array Curve2D::get_points() const {
  871. return _get_data()["points"];
  872. }
  873. Vector2 Curve2D::get_closest_point(const Vector2 &p_to_point) const {
  874. // Brute force method.
  875. if (baked_cache_dirty) {
  876. _bake();
  877. }
  878. // Validate: Curve may not have baked points.
  879. int pc = baked_point_cache.size();
  880. ERR_FAIL_COND_V_MSG(pc == 0, Vector2(), "No points in Curve2D.");
  881. if (pc == 1) {
  882. return baked_point_cache.get(0);
  883. }
  884. const Vector2 *r = baked_point_cache.ptr();
  885. Vector2 nearest;
  886. real_t nearest_dist = -1.0f;
  887. for (int i = 0; i < pc - 1; i++) {
  888. const real_t interval = baked_dist_cache[i + 1] - baked_dist_cache[i];
  889. Vector2 origin = r[i];
  890. Vector2 direction = (r[i + 1] - origin) / interval;
  891. real_t d = CLAMP((p_to_point - origin).dot(direction), 0.0f, interval);
  892. Vector2 proj = origin + direction * d;
  893. real_t dist = proj.distance_squared_to(p_to_point);
  894. if (nearest_dist < 0.0f || dist < nearest_dist) {
  895. nearest = proj;
  896. nearest_dist = dist;
  897. }
  898. }
  899. return nearest;
  900. }
  901. real_t Curve2D::get_closest_offset(const Vector2 &p_to_point) const {
  902. // Brute force method.
  903. if (baked_cache_dirty) {
  904. _bake();
  905. }
  906. // Validate: Curve may not have baked points.
  907. int pc = baked_point_cache.size();
  908. ERR_FAIL_COND_V_MSG(pc == 0, 0.0f, "No points in Curve2D.");
  909. if (pc == 1) {
  910. return 0.0f;
  911. }
  912. const Vector2 *r = baked_point_cache.ptr();
  913. real_t nearest = 0.0f;
  914. real_t nearest_dist = -1.0f;
  915. real_t offset = 0.0f;
  916. for (int i = 0; i < pc - 1; i++) {
  917. offset = baked_dist_cache[i];
  918. const real_t interval = baked_dist_cache[i + 1] - baked_dist_cache[i];
  919. Vector2 origin = r[i];
  920. Vector2 direction = (r[i + 1] - origin) / interval;
  921. real_t d = CLAMP((p_to_point - origin).dot(direction), 0.0f, interval);
  922. Vector2 proj = origin + direction * d;
  923. real_t dist = proj.distance_squared_to(p_to_point);
  924. if (nearest_dist < 0.0f || dist < nearest_dist) {
  925. nearest = offset + d;
  926. nearest_dist = dist;
  927. }
  928. }
  929. return nearest;
  930. }
  931. Dictionary Curve2D::_get_data() const {
  932. Dictionary dc;
  933. PackedVector2Array d;
  934. d.resize(points.size() * 3);
  935. Vector2 *w = d.ptrw();
  936. for (int i = 0; i < points.size(); i++) {
  937. w[i * 3 + 0] = points[i].in;
  938. w[i * 3 + 1] = points[i].out;
  939. w[i * 3 + 2] = points[i].position;
  940. }
  941. dc["points"] = d;
  942. return dc;
  943. }
  944. void Curve2D::_set_data(const Dictionary &p_data) {
  945. ERR_FAIL_COND(!p_data.has("points"));
  946. PackedVector2Array rp = p_data["points"];
  947. int pc = rp.size();
  948. ERR_FAIL_COND(pc % 3 != 0);
  949. int old_size = points.size();
  950. int new_size = pc / 3;
  951. if (old_size != new_size) {
  952. points.resize(new_size);
  953. }
  954. const Vector2 *r = rp.ptr();
  955. for (int i = 0; i < points.size(); i++) {
  956. points.write[i].in = r[i * 3 + 0];
  957. points.write[i].out = r[i * 3 + 1];
  958. points.write[i].position = r[i * 3 + 2];
  959. }
  960. mark_dirty();
  961. if (old_size != new_size) {
  962. notify_property_list_changed();
  963. }
  964. }
  965. PackedVector2Array Curve2D::tessellate(int p_max_stages, real_t p_tolerance) const {
  966. PackedVector2Array tess;
  967. if (points.size() == 0) {
  968. return tess;
  969. }
  970. // The current implementation requires a sorted map.
  971. Vector<RBMap<real_t, Vector2>> midpoints;
  972. midpoints.resize(points.size() - 1);
  973. int pc = 1;
  974. for (int i = 0; i < points.size() - 1; i++) {
  975. _bake_segment2d(midpoints.write[i], 0, 1, points[i].position, points[i].out, points[i + 1].position, points[i + 1].in, 0, p_max_stages, p_tolerance);
  976. pc++;
  977. pc += midpoints[i].size();
  978. }
  979. tess.resize(pc);
  980. Vector2 *bpw = tess.ptrw();
  981. bpw[0] = points[0].position;
  982. int pidx = 0;
  983. for (int i = 0; i < points.size() - 1; i++) {
  984. for (const KeyValue<real_t, Vector2> &E : midpoints[i]) {
  985. pidx++;
  986. bpw[pidx] = E.value;
  987. }
  988. pidx++;
  989. bpw[pidx] = points[i + 1].position;
  990. }
  991. return tess;
  992. }
  993. Vector<RBMap<real_t, Vector2>> Curve2D::_tessellate_even_length(int p_max_stages, real_t p_length) const {
  994. Vector<RBMap<real_t, Vector2>> midpoints;
  995. ERR_FAIL_COND_V_MSG(points.size() < 2, midpoints, "Curve must have at least 2 control point");
  996. midpoints.resize(points.size() - 1);
  997. for (int i = 0; i < points.size() - 1; i++) {
  998. _bake_segment2d_even_length(midpoints.write[i], 0, 1, points[i].position, points[i].out, points[i + 1].position, points[i + 1].in, 0, p_max_stages, p_length);
  999. }
  1000. return midpoints;
  1001. }
  1002. PackedVector2Array Curve2D::tessellate_even_length(int p_max_stages, real_t p_length) const {
  1003. PackedVector2Array tess;
  1004. Vector<RBMap<real_t, Vector2>> midpoints = _tessellate_even_length(p_max_stages, p_length);
  1005. if (midpoints.size() == 0) {
  1006. return tess;
  1007. }
  1008. int pc = 1;
  1009. for (int i = 0; i < points.size() - 1; i++) {
  1010. pc++;
  1011. pc += midpoints[i].size();
  1012. }
  1013. tess.resize(pc);
  1014. Vector2 *bpw = tess.ptrw();
  1015. bpw[0] = points[0].position;
  1016. int pidx = 0;
  1017. for (int i = 0; i < points.size() - 1; i++) {
  1018. for (const KeyValue<real_t, Vector2> &E : midpoints[i]) {
  1019. pidx++;
  1020. bpw[pidx] = E.value;
  1021. }
  1022. pidx++;
  1023. bpw[pidx] = points[i + 1].position;
  1024. }
  1025. return tess;
  1026. }
  1027. bool Curve2D::_set(const StringName &p_name, const Variant &p_value) {
  1028. Vector<String> components = String(p_name).split("/", true, 2);
  1029. if (components.size() >= 2 && components[0].begins_with("point_") && components[0].trim_prefix("point_").is_valid_int()) {
  1030. int point_index = components[0].trim_prefix("point_").to_int();
  1031. const String &property = components[1];
  1032. if (property == "position") {
  1033. set_point_position(point_index, p_value);
  1034. return true;
  1035. } else if (property == "in") {
  1036. set_point_in(point_index, p_value);
  1037. return true;
  1038. } else if (property == "out") {
  1039. set_point_out(point_index, p_value);
  1040. return true;
  1041. }
  1042. }
  1043. return false;
  1044. }
  1045. bool Curve2D::_get(const StringName &p_name, Variant &r_ret) const {
  1046. Vector<String> components = String(p_name).split("/", true, 2);
  1047. if (components.size() >= 2 && components[0].begins_with("point_") && components[0].trim_prefix("point_").is_valid_int()) {
  1048. int point_index = components[0].trim_prefix("point_").to_int();
  1049. const String &property = components[1];
  1050. if (property == "position") {
  1051. r_ret = get_point_position(point_index);
  1052. return true;
  1053. } else if (property == "in") {
  1054. r_ret = get_point_in(point_index);
  1055. return true;
  1056. } else if (property == "out") {
  1057. r_ret = get_point_out(point_index);
  1058. return true;
  1059. }
  1060. }
  1061. return false;
  1062. }
  1063. void Curve2D::_get_property_list(List<PropertyInfo> *p_list) const {
  1064. for (int i = 0; i < points.size(); i++) {
  1065. PropertyInfo pi = PropertyInfo(Variant::VECTOR2, vformat("point_%d/position", i));
  1066. pi.usage &= ~PROPERTY_USAGE_STORAGE;
  1067. p_list->push_back(pi);
  1068. if (i != 0) {
  1069. pi = PropertyInfo(Variant::VECTOR2, vformat("point_%d/in", i));
  1070. pi.usage &= ~PROPERTY_USAGE_STORAGE;
  1071. p_list->push_back(pi);
  1072. }
  1073. if (i != points.size() - 1) {
  1074. pi = PropertyInfo(Variant::VECTOR2, vformat("point_%d/out", i));
  1075. pi.usage &= ~PROPERTY_USAGE_STORAGE;
  1076. p_list->push_back(pi);
  1077. }
  1078. }
  1079. }
  1080. void Curve2D::_bind_methods() {
  1081. ClassDB::bind_method(D_METHOD("get_point_count"), &Curve2D::get_point_count);
  1082. ClassDB::bind_method(D_METHOD("set_point_count", "count"), &Curve2D::set_point_count);
  1083. ClassDB::bind_method(D_METHOD("add_point", "position", "in", "out", "index"), &Curve2D::add_point, DEFVAL(Vector2()), DEFVAL(Vector2()), DEFVAL(-1));
  1084. ClassDB::bind_method(D_METHOD("set_point_position", "idx", "position"), &Curve2D::set_point_position);
  1085. ClassDB::bind_method(D_METHOD("get_point_position", "idx"), &Curve2D::get_point_position);
  1086. ClassDB::bind_method(D_METHOD("set_point_in", "idx", "position"), &Curve2D::set_point_in);
  1087. ClassDB::bind_method(D_METHOD("get_point_in", "idx"), &Curve2D::get_point_in);
  1088. ClassDB::bind_method(D_METHOD("set_point_out", "idx", "position"), &Curve2D::set_point_out);
  1089. ClassDB::bind_method(D_METHOD("get_point_out", "idx"), &Curve2D::get_point_out);
  1090. ClassDB::bind_method(D_METHOD("remove_point", "idx"), &Curve2D::remove_point);
  1091. ClassDB::bind_method(D_METHOD("clear_points"), &Curve2D::clear_points);
  1092. ClassDB::bind_method(D_METHOD("sample", "idx", "t"), &Curve2D::sample);
  1093. ClassDB::bind_method(D_METHOD("samplef", "fofs"), &Curve2D::samplef);
  1094. //ClassDB::bind_method(D_METHOD("bake","subdivs"),&Curve2D::bake,DEFVAL(10));
  1095. ClassDB::bind_method(D_METHOD("set_bake_interval", "distance"), &Curve2D::set_bake_interval);
  1096. ClassDB::bind_method(D_METHOD("get_bake_interval"), &Curve2D::get_bake_interval);
  1097. ClassDB::bind_method(D_METHOD("get_baked_length"), &Curve2D::get_baked_length);
  1098. ClassDB::bind_method(D_METHOD("sample_baked", "offset", "cubic"), &Curve2D::sample_baked, DEFVAL(0.0), DEFVAL(false));
  1099. ClassDB::bind_method(D_METHOD("sample_baked_with_rotation", "offset", "cubic"), &Curve2D::sample_baked_with_rotation, DEFVAL(0.0), DEFVAL(false));
  1100. ClassDB::bind_method(D_METHOD("get_baked_points"), &Curve2D::get_baked_points);
  1101. ClassDB::bind_method(D_METHOD("get_closest_point", "to_point"), &Curve2D::get_closest_point);
  1102. ClassDB::bind_method(D_METHOD("get_closest_offset", "to_point"), &Curve2D::get_closest_offset);
  1103. ClassDB::bind_method(D_METHOD("tessellate", "max_stages", "tolerance_degrees"), &Curve2D::tessellate, DEFVAL(5), DEFVAL(4));
  1104. ClassDB::bind_method(D_METHOD("tessellate_even_length", "max_stages", "tolerance_length"), &Curve2D::tessellate_even_length, DEFVAL(5), DEFVAL(20.0));
  1105. ClassDB::bind_method(D_METHOD("_get_data"), &Curve2D::_get_data);
  1106. ClassDB::bind_method(D_METHOD("_set_data", "data"), &Curve2D::_set_data);
  1107. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "bake_interval", PROPERTY_HINT_RANGE, "0.01,512,0.01"), "set_bake_interval", "get_bake_interval");
  1108. ADD_PROPERTY(PropertyInfo(Variant::INT, "_data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "_set_data", "_get_data");
  1109. ADD_ARRAY_COUNT("Points", "point_count", "set_point_count", "get_point_count", "point_");
  1110. }
  1111. Curve2D::Curve2D() {}
  1112. /***********************************************************************************/
  1113. /***********************************************************************************/
  1114. /***********************************************************************************/
  1115. /***********************************************************************************/
  1116. /***********************************************************************************/
  1117. /***********************************************************************************/
  1118. int Curve3D::get_point_count() const {
  1119. return points.size();
  1120. }
  1121. void Curve3D::set_point_count(int p_count) {
  1122. ERR_FAIL_COND(p_count < 0);
  1123. int old_size = points.size();
  1124. if (old_size == p_count) {
  1125. return;
  1126. }
  1127. if (old_size > p_count) {
  1128. points.resize(p_count);
  1129. mark_dirty();
  1130. } else {
  1131. for (int i = p_count - old_size; i > 0; i--) {
  1132. _add_point(Vector3());
  1133. }
  1134. }
  1135. notify_property_list_changed();
  1136. }
  1137. void Curve3D::_add_point(const Vector3 &p_position, const Vector3 &p_in, const Vector3 &p_out, int p_atpos) {
  1138. Point n;
  1139. n.position = p_position;
  1140. n.in = p_in;
  1141. n.out = p_out;
  1142. if (p_atpos >= 0 && p_atpos < points.size()) {
  1143. points.insert(p_atpos, n);
  1144. } else {
  1145. points.push_back(n);
  1146. }
  1147. mark_dirty();
  1148. }
  1149. void Curve3D::add_point(const Vector3 &p_position, const Vector3 &p_in, const Vector3 &p_out, int p_atpos) {
  1150. _add_point(p_position, p_in, p_out, p_atpos);
  1151. notify_property_list_changed();
  1152. }
  1153. void Curve3D::set_point_position(int p_index, const Vector3 &p_position) {
  1154. ERR_FAIL_INDEX(p_index, points.size());
  1155. points.write[p_index].position = p_position;
  1156. mark_dirty();
  1157. }
  1158. Vector3 Curve3D::get_point_position(int p_index) const {
  1159. ERR_FAIL_INDEX_V(p_index, points.size(), Vector3());
  1160. return points[p_index].position;
  1161. }
  1162. void Curve3D::set_point_tilt(int p_index, real_t p_tilt) {
  1163. ERR_FAIL_INDEX(p_index, points.size());
  1164. points.write[p_index].tilt = p_tilt;
  1165. mark_dirty();
  1166. }
  1167. real_t Curve3D::get_point_tilt(int p_index) const {
  1168. ERR_FAIL_INDEX_V(p_index, points.size(), 0);
  1169. return points[p_index].tilt;
  1170. }
  1171. void Curve3D::set_point_in(int p_index, const Vector3 &p_in) {
  1172. ERR_FAIL_INDEX(p_index, points.size());
  1173. points.write[p_index].in = p_in;
  1174. mark_dirty();
  1175. }
  1176. Vector3 Curve3D::get_point_in(int p_index) const {
  1177. ERR_FAIL_INDEX_V(p_index, points.size(), Vector3());
  1178. return points[p_index].in;
  1179. }
  1180. void Curve3D::set_point_out(int p_index, const Vector3 &p_out) {
  1181. ERR_FAIL_INDEX(p_index, points.size());
  1182. points.write[p_index].out = p_out;
  1183. mark_dirty();
  1184. }
  1185. Vector3 Curve3D::get_point_out(int p_index) const {
  1186. ERR_FAIL_INDEX_V(p_index, points.size(), Vector3());
  1187. return points[p_index].out;
  1188. }
  1189. void Curve3D::_remove_point(int p_index) {
  1190. ERR_FAIL_INDEX(p_index, points.size());
  1191. points.remove_at(p_index);
  1192. mark_dirty();
  1193. }
  1194. void Curve3D::remove_point(int p_index) {
  1195. _remove_point(p_index);
  1196. if (closed && points.size() < 2) {
  1197. set_closed(false);
  1198. }
  1199. notify_property_list_changed();
  1200. }
  1201. void Curve3D::clear_points() {
  1202. if (!points.is_empty()) {
  1203. points.clear();
  1204. mark_dirty();
  1205. notify_property_list_changed();
  1206. }
  1207. }
  1208. Vector3 Curve3D::sample(int p_index, real_t p_offset) const {
  1209. int pc = points.size();
  1210. ERR_FAIL_COND_V(pc == 0, Vector3());
  1211. if (p_index >= pc - 1) {
  1212. if (!closed) {
  1213. return points[pc - 1].position;
  1214. } else {
  1215. p_index = pc - 1;
  1216. }
  1217. } else if (p_index < 0) {
  1218. return points[0].position;
  1219. }
  1220. Vector3 p0 = points[p_index].position;
  1221. Vector3 p1 = p0 + points[p_index].out;
  1222. Vector3 p3, p2;
  1223. if (!closed || p_index < pc - 1) {
  1224. p3 = points[p_index + 1].position;
  1225. p2 = p3 + points[p_index + 1].in;
  1226. } else {
  1227. p3 = points[0].position;
  1228. p2 = p3 + points[0].in;
  1229. }
  1230. return p0.bezier_interpolate(p1, p2, p3, p_offset);
  1231. }
  1232. Vector3 Curve3D::samplef(real_t p_findex) const {
  1233. if (p_findex < 0) {
  1234. p_findex = 0;
  1235. } else if (p_findex >= points.size()) {
  1236. p_findex = points.size();
  1237. }
  1238. return sample((int)p_findex, Math::fmod(p_findex, (real_t)1.0));
  1239. }
  1240. void Curve3D::mark_dirty() {
  1241. baked_cache_dirty = true;
  1242. emit_changed();
  1243. }
  1244. void Curve3D::_bake_segment3d(RBMap<real_t, Vector3> &r_bake, real_t p_begin, real_t p_end, const Vector3 &p_a, const Vector3 &p_out, const Vector3 &p_b, const Vector3 &p_in, int p_depth, int p_max_depth, real_t p_tol) const {
  1245. real_t mp = p_begin + (p_end - p_begin) * 0.5;
  1246. Vector3 beg = p_a.bezier_interpolate(p_a + p_out, p_b + p_in, p_b, p_begin);
  1247. Vector3 mid = p_a.bezier_interpolate(p_a + p_out, p_b + p_in, p_b, mp);
  1248. Vector3 end = p_a.bezier_interpolate(p_a + p_out, p_b + p_in, p_b, p_end);
  1249. Vector3 na = (mid - beg).normalized();
  1250. Vector3 nb = (end - mid).normalized();
  1251. real_t dp = na.dot(nb);
  1252. if (dp < Math::cos(Math::deg_to_rad(p_tol))) {
  1253. r_bake[mp] = mid;
  1254. }
  1255. if (p_depth < p_max_depth) {
  1256. _bake_segment3d(r_bake, p_begin, mp, p_a, p_out, p_b, p_in, p_depth + 1, p_max_depth, p_tol);
  1257. _bake_segment3d(r_bake, mp, p_end, p_a, p_out, p_b, p_in, p_depth + 1, p_max_depth, p_tol);
  1258. }
  1259. }
  1260. void Curve3D::_bake_segment3d_even_length(RBMap<real_t, Vector3> &r_bake, real_t p_begin, real_t p_end, const Vector3 &p_a, const Vector3 &p_out, const Vector3 &p_b, const Vector3 &p_in, int p_depth, int p_max_depth, real_t p_length) const {
  1261. Vector3 beg = p_a.bezier_interpolate(p_a + p_out, p_b + p_in, p_b, p_begin);
  1262. Vector3 end = p_a.bezier_interpolate(p_a + p_out, p_b + p_in, p_b, p_end);
  1263. real_t length = beg.distance_to(end);
  1264. if (length > p_length && p_depth < p_max_depth) {
  1265. real_t mp = (p_begin + p_end) * 0.5;
  1266. Vector3 mid = p_a.bezier_interpolate(p_a + p_out, p_b + p_in, p_b, mp);
  1267. r_bake[mp] = mid;
  1268. _bake_segment3d_even_length(r_bake, p_begin, mp, p_a, p_out, p_b, p_in, p_depth + 1, p_max_depth, p_length);
  1269. _bake_segment3d_even_length(r_bake, mp, p_end, p_a, p_out, p_b, p_in, p_depth + 1, p_max_depth, p_length);
  1270. }
  1271. }
  1272. Vector3 Curve3D::_calculate_tangent(const Vector3 &p_begin, const Vector3 &p_control_1, const Vector3 &p_control_2, const Vector3 &p_end, const real_t p_t) {
  1273. // Handle corner cases.
  1274. if (Math::is_zero_approx(p_t - 0.0f) && p_control_1.is_equal_approx(p_begin)) {
  1275. return (p_end - p_begin).normalized();
  1276. }
  1277. if (Math::is_zero_approx(p_t - 1.0f) && p_control_2.is_equal_approx(p_end)) {
  1278. return (p_end - p_begin).normalized();
  1279. }
  1280. return p_begin.bezier_derivative(p_control_1, p_control_2, p_end, p_t).normalized();
  1281. }
  1282. void Curve3D::_bake() const {
  1283. if (!baked_cache_dirty) {
  1284. return;
  1285. }
  1286. baked_max_ofs = 0;
  1287. baked_cache_dirty = false;
  1288. if (points.size() == 0) {
  1289. #ifdef TOOLS_ENABLED
  1290. points_in_cache.clear();
  1291. #endif
  1292. baked_point_cache.clear();
  1293. baked_tilt_cache.clear();
  1294. baked_dist_cache.clear();
  1295. baked_forward_vector_cache.clear();
  1296. baked_up_vector_cache.clear();
  1297. return;
  1298. }
  1299. if (points.size() == 1) {
  1300. #ifdef TOOLS_ENABLED
  1301. points_in_cache.resize(1);
  1302. points_in_cache.set(0, 0);
  1303. #endif
  1304. baked_point_cache.resize(1);
  1305. baked_point_cache.set(0, points[0].position);
  1306. baked_tilt_cache.resize(1);
  1307. baked_tilt_cache.set(0, points[0].tilt);
  1308. baked_dist_cache.resize(1);
  1309. baked_dist_cache.set(0, 0.0);
  1310. baked_forward_vector_cache.resize(1);
  1311. baked_forward_vector_cache.set(0, Vector3(0.0, 0.0, 1.0));
  1312. if (up_vector_enabled) {
  1313. baked_up_vector_cache.resize(1);
  1314. baked_up_vector_cache.set(0, Vector3(0.0, 1.0, 0.0));
  1315. } else {
  1316. baked_up_vector_cache.clear();
  1317. }
  1318. return;
  1319. }
  1320. // Step 1: Tessellate curve to (almost) even length segments
  1321. {
  1322. Vector<RBMap<real_t, Vector3>> midpoints = _tessellate_even_length(10, bake_interval);
  1323. const int num_intervals = closed ? points.size() : points.size() - 1;
  1324. #ifdef TOOLS_ENABLED
  1325. points_in_cache.resize(closed ? (points.size() + 1) : points.size());
  1326. points_in_cache.set(0, 0);
  1327. #endif
  1328. // Point Count: Begins at 1 to account for the last point.
  1329. int pc = 1;
  1330. for (int i = 0; i < num_intervals; i++) {
  1331. pc++;
  1332. pc += midpoints[i].size();
  1333. #ifdef TOOLS_ENABLED
  1334. points_in_cache.set(i + 1, pc - 1);
  1335. #endif
  1336. }
  1337. baked_point_cache.resize(pc);
  1338. baked_tilt_cache.resize(pc);
  1339. baked_dist_cache.resize(pc);
  1340. baked_forward_vector_cache.resize(pc);
  1341. Vector3 *bpw = baked_point_cache.ptrw();
  1342. real_t *btw = baked_tilt_cache.ptrw();
  1343. Vector3 *bfw = baked_forward_vector_cache.ptrw();
  1344. // Collect positions and sample tilts and tangents for each baked points.
  1345. bpw[0] = points[0].position;
  1346. bfw[0] = _calculate_tangent(points[0].position, points[0].position + points[0].out, points[1].position + points[1].in, points[1].position, 0.0);
  1347. btw[0] = points[0].tilt;
  1348. int pidx = 0;
  1349. for (int i = 0; i < num_intervals; i++) {
  1350. for (const KeyValue<real_t, Vector3> &E : midpoints[i]) {
  1351. pidx++;
  1352. bpw[pidx] = E.value;
  1353. if (!closed || i < num_intervals - 1) {
  1354. bfw[pidx] = _calculate_tangent(points[i].position, points[i].position + points[i].out, points[i + 1].position + points[i + 1].in, points[i + 1].position, E.key);
  1355. btw[pidx] = Math::lerp(points[i].tilt, points[i + 1].tilt, E.key);
  1356. } else {
  1357. bfw[pidx] = _calculate_tangent(points[i].position, points[i].position + points[i].out, points[0].position + points[0].in, points[0].position, E.key);
  1358. btw[pidx] = Math::lerp(points[i].tilt, points[0].tilt, E.key);
  1359. }
  1360. }
  1361. pidx++;
  1362. if (!closed || i < num_intervals - 1) {
  1363. bpw[pidx] = points[i + 1].position;
  1364. bfw[pidx] = _calculate_tangent(points[i].position, points[i].position + points[i].out, points[i + 1].position + points[i + 1].in, points[i + 1].position, 1.0);
  1365. btw[pidx] = points[i + 1].tilt;
  1366. } else {
  1367. bpw[pidx] = points[0].position;
  1368. bfw[pidx] = _calculate_tangent(points[i].position, points[i].position + points[i].out, points[0].position + points[0].in, points[0].position, 1.0);
  1369. btw[pidx] = points[0].tilt;
  1370. }
  1371. }
  1372. // Recalculate the baked distances.
  1373. real_t *bdw = baked_dist_cache.ptrw();
  1374. bdw[0] = 0.0;
  1375. for (int i = 0; i < pc - 1; i++) {
  1376. bdw[i + 1] = bdw[i] + bpw[i].distance_to(bpw[i + 1]);
  1377. }
  1378. baked_max_ofs = bdw[pc - 1];
  1379. }
  1380. if (!up_vector_enabled) {
  1381. baked_up_vector_cache.resize(0);
  1382. return;
  1383. }
  1384. // Step 2: Calculate the up vectors and the whole local reference frame
  1385. //
  1386. // See Dougan, Carl. "The parallel transport frame." Game Programming Gems 2 (2001): 215-219.
  1387. // for an example discussing about why not the Frenet frame.
  1388. {
  1389. int point_count = baked_point_cache.size();
  1390. baked_up_vector_cache.resize(point_count);
  1391. Vector3 *up_write = baked_up_vector_cache.ptrw();
  1392. const Vector3 *forward_ptr = baked_forward_vector_cache.ptr();
  1393. const Vector3 *points_ptr = baked_point_cache.ptr();
  1394. Basis frame; // X-right, Y-up, -Z-forward.
  1395. Basis frame_prev;
  1396. // Set the initial frame based on Y-up rule.
  1397. {
  1398. Vector3 forward = forward_ptr[0];
  1399. if (abs(forward.dot(Vector3(0, 1, 0))) > 1.0 - UNIT_EPSILON) {
  1400. frame_prev = Basis::looking_at(forward, Vector3(1, 0, 0));
  1401. } else {
  1402. frame_prev = Basis::looking_at(forward, Vector3(0, 1, 0));
  1403. }
  1404. up_write[0] = frame_prev.get_column(1);
  1405. }
  1406. // Calculate the Parallel Transport Frame.
  1407. for (int idx = 1; idx < point_count; idx++) {
  1408. Vector3 forward = forward_ptr[idx];
  1409. Basis rotate;
  1410. rotate.rotate_to_align(-frame_prev.get_column(2), forward);
  1411. frame = rotate * frame_prev;
  1412. frame.orthonormalize(); // guard against float error accumulation
  1413. up_write[idx] = frame.get_column(1);
  1414. frame_prev = frame;
  1415. }
  1416. bool is_loop = true;
  1417. // Loop smoothing only applies when the curve is a loop, which means two ends meet, and share forward directions.
  1418. {
  1419. if (!points_ptr[0].is_equal_approx(points_ptr[point_count - 1])) {
  1420. is_loop = false;
  1421. }
  1422. real_t dot = forward_ptr[0].dot(forward_ptr[point_count - 1]);
  1423. if (dot < 1.0 - UNIT_EPSILON) { // Alignment should not be too tight, or it doesn't work for coarse bake interval.
  1424. is_loop = false;
  1425. }
  1426. }
  1427. // Twist up vectors, so that they align at two ends of the curve.
  1428. if (is_loop) {
  1429. const Vector3 up_start = up_write[0];
  1430. const Vector3 up_end = up_write[point_count - 1];
  1431. real_t sign = SIGN(up_end.cross(up_start).dot(forward_ptr[0]));
  1432. real_t full_angle = Quaternion(up_end, up_start).get_angle();
  1433. if (abs(full_angle) < CMP_EPSILON) {
  1434. return;
  1435. } else {
  1436. const real_t *dists = baked_dist_cache.ptr();
  1437. for (int idx = 1; idx < point_count; idx++) {
  1438. const real_t frac = dists[idx] / baked_max_ofs;
  1439. const real_t angle = Math::lerp((real_t)0.0, full_angle, frac);
  1440. Basis twist(forward_ptr[idx] * sign, angle);
  1441. up_write[idx] = twist.xform(up_write[idx]);
  1442. }
  1443. }
  1444. }
  1445. }
  1446. }
  1447. real_t Curve3D::get_baked_length() const {
  1448. if (baked_cache_dirty) {
  1449. _bake();
  1450. }
  1451. return baked_max_ofs;
  1452. }
  1453. Curve3D::Interval Curve3D::_find_interval(real_t p_offset) const {
  1454. Interval interval = {
  1455. -1,
  1456. 0.0
  1457. };
  1458. ERR_FAIL_COND_V_MSG(baked_cache_dirty, interval, "Backed cache is dirty");
  1459. int pc = baked_point_cache.size();
  1460. ERR_FAIL_COND_V_MSG(pc < 2, interval, "Less than two points in cache");
  1461. int start = 0;
  1462. int end = pc;
  1463. int idx = (end + start) / 2;
  1464. // Binary search to find baked points.
  1465. while (start < idx) {
  1466. real_t offset = baked_dist_cache[idx];
  1467. if (p_offset <= offset) {
  1468. end = idx;
  1469. } else {
  1470. start = idx;
  1471. }
  1472. idx = (end + start) / 2;
  1473. }
  1474. real_t offset_begin = baked_dist_cache[idx];
  1475. real_t offset_end = baked_dist_cache[idx + 1];
  1476. real_t idx_interval = offset_end - offset_begin;
  1477. ERR_FAIL_COND_V_MSG(p_offset < offset_begin || p_offset > offset_end, interval, "Offset out of range.");
  1478. interval.idx = idx;
  1479. if (idx_interval < FLT_EPSILON) {
  1480. interval.frac = 0.5; // For a very short interval, 0.5 is a reasonable choice.
  1481. ERR_FAIL_V_MSG(interval, "Zero length interval.");
  1482. }
  1483. interval.frac = (p_offset - offset_begin) / idx_interval;
  1484. return interval;
  1485. }
  1486. Vector3 Curve3D::_sample_baked(Interval p_interval, bool p_cubic) const {
  1487. // Assuming p_interval is valid.
  1488. ERR_FAIL_INDEX_V_MSG(p_interval.idx, baked_point_cache.size(), Vector3(), "Invalid interval");
  1489. int idx = p_interval.idx;
  1490. real_t frac = p_interval.frac;
  1491. const Vector3 *r = baked_point_cache.ptr();
  1492. int pc = baked_point_cache.size();
  1493. if (p_cubic) {
  1494. Vector3 pre = idx > 0 ? r[idx - 1] : r[idx];
  1495. Vector3 post = (idx < (pc - 2)) ? r[idx + 2] : r[idx + 1];
  1496. return r[idx].cubic_interpolate(r[idx + 1], pre, post, frac);
  1497. } else {
  1498. return r[idx].lerp(r[idx + 1], frac);
  1499. }
  1500. }
  1501. real_t Curve3D::_sample_baked_tilt(Interval p_interval) const {
  1502. // Assuming that p_interval is valid.
  1503. ERR_FAIL_INDEX_V_MSG(p_interval.idx, baked_tilt_cache.size(), 0.0, "Invalid interval");
  1504. int idx = p_interval.idx;
  1505. real_t frac = p_interval.frac;
  1506. const real_t *r = baked_tilt_cache.ptr();
  1507. return Math::lerp(r[idx], r[idx + 1], frac);
  1508. }
  1509. // Internal method for getting posture at a baked point. Assuming caller
  1510. // make all safety checks.
  1511. Basis Curve3D::_compose_posture(int p_index) const {
  1512. Vector3 forward = baked_forward_vector_cache[p_index];
  1513. Vector3 up;
  1514. if (up_vector_enabled) {
  1515. up = baked_up_vector_cache[p_index];
  1516. } else {
  1517. up = Vector3(0.0, 1.0, 0.0);
  1518. }
  1519. const Basis frame = Basis::looking_at(forward, up);
  1520. return frame;
  1521. }
  1522. Basis Curve3D::_sample_posture(Interval p_interval, bool p_apply_tilt) const {
  1523. // Assuming that p_interval is valid.
  1524. ERR_FAIL_INDEX_V_MSG(p_interval.idx, baked_point_cache.size(), Basis(), "Invalid interval");
  1525. if (up_vector_enabled) {
  1526. ERR_FAIL_INDEX_V_MSG(p_interval.idx, baked_up_vector_cache.size(), Basis(), "Invalid interval");
  1527. }
  1528. int idx = p_interval.idx;
  1529. real_t frac = p_interval.frac;
  1530. // Get frames at both ends of the interval, then interpolate.
  1531. const Basis frame_begin = _compose_posture(idx);
  1532. const Basis frame_end = _compose_posture(idx + 1);
  1533. const Basis frame = frame_begin.slerp(frame_end, frac).orthonormalized();
  1534. if (!p_apply_tilt) {
  1535. return frame;
  1536. }
  1537. // Applying tilt.
  1538. const real_t tilt = _sample_baked_tilt(p_interval);
  1539. Vector3 tangent = -frame.get_column(2);
  1540. const Basis twist(tangent, tilt);
  1541. return twist * frame;
  1542. }
  1543. #ifdef TOOLS_ENABLED
  1544. // Get posture at a control point. Needed for Gizmo implementation.
  1545. Basis Curve3D::get_point_baked_posture(int p_index, bool p_apply_tilt) const {
  1546. if (baked_cache_dirty) {
  1547. _bake();
  1548. }
  1549. // Assuming that p_idx is valid.
  1550. ERR_FAIL_INDEX_V_MSG(p_index, points_in_cache.size(), Basis(), "Invalid control point index");
  1551. int baked_idx = points_in_cache[p_index];
  1552. Basis frame = _compose_posture(baked_idx);
  1553. if (!p_apply_tilt) {
  1554. return frame;
  1555. }
  1556. // Applying tilt.
  1557. const real_t tilt = points[p_index].tilt;
  1558. Vector3 tangent = -frame.get_column(2);
  1559. const Basis twist(tangent, tilt);
  1560. return twist * frame;
  1561. }
  1562. #endif
  1563. Vector3 Curve3D::sample_baked(real_t p_offset, bool p_cubic) const {
  1564. // Make sure that p_offset is finite.
  1565. ERR_FAIL_COND_V_MSG(!Math::is_finite(p_offset), Vector3(), "Offset is non-finite");
  1566. if (baked_cache_dirty) {
  1567. _bake();
  1568. }
  1569. // Validate: Curve may not have baked points.
  1570. int pc = baked_point_cache.size();
  1571. ERR_FAIL_COND_V_MSG(pc == 0, Vector3(), "No points in Curve3D.");
  1572. if (pc == 1) {
  1573. return baked_point_cache[0];
  1574. }
  1575. p_offset = CLAMP(p_offset, 0.0, get_baked_length()); // PathFollower implement wrapping logic.
  1576. Curve3D::Interval interval = _find_interval(p_offset);
  1577. return _sample_baked(interval, p_cubic);
  1578. }
  1579. Transform3D Curve3D::sample_baked_with_rotation(real_t p_offset, bool p_cubic, bool p_apply_tilt) const {
  1580. // Make sure that p_offset is finite.
  1581. ERR_FAIL_COND_V_MSG(!Math::is_finite(p_offset), Transform3D(), "Offset is non-finite");
  1582. if (baked_cache_dirty) {
  1583. _bake();
  1584. }
  1585. // Validate: Curve may not have baked points.
  1586. const int point_count = baked_point_cache.size();
  1587. ERR_FAIL_COND_V_MSG(point_count == 0, Transform3D(), "No points in Curve3D.");
  1588. if (point_count == 1) {
  1589. Transform3D t;
  1590. t.origin = baked_point_cache.get(0);
  1591. ERR_FAIL_V_MSG(t, "Only 1 point in Curve3D.");
  1592. }
  1593. p_offset = CLAMP(p_offset, 0.0, get_baked_length()); // PathFollower implement wrapping logic.
  1594. // 0. Find interval for all sampling steps.
  1595. Curve3D::Interval interval = _find_interval(p_offset);
  1596. // 1. Sample position.
  1597. Vector3 pos = _sample_baked(interval, p_cubic);
  1598. // 2. Sample rotation frame.
  1599. Basis frame = _sample_posture(interval, p_apply_tilt);
  1600. return Transform3D(frame, pos);
  1601. }
  1602. real_t Curve3D::sample_baked_tilt(real_t p_offset) const {
  1603. // Make sure that p_offset is finite.
  1604. ERR_FAIL_COND_V_MSG(!Math::is_finite(p_offset), 0, "Offset is non-finite");
  1605. if (baked_cache_dirty) {
  1606. _bake();
  1607. }
  1608. // Validate: Curve may not have baked tilts.
  1609. int pc = baked_tilt_cache.size();
  1610. ERR_FAIL_COND_V_MSG(pc == 0, 0, "No tilts in Curve3D.");
  1611. if (pc == 1) {
  1612. return baked_tilt_cache.get(0);
  1613. }
  1614. p_offset = CLAMP(p_offset, 0.0, get_baked_length()); // PathFollower implement wrapping logic
  1615. Curve3D::Interval interval = _find_interval(p_offset);
  1616. return _sample_baked_tilt(interval);
  1617. }
  1618. Vector3 Curve3D::sample_baked_up_vector(real_t p_offset, bool p_apply_tilt) const {
  1619. // Make sure that p_offset is finite.
  1620. ERR_FAIL_COND_V_MSG(!Math::is_finite(p_offset), Vector3(0, 1, 0), "Offset is non-finite");
  1621. if (baked_cache_dirty) {
  1622. _bake();
  1623. }
  1624. // Validate: Curve may not have baked up vectors.
  1625. ERR_FAIL_COND_V_MSG(!up_vector_enabled, Vector3(0, 1, 0), "No up vectors in Curve3D.");
  1626. int count = baked_up_vector_cache.size();
  1627. if (count == 1) {
  1628. return baked_up_vector_cache.get(0);
  1629. }
  1630. p_offset = CLAMP(p_offset, 0.0, get_baked_length()); // PathFollower implement wrapping logic.
  1631. Curve3D::Interval interval = _find_interval(p_offset);
  1632. return _sample_posture(interval, p_apply_tilt).get_column(1);
  1633. }
  1634. PackedVector3Array Curve3D::get_baked_points() const {
  1635. if (baked_cache_dirty) {
  1636. _bake();
  1637. }
  1638. return baked_point_cache;
  1639. }
  1640. Vector<real_t> Curve3D::get_baked_tilts() const {
  1641. if (baked_cache_dirty) {
  1642. _bake();
  1643. }
  1644. return baked_tilt_cache;
  1645. }
  1646. PackedVector3Array Curve3D::get_baked_up_vectors() const {
  1647. if (baked_cache_dirty) {
  1648. _bake();
  1649. }
  1650. return baked_up_vector_cache;
  1651. }
  1652. Vector3 Curve3D::get_closest_point(const Vector3 &p_to_point) const {
  1653. // Brute force method.
  1654. if (baked_cache_dirty) {
  1655. _bake();
  1656. }
  1657. // Validate: Curve may not have baked points.
  1658. int pc = baked_point_cache.size();
  1659. ERR_FAIL_COND_V_MSG(pc == 0, Vector3(), "No points in Curve3D.");
  1660. if (pc == 1) {
  1661. return baked_point_cache.get(0);
  1662. }
  1663. const Vector3 *r = baked_point_cache.ptr();
  1664. Vector3 nearest;
  1665. real_t nearest_dist = -1.0f;
  1666. for (int i = 0; i < pc - 1; i++) {
  1667. const real_t interval = baked_dist_cache[i + 1] - baked_dist_cache[i];
  1668. Vector3 origin = r[i];
  1669. Vector3 direction = (r[i + 1] - origin) / interval;
  1670. real_t d = CLAMP((p_to_point - origin).dot(direction), 0.0f, interval);
  1671. Vector3 proj = origin + direction * d;
  1672. real_t dist = proj.distance_squared_to(p_to_point);
  1673. if (nearest_dist < 0.0f || dist < nearest_dist) {
  1674. nearest = proj;
  1675. nearest_dist = dist;
  1676. }
  1677. }
  1678. return nearest;
  1679. }
  1680. PackedVector3Array Curve3D::get_points() const {
  1681. return _get_data()["points"];
  1682. }
  1683. real_t Curve3D::get_closest_offset(const Vector3 &p_to_point) const {
  1684. // Brute force method.
  1685. if (baked_cache_dirty) {
  1686. _bake();
  1687. }
  1688. // Validate: Curve may not have baked points.
  1689. int pc = baked_point_cache.size();
  1690. ERR_FAIL_COND_V_MSG(pc == 0, 0.0f, "No points in Curve3D.");
  1691. if (pc == 1) {
  1692. return 0.0f;
  1693. }
  1694. const Vector3 *r = baked_point_cache.ptr();
  1695. real_t nearest = 0.0f;
  1696. real_t nearest_dist = -1.0f;
  1697. real_t offset;
  1698. for (int i = 0; i < pc - 1; i++) {
  1699. offset = baked_dist_cache[i];
  1700. const real_t interval = baked_dist_cache[i + 1] - baked_dist_cache[i];
  1701. Vector3 origin = r[i];
  1702. Vector3 direction = (r[i + 1] - origin) / interval;
  1703. real_t d = CLAMP((p_to_point - origin).dot(direction), 0.0f, interval);
  1704. Vector3 proj = origin + direction * d;
  1705. real_t dist = proj.distance_squared_to(p_to_point);
  1706. if (nearest_dist < 0.0f || dist < nearest_dist) {
  1707. nearest = offset + d;
  1708. nearest_dist = dist;
  1709. }
  1710. }
  1711. return nearest;
  1712. }
  1713. void Curve3D::set_closed(bool p_closed) {
  1714. if (closed == p_closed) {
  1715. return;
  1716. }
  1717. closed = p_closed;
  1718. mark_dirty();
  1719. notify_property_list_changed();
  1720. }
  1721. bool Curve3D::is_closed() const {
  1722. return closed;
  1723. }
  1724. void Curve3D::set_bake_interval(real_t p_tolerance) {
  1725. bake_interval = p_tolerance;
  1726. mark_dirty();
  1727. }
  1728. real_t Curve3D::get_bake_interval() const {
  1729. return bake_interval;
  1730. }
  1731. void Curve3D::set_up_vector_enabled(bool p_enable) {
  1732. up_vector_enabled = p_enable;
  1733. mark_dirty();
  1734. }
  1735. bool Curve3D::is_up_vector_enabled() const {
  1736. return up_vector_enabled;
  1737. }
  1738. Dictionary Curve3D::_get_data() const {
  1739. Dictionary dc;
  1740. PackedVector3Array d;
  1741. d.resize(points.size() * 3);
  1742. Vector3 *w = d.ptrw();
  1743. Vector<real_t> t;
  1744. t.resize(points.size());
  1745. real_t *wt = t.ptrw();
  1746. for (int i = 0; i < points.size(); i++) {
  1747. w[i * 3 + 0] = points[i].in;
  1748. w[i * 3 + 1] = points[i].out;
  1749. w[i * 3 + 2] = points[i].position;
  1750. wt[i] = points[i].tilt;
  1751. }
  1752. dc["points"] = d;
  1753. dc["tilts"] = t;
  1754. return dc;
  1755. }
  1756. void Curve3D::_set_data(const Dictionary &p_data) {
  1757. ERR_FAIL_COND(!p_data.has("points"));
  1758. ERR_FAIL_COND(!p_data.has("tilts"));
  1759. PackedVector3Array rp = p_data["points"];
  1760. int pc = rp.size();
  1761. ERR_FAIL_COND(pc % 3 != 0);
  1762. int old_size = points.size();
  1763. int new_size = pc / 3;
  1764. if (old_size != new_size) {
  1765. points.resize(new_size);
  1766. }
  1767. const Vector3 *r = rp.ptr();
  1768. Vector<real_t> rtl = p_data["tilts"];
  1769. const real_t *rt = rtl.ptr();
  1770. for (int i = 0; i < points.size(); i++) {
  1771. points.write[i].in = r[i * 3 + 0];
  1772. points.write[i].out = r[i * 3 + 1];
  1773. points.write[i].position = r[i * 3 + 2];
  1774. points.write[i].tilt = rt[i];
  1775. }
  1776. mark_dirty();
  1777. if (old_size != new_size) {
  1778. notify_property_list_changed();
  1779. }
  1780. }
  1781. PackedVector3Array Curve3D::tessellate(int p_max_stages, real_t p_tolerance) const {
  1782. PackedVector3Array tess;
  1783. if (points.size() == 0) {
  1784. return tess;
  1785. }
  1786. Vector<RBMap<real_t, Vector3>> midpoints;
  1787. const int num_intervals = closed ? points.size() : points.size() - 1;
  1788. midpoints.resize(num_intervals);
  1789. // Point Count: Begins at 1 to account for the last point.
  1790. int pc = 1;
  1791. for (int i = 0; i < num_intervals; i++) {
  1792. if (!closed || i < num_intervals - 1) {
  1793. _bake_segment3d(midpoints.write[i], 0, 1, points[i].position, points[i].out, points[i + 1].position, points[i + 1].in, 0, p_max_stages, p_tolerance);
  1794. } else {
  1795. _bake_segment3d(midpoints.write[i], 0, 1, points[i].position, points[i].out, points[0].position, points[0].in, 0, p_max_stages, p_tolerance);
  1796. }
  1797. pc++;
  1798. pc += midpoints[i].size();
  1799. }
  1800. tess.resize(pc);
  1801. Vector3 *bpw = tess.ptrw();
  1802. bpw[0] = points[0].position;
  1803. int pidx = 0;
  1804. for (int i = 0; i < num_intervals; i++) {
  1805. for (const KeyValue<real_t, Vector3> &E : midpoints[i]) {
  1806. pidx++;
  1807. bpw[pidx] = E.value;
  1808. }
  1809. pidx++;
  1810. if (!closed || i < num_intervals - 1) {
  1811. bpw[pidx] = points[i + 1].position;
  1812. } else {
  1813. bpw[pidx] = points[0].position;
  1814. }
  1815. }
  1816. return tess;
  1817. }
  1818. Vector<RBMap<real_t, Vector3>> Curve3D::_tessellate_even_length(int p_max_stages, real_t p_length) const {
  1819. Vector<RBMap<real_t, Vector3>> midpoints;
  1820. ERR_FAIL_COND_V_MSG(points.size() < 2, midpoints, "Curve must have at least 2 control point");
  1821. const int num_intervals = closed ? points.size() : points.size() - 1;
  1822. midpoints.resize(num_intervals);
  1823. for (int i = 0; i < num_intervals; i++) {
  1824. if (!closed || i < num_intervals - 1) {
  1825. _bake_segment3d_even_length(midpoints.write[i], 0, 1, points[i].position, points[i].out, points[i + 1].position, points[i + 1].in, 0, p_max_stages, p_length);
  1826. } else {
  1827. _bake_segment3d_even_length(midpoints.write[i], 0, 1, points[i].position, points[i].out, points[0].position, points[0].in, 0, p_max_stages, p_length);
  1828. }
  1829. }
  1830. return midpoints;
  1831. }
  1832. PackedVector3Array Curve3D::tessellate_even_length(int p_max_stages, real_t p_length) const {
  1833. PackedVector3Array tess;
  1834. Vector<RBMap<real_t, Vector3>> midpoints = _tessellate_even_length(p_max_stages, p_length);
  1835. if (midpoints.size() == 0) {
  1836. return tess;
  1837. }
  1838. const int num_intervals = closed ? points.size() : points.size() - 1;
  1839. // Point Count: Begins at 1 to account for the last point.
  1840. int pc = 1;
  1841. for (int i = 0; i < num_intervals; i++) {
  1842. pc++;
  1843. pc += midpoints[i].size();
  1844. }
  1845. tess.resize(pc);
  1846. Vector3 *bpw = tess.ptrw();
  1847. bpw[0] = points[0].position;
  1848. int pidx = 0;
  1849. for (int i = 0; i < num_intervals; i++) {
  1850. for (const KeyValue<real_t, Vector3> &E : midpoints[i]) {
  1851. pidx++;
  1852. bpw[pidx] = E.value;
  1853. }
  1854. pidx++;
  1855. if (!closed || i < num_intervals - 1) {
  1856. bpw[pidx] = points[i + 1].position;
  1857. } else {
  1858. bpw[pidx] = points[0].position;
  1859. }
  1860. }
  1861. return tess;
  1862. }
  1863. bool Curve3D::_set(const StringName &p_name, const Variant &p_value) {
  1864. Vector<String> components = String(p_name).split("/", true, 2);
  1865. if (components.size() >= 2 && components[0].begins_with("point_") && components[0].trim_prefix("point_").is_valid_int()) {
  1866. int point_index = components[0].trim_prefix("point_").to_int();
  1867. const String &property = components[1];
  1868. if (property == "position") {
  1869. set_point_position(point_index, p_value);
  1870. return true;
  1871. } else if (property == "in") {
  1872. set_point_in(point_index, p_value);
  1873. return true;
  1874. } else if (property == "out") {
  1875. set_point_out(point_index, p_value);
  1876. return true;
  1877. } else if (property == "tilt") {
  1878. set_point_tilt(point_index, p_value);
  1879. return true;
  1880. }
  1881. }
  1882. return false;
  1883. }
  1884. bool Curve3D::_get(const StringName &p_name, Variant &r_ret) const {
  1885. Vector<String> components = String(p_name).split("/", true, 2);
  1886. if (components.size() >= 2 && components[0].begins_with("point_") && components[0].trim_prefix("point_").is_valid_int()) {
  1887. int point_index = components[0].trim_prefix("point_").to_int();
  1888. const String &property = components[1];
  1889. if (property == "position") {
  1890. r_ret = get_point_position(point_index);
  1891. return true;
  1892. } else if (property == "in") {
  1893. r_ret = get_point_in(point_index);
  1894. return true;
  1895. } else if (property == "out") {
  1896. r_ret = get_point_out(point_index);
  1897. return true;
  1898. } else if (property == "tilt") {
  1899. r_ret = get_point_tilt(point_index);
  1900. return true;
  1901. }
  1902. }
  1903. return false;
  1904. }
  1905. void Curve3D::_get_property_list(List<PropertyInfo> *p_list) const {
  1906. for (int i = 0; i < points.size(); i++) {
  1907. PropertyInfo pi = PropertyInfo(Variant::VECTOR3, vformat("point_%d/position", i));
  1908. pi.usage &= ~PROPERTY_USAGE_STORAGE;
  1909. p_list->push_back(pi);
  1910. if (closed || i != 0) {
  1911. pi = PropertyInfo(Variant::VECTOR3, vformat("point_%d/in", i));
  1912. pi.usage &= ~PROPERTY_USAGE_STORAGE;
  1913. p_list->push_back(pi);
  1914. }
  1915. if (closed || i != points.size() - 1) {
  1916. pi = PropertyInfo(Variant::VECTOR3, vformat("point_%d/out", i));
  1917. pi.usage &= ~PROPERTY_USAGE_STORAGE;
  1918. p_list->push_back(pi);
  1919. }
  1920. pi = PropertyInfo(Variant::FLOAT, vformat("point_%d/tilt", i));
  1921. pi.usage &= ~PROPERTY_USAGE_STORAGE;
  1922. p_list->push_back(pi);
  1923. }
  1924. }
  1925. void Curve3D::_bind_methods() {
  1926. ClassDB::bind_method(D_METHOD("get_point_count"), &Curve3D::get_point_count);
  1927. ClassDB::bind_method(D_METHOD("set_point_count", "count"), &Curve3D::set_point_count);
  1928. ClassDB::bind_method(D_METHOD("add_point", "position", "in", "out", "index"), &Curve3D::add_point, DEFVAL(Vector3()), DEFVAL(Vector3()), DEFVAL(-1));
  1929. ClassDB::bind_method(D_METHOD("set_point_position", "idx", "position"), &Curve3D::set_point_position);
  1930. ClassDB::bind_method(D_METHOD("get_point_position", "idx"), &Curve3D::get_point_position);
  1931. ClassDB::bind_method(D_METHOD("set_point_tilt", "idx", "tilt"), &Curve3D::set_point_tilt);
  1932. ClassDB::bind_method(D_METHOD("get_point_tilt", "idx"), &Curve3D::get_point_tilt);
  1933. ClassDB::bind_method(D_METHOD("set_point_in", "idx", "position"), &Curve3D::set_point_in);
  1934. ClassDB::bind_method(D_METHOD("get_point_in", "idx"), &Curve3D::get_point_in);
  1935. ClassDB::bind_method(D_METHOD("set_point_out", "idx", "position"), &Curve3D::set_point_out);
  1936. ClassDB::bind_method(D_METHOD("get_point_out", "idx"), &Curve3D::get_point_out);
  1937. ClassDB::bind_method(D_METHOD("remove_point", "idx"), &Curve3D::remove_point);
  1938. ClassDB::bind_method(D_METHOD("clear_points"), &Curve3D::clear_points);
  1939. ClassDB::bind_method(D_METHOD("sample", "idx", "t"), &Curve3D::sample);
  1940. ClassDB::bind_method(D_METHOD("samplef", "fofs"), &Curve3D::samplef);
  1941. ClassDB::bind_method(D_METHOD("set_closed", "closed"), &Curve3D::set_closed);
  1942. ClassDB::bind_method(D_METHOD("is_closed"), &Curve3D::is_closed);
  1943. //ClassDB::bind_method(D_METHOD("bake","subdivs"),&Curve3D::bake,DEFVAL(10));
  1944. ClassDB::bind_method(D_METHOD("set_bake_interval", "distance"), &Curve3D::set_bake_interval);
  1945. ClassDB::bind_method(D_METHOD("get_bake_interval"), &Curve3D::get_bake_interval);
  1946. ClassDB::bind_method(D_METHOD("set_up_vector_enabled", "enable"), &Curve3D::set_up_vector_enabled);
  1947. ClassDB::bind_method(D_METHOD("is_up_vector_enabled"), &Curve3D::is_up_vector_enabled);
  1948. ClassDB::bind_method(D_METHOD("get_baked_length"), &Curve3D::get_baked_length);
  1949. ClassDB::bind_method(D_METHOD("sample_baked", "offset", "cubic"), &Curve3D::sample_baked, DEFVAL(0.0), DEFVAL(false));
  1950. ClassDB::bind_method(D_METHOD("sample_baked_with_rotation", "offset", "cubic", "apply_tilt"), &Curve3D::sample_baked_with_rotation, DEFVAL(0.0), DEFVAL(false), DEFVAL(false));
  1951. ClassDB::bind_method(D_METHOD("sample_baked_up_vector", "offset", "apply_tilt"), &Curve3D::sample_baked_up_vector, DEFVAL(false));
  1952. ClassDB::bind_method(D_METHOD("get_baked_points"), &Curve3D::get_baked_points);
  1953. ClassDB::bind_method(D_METHOD("get_baked_tilts"), &Curve3D::get_baked_tilts);
  1954. ClassDB::bind_method(D_METHOD("get_baked_up_vectors"), &Curve3D::get_baked_up_vectors);
  1955. ClassDB::bind_method(D_METHOD("get_closest_point", "to_point"), &Curve3D::get_closest_point);
  1956. ClassDB::bind_method(D_METHOD("get_closest_offset", "to_point"), &Curve3D::get_closest_offset);
  1957. ClassDB::bind_method(D_METHOD("tessellate", "max_stages", "tolerance_degrees"), &Curve3D::tessellate, DEFVAL(5), DEFVAL(4));
  1958. ClassDB::bind_method(D_METHOD("tessellate_even_length", "max_stages", "tolerance_length"), &Curve3D::tessellate_even_length, DEFVAL(5), DEFVAL(0.2));
  1959. ClassDB::bind_method(D_METHOD("_get_data"), &Curve3D::_get_data);
  1960. ClassDB::bind_method(D_METHOD("_set_data", "data"), &Curve3D::_set_data);
  1961. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "closed"), "set_closed", "is_closed");
  1962. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "bake_interval", PROPERTY_HINT_RANGE, "0.01,512,0.01"), "set_bake_interval", "get_bake_interval");
  1963. ADD_PROPERTY(PropertyInfo(Variant::INT, "_data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "_set_data", "_get_data");
  1964. ADD_ARRAY_COUNT("Points", "point_count", "set_point_count", "get_point_count", "point_");
  1965. ADD_GROUP("Up Vector", "up_vector_");
  1966. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "up_vector_enabled"), "set_up_vector_enabled", "is_up_vector_enabled");
  1967. }
  1968. Curve3D::Curve3D() {}