curve.cpp 58 KB

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