curve.cpp 76 KB

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