curve.cpp 77 KB

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