curve.cpp 64 KB

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