curve.cpp 70 KB

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