curve.cpp 77 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518
  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. Curve2D::Curve2D() {}
  1175. /***********************************************************************************/
  1176. /***********************************************************************************/
  1177. /***********************************************************************************/
  1178. /***********************************************************************************/
  1179. /***********************************************************************************/
  1180. /***********************************************************************************/
  1181. int Curve3D::get_point_count() const {
  1182. return points.size();
  1183. }
  1184. void Curve3D::set_point_count(int p_count) {
  1185. ERR_FAIL_COND(p_count < 0);
  1186. int old_size = points.size();
  1187. if (old_size == p_count) {
  1188. return;
  1189. }
  1190. if (old_size > p_count) {
  1191. points.resize(p_count);
  1192. mark_dirty();
  1193. } else {
  1194. for (int i = p_count - old_size; i > 0; i--) {
  1195. _add_point(Vector3());
  1196. }
  1197. }
  1198. notify_property_list_changed();
  1199. }
  1200. void Curve3D::_add_point(const Vector3 &p_position, const Vector3 &p_in, const Vector3 &p_out, int p_atpos) {
  1201. Point n;
  1202. n.position = p_position;
  1203. n.in = p_in;
  1204. n.out = p_out;
  1205. if ((uint32_t)p_atpos < points.size()) {
  1206. points.insert(p_atpos, n);
  1207. } else {
  1208. points.push_back(n);
  1209. }
  1210. mark_dirty();
  1211. }
  1212. void Curve3D::add_point(const Vector3 &p_position, const Vector3 &p_in, const Vector3 &p_out, int p_atpos) {
  1213. _add_point(p_position, p_in, p_out, p_atpos);
  1214. notify_property_list_changed();
  1215. }
  1216. void Curve3D::set_point_position(int p_index, const Vector3 &p_position) {
  1217. ERR_FAIL_UNSIGNED_INDEX((uint32_t)p_index, points.size());
  1218. points[p_index].position = p_position;
  1219. mark_dirty();
  1220. }
  1221. Vector3 Curve3D::get_point_position(int p_index) const {
  1222. ERR_FAIL_UNSIGNED_INDEX_V((uint32_t)p_index, points.size(), Vector3());
  1223. return points[p_index].position;
  1224. }
  1225. void Curve3D::set_point_tilt(int p_index, real_t p_tilt) {
  1226. ERR_FAIL_UNSIGNED_INDEX((uint32_t)p_index, points.size());
  1227. points[p_index].tilt = p_tilt;
  1228. mark_dirty();
  1229. }
  1230. real_t Curve3D::get_point_tilt(int p_index) const {
  1231. ERR_FAIL_UNSIGNED_INDEX_V((uint32_t)p_index, points.size(), 0);
  1232. return points[p_index].tilt;
  1233. }
  1234. void Curve3D::set_point_in(int p_index, const Vector3 &p_in) {
  1235. ERR_FAIL_UNSIGNED_INDEX((uint32_t)p_index, points.size());
  1236. points[p_index].in = p_in;
  1237. mark_dirty();
  1238. }
  1239. Vector3 Curve3D::get_point_in(int p_index) const {
  1240. ERR_FAIL_UNSIGNED_INDEX_V((uint32_t)p_index, points.size(), Vector3());
  1241. return points[p_index].in;
  1242. }
  1243. void Curve3D::set_point_out(int p_index, const Vector3 &p_out) {
  1244. ERR_FAIL_UNSIGNED_INDEX((uint32_t)p_index, points.size());
  1245. points[p_index].out = p_out;
  1246. mark_dirty();
  1247. }
  1248. Vector3 Curve3D::get_point_out(int p_index) const {
  1249. ERR_FAIL_UNSIGNED_INDEX_V((uint32_t)p_index, points.size(), Vector3());
  1250. return points[p_index].out;
  1251. }
  1252. void Curve3D::_remove_point(int p_index) {
  1253. ERR_FAIL_UNSIGNED_INDEX((uint32_t)p_index, points.size());
  1254. points.remove_at(p_index);
  1255. mark_dirty();
  1256. }
  1257. void Curve3D::remove_point(int p_index) {
  1258. _remove_point(p_index);
  1259. if (closed && points.size() < 2) {
  1260. set_closed(false);
  1261. }
  1262. notify_property_list_changed();
  1263. }
  1264. void Curve3D::clear_points() {
  1265. if (!points.is_empty()) {
  1266. points.clear();
  1267. mark_dirty();
  1268. notify_property_list_changed();
  1269. }
  1270. }
  1271. Vector3 Curve3D::sample(int p_index, real_t p_offset) const {
  1272. int pc = points.size();
  1273. ERR_FAIL_COND_V(pc == 0, Vector3());
  1274. if (p_index >= pc - 1) {
  1275. if (!closed) {
  1276. return points[pc - 1].position;
  1277. } else {
  1278. p_index = pc - 1;
  1279. }
  1280. } else if (p_index < 0) {
  1281. return points[0].position;
  1282. }
  1283. Vector3 p0 = points[p_index].position;
  1284. Vector3 p1 = p0 + points[p_index].out;
  1285. Vector3 p3, p2;
  1286. if (!closed || p_index < pc - 1) {
  1287. p3 = points[p_index + 1].position;
  1288. p2 = p3 + points[p_index + 1].in;
  1289. } else {
  1290. p3 = points[0].position;
  1291. p2 = p3 + points[0].in;
  1292. }
  1293. return p0.bezier_interpolate(p1, p2, p3, p_offset);
  1294. }
  1295. Vector3 Curve3D::samplef(real_t p_findex) const {
  1296. if (p_findex < 0) {
  1297. p_findex = 0;
  1298. } else if (p_findex >= points.size()) {
  1299. p_findex = points.size();
  1300. }
  1301. return sample((int)p_findex, Math::fmod(p_findex, (real_t)1.0));
  1302. }
  1303. void Curve3D::mark_dirty() {
  1304. baked_cache_dirty = true;
  1305. emit_changed();
  1306. }
  1307. 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 {
  1308. real_t mp = p_begin + (p_end - p_begin) * 0.5;
  1309. Vector3 beg = p_a.bezier_interpolate(p_a + p_out, p_b + p_in, p_b, p_begin);
  1310. Vector3 mid = p_a.bezier_interpolate(p_a + p_out, p_b + p_in, p_b, mp);
  1311. Vector3 end = p_a.bezier_interpolate(p_a + p_out, p_b + p_in, p_b, p_end);
  1312. Vector3 na = (mid - beg).normalized();
  1313. Vector3 nb = (end - mid).normalized();
  1314. real_t dp = na.dot(nb);
  1315. if (dp < Math::cos(Math::deg_to_rad(p_tol))) {
  1316. r_bake[mp] = mid;
  1317. }
  1318. if (p_depth < p_max_depth) {
  1319. _bake_segment3d(r_bake, p_begin, mp, p_a, p_out, p_b, p_in, p_depth + 1, p_max_depth, p_tol);
  1320. _bake_segment3d(r_bake, mp, p_end, p_a, p_out, p_b, p_in, p_depth + 1, p_max_depth, p_tol);
  1321. }
  1322. }
  1323. 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 {
  1324. Vector3 beg = p_a.bezier_interpolate(p_a + p_out, p_b + p_in, p_b, p_begin);
  1325. Vector3 end = p_a.bezier_interpolate(p_a + p_out, p_b + p_in, p_b, p_end);
  1326. real_t length = beg.distance_to(end);
  1327. if (length > p_length && p_depth < p_max_depth) {
  1328. real_t mp = (p_begin + p_end) * 0.5;
  1329. Vector3 mid = p_a.bezier_interpolate(p_a + p_out, p_b + p_in, p_b, mp);
  1330. r_bake[mp] = mid;
  1331. _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);
  1332. _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);
  1333. }
  1334. }
  1335. 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) {
  1336. // Handle corner cases.
  1337. if (Math::is_zero_approx(p_t - 0.0f)) {
  1338. if (p_control_1.is_equal_approx(p_begin)) {
  1339. if (p_control_1.is_equal_approx(p_control_2)) {
  1340. return (p_end - p_begin).normalized();
  1341. } else {
  1342. return (p_control_2 - p_begin).normalized();
  1343. }
  1344. }
  1345. } else if (Math::is_zero_approx(p_t - 1.0f)) {
  1346. if (p_control_2.is_equal_approx(p_end)) {
  1347. if (p_control_2.is_equal_approx(p_control_1)) {
  1348. return (p_end - p_begin).normalized();
  1349. } else {
  1350. return (p_end - p_control_1).normalized();
  1351. }
  1352. }
  1353. }
  1354. if (p_control_1.is_equal_approx(p_end) && p_control_2.is_equal_approx(p_begin)) {
  1355. return (p_end - p_begin).normalized();
  1356. }
  1357. return p_begin.bezier_derivative(p_control_1, p_control_2, p_end, p_t).normalized();
  1358. }
  1359. void Curve3D::_bake() const {
  1360. if (!baked_cache_dirty) {
  1361. return;
  1362. }
  1363. baked_max_ofs = 0;
  1364. baked_cache_dirty = false;
  1365. if (points.is_empty()) {
  1366. #ifdef TOOLS_ENABLED
  1367. points_in_cache.clear();
  1368. #endif
  1369. baked_point_cache.clear();
  1370. baked_tilt_cache.clear();
  1371. baked_dist_cache.clear();
  1372. baked_forward_vector_cache.clear();
  1373. baked_up_vector_cache.clear();
  1374. return;
  1375. }
  1376. if (points.size() == 1) {
  1377. #ifdef TOOLS_ENABLED
  1378. points_in_cache.resize(1);
  1379. points_in_cache.set(0, 0);
  1380. #endif
  1381. baked_point_cache.resize(1);
  1382. baked_point_cache.set(0, points[0].position);
  1383. baked_tilt_cache.resize(1);
  1384. baked_tilt_cache.set(0, points[0].tilt);
  1385. baked_dist_cache.resize(1);
  1386. baked_dist_cache.set(0, 0.0);
  1387. baked_forward_vector_cache.resize(1);
  1388. baked_forward_vector_cache.set(0, Vector3(0.0, 0.0, 1.0));
  1389. if (up_vector_enabled) {
  1390. baked_up_vector_cache.resize(1);
  1391. baked_up_vector_cache.set(0, Vector3(0.0, 1.0, 0.0));
  1392. } else {
  1393. baked_up_vector_cache.clear();
  1394. }
  1395. return;
  1396. }
  1397. // Step 1: Tessellate curve to (almost) even length segments.
  1398. {
  1399. Vector<RBMap<real_t, Vector3>> midpoints = _tessellate_even_length(10, bake_interval);
  1400. const int num_intervals = closed ? points.size() : points.size() - 1;
  1401. #ifdef TOOLS_ENABLED
  1402. points_in_cache.resize(closed ? (points.size() + 1) : points.size());
  1403. points_in_cache.set(0, 0);
  1404. #endif
  1405. // Point Count: Begins at 1 to account for the last point.
  1406. int pc = 1;
  1407. for (int i = 0; i < num_intervals; i++) {
  1408. pc++;
  1409. pc += midpoints[i].size();
  1410. #ifdef TOOLS_ENABLED
  1411. points_in_cache.set(i + 1, pc - 1);
  1412. #endif
  1413. }
  1414. baked_point_cache.resize(pc);
  1415. baked_tilt_cache.resize(pc);
  1416. baked_dist_cache.resize(pc);
  1417. baked_forward_vector_cache.resize(pc);
  1418. Vector3 *bpw = baked_point_cache.ptrw();
  1419. real_t *btw = baked_tilt_cache.ptrw();
  1420. Vector3 *bfw = baked_forward_vector_cache.ptrw();
  1421. // Collect positions and sample tilts and tangents for each baked points.
  1422. bpw[0] = points[0].position;
  1423. bfw[0] = _calculate_tangent(points[0].position, points[0].position + points[0].out, points[1].position + points[1].in, points[1].position, 0.0);
  1424. btw[0] = points[0].tilt;
  1425. int pidx = 0;
  1426. for (int i = 0; i < num_intervals; i++) {
  1427. for (const KeyValue<real_t, Vector3> &E : midpoints[i]) {
  1428. pidx++;
  1429. bpw[pidx] = E.value;
  1430. if (!closed || i < num_intervals - 1) {
  1431. 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);
  1432. btw[pidx] = Math::lerp(points[i].tilt, points[i + 1].tilt, E.key);
  1433. } else {
  1434. bfw[pidx] = _calculate_tangent(points[i].position, points[i].position + points[i].out, points[0].position + points[0].in, points[0].position, E.key);
  1435. btw[pidx] = Math::lerp(points[i].tilt, points[0].tilt, E.key);
  1436. }
  1437. }
  1438. pidx++;
  1439. if (!closed || i < num_intervals - 1) {
  1440. bpw[pidx] = points[i + 1].position;
  1441. 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);
  1442. btw[pidx] = points[i + 1].tilt;
  1443. } else {
  1444. bpw[pidx] = points[0].position;
  1445. bfw[pidx] = _calculate_tangent(points[i].position, points[i].position + points[i].out, points[0].position + points[0].in, points[0].position, 1.0);
  1446. btw[pidx] = points[0].tilt;
  1447. }
  1448. }
  1449. // Recalculate the baked distances.
  1450. real_t *bdw = baked_dist_cache.ptrw();
  1451. bdw[0] = 0.0;
  1452. for (int i = 0; i < pc - 1; i++) {
  1453. bdw[i + 1] = bdw[i] + bpw[i].distance_to(bpw[i + 1]);
  1454. }
  1455. baked_max_ofs = bdw[pc - 1];
  1456. }
  1457. if (!up_vector_enabled) {
  1458. baked_up_vector_cache.resize(0);
  1459. return;
  1460. }
  1461. // Step 2: Calculate the up vectors and the whole local reference frame.
  1462. //
  1463. // See Dougan, Carl. "The parallel transport frame." Game Programming Gems 2 (2001): 215-219.
  1464. // for an example discussing about why not the Frenet frame.
  1465. {
  1466. int point_count = baked_point_cache.size();
  1467. baked_up_vector_cache.resize(point_count);
  1468. Vector3 *up_write = baked_up_vector_cache.ptrw();
  1469. const Vector3 *forward_ptr = baked_forward_vector_cache.ptr();
  1470. const Vector3 *points_ptr = baked_point_cache.ptr();
  1471. Basis frame; // X-right, Y-up, -Z-forward.
  1472. Basis frame_prev;
  1473. // Set the initial frame based on Y-up rule.
  1474. {
  1475. Vector3 forward = forward_ptr[0];
  1476. if (std::abs(forward.dot(Vector3(0, 1, 0))) > 1.0 - UNIT_EPSILON) {
  1477. frame_prev = Basis::looking_at(forward, Vector3(1, 0, 0));
  1478. } else {
  1479. frame_prev = Basis::looking_at(forward, Vector3(0, 1, 0));
  1480. }
  1481. up_write[0] = frame_prev.get_column(1);
  1482. }
  1483. // Calculate the Parallel Transport Frame.
  1484. for (int idx = 1; idx < point_count; idx++) {
  1485. Vector3 forward = forward_ptr[idx];
  1486. Basis rotate;
  1487. rotate.rotate_to_align(-frame_prev.get_column(2), forward);
  1488. frame = rotate * frame_prev;
  1489. frame.orthonormalize(); // Guard against float error accumulation.
  1490. up_write[idx] = frame.get_column(1);
  1491. frame_prev = frame;
  1492. }
  1493. bool is_loop = true;
  1494. // Loop smoothing only applies when the curve is a loop, which means two ends meet, and share forward directions.
  1495. {
  1496. if (!points_ptr[0].is_equal_approx(points_ptr[point_count - 1])) {
  1497. is_loop = false;
  1498. }
  1499. real_t dot = forward_ptr[0].dot(forward_ptr[point_count - 1]);
  1500. if (dot < 1.0 - UNIT_EPSILON) { // Alignment should not be too tight, or it doesn't work for coarse bake interval.
  1501. is_loop = false;
  1502. }
  1503. }
  1504. // Twist up vectors, so that they align at two ends of the curve.
  1505. if (is_loop) {
  1506. const Vector3 up_start = up_write[0];
  1507. const Vector3 up_end = up_write[point_count - 1];
  1508. real_t sign = SIGN(up_end.cross(up_start).dot(forward_ptr[0]));
  1509. real_t full_angle = Quaternion(up_end, up_start).get_angle();
  1510. if (std::abs(full_angle) < CMP_EPSILON) {
  1511. return;
  1512. } else {
  1513. const real_t *dists = baked_dist_cache.ptr();
  1514. for (int idx = 1; idx < point_count; idx++) {
  1515. const real_t frac = dists[idx] / baked_max_ofs;
  1516. const real_t angle = Math::lerp((real_t)0.0, full_angle, frac);
  1517. Basis twist(forward_ptr[idx] * sign, angle);
  1518. up_write[idx] = twist.xform(up_write[idx]);
  1519. }
  1520. }
  1521. }
  1522. }
  1523. }
  1524. real_t Curve3D::get_baked_length() const {
  1525. if (baked_cache_dirty) {
  1526. _bake();
  1527. }
  1528. return baked_max_ofs;
  1529. }
  1530. Curve3D::Interval Curve3D::_find_interval(real_t p_offset) const {
  1531. Interval interval = {
  1532. -1,
  1533. 0.0
  1534. };
  1535. ERR_FAIL_COND_V_MSG(baked_cache_dirty, interval, "Backed cache is dirty");
  1536. int pc = baked_point_cache.size();
  1537. ERR_FAIL_COND_V_MSG(pc < 2, interval, "Less than two points in cache");
  1538. int start = 0;
  1539. int end = pc;
  1540. int idx = (end + start) / 2;
  1541. // Binary search to find baked points.
  1542. while (start < idx) {
  1543. real_t offset = baked_dist_cache[idx];
  1544. if (p_offset <= offset) {
  1545. end = idx;
  1546. } else {
  1547. start = idx;
  1548. }
  1549. idx = (end + start) / 2;
  1550. }
  1551. real_t offset_begin = baked_dist_cache[idx];
  1552. real_t offset_end = baked_dist_cache[idx + 1];
  1553. real_t idx_interval = offset_end - offset_begin;
  1554. ERR_FAIL_COND_V_MSG(p_offset < offset_begin || p_offset > offset_end, interval, "Offset out of range.");
  1555. interval.idx = idx;
  1556. if (idx_interval < FLT_EPSILON) {
  1557. interval.frac = 0.5; // For a very short interval, 0.5 is a reasonable choice.
  1558. ERR_FAIL_V_MSG(interval, "Zero length interval.");
  1559. }
  1560. interval.frac = (p_offset - offset_begin) / idx_interval;
  1561. return interval;
  1562. }
  1563. Vector3 Curve3D::_sample_baked(Interval p_interval, bool p_cubic) const {
  1564. // Assuming p_interval is valid.
  1565. ERR_FAIL_INDEX_V_MSG(p_interval.idx, baked_point_cache.size(), Vector3(), "Invalid interval");
  1566. int idx = p_interval.idx;
  1567. real_t frac = p_interval.frac;
  1568. const Vector3 *r = baked_point_cache.ptr();
  1569. int pc = baked_point_cache.size();
  1570. if (p_cubic) {
  1571. Vector3 pre = idx > 0 ? r[idx - 1] : r[idx];
  1572. Vector3 post = (idx < (pc - 2)) ? r[idx + 2] : r[idx + 1];
  1573. return r[idx].cubic_interpolate(r[idx + 1], pre, post, frac);
  1574. } else {
  1575. return r[idx].lerp(r[idx + 1], frac);
  1576. }
  1577. }
  1578. real_t Curve3D::_sample_baked_tilt(Interval p_interval) const {
  1579. // Assuming that p_interval is valid.
  1580. ERR_FAIL_INDEX_V_MSG(p_interval.idx, baked_tilt_cache.size(), 0.0, "Invalid interval");
  1581. int idx = p_interval.idx;
  1582. real_t frac = p_interval.frac;
  1583. const real_t *r = baked_tilt_cache.ptr();
  1584. return Math::lerp(r[idx], r[idx + 1], frac);
  1585. }
  1586. // Internal method for getting posture at a baked point. Assuming caller
  1587. // make all safety checks.
  1588. Basis Curve3D::_compose_posture(int p_index) const {
  1589. Vector3 forward = baked_forward_vector_cache[p_index];
  1590. Vector3 up;
  1591. if (up_vector_enabled) {
  1592. up = baked_up_vector_cache[p_index];
  1593. } else {
  1594. up = Vector3(0.0, 1.0, 0.0);
  1595. }
  1596. const Basis frame = Basis::looking_at(forward, up);
  1597. return frame;
  1598. }
  1599. Basis Curve3D::_sample_posture(Interval p_interval, bool p_apply_tilt) const {
  1600. // Assuming that p_interval is valid.
  1601. ERR_FAIL_INDEX_V_MSG(p_interval.idx, baked_point_cache.size(), Basis(), "Invalid interval");
  1602. if (up_vector_enabled) {
  1603. ERR_FAIL_INDEX_V_MSG(p_interval.idx, baked_up_vector_cache.size(), Basis(), "Invalid interval");
  1604. }
  1605. int idx = p_interval.idx;
  1606. real_t frac = p_interval.frac;
  1607. // Get frames at both ends of the interval, then interpolate.
  1608. const Basis frame_begin = _compose_posture(idx);
  1609. const Basis frame_end = _compose_posture(idx + 1);
  1610. const Basis frame = frame_begin.slerp(frame_end, frac).orthonormalized();
  1611. if (!p_apply_tilt) {
  1612. return frame;
  1613. }
  1614. // Applying tilt.
  1615. const real_t tilt = _sample_baked_tilt(p_interval);
  1616. Vector3 tangent = -frame.get_column(2);
  1617. const Basis twist(tangent, tilt);
  1618. return twist * frame;
  1619. }
  1620. #ifdef TOOLS_ENABLED
  1621. // Get posture at a control point. Needed for Gizmo implementation.
  1622. Basis Curve3D::get_point_baked_posture(int p_index, bool p_apply_tilt) const {
  1623. if (baked_cache_dirty) {
  1624. _bake();
  1625. }
  1626. // Assuming that p_idx is valid.
  1627. ERR_FAIL_INDEX_V_MSG(p_index, points_in_cache.size(), Basis(), "Invalid control point index");
  1628. int baked_idx = points_in_cache[p_index];
  1629. Basis frame = _compose_posture(baked_idx);
  1630. if (!p_apply_tilt) {
  1631. return frame;
  1632. }
  1633. // Applying tilt.
  1634. const real_t tilt = points[p_index].tilt;
  1635. Vector3 tangent = -frame.get_column(2);
  1636. const Basis twist(tangent, tilt);
  1637. return twist * frame;
  1638. }
  1639. #endif
  1640. Vector3 Curve3D::sample_baked(real_t p_offset, bool p_cubic) const {
  1641. // Make sure that p_offset is finite.
  1642. ERR_FAIL_COND_V_MSG(!Math::is_finite(p_offset), Vector3(), "Offset is non-finite");
  1643. if (baked_cache_dirty) {
  1644. _bake();
  1645. }
  1646. // Validate: Curve may not have baked points.
  1647. int pc = baked_point_cache.size();
  1648. ERR_FAIL_COND_V_MSG(pc == 0, Vector3(), "No points in Curve3D.");
  1649. if (pc == 1) {
  1650. return baked_point_cache[0];
  1651. }
  1652. p_offset = CLAMP(p_offset, 0.0, get_baked_length()); // PathFollower implement wrapping logic.
  1653. Curve3D::Interval interval = _find_interval(p_offset);
  1654. return _sample_baked(interval, p_cubic);
  1655. }
  1656. Transform3D Curve3D::sample_baked_with_rotation(real_t p_offset, bool p_cubic, bool p_apply_tilt) const {
  1657. // Make sure that p_offset is finite.
  1658. ERR_FAIL_COND_V_MSG(!Math::is_finite(p_offset), Transform3D(), "Offset is non-finite");
  1659. if (baked_cache_dirty) {
  1660. _bake();
  1661. }
  1662. // Validate: Curve may not have baked points.
  1663. const int point_count = baked_point_cache.size();
  1664. ERR_FAIL_COND_V_MSG(point_count == 0, Transform3D(), "No points in Curve3D.");
  1665. if (point_count == 1) {
  1666. Transform3D t;
  1667. t.origin = baked_point_cache.get(0);
  1668. ERR_FAIL_V_MSG(t, "Only 1 point in Curve3D.");
  1669. }
  1670. p_offset = CLAMP(p_offset, 0.0, get_baked_length()); // PathFollower implement wrapping logic.
  1671. // 0. Find interval for all sampling steps.
  1672. Curve3D::Interval interval = _find_interval(p_offset);
  1673. // 1. Sample position.
  1674. Vector3 pos = _sample_baked(interval, p_cubic);
  1675. // 2. Sample rotation frame.
  1676. Basis frame = _sample_posture(interval, p_apply_tilt);
  1677. return Transform3D(frame, pos);
  1678. }
  1679. real_t Curve3D::sample_baked_tilt(real_t p_offset) const {
  1680. // Make sure that p_offset is finite.
  1681. ERR_FAIL_COND_V_MSG(!Math::is_finite(p_offset), 0, "Offset is non-finite");
  1682. if (baked_cache_dirty) {
  1683. _bake();
  1684. }
  1685. // Validate: Curve may not have baked tilts.
  1686. int pc = baked_tilt_cache.size();
  1687. ERR_FAIL_COND_V_MSG(pc == 0, 0, "No tilts in Curve3D.");
  1688. if (pc == 1) {
  1689. return baked_tilt_cache.get(0);
  1690. }
  1691. p_offset = CLAMP(p_offset, 0.0, get_baked_length()); // PathFollower implement wrapping logic.
  1692. Curve3D::Interval interval = _find_interval(p_offset);
  1693. return _sample_baked_tilt(interval);
  1694. }
  1695. Vector3 Curve3D::sample_baked_up_vector(real_t p_offset, bool p_apply_tilt) const {
  1696. // Make sure that p_offset is finite.
  1697. ERR_FAIL_COND_V_MSG(!Math::is_finite(p_offset), Vector3(0, 1, 0), "Offset is non-finite");
  1698. if (baked_cache_dirty) {
  1699. _bake();
  1700. }
  1701. // Validate: Curve may not have baked up vectors.
  1702. ERR_FAIL_COND_V_MSG(!up_vector_enabled, Vector3(0, 1, 0), "No up vectors in Curve3D.");
  1703. int count = baked_up_vector_cache.size();
  1704. if (count == 1) {
  1705. return baked_up_vector_cache.get(0);
  1706. }
  1707. p_offset = CLAMP(p_offset, 0.0, get_baked_length()); // PathFollower implement wrapping logic.
  1708. Curve3D::Interval interval = _find_interval(p_offset);
  1709. return _sample_posture(interval, p_apply_tilt).get_column(1);
  1710. }
  1711. PackedVector3Array Curve3D::get_baked_points() const {
  1712. if (baked_cache_dirty) {
  1713. _bake();
  1714. }
  1715. return baked_point_cache;
  1716. }
  1717. Vector<real_t> Curve3D::get_baked_tilts() const {
  1718. if (baked_cache_dirty) {
  1719. _bake();
  1720. }
  1721. return baked_tilt_cache;
  1722. }
  1723. PackedVector3Array Curve3D::get_baked_up_vectors() const {
  1724. if (baked_cache_dirty) {
  1725. _bake();
  1726. }
  1727. return baked_up_vector_cache;
  1728. }
  1729. Vector3 Curve3D::get_closest_point(const Vector3 &p_to_point) const {
  1730. // Brute force method.
  1731. if (baked_cache_dirty) {
  1732. _bake();
  1733. }
  1734. // Validate: Curve may not have baked points.
  1735. int pc = baked_point_cache.size();
  1736. ERR_FAIL_COND_V_MSG(pc == 0, Vector3(), "No points in Curve3D.");
  1737. if (pc == 1) {
  1738. return baked_point_cache.get(0);
  1739. }
  1740. const Vector3 *r = baked_point_cache.ptr();
  1741. Vector3 nearest;
  1742. real_t nearest_dist = -1.0f;
  1743. for (int i = 0; i < pc - 1; i++) {
  1744. const real_t interval = baked_dist_cache[i + 1] - baked_dist_cache[i];
  1745. Vector3 origin = r[i];
  1746. Vector3 direction = (r[i + 1] - origin) / interval;
  1747. real_t d = CLAMP((p_to_point - origin).dot(direction), 0.0f, interval);
  1748. Vector3 proj = origin + direction * d;
  1749. real_t dist = proj.distance_squared_to(p_to_point);
  1750. if (nearest_dist < 0.0f || dist < nearest_dist) {
  1751. nearest = proj;
  1752. nearest_dist = dist;
  1753. }
  1754. }
  1755. return nearest;
  1756. }
  1757. PackedVector3Array Curve3D::get_points() const {
  1758. return _get_data()["points"];
  1759. }
  1760. real_t Curve3D::get_closest_offset(const Vector3 &p_to_point) const {
  1761. // Brute force method.
  1762. if (baked_cache_dirty) {
  1763. _bake();
  1764. }
  1765. // Validate: Curve may not have baked points.
  1766. int pc = baked_point_cache.size();
  1767. ERR_FAIL_COND_V_MSG(pc == 0, 0.0f, "No points in Curve3D.");
  1768. if (pc == 1) {
  1769. return 0.0f;
  1770. }
  1771. const Vector3 *r = baked_point_cache.ptr();
  1772. real_t nearest = 0.0f;
  1773. real_t nearest_dist = -1.0f;
  1774. real_t offset;
  1775. for (int i = 0; i < pc - 1; i++) {
  1776. offset = baked_dist_cache[i];
  1777. const real_t interval = baked_dist_cache[i + 1] - baked_dist_cache[i];
  1778. Vector3 origin = r[i];
  1779. Vector3 direction = (r[i + 1] - origin) / interval;
  1780. real_t d = CLAMP((p_to_point - origin).dot(direction), 0.0f, interval);
  1781. Vector3 proj = origin + direction * d;
  1782. real_t dist = proj.distance_squared_to(p_to_point);
  1783. if (nearest_dist < 0.0f || dist < nearest_dist) {
  1784. nearest = offset + d;
  1785. nearest_dist = dist;
  1786. }
  1787. }
  1788. return nearest;
  1789. }
  1790. void Curve3D::set_closed(bool p_closed) {
  1791. if (closed == p_closed) {
  1792. return;
  1793. }
  1794. closed = p_closed;
  1795. mark_dirty();
  1796. notify_property_list_changed();
  1797. }
  1798. bool Curve3D::is_closed() const {
  1799. return closed;
  1800. }
  1801. void Curve3D::set_bake_interval(real_t p_tolerance) {
  1802. bake_interval = p_tolerance;
  1803. mark_dirty();
  1804. }
  1805. real_t Curve3D::get_bake_interval() const {
  1806. return bake_interval;
  1807. }
  1808. void Curve3D::set_up_vector_enabled(bool p_enable) {
  1809. up_vector_enabled = p_enable;
  1810. mark_dirty();
  1811. }
  1812. bool Curve3D::is_up_vector_enabled() const {
  1813. return up_vector_enabled;
  1814. }
  1815. Dictionary Curve3D::_get_data() const {
  1816. Dictionary dc;
  1817. PackedVector3Array d;
  1818. d.resize(points.size() * 3);
  1819. Vector3 *w = d.ptrw();
  1820. Vector<real_t> t;
  1821. t.resize(points.size());
  1822. real_t *wt = t.ptrw();
  1823. for (uint32_t i = 0; i < points.size(); i++) {
  1824. w[i * 3 + 0] = points[i].in;
  1825. w[i * 3 + 1] = points[i].out;
  1826. w[i * 3 + 2] = points[i].position;
  1827. wt[i] = points[i].tilt;
  1828. }
  1829. dc["points"] = d;
  1830. dc["tilts"] = t;
  1831. return dc;
  1832. }
  1833. void Curve3D::_set_data(const Dictionary &p_data) {
  1834. ERR_FAIL_COND(!p_data.has("points"));
  1835. ERR_FAIL_COND(!p_data.has("tilts"));
  1836. PackedVector3Array rp = p_data["points"];
  1837. int pc = rp.size();
  1838. ERR_FAIL_COND(pc % 3 != 0);
  1839. int old_size = points.size();
  1840. int new_size = pc / 3;
  1841. if (old_size != new_size) {
  1842. points.resize(new_size);
  1843. }
  1844. const Vector3 *r = rp.ptr();
  1845. Vector<real_t> rtl = p_data["tilts"];
  1846. const real_t *rt = rtl.ptr();
  1847. for (uint32_t i = 0; i < points.size(); i++) {
  1848. points[i].in = r[i * 3 + 0];
  1849. points[i].out = r[i * 3 + 1];
  1850. points[i].position = r[i * 3 + 2];
  1851. points[i].tilt = rt[i];
  1852. }
  1853. mark_dirty();
  1854. if (old_size != new_size) {
  1855. notify_property_list_changed();
  1856. }
  1857. }
  1858. PackedVector3Array Curve3D::tessellate(int p_max_stages, real_t p_tolerance) const {
  1859. PackedVector3Array tess;
  1860. if (points.is_empty()) {
  1861. return tess;
  1862. }
  1863. Vector<RBMap<real_t, Vector3>> midpoints;
  1864. const int num_intervals = closed ? points.size() : points.size() - 1;
  1865. midpoints.resize(num_intervals);
  1866. // Point Count: Begins at 1 to account for the last point.
  1867. int pc = 1;
  1868. for (int i = 0; i < num_intervals; i++) {
  1869. if (!closed || i < num_intervals - 1) {
  1870. _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);
  1871. } else {
  1872. _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);
  1873. }
  1874. pc++;
  1875. pc += midpoints[i].size();
  1876. }
  1877. tess.resize(pc);
  1878. Vector3 *bpw = tess.ptrw();
  1879. bpw[0] = points[0].position;
  1880. int pidx = 0;
  1881. for (int i = 0; i < num_intervals; i++) {
  1882. for (const KeyValue<real_t, Vector3> &E : midpoints[i]) {
  1883. pidx++;
  1884. bpw[pidx] = E.value;
  1885. }
  1886. pidx++;
  1887. if (!closed || i < num_intervals - 1) {
  1888. bpw[pidx] = points[i + 1].position;
  1889. } else {
  1890. bpw[pidx] = points[0].position;
  1891. }
  1892. }
  1893. return tess;
  1894. }
  1895. Vector<RBMap<real_t, Vector3>> Curve3D::_tessellate_even_length(int p_max_stages, real_t p_length) const {
  1896. Vector<RBMap<real_t, Vector3>> midpoints;
  1897. ERR_FAIL_COND_V_MSG(points.size() < 2, midpoints, "Curve must have at least 2 control point");
  1898. const int num_intervals = closed ? points.size() : points.size() - 1;
  1899. midpoints.resize(num_intervals);
  1900. for (int i = 0; i < num_intervals; i++) {
  1901. if (!closed || i < num_intervals - 1) {
  1902. _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);
  1903. } else {
  1904. _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);
  1905. }
  1906. }
  1907. return midpoints;
  1908. }
  1909. PackedVector3Array Curve3D::tessellate_even_length(int p_max_stages, real_t p_length) const {
  1910. PackedVector3Array tess;
  1911. Vector<RBMap<real_t, Vector3>> midpoints = _tessellate_even_length(p_max_stages, p_length);
  1912. if (midpoints.is_empty()) {
  1913. return tess;
  1914. }
  1915. const int num_intervals = closed ? points.size() : points.size() - 1;
  1916. // Point Count: Begins at 1 to account for the last point.
  1917. int pc = 1;
  1918. for (int i = 0; i < num_intervals; i++) {
  1919. pc++;
  1920. pc += midpoints[i].size();
  1921. }
  1922. tess.resize(pc);
  1923. Vector3 *bpw = tess.ptrw();
  1924. bpw[0] = points[0].position;
  1925. int pidx = 0;
  1926. for (int i = 0; i < num_intervals; i++) {
  1927. for (const KeyValue<real_t, Vector3> &E : midpoints[i]) {
  1928. pidx++;
  1929. bpw[pidx] = E.value;
  1930. }
  1931. pidx++;
  1932. if (!closed || i < num_intervals - 1) {
  1933. bpw[pidx] = points[i + 1].position;
  1934. } else {
  1935. bpw[pidx] = points[0].position;
  1936. }
  1937. }
  1938. return tess;
  1939. }
  1940. bool Curve3D::_set(const StringName &p_name, const Variant &p_value) {
  1941. Vector<String> components = String(p_name).split("/", true, 2);
  1942. if (components.size() >= 2 && components[0].begins_with("point_") && components[0].trim_prefix("point_").is_valid_int()) {
  1943. int point_index = components[0].trim_prefix("point_").to_int();
  1944. const String &property = components[1];
  1945. if (property == "position") {
  1946. set_point_position(point_index, p_value);
  1947. return true;
  1948. } else if (property == "in") {
  1949. set_point_in(point_index, p_value);
  1950. return true;
  1951. } else if (property == "out") {
  1952. set_point_out(point_index, p_value);
  1953. return true;
  1954. } else if (property == "tilt") {
  1955. set_point_tilt(point_index, p_value);
  1956. return true;
  1957. }
  1958. }
  1959. return false;
  1960. }
  1961. bool Curve3D::_get(const StringName &p_name, Variant &r_ret) const {
  1962. Vector<String> components = String(p_name).split("/", true, 2);
  1963. if (components.size() >= 2 && components[0].begins_with("point_") && components[0].trim_prefix("point_").is_valid_int()) {
  1964. int point_index = components[0].trim_prefix("point_").to_int();
  1965. const String &property = components[1];
  1966. if (property == "position") {
  1967. r_ret = get_point_position(point_index);
  1968. return true;
  1969. } else if (property == "in") {
  1970. r_ret = get_point_in(point_index);
  1971. return true;
  1972. } else if (property == "out") {
  1973. r_ret = get_point_out(point_index);
  1974. return true;
  1975. } else if (property == "tilt") {
  1976. r_ret = get_point_tilt(point_index);
  1977. return true;
  1978. }
  1979. }
  1980. return false;
  1981. }
  1982. void Curve3D::_get_property_list(List<PropertyInfo> *p_list) const {
  1983. for (uint32_t i = 0; i < points.size(); i++) {
  1984. PropertyInfo pi = PropertyInfo(Variant::VECTOR3, vformat("point_%d/position", i));
  1985. pi.usage &= ~PROPERTY_USAGE_STORAGE;
  1986. p_list->push_back(pi);
  1987. if (closed || i != 0) {
  1988. pi = PropertyInfo(Variant::VECTOR3, vformat("point_%d/in", i));
  1989. pi.usage &= ~PROPERTY_USAGE_STORAGE;
  1990. p_list->push_back(pi);
  1991. }
  1992. if (closed || i != points.size() - 1) {
  1993. pi = PropertyInfo(Variant::VECTOR3, vformat("point_%d/out", i));
  1994. pi.usage &= ~PROPERTY_USAGE_STORAGE;
  1995. p_list->push_back(pi);
  1996. }
  1997. pi = PropertyInfo(Variant::FLOAT, vformat("point_%d/tilt", i));
  1998. pi.usage &= ~PROPERTY_USAGE_STORAGE;
  1999. p_list->push_back(pi);
  2000. }
  2001. }
  2002. void Curve3D::_bind_methods() {
  2003. ClassDB::bind_method(D_METHOD("get_point_count"), &Curve3D::get_point_count);
  2004. ClassDB::bind_method(D_METHOD("set_point_count", "count"), &Curve3D::set_point_count);
  2005. ClassDB::bind_method(D_METHOD("add_point", "position", "in", "out", "index"), &Curve3D::add_point, DEFVAL(Vector3()), DEFVAL(Vector3()), DEFVAL(-1));
  2006. ClassDB::bind_method(D_METHOD("set_point_position", "idx", "position"), &Curve3D::set_point_position);
  2007. ClassDB::bind_method(D_METHOD("get_point_position", "idx"), &Curve3D::get_point_position);
  2008. ClassDB::bind_method(D_METHOD("set_point_tilt", "idx", "tilt"), &Curve3D::set_point_tilt);
  2009. ClassDB::bind_method(D_METHOD("get_point_tilt", "idx"), &Curve3D::get_point_tilt);
  2010. ClassDB::bind_method(D_METHOD("set_point_in", "idx", "position"), &Curve3D::set_point_in);
  2011. ClassDB::bind_method(D_METHOD("get_point_in", "idx"), &Curve3D::get_point_in);
  2012. ClassDB::bind_method(D_METHOD("set_point_out", "idx", "position"), &Curve3D::set_point_out);
  2013. ClassDB::bind_method(D_METHOD("get_point_out", "idx"), &Curve3D::get_point_out);
  2014. ClassDB::bind_method(D_METHOD("remove_point", "idx"), &Curve3D::remove_point);
  2015. ClassDB::bind_method(D_METHOD("clear_points"), &Curve3D::clear_points);
  2016. ClassDB::bind_method(D_METHOD("sample", "idx", "t"), &Curve3D::sample);
  2017. ClassDB::bind_method(D_METHOD("samplef", "fofs"), &Curve3D::samplef);
  2018. ClassDB::bind_method(D_METHOD("set_closed", "closed"), &Curve3D::set_closed);
  2019. ClassDB::bind_method(D_METHOD("is_closed"), &Curve3D::is_closed);
  2020. //ClassDB::bind_method(D_METHOD("bake","subdivs"),&Curve3D::bake,DEFVAL(10));
  2021. ClassDB::bind_method(D_METHOD("set_bake_interval", "distance"), &Curve3D::set_bake_interval);
  2022. ClassDB::bind_method(D_METHOD("get_bake_interval"), &Curve3D::get_bake_interval);
  2023. ClassDB::bind_method(D_METHOD("set_up_vector_enabled", "enable"), &Curve3D::set_up_vector_enabled);
  2024. ClassDB::bind_method(D_METHOD("is_up_vector_enabled"), &Curve3D::is_up_vector_enabled);
  2025. ClassDB::bind_method(D_METHOD("get_baked_length"), &Curve3D::get_baked_length);
  2026. ClassDB::bind_method(D_METHOD("sample_baked", "offset", "cubic"), &Curve3D::sample_baked, DEFVAL(0.0), DEFVAL(false));
  2027. 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));
  2028. ClassDB::bind_method(D_METHOD("sample_baked_up_vector", "offset", "apply_tilt"), &Curve3D::sample_baked_up_vector, DEFVAL(false));
  2029. ClassDB::bind_method(D_METHOD("get_baked_points"), &Curve3D::get_baked_points);
  2030. ClassDB::bind_method(D_METHOD("get_baked_tilts"), &Curve3D::get_baked_tilts);
  2031. ClassDB::bind_method(D_METHOD("get_baked_up_vectors"), &Curve3D::get_baked_up_vectors);
  2032. ClassDB::bind_method(D_METHOD("get_closest_point", "to_point"), &Curve3D::get_closest_point);
  2033. ClassDB::bind_method(D_METHOD("get_closest_offset", "to_point"), &Curve3D::get_closest_offset);
  2034. ClassDB::bind_method(D_METHOD("tessellate", "max_stages", "tolerance_degrees"), &Curve3D::tessellate, DEFVAL(5), DEFVAL(4));
  2035. ClassDB::bind_method(D_METHOD("tessellate_even_length", "max_stages", "tolerance_length"), &Curve3D::tessellate_even_length, DEFVAL(5), DEFVAL(0.2));
  2036. ClassDB::bind_method(D_METHOD("_get_data"), &Curve3D::_get_data);
  2037. ClassDB::bind_method(D_METHOD("_set_data", "data"), &Curve3D::_set_data);
  2038. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "closed"), "set_closed", "is_closed");
  2039. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "bake_interval", PROPERTY_HINT_RANGE, "0.01,512,0.01"), "set_bake_interval", "get_bake_interval");
  2040. ADD_PROPERTY(PropertyInfo(Variant::INT, "_data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "_set_data", "_get_data");
  2041. ADD_ARRAY_COUNT("Points", "point_count", "set_point_count", "get_point_count", "point_");
  2042. ADD_GROUP("Up Vector", "up_vector_");
  2043. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "up_vector_enabled"), "set_up_vector_enabled", "is_up_vector_enabled");
  2044. }
  2045. Curve3D::Curve3D() {}