curve.cpp 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219
  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 bézier
  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_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 {
  651. Vector2 beg = p_a.bezier_interpolate(p_a + p_out, p_b + p_in, p_b, p_begin);
  652. Vector2 end = p_a.bezier_interpolate(p_a + p_out, p_b + p_in, p_b, p_end);
  653. real_t length = beg.distance_to(end);
  654. if (length > p_length && p_depth < p_max_depth) {
  655. real_t mp = (p_begin + p_end) * 0.5;
  656. Vector2 mid = p_a.bezier_interpolate(p_a + p_out, p_b + p_in, p_b, mp);
  657. r_bake[mp] = mid;
  658. _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);
  659. _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);
  660. }
  661. }
  662. void Curve2D::_bake() const {
  663. if (!baked_cache_dirty) {
  664. return;
  665. }
  666. baked_max_ofs = 0;
  667. baked_cache_dirty = false;
  668. if (points.size() == 0) {
  669. baked_point_cache.clear();
  670. baked_dist_cache.clear();
  671. baked_forward_vector_cache.clear();
  672. return;
  673. }
  674. if (points.size() == 1) {
  675. baked_point_cache.resize(1);
  676. baked_point_cache.set(0, points[0].position);
  677. baked_dist_cache.resize(1);
  678. baked_dist_cache.set(0, 0.0);
  679. baked_forward_vector_cache.resize(1);
  680. baked_forward_vector_cache.set(0, Vector2(0.0, 0.1));
  681. return;
  682. }
  683. // Tesselate curve to (almost) even length segments
  684. {
  685. Vector<RBMap<real_t, Vector2>> midpoints = _tessellate_even_length(10, bake_interval);
  686. int pc = 1;
  687. for (int i = 0; i < points.size() - 1; i++) {
  688. pc++;
  689. pc += midpoints[i].size();
  690. }
  691. baked_point_cache.resize(pc);
  692. baked_dist_cache.resize(pc);
  693. baked_forward_vector_cache.resize(pc);
  694. Vector2 *bpw = baked_point_cache.ptrw();
  695. Vector2 *bfw = baked_forward_vector_cache.ptrw();
  696. // Collect positions and sample tilts and tangents for each baked points.
  697. bpw[0] = points[0].position;
  698. 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();
  699. int pidx = 0;
  700. for (int i = 0; i < points.size() - 1; i++) {
  701. for (const KeyValue<real_t, Vector2> &E : midpoints[i]) {
  702. pidx++;
  703. bpw[pidx] = E.value;
  704. 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();
  705. }
  706. pidx++;
  707. bpw[pidx] = points[i + 1].position;
  708. 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();
  709. }
  710. // Recalculate the baked distances.
  711. real_t *bdw = baked_dist_cache.ptrw();
  712. bdw[0] = 0.0;
  713. for (int i = 0; i < pc - 1; i++) {
  714. bdw[i + 1] = bdw[i] + bpw[i].distance_to(bpw[i + 1]);
  715. }
  716. baked_max_ofs = bdw[pc - 1];
  717. }
  718. }
  719. real_t Curve2D::get_baked_length() const {
  720. if (baked_cache_dirty) {
  721. _bake();
  722. }
  723. return baked_max_ofs;
  724. }
  725. Curve2D::Interval Curve2D::_find_interval(real_t p_offset) const {
  726. Interval interval = {
  727. -1,
  728. 0.0
  729. };
  730. ERR_FAIL_COND_V_MSG(baked_cache_dirty, interval, "Backed cache is dirty");
  731. int pc = baked_point_cache.size();
  732. ERR_FAIL_COND_V_MSG(pc < 2, interval, "Less than two points in cache");
  733. int start = 0;
  734. int end = pc;
  735. int idx = (end + start) / 2;
  736. // Binary search to find baked points.
  737. while (start < idx) {
  738. real_t offset = baked_dist_cache[idx];
  739. if (p_offset <= offset) {
  740. end = idx;
  741. } else {
  742. start = idx;
  743. }
  744. idx = (end + start) / 2;
  745. }
  746. real_t offset_begin = baked_dist_cache[idx];
  747. real_t offset_end = baked_dist_cache[idx + 1];
  748. real_t idx_interval = offset_end - offset_begin;
  749. ERR_FAIL_COND_V_MSG(p_offset < offset_begin || p_offset > offset_end, interval, "Offset out of range.");
  750. interval.idx = idx;
  751. if (idx_interval < FLT_EPSILON) {
  752. interval.frac = 0.5; // For a very short interval, 0.5 is a reasonable choice.
  753. ERR_FAIL_V_MSG(interval, "Zero length interval.");
  754. }
  755. interval.frac = (p_offset - offset_begin) / idx_interval;
  756. return interval;
  757. }
  758. Vector2 Curve2D::_sample_baked(Interval p_interval, bool p_cubic) const {
  759. // Assuming p_interval is valid.
  760. ERR_FAIL_INDEX_V_MSG(p_interval.idx, baked_point_cache.size(), Vector2(), "Invalid interval");
  761. int idx = p_interval.idx;
  762. real_t frac = p_interval.frac;
  763. const Vector2 *r = baked_point_cache.ptr();
  764. int pc = baked_point_cache.size();
  765. if (p_cubic) {
  766. Vector2 pre = idx > 0 ? r[idx - 1] : r[idx];
  767. Vector2 post = (idx < (pc - 2)) ? r[idx + 2] : r[idx + 1];
  768. return r[idx].cubic_interpolate(r[idx + 1], pre, post, frac);
  769. } else {
  770. return r[idx].lerp(r[idx + 1], frac);
  771. }
  772. }
  773. Transform2D Curve2D::_sample_posture(Interval p_interval) const {
  774. // Assuming that p_interval is valid.
  775. ERR_FAIL_INDEX_V_MSG(p_interval.idx, baked_point_cache.size(), Transform2D(), "Invalid interval");
  776. int idx = p_interval.idx;
  777. real_t frac = p_interval.frac;
  778. Vector2 forward_begin = baked_forward_vector_cache[idx];
  779. Vector2 forward_end = baked_forward_vector_cache[idx + 1];
  780. // Build frames at both ends of the interval, then interpolate.
  781. const Vector2 forward = forward_begin.slerp(forward_end, frac).normalized();
  782. const Vector2 side = Vector2(-forward.y, forward.x);
  783. return Transform2D(forward, side, Vector2(0.0, 0.0));
  784. }
  785. Vector2 Curve2D::sample_baked(real_t p_offset, bool p_cubic) const {
  786. if (baked_cache_dirty) {
  787. _bake();
  788. }
  789. // Validate: Curve may not have baked points.
  790. int pc = baked_point_cache.size();
  791. ERR_FAIL_COND_V_MSG(pc == 0, Vector2(), "No points in Curve2D.");
  792. if (pc == 1) {
  793. return baked_point_cache[0];
  794. }
  795. p_offset = CLAMP(p_offset, 0.0, get_baked_length()); // PathFollower implement wrapping logic.
  796. Curve2D::Interval interval = _find_interval(p_offset);
  797. return _sample_baked(interval, p_cubic);
  798. }
  799. Transform2D Curve2D::sample_baked_with_rotation(real_t p_offset, bool p_cubic) const {
  800. if (baked_cache_dirty) {
  801. _bake();
  802. }
  803. // Validate: Curve may not have baked points.
  804. const int point_count = baked_point_cache.size();
  805. ERR_FAIL_COND_V_MSG(point_count == 0, Transform2D(), "No points in Curve3D.");
  806. if (point_count == 1) {
  807. Transform2D t;
  808. t.set_origin(baked_point_cache.get(0));
  809. ERR_FAIL_V_MSG(t, "Only 1 point in Curve2D.");
  810. }
  811. p_offset = CLAMP(p_offset, 0.0, get_baked_length()); // PathFollower implement wrapping logic.
  812. // 0. Find interval for all sampling steps.
  813. Curve2D::Interval interval = _find_interval(p_offset);
  814. // 1. Sample position.
  815. Vector2 pos = _sample_baked(interval, p_cubic);
  816. // 2. Sample rotation frame.
  817. Transform2D frame = _sample_posture(interval);
  818. frame.set_origin(pos);
  819. return frame;
  820. }
  821. PackedVector2Array Curve2D::get_baked_points() const {
  822. if (baked_cache_dirty) {
  823. _bake();
  824. }
  825. return baked_point_cache;
  826. }
  827. void Curve2D::set_bake_interval(real_t p_tolerance) {
  828. bake_interval = p_tolerance;
  829. mark_dirty();
  830. }
  831. real_t Curve2D::get_bake_interval() const {
  832. return bake_interval;
  833. }
  834. Vector2 Curve2D::get_closest_point(const Vector2 &p_to_point) const {
  835. // Brute force method.
  836. if (baked_cache_dirty) {
  837. _bake();
  838. }
  839. // Validate: Curve may not have baked points.
  840. int pc = baked_point_cache.size();
  841. ERR_FAIL_COND_V_MSG(pc == 0, Vector2(), "No points in Curve2D.");
  842. if (pc == 1) {
  843. return baked_point_cache.get(0);
  844. }
  845. const Vector2 *r = baked_point_cache.ptr();
  846. Vector2 nearest;
  847. real_t nearest_dist = -1.0f;
  848. for (int i = 0; i < pc - 1; i++) {
  849. Vector2 origin = r[i];
  850. Vector2 direction = (r[i + 1] - origin) / bake_interval;
  851. real_t d = CLAMP((p_to_point - origin).dot(direction), 0.0f, bake_interval);
  852. Vector2 proj = origin + direction * d;
  853. real_t dist = proj.distance_squared_to(p_to_point);
  854. if (nearest_dist < 0.0f || dist < nearest_dist) {
  855. nearest = proj;
  856. nearest_dist = dist;
  857. }
  858. }
  859. return nearest;
  860. }
  861. real_t Curve2D::get_closest_offset(const Vector2 &p_to_point) const {
  862. // Brute force method.
  863. if (baked_cache_dirty) {
  864. _bake();
  865. }
  866. // Validate: Curve may not have baked points.
  867. int pc = baked_point_cache.size();
  868. ERR_FAIL_COND_V_MSG(pc == 0, 0.0f, "No points in Curve2D.");
  869. if (pc == 1) {
  870. return 0.0f;
  871. }
  872. const Vector2 *r = baked_point_cache.ptr();
  873. real_t nearest = 0.0f;
  874. real_t nearest_dist = -1.0f;
  875. real_t offset = 0.0f;
  876. for (int i = 0; i < pc - 1; i++) {
  877. Vector2 origin = r[i];
  878. Vector2 direction = (r[i + 1] - origin) / bake_interval;
  879. real_t d = CLAMP((p_to_point - origin).dot(direction), 0.0f, bake_interval);
  880. Vector2 proj = origin + direction * d;
  881. real_t dist = proj.distance_squared_to(p_to_point);
  882. if (nearest_dist < 0.0f || dist < nearest_dist) {
  883. nearest = offset + d;
  884. nearest_dist = dist;
  885. }
  886. offset += bake_interval;
  887. }
  888. return nearest;
  889. }
  890. Dictionary Curve2D::_get_data() const {
  891. Dictionary dc;
  892. PackedVector2Array d;
  893. d.resize(points.size() * 3);
  894. Vector2 *w = d.ptrw();
  895. for (int i = 0; i < points.size(); i++) {
  896. w[i * 3 + 0] = points[i].in;
  897. w[i * 3 + 1] = points[i].out;
  898. w[i * 3 + 2] = points[i].position;
  899. }
  900. dc["points"] = d;
  901. return dc;
  902. }
  903. void Curve2D::_set_data(const Dictionary &p_data) {
  904. ERR_FAIL_COND(!p_data.has("points"));
  905. PackedVector2Array rp = p_data["points"];
  906. int pc = rp.size();
  907. ERR_FAIL_COND(pc % 3 != 0);
  908. points.resize(pc / 3);
  909. const Vector2 *r = rp.ptr();
  910. for (int i = 0; i < points.size(); i++) {
  911. points.write[i].in = r[i * 3 + 0];
  912. points.write[i].out = r[i * 3 + 1];
  913. points.write[i].position = r[i * 3 + 2];
  914. }
  915. mark_dirty();
  916. notify_property_list_changed();
  917. }
  918. PackedVector2Array Curve2D::tessellate(int p_max_stages, real_t p_tolerance) const {
  919. PackedVector2Array tess;
  920. if (points.size() == 0) {
  921. return tess;
  922. }
  923. // The current implementation requires a sorted map.
  924. Vector<RBMap<real_t, Vector2>> midpoints;
  925. midpoints.resize(points.size() - 1);
  926. int pc = 1;
  927. for (int i = 0; i < points.size() - 1; i++) {
  928. _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);
  929. pc++;
  930. pc += midpoints[i].size();
  931. }
  932. tess.resize(pc);
  933. Vector2 *bpw = tess.ptrw();
  934. bpw[0] = points[0].position;
  935. int pidx = 0;
  936. for (int i = 0; i < points.size() - 1; i++) {
  937. for (const KeyValue<real_t, Vector2> &E : midpoints[i]) {
  938. pidx++;
  939. bpw[pidx] = E.value;
  940. }
  941. pidx++;
  942. bpw[pidx] = points[i + 1].position;
  943. }
  944. return tess;
  945. }
  946. Vector<RBMap<real_t, Vector2>> Curve2D::_tessellate_even_length(int p_max_stages, real_t p_length) const {
  947. Vector<RBMap<real_t, Vector2>> midpoints;
  948. ERR_FAIL_COND_V_MSG(points.size() < 2, midpoints, "Curve must have at least 2 control point");
  949. midpoints.resize(points.size() - 1);
  950. for (int i = 0; i < points.size() - 1; i++) {
  951. _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);
  952. }
  953. return midpoints;
  954. }
  955. PackedVector2Array Curve2D::tessellate_even_length(int p_max_stages, real_t p_length) const {
  956. PackedVector2Array tess;
  957. Vector<RBMap<real_t, Vector2>> midpoints = _tessellate_even_length(p_max_stages, p_length);
  958. if (midpoints.size() == 0) {
  959. return tess;
  960. }
  961. int pc = 1;
  962. for (int i = 0; i < points.size() - 1; i++) {
  963. pc++;
  964. pc += midpoints[i].size();
  965. }
  966. tess.resize(pc);
  967. Vector2 *bpw = tess.ptrw();
  968. bpw[0] = points[0].position;
  969. int pidx = 0;
  970. for (int i = 0; i < points.size() - 1; i++) {
  971. for (const KeyValue<real_t, Vector2> &E : midpoints[i]) {
  972. pidx++;
  973. bpw[pidx] = E.value;
  974. }
  975. pidx++;
  976. bpw[pidx] = points[i + 1].position;
  977. }
  978. return tess;
  979. }
  980. bool Curve2D::_set(const StringName &p_name, const Variant &p_value) {
  981. Vector<String> components = String(p_name).split("/", true, 2);
  982. if (components.size() >= 2 && components[0].begins_with("point_") && components[0].trim_prefix("point_").is_valid_int()) {
  983. int point_index = components[0].trim_prefix("point_").to_int();
  984. String property = components[1];
  985. if (property == "position") {
  986. set_point_position(point_index, p_value);
  987. return true;
  988. } else if (property == "in") {
  989. set_point_in(point_index, p_value);
  990. return true;
  991. } else if (property == "out") {
  992. set_point_out(point_index, p_value);
  993. return true;
  994. }
  995. }
  996. return false;
  997. }
  998. bool Curve2D::_get(const StringName &p_name, Variant &r_ret) const {
  999. Vector<String> components = String(p_name).split("/", true, 2);
  1000. if (components.size() >= 2 && components[0].begins_with("point_") && components[0].trim_prefix("point_").is_valid_int()) {
  1001. int point_index = components[0].trim_prefix("point_").to_int();
  1002. String property = components[1];
  1003. if (property == "position") {
  1004. r_ret = get_point_position(point_index);
  1005. return true;
  1006. } else if (property == "in") {
  1007. r_ret = get_point_in(point_index);
  1008. return true;
  1009. } else if (property == "out") {
  1010. r_ret = get_point_out(point_index);
  1011. return true;
  1012. }
  1013. }
  1014. return false;
  1015. }
  1016. void Curve2D::_get_property_list(List<PropertyInfo> *p_list) const {
  1017. for (int i = 0; i < points.size(); i++) {
  1018. PropertyInfo pi = PropertyInfo(Variant::VECTOR2, vformat("point_%d/position", i));
  1019. pi.usage &= ~PROPERTY_USAGE_STORAGE;
  1020. p_list->push_back(pi);
  1021. if (i != 0) {
  1022. pi = PropertyInfo(Variant::VECTOR2, vformat("point_%d/in", i));
  1023. pi.usage &= ~PROPERTY_USAGE_STORAGE;
  1024. p_list->push_back(pi);
  1025. }
  1026. if (i != points.size() - 1) {
  1027. pi = PropertyInfo(Variant::VECTOR2, vformat("point_%d/out", i));
  1028. pi.usage &= ~PROPERTY_USAGE_STORAGE;
  1029. p_list->push_back(pi);
  1030. }
  1031. }
  1032. }
  1033. void Curve2D::_bind_methods() {
  1034. ClassDB::bind_method(D_METHOD("get_point_count"), &Curve2D::get_point_count);
  1035. ClassDB::bind_method(D_METHOD("set_point_count", "count"), &Curve2D::set_point_count);
  1036. ClassDB::bind_method(D_METHOD("add_point", "position", "in", "out", "index"), &Curve2D::add_point, DEFVAL(Vector2()), DEFVAL(Vector2()), DEFVAL(-1));
  1037. ClassDB::bind_method(D_METHOD("set_point_position", "idx", "position"), &Curve2D::set_point_position);
  1038. ClassDB::bind_method(D_METHOD("get_point_position", "idx"), &Curve2D::get_point_position);
  1039. ClassDB::bind_method(D_METHOD("set_point_in", "idx", "position"), &Curve2D::set_point_in);
  1040. ClassDB::bind_method(D_METHOD("get_point_in", "idx"), &Curve2D::get_point_in);
  1041. ClassDB::bind_method(D_METHOD("set_point_out", "idx", "position"), &Curve2D::set_point_out);
  1042. ClassDB::bind_method(D_METHOD("get_point_out", "idx"), &Curve2D::get_point_out);
  1043. ClassDB::bind_method(D_METHOD("remove_point", "idx"), &Curve2D::remove_point);
  1044. ClassDB::bind_method(D_METHOD("clear_points"), &Curve2D::clear_points);
  1045. ClassDB::bind_method(D_METHOD("sample", "idx", "t"), &Curve2D::sample);
  1046. ClassDB::bind_method(D_METHOD("samplef", "fofs"), &Curve2D::samplef);
  1047. //ClassDB::bind_method(D_METHOD("bake","subdivs"),&Curve2D::bake,DEFVAL(10));
  1048. ClassDB::bind_method(D_METHOD("set_bake_interval", "distance"), &Curve2D::set_bake_interval);
  1049. ClassDB::bind_method(D_METHOD("get_bake_interval"), &Curve2D::get_bake_interval);
  1050. ClassDB::bind_method(D_METHOD("get_baked_length"), &Curve2D::get_baked_length);
  1051. ClassDB::bind_method(D_METHOD("sample_baked", "offset", "cubic"), &Curve2D::sample_baked, DEFVAL(0.0), DEFVAL(false));
  1052. ClassDB::bind_method(D_METHOD("sample_baked_with_rotation", "offset", "cubic"), &Curve2D::sample_baked_with_rotation, DEFVAL(0.0), DEFVAL(false));
  1053. ClassDB::bind_method(D_METHOD("get_baked_points"), &Curve2D::get_baked_points);
  1054. ClassDB::bind_method(D_METHOD("get_closest_point", "to_point"), &Curve2D::get_closest_point);
  1055. ClassDB::bind_method(D_METHOD("get_closest_offset", "to_point"), &Curve2D::get_closest_offset);
  1056. ClassDB::bind_method(D_METHOD("tessellate", "max_stages", "tolerance_degrees"), &Curve2D::tessellate, DEFVAL(5), DEFVAL(4));
  1057. ClassDB::bind_method(D_METHOD("tessellate_even_length", "max_stages", "tolerance_length"), &Curve2D::tessellate_even_length, DEFVAL(5), DEFVAL(20.0));
  1058. ClassDB::bind_method(D_METHOD("_get_data"), &Curve2D::_get_data);
  1059. ClassDB::bind_method(D_METHOD("_set_data", "data"), &Curve2D::_set_data);
  1060. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "bake_interval", PROPERTY_HINT_RANGE, "0.01,512,0.01"), "set_bake_interval", "get_bake_interval");
  1061. ADD_PROPERTY(PropertyInfo(Variant::INT, "_data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "_set_data", "_get_data");
  1062. ADD_ARRAY_COUNT("Points", "point_count", "set_point_count", "get_point_count", "point_");
  1063. }
  1064. Curve2D::Curve2D() {}
  1065. /***********************************************************************************/
  1066. /***********************************************************************************/
  1067. /***********************************************************************************/
  1068. /***********************************************************************************/
  1069. /***********************************************************************************/
  1070. /***********************************************************************************/
  1071. int Curve3D::get_point_count() const {
  1072. return points.size();
  1073. }
  1074. void Curve3D::set_point_count(int p_count) {
  1075. ERR_FAIL_COND(p_count < 0);
  1076. if (points.size() >= p_count) {
  1077. points.resize(p_count);
  1078. mark_dirty();
  1079. } else {
  1080. for (int i = p_count - points.size(); i > 0; i--) {
  1081. _add_point(Vector3());
  1082. }
  1083. }
  1084. notify_property_list_changed();
  1085. }
  1086. void Curve3D::_add_point(const Vector3 &p_position, const Vector3 &p_in, const Vector3 &p_out, int p_atpos) {
  1087. Point n;
  1088. n.position = p_position;
  1089. n.in = p_in;
  1090. n.out = p_out;
  1091. if (p_atpos >= 0 && p_atpos < points.size()) {
  1092. points.insert(p_atpos, n);
  1093. } else {
  1094. points.push_back(n);
  1095. }
  1096. mark_dirty();
  1097. }
  1098. void Curve3D::add_point(const Vector3 &p_position, const Vector3 &p_in, const Vector3 &p_out, int p_atpos) {
  1099. _add_point(p_position, p_in, p_out, p_atpos);
  1100. notify_property_list_changed();
  1101. }
  1102. void Curve3D::set_point_position(int p_index, const Vector3 &p_position) {
  1103. ERR_FAIL_INDEX(p_index, points.size());
  1104. points.write[p_index].position = p_position;
  1105. mark_dirty();
  1106. }
  1107. Vector3 Curve3D::get_point_position(int p_index) const {
  1108. ERR_FAIL_INDEX_V(p_index, points.size(), Vector3());
  1109. return points[p_index].position;
  1110. }
  1111. void Curve3D::set_point_tilt(int p_index, real_t p_tilt) {
  1112. ERR_FAIL_INDEX(p_index, points.size());
  1113. points.write[p_index].tilt = p_tilt;
  1114. mark_dirty();
  1115. }
  1116. real_t Curve3D::get_point_tilt(int p_index) const {
  1117. ERR_FAIL_INDEX_V(p_index, points.size(), 0);
  1118. return points[p_index].tilt;
  1119. }
  1120. void Curve3D::set_point_in(int p_index, const Vector3 &p_in) {
  1121. ERR_FAIL_INDEX(p_index, points.size());
  1122. points.write[p_index].in = p_in;
  1123. mark_dirty();
  1124. }
  1125. Vector3 Curve3D::get_point_in(int p_index) const {
  1126. ERR_FAIL_INDEX_V(p_index, points.size(), Vector3());
  1127. return points[p_index].in;
  1128. }
  1129. void Curve3D::set_point_out(int p_index, const Vector3 &p_out) {
  1130. ERR_FAIL_INDEX(p_index, points.size());
  1131. points.write[p_index].out = p_out;
  1132. mark_dirty();
  1133. }
  1134. Vector3 Curve3D::get_point_out(int p_index) const {
  1135. ERR_FAIL_INDEX_V(p_index, points.size(), Vector3());
  1136. return points[p_index].out;
  1137. }
  1138. void Curve3D::_remove_point(int p_index) {
  1139. ERR_FAIL_INDEX(p_index, points.size());
  1140. points.remove_at(p_index);
  1141. mark_dirty();
  1142. }
  1143. void Curve3D::remove_point(int p_index) {
  1144. _remove_point(p_index);
  1145. notify_property_list_changed();
  1146. }
  1147. void Curve3D::clear_points() {
  1148. if (!points.is_empty()) {
  1149. points.clear();
  1150. mark_dirty();
  1151. notify_property_list_changed();
  1152. }
  1153. }
  1154. Vector3 Curve3D::sample(int p_index, real_t p_offset) const {
  1155. int pc = points.size();
  1156. ERR_FAIL_COND_V(pc == 0, Vector3());
  1157. if (p_index >= pc - 1) {
  1158. return points[pc - 1].position;
  1159. } else if (p_index < 0) {
  1160. return points[0].position;
  1161. }
  1162. Vector3 p0 = points[p_index].position;
  1163. Vector3 p1 = p0 + points[p_index].out;
  1164. Vector3 p3 = points[p_index + 1].position;
  1165. Vector3 p2 = p3 + points[p_index + 1].in;
  1166. return p0.bezier_interpolate(p1, p2, p3, p_offset);
  1167. }
  1168. Vector3 Curve3D::samplef(real_t p_findex) const {
  1169. if (p_findex < 0) {
  1170. p_findex = 0;
  1171. } else if (p_findex >= points.size()) {
  1172. p_findex = points.size();
  1173. }
  1174. return sample((int)p_findex, Math::fmod(p_findex, (real_t)1.0));
  1175. }
  1176. void Curve3D::mark_dirty() {
  1177. baked_cache_dirty = true;
  1178. emit_signal(CoreStringNames::get_singleton()->changed);
  1179. }
  1180. 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 {
  1181. real_t mp = p_begin + (p_end - p_begin) * 0.5;
  1182. Vector3 beg = p_a.bezier_interpolate(p_a + p_out, p_b + p_in, p_b, p_begin);
  1183. Vector3 mid = p_a.bezier_interpolate(p_a + p_out, p_b + p_in, p_b, mp);
  1184. Vector3 end = p_a.bezier_interpolate(p_a + p_out, p_b + p_in, p_b, p_end);
  1185. Vector3 na = (mid - beg).normalized();
  1186. Vector3 nb = (end - mid).normalized();
  1187. real_t dp = na.dot(nb);
  1188. if (dp < Math::cos(Math::deg_to_rad(p_tol))) {
  1189. r_bake[mp] = mid;
  1190. }
  1191. if (p_depth < p_max_depth) {
  1192. _bake_segment3d(r_bake, p_begin, mp, p_a, p_out, p_b, p_in, p_depth + 1, p_max_depth, p_tol);
  1193. _bake_segment3d(r_bake, mp, p_end, p_a, p_out, p_b, p_in, p_depth + 1, p_max_depth, p_tol);
  1194. }
  1195. }
  1196. 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 {
  1197. Vector3 beg = p_a.bezier_interpolate(p_a + p_out, p_b + p_in, p_b, p_begin);
  1198. Vector3 end = p_a.bezier_interpolate(p_a + p_out, p_b + p_in, p_b, p_end);
  1199. real_t length = beg.distance_to(end);
  1200. if (length > p_length && p_depth < p_max_depth) {
  1201. real_t mp = (p_begin + p_end) * 0.5;
  1202. Vector3 mid = p_a.bezier_interpolate(p_a + p_out, p_b + p_in, p_b, mp);
  1203. r_bake[mp] = mid;
  1204. _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);
  1205. _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);
  1206. }
  1207. }
  1208. void Curve3D::_bake() const {
  1209. if (!baked_cache_dirty) {
  1210. return;
  1211. }
  1212. baked_max_ofs = 0;
  1213. baked_cache_dirty = false;
  1214. if (points.size() == 0) {
  1215. baked_point_cache.clear();
  1216. baked_tilt_cache.clear();
  1217. baked_dist_cache.clear();
  1218. baked_forward_vector_cache.clear();
  1219. baked_up_vector_cache.clear();
  1220. return;
  1221. }
  1222. if (points.size() == 1) {
  1223. baked_point_cache.resize(1);
  1224. baked_point_cache.set(0, points[0].position);
  1225. baked_tilt_cache.resize(1);
  1226. baked_tilt_cache.set(0, points[0].tilt);
  1227. baked_dist_cache.resize(1);
  1228. baked_dist_cache.set(0, 0.0);
  1229. baked_forward_vector_cache.resize(1);
  1230. baked_forward_vector_cache.set(0, Vector3(0.0, 0.0, 1.0));
  1231. if (up_vector_enabled) {
  1232. baked_up_vector_cache.resize(1);
  1233. baked_up_vector_cache.set(0, Vector3(0.0, 1.0, 0.0));
  1234. } else {
  1235. baked_up_vector_cache.clear();
  1236. }
  1237. return;
  1238. }
  1239. // Step 1: Tesselate curve to (almost) even length segments
  1240. {
  1241. Vector<RBMap<real_t, Vector3>> midpoints = _tessellate_even_length(10, bake_interval);
  1242. int pc = 1;
  1243. for (int i = 0; i < points.size() - 1; i++) {
  1244. pc++;
  1245. pc += midpoints[i].size();
  1246. }
  1247. baked_point_cache.resize(pc);
  1248. baked_tilt_cache.resize(pc);
  1249. baked_dist_cache.resize(pc);
  1250. baked_forward_vector_cache.resize(pc);
  1251. Vector3 *bpw = baked_point_cache.ptrw();
  1252. real_t *btw = baked_tilt_cache.ptrw();
  1253. Vector3 *bfw = baked_forward_vector_cache.ptrw();
  1254. // Collect positions and sample tilts and tangents for each baked points.
  1255. bpw[0] = points[0].position;
  1256. 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();
  1257. btw[0] = points[0].tilt;
  1258. int pidx = 0;
  1259. for (int i = 0; i < points.size() - 1; i++) {
  1260. for (const KeyValue<real_t, Vector3> &E : midpoints[i]) {
  1261. pidx++;
  1262. bpw[pidx] = E.value;
  1263. 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();
  1264. btw[pidx] = Math::lerp(points[i].tilt, points[i + 1].tilt, E.key);
  1265. }
  1266. pidx++;
  1267. bpw[pidx] = points[i + 1].position;
  1268. 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();
  1269. btw[pidx] = points[i + 1].tilt;
  1270. }
  1271. // Recalculate the baked distances.
  1272. real_t *bdw = baked_dist_cache.ptrw();
  1273. bdw[0] = 0.0;
  1274. for (int i = 0; i < pc - 1; i++) {
  1275. bdw[i + 1] = bdw[i] + bpw[i].distance_to(bpw[i + 1]);
  1276. }
  1277. baked_max_ofs = bdw[pc - 1];
  1278. }
  1279. if (!up_vector_enabled) {
  1280. baked_up_vector_cache.resize(0);
  1281. return;
  1282. }
  1283. // Step 2: Calculate the up vectors and the whole local reference frame
  1284. //
  1285. // See Dougan, Carl. "The parallel transport frame." Game Programming Gems 2 (2001): 215-219.
  1286. // for an example discussing about why not the Frenet frame.
  1287. {
  1288. int point_count = baked_point_cache.size();
  1289. baked_up_vector_cache.resize(point_count);
  1290. Vector3 *up_write = baked_up_vector_cache.ptrw();
  1291. const Vector3 *forward_ptr = baked_forward_vector_cache.ptr();
  1292. const Vector3 *points_ptr = baked_point_cache.ptr();
  1293. Basis frame; // X-right, Y-up, Z-forward.
  1294. Basis frame_prev;
  1295. // Set the initial frame based on Y-up rule.
  1296. {
  1297. Vector3 forward = forward_ptr[0];
  1298. if (abs(forward.dot(Vector3(0, 1, 0))) > 1.0 - UNIT_EPSILON) {
  1299. frame_prev = Basis::looking_at(-forward, Vector3(1, 0, 0));
  1300. } else {
  1301. frame_prev = Basis::looking_at(-forward, Vector3(0, 1, 0));
  1302. }
  1303. up_write[0] = frame_prev.get_column(1);
  1304. }
  1305. // Calculate the Parallel Transport Frame.
  1306. for (int idx = 1; idx < point_count; idx++) {
  1307. Vector3 forward = forward_ptr[idx];
  1308. Basis rotate;
  1309. rotate.rotate_to_align(frame_prev.get_column(2), forward);
  1310. frame = rotate * frame_prev;
  1311. frame.orthonormalize(); // guard against float error accumulation
  1312. up_write[idx] = frame.get_column(1);
  1313. frame_prev = frame;
  1314. }
  1315. bool is_loop = true;
  1316. // Loop smoothing only applies when the curve is a loop, which means two ends meet, and share forward directions.
  1317. {
  1318. if (!points_ptr[0].is_equal_approx(points_ptr[point_count - 1])) {
  1319. is_loop = false;
  1320. }
  1321. real_t dot = forward_ptr[0].dot(forward_ptr[point_count - 1]);
  1322. if (dot < 1.0 - UNIT_EPSILON) { // Alignment should not be too tight, or it dosen't work for coarse bake interval.
  1323. is_loop = false;
  1324. }
  1325. }
  1326. // Twist up vectors, so that they align at two ends of the curve.
  1327. if (is_loop) {
  1328. const Vector3 up_start = up_write[0];
  1329. const Vector3 up_end = up_write[point_count - 1];
  1330. real_t sign = SIGN(up_end.cross(up_start).dot(forward_ptr[0]));
  1331. real_t full_angle = Quaternion(up_end, up_start).get_angle();
  1332. if (abs(full_angle) < CMP_EPSILON) {
  1333. return;
  1334. } else {
  1335. const real_t *dists = baked_dist_cache.ptr();
  1336. for (int idx = 1; idx < point_count; idx++) {
  1337. const real_t frac = dists[idx] / baked_max_ofs;
  1338. const real_t angle = Math::lerp((real_t)0.0, full_angle, frac);
  1339. Basis twist(forward_ptr[idx] * sign, angle);
  1340. up_write[idx] = twist.xform(up_write[idx]);
  1341. }
  1342. }
  1343. }
  1344. }
  1345. }
  1346. real_t Curve3D::get_baked_length() const {
  1347. if (baked_cache_dirty) {
  1348. _bake();
  1349. }
  1350. return baked_max_ofs;
  1351. }
  1352. Curve3D::Interval Curve3D::_find_interval(real_t p_offset) const {
  1353. Interval interval = {
  1354. -1,
  1355. 0.0
  1356. };
  1357. ERR_FAIL_COND_V_MSG(baked_cache_dirty, interval, "Backed cache is dirty");
  1358. int pc = baked_point_cache.size();
  1359. ERR_FAIL_COND_V_MSG(pc < 2, interval, "Less than two points in cache");
  1360. int start = 0;
  1361. int end = pc;
  1362. int idx = (end + start) / 2;
  1363. // Binary search to find baked points.
  1364. while (start < idx) {
  1365. real_t offset = baked_dist_cache[idx];
  1366. if (p_offset <= offset) {
  1367. end = idx;
  1368. } else {
  1369. start = idx;
  1370. }
  1371. idx = (end + start) / 2;
  1372. }
  1373. real_t offset_begin = baked_dist_cache[idx];
  1374. real_t offset_end = baked_dist_cache[idx + 1];
  1375. real_t idx_interval = offset_end - offset_begin;
  1376. ERR_FAIL_COND_V_MSG(p_offset < offset_begin || p_offset > offset_end, interval, "Offset out of range.");
  1377. interval.idx = idx;
  1378. if (idx_interval < FLT_EPSILON) {
  1379. interval.frac = 0.5; // For a very short interval, 0.5 is a reasonable choice.
  1380. ERR_FAIL_V_MSG(interval, "Zero length interval.");
  1381. }
  1382. interval.frac = (p_offset - offset_begin) / idx_interval;
  1383. return interval;
  1384. }
  1385. Vector3 Curve3D::_sample_baked(Interval p_interval, bool p_cubic) const {
  1386. // Assuming p_interval is valid.
  1387. ERR_FAIL_INDEX_V_MSG(p_interval.idx, baked_point_cache.size(), Vector3(), "Invalid interval");
  1388. int idx = p_interval.idx;
  1389. real_t frac = p_interval.frac;
  1390. const Vector3 *r = baked_point_cache.ptr();
  1391. int pc = baked_point_cache.size();
  1392. if (p_cubic) {
  1393. Vector3 pre = idx > 0 ? r[idx - 1] : r[idx];
  1394. Vector3 post = (idx < (pc - 2)) ? r[idx + 2] : r[idx + 1];
  1395. return r[idx].cubic_interpolate(r[idx + 1], pre, post, frac);
  1396. } else {
  1397. return r[idx].lerp(r[idx + 1], frac);
  1398. }
  1399. }
  1400. real_t Curve3D::_sample_baked_tilt(Interval p_interval) const {
  1401. // Assuming that p_interval is valid.
  1402. ERR_FAIL_INDEX_V_MSG(p_interval.idx, baked_tilt_cache.size(), 0.0, "Invalid interval");
  1403. int idx = p_interval.idx;
  1404. real_t frac = p_interval.frac;
  1405. const real_t *r = baked_tilt_cache.ptr();
  1406. return Math::lerp(r[idx], r[idx + 1], frac);
  1407. }
  1408. Basis Curve3D::_sample_posture(Interval p_interval, bool p_apply_tilt) const {
  1409. // Assuming that p_interval is valid.
  1410. ERR_FAIL_INDEX_V_MSG(p_interval.idx, baked_point_cache.size(), Basis(), "Invalid interval");
  1411. if (up_vector_enabled) {
  1412. ERR_FAIL_INDEX_V_MSG(p_interval.idx, baked_up_vector_cache.size(), Basis(), "Invalid interval");
  1413. }
  1414. int idx = p_interval.idx;
  1415. real_t frac = p_interval.frac;
  1416. Vector3 forward_begin = baked_forward_vector_cache[idx];
  1417. Vector3 forward_end = baked_forward_vector_cache[idx + 1];
  1418. Vector3 up_begin;
  1419. Vector3 up_end;
  1420. if (up_vector_enabled) {
  1421. up_begin = baked_up_vector_cache[idx];
  1422. up_end = baked_up_vector_cache[idx + 1];
  1423. } else {
  1424. up_begin = Vector3(0.0, 1.0, 0.0);
  1425. up_end = Vector3(0.0, 1.0, 0.0);
  1426. }
  1427. // Build frames at both ends of the interval, then interpolate.
  1428. const Basis frame_begin = Basis::looking_at(-forward_begin, up_begin);
  1429. const Basis frame_end = Basis::looking_at(-forward_end, up_end);
  1430. const Basis frame = frame_begin.slerp(frame_end, frac).orthonormalized();
  1431. if (!p_apply_tilt) {
  1432. return frame;
  1433. }
  1434. // Applying tilt.
  1435. const real_t tilt = _sample_baked_tilt(p_interval);
  1436. Vector3 forward = frame.get_column(2);
  1437. const Basis twist(forward, tilt);
  1438. return twist * frame;
  1439. }
  1440. Vector3 Curve3D::sample_baked(real_t p_offset, bool p_cubic) const {
  1441. if (baked_cache_dirty) {
  1442. _bake();
  1443. }
  1444. // Validate: Curve may not have baked points.
  1445. int pc = baked_point_cache.size();
  1446. ERR_FAIL_COND_V_MSG(pc == 0, Vector3(), "No points in Curve3D.");
  1447. if (pc == 1) {
  1448. return baked_point_cache[0];
  1449. }
  1450. p_offset = CLAMP(p_offset, 0.0, get_baked_length()); // PathFollower implement wrapping logic.
  1451. Curve3D::Interval interval = _find_interval(p_offset);
  1452. return _sample_baked(interval, p_cubic);
  1453. }
  1454. Transform3D Curve3D::sample_baked_with_rotation(real_t p_offset, bool p_cubic, bool p_apply_tilt) const {
  1455. if (baked_cache_dirty) {
  1456. _bake();
  1457. }
  1458. // Validate: Curve may not have baked points.
  1459. const int point_count = baked_point_cache.size();
  1460. ERR_FAIL_COND_V_MSG(point_count == 0, Transform3D(), "No points in Curve3D.");
  1461. if (point_count == 1) {
  1462. Transform3D t;
  1463. t.origin = baked_point_cache.get(0);
  1464. ERR_FAIL_V_MSG(t, "Only 1 point in Curve3D.");
  1465. }
  1466. p_offset = CLAMP(p_offset, 0.0, get_baked_length()); // PathFollower implement wrapping logic.
  1467. // 0. Find interval for all sampling steps.
  1468. Curve3D::Interval interval = _find_interval(p_offset);
  1469. // 1. Sample position.
  1470. Vector3 pos = _sample_baked(interval, p_cubic);
  1471. // 2. Sample rotation frame.
  1472. Basis frame = _sample_posture(interval, p_apply_tilt);
  1473. return Transform3D(frame, pos);
  1474. }
  1475. real_t Curve3D::sample_baked_tilt(real_t p_offset) const {
  1476. if (baked_cache_dirty) {
  1477. _bake();
  1478. }
  1479. // Validate: Curve may not have baked tilts.
  1480. int pc = baked_tilt_cache.size();
  1481. ERR_FAIL_COND_V_MSG(pc == 0, 0, "No tilts in Curve3D.");
  1482. if (pc == 1) {
  1483. return baked_tilt_cache.get(0);
  1484. }
  1485. p_offset = CLAMP(p_offset, 0.0, get_baked_length()); // PathFollower implement wrapping logic
  1486. Curve3D::Interval interval = _find_interval(p_offset);
  1487. return _sample_baked_tilt(interval);
  1488. }
  1489. Vector3 Curve3D::sample_baked_up_vector(real_t p_offset, bool p_apply_tilt) const {
  1490. if (baked_cache_dirty) {
  1491. _bake();
  1492. }
  1493. // Validate: Curve may not have baked up vectors.
  1494. ERR_FAIL_COND_V_MSG(!up_vector_enabled, Vector3(0, 1, 0), "No up vectors in Curve3D.");
  1495. int count = baked_up_vector_cache.size();
  1496. if (count == 1) {
  1497. return baked_up_vector_cache.get(0);
  1498. }
  1499. p_offset = CLAMP(p_offset, 0.0, get_baked_length()); // PathFollower implement wrapping logic.
  1500. Curve3D::Interval interval = _find_interval(p_offset);
  1501. return _sample_posture(interval, p_apply_tilt).get_column(1);
  1502. }
  1503. PackedVector3Array Curve3D::get_baked_points() const {
  1504. if (baked_cache_dirty) {
  1505. _bake();
  1506. }
  1507. return baked_point_cache;
  1508. }
  1509. Vector<real_t> Curve3D::get_baked_tilts() const {
  1510. if (baked_cache_dirty) {
  1511. _bake();
  1512. }
  1513. return baked_tilt_cache;
  1514. }
  1515. PackedVector3Array Curve3D::get_baked_up_vectors() const {
  1516. if (baked_cache_dirty) {
  1517. _bake();
  1518. }
  1519. return baked_up_vector_cache;
  1520. }
  1521. Vector3 Curve3D::get_closest_point(const Vector3 &p_to_point) const {
  1522. // Brute force method.
  1523. if (baked_cache_dirty) {
  1524. _bake();
  1525. }
  1526. // Validate: Curve may not have baked points.
  1527. int pc = baked_point_cache.size();
  1528. ERR_FAIL_COND_V_MSG(pc == 0, Vector3(), "No points in Curve3D.");
  1529. if (pc == 1) {
  1530. return baked_point_cache.get(0);
  1531. }
  1532. const Vector3 *r = baked_point_cache.ptr();
  1533. Vector3 nearest;
  1534. real_t nearest_dist = -1.0f;
  1535. for (int i = 0; i < pc - 1; i++) {
  1536. const real_t interval = baked_dist_cache[i + 1] - baked_dist_cache[i];
  1537. Vector3 origin = r[i];
  1538. Vector3 direction = (r[i + 1] - origin) / interval;
  1539. real_t d = CLAMP((p_to_point - origin).dot(direction), 0.0f, interval);
  1540. Vector3 proj = origin + direction * d;
  1541. real_t dist = proj.distance_squared_to(p_to_point);
  1542. if (nearest_dist < 0.0f || dist < nearest_dist) {
  1543. nearest = proj;
  1544. nearest_dist = dist;
  1545. }
  1546. }
  1547. return nearest;
  1548. }
  1549. real_t Curve3D::get_closest_offset(const Vector3 &p_to_point) const {
  1550. // Brute force method.
  1551. if (baked_cache_dirty) {
  1552. _bake();
  1553. }
  1554. // Validate: Curve may not have baked points.
  1555. int pc = baked_point_cache.size();
  1556. ERR_FAIL_COND_V_MSG(pc == 0, 0.0f, "No points in Curve3D.");
  1557. if (pc == 1) {
  1558. return 0.0f;
  1559. }
  1560. const Vector3 *r = baked_point_cache.ptr();
  1561. real_t nearest = 0.0f;
  1562. real_t nearest_dist = -1.0f;
  1563. real_t offset;
  1564. for (int i = 0; i < pc - 1; i++) {
  1565. offset = baked_dist_cache[i];
  1566. const real_t interval = baked_dist_cache[i + 1] - baked_dist_cache[i];
  1567. Vector3 origin = r[i];
  1568. Vector3 direction = (r[i + 1] - origin) / interval;
  1569. real_t d = CLAMP((p_to_point - origin).dot(direction), 0.0f, interval);
  1570. Vector3 proj = origin + direction * d;
  1571. real_t dist = proj.distance_squared_to(p_to_point);
  1572. if (nearest_dist < 0.0f || dist < nearest_dist) {
  1573. nearest = offset + d;
  1574. nearest_dist = dist;
  1575. }
  1576. }
  1577. return nearest;
  1578. }
  1579. void Curve3D::set_bake_interval(real_t p_tolerance) {
  1580. bake_interval = p_tolerance;
  1581. mark_dirty();
  1582. }
  1583. real_t Curve3D::get_bake_interval() const {
  1584. return bake_interval;
  1585. }
  1586. void Curve3D::set_up_vector_enabled(bool p_enable) {
  1587. up_vector_enabled = p_enable;
  1588. mark_dirty();
  1589. }
  1590. bool Curve3D::is_up_vector_enabled() const {
  1591. return up_vector_enabled;
  1592. }
  1593. Dictionary Curve3D::_get_data() const {
  1594. Dictionary dc;
  1595. PackedVector3Array d;
  1596. d.resize(points.size() * 3);
  1597. Vector3 *w = d.ptrw();
  1598. Vector<real_t> t;
  1599. t.resize(points.size());
  1600. real_t *wt = t.ptrw();
  1601. for (int i = 0; i < points.size(); i++) {
  1602. w[i * 3 + 0] = points[i].in;
  1603. w[i * 3 + 1] = points[i].out;
  1604. w[i * 3 + 2] = points[i].position;
  1605. wt[i] = points[i].tilt;
  1606. }
  1607. dc["points"] = d;
  1608. dc["tilts"] = t;
  1609. return dc;
  1610. }
  1611. void Curve3D::_set_data(const Dictionary &p_data) {
  1612. ERR_FAIL_COND(!p_data.has("points"));
  1613. ERR_FAIL_COND(!p_data.has("tilts"));
  1614. PackedVector3Array rp = p_data["points"];
  1615. int pc = rp.size();
  1616. ERR_FAIL_COND(pc % 3 != 0);
  1617. points.resize(pc / 3);
  1618. const Vector3 *r = rp.ptr();
  1619. Vector<real_t> rtl = p_data["tilts"];
  1620. const real_t *rt = rtl.ptr();
  1621. for (int i = 0; i < points.size(); i++) {
  1622. points.write[i].in = r[i * 3 + 0];
  1623. points.write[i].out = r[i * 3 + 1];
  1624. points.write[i].position = r[i * 3 + 2];
  1625. points.write[i].tilt = rt[i];
  1626. }
  1627. mark_dirty();
  1628. notify_property_list_changed();
  1629. }
  1630. PackedVector3Array Curve3D::tessellate(int p_max_stages, real_t p_tolerance) const {
  1631. PackedVector3Array tess;
  1632. if (points.size() == 0) {
  1633. return tess;
  1634. }
  1635. Vector<RBMap<real_t, Vector3>> midpoints;
  1636. midpoints.resize(points.size() - 1);
  1637. int pc = 1;
  1638. for (int i = 0; i < points.size() - 1; i++) {
  1639. _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);
  1640. pc++;
  1641. pc += midpoints[i].size();
  1642. }
  1643. tess.resize(pc);
  1644. Vector3 *bpw = tess.ptrw();
  1645. bpw[0] = points[0].position;
  1646. int pidx = 0;
  1647. for (int i = 0; i < points.size() - 1; i++) {
  1648. for (const KeyValue<real_t, Vector3> &E : midpoints[i]) {
  1649. pidx++;
  1650. bpw[pidx] = E.value;
  1651. }
  1652. pidx++;
  1653. bpw[pidx] = points[i + 1].position;
  1654. }
  1655. return tess;
  1656. }
  1657. Vector<RBMap<real_t, Vector3>> Curve3D::_tessellate_even_length(int p_max_stages, real_t p_length) const {
  1658. Vector<RBMap<real_t, Vector3>> midpoints;
  1659. ERR_FAIL_COND_V_MSG(points.size() < 2, midpoints, "Curve must have at least 2 control point");
  1660. midpoints.resize(points.size() - 1);
  1661. for (int i = 0; i < points.size() - 1; i++) {
  1662. _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);
  1663. }
  1664. return midpoints;
  1665. }
  1666. PackedVector3Array Curve3D::tessellate_even_length(int p_max_stages, real_t p_length) const {
  1667. PackedVector3Array tess;
  1668. Vector<RBMap<real_t, Vector3>> midpoints = _tessellate_even_length(p_max_stages, p_length);
  1669. if (midpoints.size() == 0) {
  1670. return tess;
  1671. }
  1672. int pc = 1;
  1673. for (int i = 0; i < points.size() - 1; i++) {
  1674. pc++;
  1675. pc += midpoints[i].size();
  1676. }
  1677. tess.resize(pc);
  1678. Vector3 *bpw = tess.ptrw();
  1679. bpw[0] = points[0].position;
  1680. int pidx = 0;
  1681. for (int i = 0; i < points.size() - 1; i++) {
  1682. for (const KeyValue<real_t, Vector3> &E : midpoints[i]) {
  1683. pidx++;
  1684. bpw[pidx] = E.value;
  1685. }
  1686. pidx++;
  1687. bpw[pidx] = points[i + 1].position;
  1688. }
  1689. return tess;
  1690. }
  1691. bool Curve3D::_set(const StringName &p_name, const Variant &p_value) {
  1692. Vector<String> components = String(p_name).split("/", true, 2);
  1693. if (components.size() >= 2 && components[0].begins_with("point_") && components[0].trim_prefix("point_").is_valid_int()) {
  1694. int point_index = components[0].trim_prefix("point_").to_int();
  1695. String property = components[1];
  1696. if (property == "position") {
  1697. set_point_position(point_index, p_value);
  1698. return true;
  1699. } else if (property == "in") {
  1700. set_point_in(point_index, p_value);
  1701. return true;
  1702. } else if (property == "out") {
  1703. set_point_out(point_index, p_value);
  1704. return true;
  1705. } else if (property == "tilt") {
  1706. set_point_tilt(point_index, p_value);
  1707. return true;
  1708. }
  1709. }
  1710. return false;
  1711. }
  1712. bool Curve3D::_get(const StringName &p_name, Variant &r_ret) const {
  1713. Vector<String> components = String(p_name).split("/", true, 2);
  1714. if (components.size() >= 2 && components[0].begins_with("point_") && components[0].trim_prefix("point_").is_valid_int()) {
  1715. int point_index = components[0].trim_prefix("point_").to_int();
  1716. String property = components[1];
  1717. if (property == "position") {
  1718. r_ret = get_point_position(point_index);
  1719. return true;
  1720. } else if (property == "in") {
  1721. r_ret = get_point_in(point_index);
  1722. return true;
  1723. } else if (property == "out") {
  1724. r_ret = get_point_out(point_index);
  1725. return true;
  1726. } else if (property == "tilt") {
  1727. r_ret = get_point_tilt(point_index);
  1728. return true;
  1729. }
  1730. }
  1731. return false;
  1732. }
  1733. void Curve3D::_get_property_list(List<PropertyInfo> *p_list) const {
  1734. for (int i = 0; i < points.size(); i++) {
  1735. PropertyInfo pi = PropertyInfo(Variant::VECTOR3, vformat("point_%d/position", i));
  1736. pi.usage &= ~PROPERTY_USAGE_STORAGE;
  1737. p_list->push_back(pi);
  1738. if (i != 0) {
  1739. pi = PropertyInfo(Variant::VECTOR3, vformat("point_%d/in", i));
  1740. pi.usage &= ~PROPERTY_USAGE_STORAGE;
  1741. p_list->push_back(pi);
  1742. }
  1743. if (i != points.size() - 1) {
  1744. pi = PropertyInfo(Variant::VECTOR3, vformat("point_%d/out", i));
  1745. pi.usage &= ~PROPERTY_USAGE_STORAGE;
  1746. p_list->push_back(pi);
  1747. }
  1748. pi = PropertyInfo(Variant::FLOAT, vformat("point_%d/tilt", i));
  1749. pi.usage &= ~PROPERTY_USAGE_STORAGE;
  1750. p_list->push_back(pi);
  1751. }
  1752. }
  1753. void Curve3D::_bind_methods() {
  1754. ClassDB::bind_method(D_METHOD("get_point_count"), &Curve3D::get_point_count);
  1755. ClassDB::bind_method(D_METHOD("set_point_count", "count"), &Curve3D::set_point_count);
  1756. ClassDB::bind_method(D_METHOD("add_point", "position", "in", "out", "index"), &Curve3D::add_point, DEFVAL(Vector3()), DEFVAL(Vector3()), DEFVAL(-1));
  1757. ClassDB::bind_method(D_METHOD("set_point_position", "idx", "position"), &Curve3D::set_point_position);
  1758. ClassDB::bind_method(D_METHOD("get_point_position", "idx"), &Curve3D::get_point_position);
  1759. ClassDB::bind_method(D_METHOD("set_point_tilt", "idx", "tilt"), &Curve3D::set_point_tilt);
  1760. ClassDB::bind_method(D_METHOD("get_point_tilt", "idx"), &Curve3D::get_point_tilt);
  1761. ClassDB::bind_method(D_METHOD("set_point_in", "idx", "position"), &Curve3D::set_point_in);
  1762. ClassDB::bind_method(D_METHOD("get_point_in", "idx"), &Curve3D::get_point_in);
  1763. ClassDB::bind_method(D_METHOD("set_point_out", "idx", "position"), &Curve3D::set_point_out);
  1764. ClassDB::bind_method(D_METHOD("get_point_out", "idx"), &Curve3D::get_point_out);
  1765. ClassDB::bind_method(D_METHOD("remove_point", "idx"), &Curve3D::remove_point);
  1766. ClassDB::bind_method(D_METHOD("clear_points"), &Curve3D::clear_points);
  1767. ClassDB::bind_method(D_METHOD("sample", "idx", "t"), &Curve3D::sample);
  1768. ClassDB::bind_method(D_METHOD("samplef", "fofs"), &Curve3D::samplef);
  1769. //ClassDB::bind_method(D_METHOD("bake","subdivs"),&Curve3D::bake,DEFVAL(10));
  1770. ClassDB::bind_method(D_METHOD("set_bake_interval", "distance"), &Curve3D::set_bake_interval);
  1771. ClassDB::bind_method(D_METHOD("get_bake_interval"), &Curve3D::get_bake_interval);
  1772. ClassDB::bind_method(D_METHOD("set_up_vector_enabled", "enable"), &Curve3D::set_up_vector_enabled);
  1773. ClassDB::bind_method(D_METHOD("is_up_vector_enabled"), &Curve3D::is_up_vector_enabled);
  1774. ClassDB::bind_method(D_METHOD("get_baked_length"), &Curve3D::get_baked_length);
  1775. ClassDB::bind_method(D_METHOD("sample_baked", "offset", "cubic"), &Curve3D::sample_baked, DEFVAL(0.0), DEFVAL(false));
  1776. 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));
  1777. ClassDB::bind_method(D_METHOD("sample_baked_up_vector", "offset", "apply_tilt"), &Curve3D::sample_baked_up_vector, DEFVAL(false));
  1778. ClassDB::bind_method(D_METHOD("get_baked_points"), &Curve3D::get_baked_points);
  1779. ClassDB::bind_method(D_METHOD("get_baked_tilts"), &Curve3D::get_baked_tilts);
  1780. ClassDB::bind_method(D_METHOD("get_baked_up_vectors"), &Curve3D::get_baked_up_vectors);
  1781. ClassDB::bind_method(D_METHOD("get_closest_point", "to_point"), &Curve3D::get_closest_point);
  1782. ClassDB::bind_method(D_METHOD("get_closest_offset", "to_point"), &Curve3D::get_closest_offset);
  1783. ClassDB::bind_method(D_METHOD("tessellate", "max_stages", "tolerance_degrees"), &Curve3D::tessellate, DEFVAL(5), DEFVAL(4));
  1784. ClassDB::bind_method(D_METHOD("tessellate_even_length", "max_stages", "tolerance_length"), &Curve3D::tessellate_even_length, DEFVAL(5), DEFVAL(0.2));
  1785. ClassDB::bind_method(D_METHOD("_get_data"), &Curve3D::_get_data);
  1786. ClassDB::bind_method(D_METHOD("_set_data", "data"), &Curve3D::_set_data);
  1787. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "bake_interval", PROPERTY_HINT_RANGE, "0.01,512,0.01"), "set_bake_interval", "get_bake_interval");
  1788. ADD_PROPERTY(PropertyInfo(Variant::INT, "_data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "_set_data", "_get_data");
  1789. ADD_ARRAY_COUNT("Points", "point_count", "set_point_count", "get_point_count", "point_");
  1790. ADD_GROUP("Up Vector", "up_vector_");
  1791. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "up_vector_enabled"), "set_up_vector_enabled", "is_up_vector_enabled");
  1792. }
  1793. Curve3D::Curve3D() {}