curve.cpp 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677
  1. /**************************************************************************/
  2. /* curve.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "curve.h"
  31. #include "core/core_string_names.h"
  32. template <class T>
  33. static _FORCE_INLINE_ T _bezier_interp(real_t t, T start, T control_1, T control_2, T end) {
  34. /* Formula from Wikipedia article on Bezier curves. */
  35. real_t omt = (1.0 - t);
  36. real_t omt2 = omt * omt;
  37. real_t omt3 = omt2 * omt;
  38. real_t t2 = t * t;
  39. real_t t3 = t2 * t;
  40. return start * omt3 + control_1 * omt2 * t * 3.0 + control_2 * omt * t2 * 3.0 + end * t3;
  41. }
  42. const char *Curve::SIGNAL_RANGE_CHANGED = "range_changed";
  43. Curve::Curve() {
  44. _bake_resolution = 100;
  45. _baked_cache_dirty = false;
  46. _min_value = 0;
  47. _max_value = 1;
  48. _minmax_set_once = 0b00;
  49. }
  50. int Curve::add_point(Vector2 p_pos, real_t left_tangent, real_t right_tangent, TangentMode left_mode, TangentMode right_mode) {
  51. // Add a point and preserve order
  52. // Curve bounds is in 0..1
  53. if (p_pos.x > MAX_X) {
  54. p_pos.x = MAX_X;
  55. } else if (p_pos.x < MIN_X) {
  56. p_pos.x = MIN_X;
  57. }
  58. int ret = -1;
  59. if (_points.size() == 0) {
  60. _points.push_back(Point(p_pos, left_tangent, right_tangent, left_mode, right_mode));
  61. ret = 0;
  62. } else if (_points.size() == 1) {
  63. // TODO Is the `else` able to handle this block already?
  64. real_t diff = p_pos.x - _points[0].pos.x;
  65. if (diff > 0) {
  66. _points.push_back(Point(p_pos, left_tangent, right_tangent, left_mode, right_mode));
  67. ret = 1;
  68. } else {
  69. _points.insert(0, Point(p_pos, left_tangent, right_tangent, left_mode, right_mode));
  70. ret = 0;
  71. }
  72. } else {
  73. int i = get_index(p_pos.x);
  74. if (i == 0 && p_pos.x < _points[0].pos.x) {
  75. // Insert before anything else
  76. _points.insert(0, Point(p_pos, left_tangent, right_tangent, left_mode, right_mode));
  77. ret = 0;
  78. } else {
  79. // Insert between i and i+1
  80. ++i;
  81. _points.insert(i, Point(p_pos, left_tangent, right_tangent, left_mode, right_mode));
  82. ret = i;
  83. }
  84. }
  85. update_auto_tangents(ret);
  86. mark_dirty();
  87. return ret;
  88. }
  89. int Curve::get_index(real_t offset) const {
  90. // Lower-bound float binary search
  91. int imin = 0;
  92. int imax = _points.size() - 1;
  93. while (imax - imin > 1) {
  94. int m = (imin + imax) / 2;
  95. real_t a = _points[m].pos.x;
  96. real_t b = _points[m + 1].pos.x;
  97. if (a < offset && b < offset) {
  98. imin = m;
  99. } else if (a > offset) {
  100. imax = m;
  101. } else {
  102. return m;
  103. }
  104. }
  105. // Will happen if the offset is out of bounds
  106. if (offset > _points[imax].pos.x) {
  107. return imax;
  108. }
  109. return imin;
  110. }
  111. void Curve::clean_dupes() {
  112. bool dirty = false;
  113. for (int i = 1; i < _points.size(); ++i) {
  114. real_t diff = _points[i - 1].pos.x - _points[i].pos.x;
  115. if (diff <= CMP_EPSILON) {
  116. _points.remove(i);
  117. --i;
  118. dirty = true;
  119. }
  120. }
  121. if (dirty) {
  122. mark_dirty();
  123. }
  124. }
  125. void Curve::set_point_left_tangent(int i, real_t tangent) {
  126. ERR_FAIL_INDEX(i, _points.size());
  127. _points.write[i].left_tangent = tangent;
  128. _points.write[i].left_mode = TANGENT_FREE;
  129. mark_dirty();
  130. }
  131. void Curve::set_point_right_tangent(int i, real_t tangent) {
  132. ERR_FAIL_INDEX(i, _points.size());
  133. _points.write[i].right_tangent = tangent;
  134. _points.write[i].right_mode = TANGENT_FREE;
  135. mark_dirty();
  136. }
  137. void Curve::set_point_left_mode(int i, TangentMode p_mode) {
  138. ERR_FAIL_INDEX(i, _points.size());
  139. _points.write[i].left_mode = p_mode;
  140. if (i > 0) {
  141. if (p_mode == TANGENT_LINEAR) {
  142. Vector2 v = (_points[i - 1].pos - _points[i].pos).normalized();
  143. _points.write[i].left_tangent = v.y / v.x;
  144. }
  145. }
  146. mark_dirty();
  147. }
  148. void Curve::set_point_right_mode(int i, TangentMode p_mode) {
  149. ERR_FAIL_INDEX(i, _points.size());
  150. _points.write[i].right_mode = p_mode;
  151. if (i + 1 < _points.size()) {
  152. if (p_mode == TANGENT_LINEAR) {
  153. Vector2 v = (_points[i + 1].pos - _points[i].pos).normalized();
  154. _points.write[i].right_tangent = v.y / v.x;
  155. }
  156. }
  157. mark_dirty();
  158. }
  159. real_t Curve::get_point_left_tangent(int i) const {
  160. ERR_FAIL_INDEX_V(i, _points.size(), 0);
  161. return _points[i].left_tangent;
  162. }
  163. real_t Curve::get_point_right_tangent(int i) const {
  164. ERR_FAIL_INDEX_V(i, _points.size(), 0);
  165. return _points[i].right_tangent;
  166. }
  167. Curve::TangentMode Curve::get_point_left_mode(int i) const {
  168. ERR_FAIL_INDEX_V(i, _points.size(), TANGENT_FREE);
  169. return _points[i].left_mode;
  170. }
  171. Curve::TangentMode Curve::get_point_right_mode(int i) const {
  172. ERR_FAIL_INDEX_V(i, _points.size(), TANGENT_FREE);
  173. return _points[i].right_mode;
  174. }
  175. void Curve::remove_point(int p_index) {
  176. ERR_FAIL_INDEX(p_index, _points.size());
  177. _points.remove(p_index);
  178. mark_dirty();
  179. }
  180. void Curve::clear_points() {
  181. _points.clear();
  182. mark_dirty();
  183. }
  184. void Curve::set_point_value(int p_index, real_t pos) {
  185. ERR_FAIL_INDEX(p_index, _points.size());
  186. _points.write[p_index].pos.y = pos;
  187. update_auto_tangents(p_index);
  188. mark_dirty();
  189. }
  190. int Curve::set_point_offset(int p_index, float offset) {
  191. ERR_FAIL_INDEX_V(p_index, _points.size(), -1);
  192. Point p = _points[p_index];
  193. remove_point(p_index);
  194. int i = add_point(Vector2(offset, p.pos.y));
  195. _points.write[i].left_tangent = p.left_tangent;
  196. _points.write[i].right_tangent = p.right_tangent;
  197. _points.write[i].left_mode = p.left_mode;
  198. _points.write[i].right_mode = p.right_mode;
  199. if (p_index != i) {
  200. update_auto_tangents(p_index);
  201. }
  202. update_auto_tangents(i);
  203. return i;
  204. }
  205. Vector2 Curve::get_point_position(int p_index) const {
  206. ERR_FAIL_INDEX_V(p_index, _points.size(), Vector2(0, 0));
  207. return _points[p_index].pos;
  208. }
  209. Curve::Point Curve::get_point(int p_index) const {
  210. ERR_FAIL_INDEX_V(p_index, _points.size(), Point());
  211. return _points[p_index];
  212. }
  213. void Curve::update_auto_tangents(int i) {
  214. Point &p = _points.write[i];
  215. if (i > 0) {
  216. if (p.left_mode == TANGENT_LINEAR) {
  217. Vector2 v = (_points[i - 1].pos - p.pos).normalized();
  218. p.left_tangent = v.y / v.x;
  219. }
  220. if (_points[i - 1].right_mode == TANGENT_LINEAR) {
  221. Vector2 v = (_points[i - 1].pos - p.pos).normalized();
  222. _points.write[i - 1].right_tangent = v.y / v.x;
  223. }
  224. }
  225. if (i + 1 < _points.size()) {
  226. if (p.right_mode == TANGENT_LINEAR) {
  227. Vector2 v = (_points[i + 1].pos - p.pos).normalized();
  228. p.right_tangent = v.y / v.x;
  229. }
  230. if (_points[i + 1].left_mode == TANGENT_LINEAR) {
  231. Vector2 v = (_points[i + 1].pos - p.pos).normalized();
  232. _points.write[i + 1].left_tangent = v.y / v.x;
  233. }
  234. }
  235. }
  236. #define MIN_Y_RANGE 0.01
  237. void Curve::set_min_value(float p_min) {
  238. if (_minmax_set_once & 0b11 && p_min > _max_value - MIN_Y_RANGE) {
  239. _min_value = _max_value - MIN_Y_RANGE;
  240. } else {
  241. _minmax_set_once |= 0b10; // first bit is "min set"
  242. _min_value = p_min;
  243. }
  244. // Note: min and max are indicative values,
  245. // it's still possible that existing points are out of range at this point.
  246. emit_signal(SIGNAL_RANGE_CHANGED);
  247. }
  248. void Curve::set_max_value(float p_max) {
  249. if (_minmax_set_once & 0b11 && p_max < _min_value + MIN_Y_RANGE) {
  250. _max_value = _min_value + MIN_Y_RANGE;
  251. } else {
  252. _minmax_set_once |= 0b01; // second bit is "max set"
  253. _max_value = p_max;
  254. }
  255. emit_signal(SIGNAL_RANGE_CHANGED);
  256. }
  257. real_t Curve::interpolate(real_t offset) const {
  258. if (_points.size() == 0) {
  259. return 0;
  260. }
  261. if (_points.size() == 1) {
  262. return _points[0].pos.y;
  263. }
  264. int i = get_index(offset);
  265. if (i == _points.size() - 1) {
  266. return _points[i].pos.y;
  267. }
  268. real_t local = offset - _points[i].pos.x;
  269. if (i == 0 && local <= 0) {
  270. return _points[0].pos.y;
  271. }
  272. return interpolate_local_nocheck(i, local);
  273. }
  274. real_t Curve::interpolate_local_nocheck(int index, real_t local_offset) const {
  275. const Point a = _points[index];
  276. const Point b = _points[index + 1];
  277. /* Cubic bezier
  278. *
  279. * ac-----bc
  280. * / \
  281. * / \ Here with a.right_tangent > 0
  282. * / \ and b.left_tangent < 0
  283. * / \
  284. * a b
  285. *
  286. * |-d1--|-d2--|-d3--|
  287. *
  288. * d1 == d2 == d3 == d / 3
  289. */
  290. // Control points are chosen at equal distances
  291. real_t d = b.pos.x - a.pos.x;
  292. if (Math::abs(d) <= CMP_EPSILON) {
  293. return b.pos.y;
  294. }
  295. local_offset /= d;
  296. d /= 3.0;
  297. real_t yac = a.pos.y + d * a.right_tangent;
  298. real_t ybc = b.pos.y - d * b.left_tangent;
  299. real_t y = _bezier_interp(local_offset, a.pos.y, yac, ybc, b.pos.y);
  300. return y;
  301. }
  302. void Curve::mark_dirty() {
  303. _baked_cache_dirty = true;
  304. emit_signal(CoreStringNames::get_singleton()->changed);
  305. }
  306. Array Curve::get_data() const {
  307. Array output;
  308. const unsigned int ELEMS = 5;
  309. output.resize(_points.size() * ELEMS);
  310. for (int j = 0; j < _points.size(); ++j) {
  311. const Point p = _points[j];
  312. int i = j * ELEMS;
  313. output[i] = p.pos;
  314. output[i + 1] = p.left_tangent;
  315. output[i + 2] = p.right_tangent;
  316. output[i + 3] = p.left_mode;
  317. output[i + 4] = p.right_mode;
  318. }
  319. return output;
  320. }
  321. void Curve::set_data(Array input) {
  322. const unsigned int ELEMS = 5;
  323. ERR_FAIL_COND(input.size() % ELEMS != 0);
  324. _points.clear();
  325. // Validate input
  326. for (int i = 0; i < input.size(); i += ELEMS) {
  327. ERR_FAIL_COND(input[i].get_type() != Variant::VECTOR2);
  328. ERR_FAIL_COND(!input[i + 1].is_num());
  329. ERR_FAIL_COND(input[i + 2].get_type() != Variant::REAL);
  330. ERR_FAIL_COND(input[i + 3].get_type() != Variant::INT);
  331. int left_mode = input[i + 3];
  332. ERR_FAIL_COND(left_mode < 0 || left_mode >= TANGENT_MODE_COUNT);
  333. ERR_FAIL_COND(input[i + 4].get_type() != Variant::INT);
  334. int right_mode = input[i + 4];
  335. ERR_FAIL_COND(right_mode < 0 || right_mode >= TANGENT_MODE_COUNT);
  336. }
  337. _points.resize(input.size() / ELEMS);
  338. for (int j = 0; j < _points.size(); ++j) {
  339. Point &p = _points.write[j];
  340. int i = j * ELEMS;
  341. p.pos = input[i];
  342. p.left_tangent = input[i + 1];
  343. p.right_tangent = input[i + 2];
  344. // TODO For some reason the compiler won't convert from Variant to enum
  345. int left_mode = input[i + 3];
  346. int right_mode = input[i + 4];
  347. p.left_mode = (TangentMode)left_mode;
  348. p.right_mode = (TangentMode)right_mode;
  349. }
  350. mark_dirty();
  351. }
  352. void Curve::bake() {
  353. _baked_cache.clear();
  354. _baked_cache.resize(_bake_resolution);
  355. for (int i = 1; i < _bake_resolution - 1; ++i) {
  356. real_t x = i / static_cast<real_t>(_bake_resolution);
  357. real_t y = interpolate(x);
  358. _baked_cache.write[i] = y;
  359. }
  360. if (_points.size() != 0) {
  361. _baked_cache.write[0] = _points[0].pos.y;
  362. _baked_cache.write[_baked_cache.size() - 1] = _points[_points.size() - 1].pos.y;
  363. }
  364. _baked_cache_dirty = false;
  365. }
  366. void Curve::set_bake_resolution(int p_resolution) {
  367. ERR_FAIL_COND(p_resolution < 1);
  368. ERR_FAIL_COND(p_resolution > 1000);
  369. _bake_resolution = p_resolution;
  370. _baked_cache_dirty = true;
  371. }
  372. real_t Curve::interpolate_baked(real_t offset) {
  373. if (_baked_cache_dirty) {
  374. // Last-second bake if not done already
  375. bake();
  376. }
  377. // Special cases if the cache is too small
  378. if (_baked_cache.size() == 0) {
  379. if (_points.size() == 0) {
  380. return 0;
  381. }
  382. return _points[0].pos.y;
  383. } else if (_baked_cache.size() == 1) {
  384. return _baked_cache[0];
  385. }
  386. // Get interpolation index
  387. real_t fi = offset * _baked_cache.size();
  388. int i = Math::floor(fi);
  389. if (i < 0) {
  390. i = 0;
  391. fi = 0;
  392. } else if (i >= _baked_cache.size()) {
  393. i = _baked_cache.size() - 1;
  394. fi = 0;
  395. }
  396. // Interpolate
  397. if (i + 1 < _baked_cache.size()) {
  398. real_t t = fi - i;
  399. return Math::lerp(_baked_cache[i], _baked_cache[i + 1], t);
  400. } else {
  401. return _baked_cache[_baked_cache.size() - 1];
  402. }
  403. }
  404. void Curve::ensure_default_setup(float p_min, float p_max) {
  405. if (_points.size() == 0 && _min_value == 0 && _max_value == 1) {
  406. add_point(Vector2(0, 1));
  407. add_point(Vector2(1, 1));
  408. set_min_value(p_min);
  409. set_max_value(p_max);
  410. }
  411. }
  412. void Curve::_bind_methods() {
  413. ClassDB::bind_method(D_METHOD("get_point_count"), &Curve::get_point_count);
  414. 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));
  415. ClassDB::bind_method(D_METHOD("remove_point", "index"), &Curve::remove_point);
  416. ClassDB::bind_method(D_METHOD("clear_points"), &Curve::clear_points);
  417. ClassDB::bind_method(D_METHOD("get_point_position", "index"), &Curve::get_point_position);
  418. ClassDB::bind_method(D_METHOD("set_point_value", "index", "y"), &Curve::set_point_value);
  419. ClassDB::bind_method(D_METHOD("set_point_offset", "index", "offset"), &Curve::set_point_offset);
  420. ClassDB::bind_method(D_METHOD("interpolate", "offset"), &Curve::interpolate);
  421. ClassDB::bind_method(D_METHOD("interpolate_baked", "offset"), &Curve::interpolate_baked);
  422. ClassDB::bind_method(D_METHOD("get_point_left_tangent", "index"), &Curve::get_point_left_tangent);
  423. ClassDB::bind_method(D_METHOD("get_point_right_tangent", "index"), &Curve::get_point_right_tangent);
  424. ClassDB::bind_method(D_METHOD("get_point_left_mode", "index"), &Curve::get_point_left_mode);
  425. ClassDB::bind_method(D_METHOD("get_point_right_mode", "index"), &Curve::get_point_right_mode);
  426. ClassDB::bind_method(D_METHOD("set_point_left_tangent", "index", "tangent"), &Curve::set_point_left_tangent);
  427. ClassDB::bind_method(D_METHOD("set_point_right_tangent", "index", "tangent"), &Curve::set_point_right_tangent);
  428. ClassDB::bind_method(D_METHOD("set_point_left_mode", "index", "mode"), &Curve::set_point_left_mode);
  429. ClassDB::bind_method(D_METHOD("set_point_right_mode", "index", "mode"), &Curve::set_point_right_mode);
  430. ClassDB::bind_method(D_METHOD("get_min_value"), &Curve::get_min_value);
  431. ClassDB::bind_method(D_METHOD("set_min_value", "min"), &Curve::set_min_value);
  432. ClassDB::bind_method(D_METHOD("get_max_value"), &Curve::get_max_value);
  433. ClassDB::bind_method(D_METHOD("set_max_value", "max"), &Curve::set_max_value);
  434. ClassDB::bind_method(D_METHOD("clean_dupes"), &Curve::clean_dupes);
  435. ClassDB::bind_method(D_METHOD("bake"), &Curve::bake);
  436. ClassDB::bind_method(D_METHOD("get_bake_resolution"), &Curve::get_bake_resolution);
  437. ClassDB::bind_method(D_METHOD("set_bake_resolution", "resolution"), &Curve::set_bake_resolution);
  438. ClassDB::bind_method(D_METHOD("_get_data"), &Curve::get_data);
  439. ClassDB::bind_method(D_METHOD("_set_data", "data"), &Curve::set_data);
  440. ADD_PROPERTY(PropertyInfo(Variant::REAL, "min_value", PROPERTY_HINT_RANGE, "-1024,1024,0.01"), "set_min_value", "get_min_value");
  441. ADD_PROPERTY(PropertyInfo(Variant::REAL, "max_value", PROPERTY_HINT_RANGE, "-1024,1024,0.01"), "set_max_value", "get_max_value");
  442. ADD_PROPERTY(PropertyInfo(Variant::INT, "bake_resolution", PROPERTY_HINT_RANGE, "1,1000,1"), "set_bake_resolution", "get_bake_resolution");
  443. ADD_PROPERTY(PropertyInfo(Variant::INT, "_data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL), "_set_data", "_get_data");
  444. ADD_SIGNAL(MethodInfo(SIGNAL_RANGE_CHANGED));
  445. BIND_ENUM_CONSTANT(TANGENT_FREE);
  446. BIND_ENUM_CONSTANT(TANGENT_LINEAR);
  447. BIND_ENUM_CONSTANT(TANGENT_MODE_COUNT);
  448. }
  449. int Curve2D::get_point_count() const {
  450. return points.size();
  451. }
  452. void Curve2D::add_point(const Vector2 &p_pos, const Vector2 &p_in, const Vector2 &p_out, int p_atpos) {
  453. Point n;
  454. n.pos = p_pos;
  455. n.in = p_in;
  456. n.out = p_out;
  457. if (p_atpos >= 0 && p_atpos < points.size()) {
  458. points.insert(p_atpos, n);
  459. } else {
  460. points.push_back(n);
  461. }
  462. baked_cache_dirty = true;
  463. emit_signal(CoreStringNames::get_singleton()->changed);
  464. }
  465. void Curve2D::set_point_position(int p_index, const Vector2 &p_pos) {
  466. ERR_FAIL_INDEX(p_index, points.size());
  467. points.write[p_index].pos = p_pos;
  468. baked_cache_dirty = true;
  469. emit_signal(CoreStringNames::get_singleton()->changed);
  470. }
  471. Vector2 Curve2D::get_point_position(int p_index) const {
  472. ERR_FAIL_INDEX_V(p_index, points.size(), Vector2());
  473. return points[p_index].pos;
  474. }
  475. void Curve2D::set_point_in(int p_index, const Vector2 &p_in) {
  476. ERR_FAIL_INDEX(p_index, points.size());
  477. points.write[p_index].in = p_in;
  478. baked_cache_dirty = true;
  479. emit_signal(CoreStringNames::get_singleton()->changed);
  480. }
  481. Vector2 Curve2D::get_point_in(int p_index) const {
  482. ERR_FAIL_INDEX_V(p_index, points.size(), Vector2());
  483. return points[p_index].in;
  484. }
  485. void Curve2D::set_point_out(int p_index, const Vector2 &p_out) {
  486. ERR_FAIL_INDEX(p_index, points.size());
  487. points.write[p_index].out = p_out;
  488. baked_cache_dirty = true;
  489. emit_signal(CoreStringNames::get_singleton()->changed);
  490. }
  491. Vector2 Curve2D::get_point_out(int p_index) const {
  492. ERR_FAIL_INDEX_V(p_index, points.size(), Vector2());
  493. return points[p_index].out;
  494. }
  495. void Curve2D::remove_point(int p_index) {
  496. ERR_FAIL_INDEX(p_index, points.size());
  497. points.remove(p_index);
  498. baked_cache_dirty = true;
  499. emit_signal(CoreStringNames::get_singleton()->changed);
  500. }
  501. void Curve2D::clear_points() {
  502. if (!points.empty()) {
  503. points.clear();
  504. baked_cache_dirty = true;
  505. emit_signal(CoreStringNames::get_singleton()->changed);
  506. }
  507. }
  508. Vector2 Curve2D::interpolate(int p_index, float p_offset) const {
  509. int pc = points.size();
  510. ERR_FAIL_COND_V(pc == 0, Vector2());
  511. if (p_index >= pc - 1) {
  512. return points[pc - 1].pos;
  513. } else if (p_index < 0) {
  514. return points[0].pos;
  515. }
  516. Vector2 p0 = points[p_index].pos;
  517. Vector2 p1 = p0 + points[p_index].out;
  518. Vector2 p3 = points[p_index + 1].pos;
  519. Vector2 p2 = p3 + points[p_index + 1].in;
  520. return _bezier_interp(p_offset, p0, p1, p2, p3);
  521. }
  522. Vector2 Curve2D::interpolatef(real_t p_findex) const {
  523. if (p_findex < 0) {
  524. p_findex = 0;
  525. } else if (p_findex >= points.size()) {
  526. p_findex = points.size();
  527. }
  528. return interpolate((int)p_findex, Math::fmod(p_findex, (real_t)1.0));
  529. }
  530. void Curve2D::_bake_segment2d(Map<float, Vector2> &r_bake, float p_begin, float 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, float p_tol) const {
  531. float mp = p_begin + (p_end - p_begin) * 0.5;
  532. Vector2 beg = _bezier_interp(p_begin, p_a, p_a + p_out, p_b + p_in, p_b);
  533. Vector2 mid = _bezier_interp(mp, p_a, p_a + p_out, p_b + p_in, p_b);
  534. Vector2 end = _bezier_interp(p_end, p_a, p_a + p_out, p_b + p_in, p_b);
  535. Vector2 na = (mid - beg).normalized();
  536. Vector2 nb = (end - mid).normalized();
  537. float dp = na.dot(nb);
  538. if (dp < Math::cos(Math::deg2rad(p_tol))) {
  539. r_bake[mp] = mid;
  540. }
  541. if (p_depth < p_max_depth) {
  542. _bake_segment2d(r_bake, p_begin, mp, p_a, p_out, p_b, p_in, p_depth + 1, p_max_depth, p_tol);
  543. _bake_segment2d(r_bake, mp, p_end, p_a, p_out, p_b, p_in, p_depth + 1, p_max_depth, p_tol);
  544. }
  545. }
  546. void Curve2D::_bake() const {
  547. if (!baked_cache_dirty) {
  548. return;
  549. }
  550. baked_max_ofs = 0;
  551. baked_cache_dirty = false;
  552. if (points.size() == 0) {
  553. baked_point_cache.resize(0);
  554. return;
  555. }
  556. if (points.size() == 1) {
  557. baked_point_cache.resize(1);
  558. baked_point_cache.set(0, points[0].pos);
  559. return;
  560. }
  561. Vector2 pos = points[0].pos;
  562. List<Vector2> pointlist;
  563. pointlist.push_back(pos); //start always from origin
  564. for (int i = 0; i < points.size() - 1; i++) {
  565. float step = 0.1; // at least 10 substeps ought to be enough?
  566. float p = 0;
  567. while (p < 1.0) {
  568. float np = p + step;
  569. if (np > 1.0) {
  570. np = 1.0;
  571. }
  572. Vector2 npp = _bezier_interp(np, points[i].pos, points[i].pos + points[i].out, points[i + 1].pos + points[i + 1].in, points[i + 1].pos);
  573. float d = pos.distance_to(npp);
  574. if (d > bake_interval) {
  575. // OK! between P and NP there _has_ to be Something, let's go searching!
  576. int iterations = 10; //lots of detail!
  577. float low = p;
  578. float hi = np;
  579. float mid = low + (hi - low) * 0.5;
  580. for (int j = 0; j < iterations; j++) {
  581. npp = _bezier_interp(mid, points[i].pos, points[i].pos + points[i].out, points[i + 1].pos + points[i + 1].in, points[i + 1].pos);
  582. d = pos.distance_to(npp);
  583. if (bake_interval < d) {
  584. hi = mid;
  585. } else {
  586. low = mid;
  587. }
  588. mid = low + (hi - low) * 0.5;
  589. }
  590. pos = npp;
  591. p = mid;
  592. pointlist.push_back(pos);
  593. } else {
  594. p = np;
  595. }
  596. }
  597. }
  598. Vector2 lastpos = points[points.size() - 1].pos;
  599. float rem = pos.distance_to(lastpos);
  600. baked_max_ofs = (pointlist.size() - 1) * bake_interval + rem;
  601. pointlist.push_back(lastpos);
  602. baked_point_cache.resize(pointlist.size());
  603. PoolVector2Array::Write w = baked_point_cache.write();
  604. int idx = 0;
  605. for (List<Vector2>::Element *E = pointlist.front(); E; E = E->next()) {
  606. w[idx] = E->get();
  607. idx++;
  608. }
  609. }
  610. float Curve2D::get_baked_length() const {
  611. if (baked_cache_dirty) {
  612. _bake();
  613. }
  614. return baked_max_ofs;
  615. }
  616. Vector2 Curve2D::interpolate_baked(float p_offset, bool p_cubic) const {
  617. if (baked_cache_dirty) {
  618. _bake();
  619. }
  620. //validate//
  621. int pc = baked_point_cache.size();
  622. ERR_FAIL_COND_V_MSG(pc == 0, Vector2(), "No points in Curve2D.");
  623. if (pc == 1) {
  624. return baked_point_cache.get(0);
  625. }
  626. int bpc = baked_point_cache.size();
  627. PoolVector2Array::Read r = baked_point_cache.read();
  628. if (p_offset < 0) {
  629. return r[0];
  630. }
  631. if (p_offset >= baked_max_ofs) {
  632. return r[bpc - 1];
  633. }
  634. int idx = Math::floor((double)p_offset / (double)bake_interval);
  635. float frac = Math::fmod(p_offset, (float)bake_interval);
  636. if (idx >= bpc - 1) {
  637. return r[bpc - 1];
  638. } else if (idx == bpc - 2) {
  639. if (frac > 0) {
  640. frac /= Math::fmod(baked_max_ofs, bake_interval);
  641. }
  642. } else {
  643. frac /= bake_interval;
  644. }
  645. if (p_cubic) {
  646. Vector2 pre = idx > 0 ? r[idx - 1] : r[idx];
  647. Vector2 post = (idx < (bpc - 2)) ? r[idx + 2] : r[idx + 1];
  648. return r[idx].cubic_interpolate(r[idx + 1], pre, post, frac);
  649. } else {
  650. return r[idx].linear_interpolate(r[idx + 1], frac);
  651. }
  652. }
  653. PoolVector2Array Curve2D::get_baked_points() const {
  654. if (baked_cache_dirty) {
  655. _bake();
  656. }
  657. return baked_point_cache;
  658. }
  659. void Curve2D::set_bake_interval(float p_tolerance) {
  660. bake_interval = p_tolerance;
  661. baked_cache_dirty = true;
  662. emit_signal(CoreStringNames::get_singleton()->changed);
  663. }
  664. float Curve2D::get_bake_interval() const {
  665. return bake_interval;
  666. }
  667. Vector2 Curve2D::get_closest_point(const Vector2 &p_to_point) const {
  668. // Brute force method
  669. if (baked_cache_dirty) {
  670. _bake();
  671. }
  672. //validate//
  673. int pc = baked_point_cache.size();
  674. ERR_FAIL_COND_V_MSG(pc == 0, Vector2(), "No points in Curve2D.");
  675. if (pc == 1) {
  676. return baked_point_cache.get(0);
  677. }
  678. PoolVector2Array::Read r = baked_point_cache.read();
  679. Vector2 nearest;
  680. float nearest_dist = -1.0f;
  681. for (int i = 0; i < pc - 1; i++) {
  682. Vector2 origin = r[i];
  683. Vector2 direction = (r[i + 1] - origin) / bake_interval;
  684. float d = CLAMP((p_to_point - origin).dot(direction), 0.0f, bake_interval);
  685. Vector2 proj = origin + direction * d;
  686. float dist = proj.distance_squared_to(p_to_point);
  687. if (nearest_dist < 0.0f || dist < nearest_dist) {
  688. nearest = proj;
  689. nearest_dist = dist;
  690. }
  691. }
  692. return nearest;
  693. }
  694. float Curve2D::get_closest_offset(const Vector2 &p_to_point) const {
  695. // Brute force method
  696. if (baked_cache_dirty) {
  697. _bake();
  698. }
  699. //validate//
  700. int pc = baked_point_cache.size();
  701. ERR_FAIL_COND_V_MSG(pc == 0, 0.0f, "No points in Curve2D.");
  702. if (pc == 1) {
  703. return 0.0f;
  704. }
  705. PoolVector2Array::Read r = baked_point_cache.read();
  706. float nearest = 0.0f;
  707. float nearest_dist = -1.0f;
  708. float offset = 0.0f;
  709. for (int i = 0; i < pc - 1; i++) {
  710. Vector2 origin = r[i];
  711. Vector2 direction = (r[i + 1] - origin) / bake_interval;
  712. float d = CLAMP((p_to_point - origin).dot(direction), 0.0f, bake_interval);
  713. Vector2 proj = origin + direction * d;
  714. float dist = proj.distance_squared_to(p_to_point);
  715. if (nearest_dist < 0.0f || dist < nearest_dist) {
  716. nearest = offset + d;
  717. nearest_dist = dist;
  718. }
  719. offset += bake_interval;
  720. }
  721. return nearest;
  722. }
  723. Dictionary Curve2D::_get_data() const {
  724. Dictionary dc;
  725. PoolVector2Array d;
  726. d.resize(points.size() * 3);
  727. PoolVector2Array::Write w = d.write();
  728. for (int i = 0; i < points.size(); i++) {
  729. w[i * 3 + 0] = points[i].in;
  730. w[i * 3 + 1] = points[i].out;
  731. w[i * 3 + 2] = points[i].pos;
  732. }
  733. w = PoolVector2Array::Write();
  734. dc["points"] = d;
  735. return dc;
  736. }
  737. void Curve2D::_set_data(const Dictionary &p_data) {
  738. ERR_FAIL_COND(!p_data.has("points"));
  739. PoolVector2Array rp = p_data["points"];
  740. int pc = rp.size();
  741. ERR_FAIL_COND(pc % 3 != 0);
  742. points.resize(pc / 3);
  743. PoolVector2Array::Read r = rp.read();
  744. for (int i = 0; i < points.size(); i++) {
  745. points.write[i].in = r[i * 3 + 0];
  746. points.write[i].out = r[i * 3 + 1];
  747. points.write[i].pos = r[i * 3 + 2];
  748. }
  749. baked_cache_dirty = true;
  750. }
  751. PoolVector2Array Curve2D::tessellate(int p_max_stages, float p_tolerance) const {
  752. PoolVector2Array tess;
  753. if (points.size() == 0) {
  754. return tess;
  755. }
  756. Vector<Map<float, Vector2>> midpoints;
  757. midpoints.resize(points.size() - 1);
  758. int pc = 1;
  759. for (int i = 0; i < points.size() - 1; i++) {
  760. _bake_segment2d(midpoints.write[i], 0, 1, points[i].pos, points[i].out, points[i + 1].pos, points[i + 1].in, 0, p_max_stages, p_tolerance);
  761. pc++;
  762. pc += midpoints[i].size();
  763. }
  764. tess.resize(pc);
  765. PoolVector2Array::Write bpw = tess.write();
  766. bpw[0] = points[0].pos;
  767. int pidx = 0;
  768. for (int i = 0; i < points.size() - 1; i++) {
  769. for (Map<float, Vector2>::Element *E = midpoints[i].front(); E; E = E->next()) {
  770. pidx++;
  771. bpw[pidx] = E->get();
  772. }
  773. pidx++;
  774. bpw[pidx] = points[i + 1].pos;
  775. }
  776. bpw = PoolVector2Array::Write();
  777. return tess;
  778. }
  779. void Curve2D::_bind_methods() {
  780. ClassDB::bind_method(D_METHOD("get_point_count"), &Curve2D::get_point_count);
  781. ClassDB::bind_method(D_METHOD("add_point", "position", "in", "out", "index"), &Curve2D::add_point, DEFVAL(Vector2()), DEFVAL(Vector2()), DEFVAL(-1));
  782. ClassDB::bind_method(D_METHOD("set_point_position", "idx", "position"), &Curve2D::set_point_position);
  783. ClassDB::bind_method(D_METHOD("get_point_position", "idx"), &Curve2D::get_point_position);
  784. ClassDB::bind_method(D_METHOD("set_point_in", "idx", "position"), &Curve2D::set_point_in);
  785. ClassDB::bind_method(D_METHOD("get_point_in", "idx"), &Curve2D::get_point_in);
  786. ClassDB::bind_method(D_METHOD("set_point_out", "idx", "position"), &Curve2D::set_point_out);
  787. ClassDB::bind_method(D_METHOD("get_point_out", "idx"), &Curve2D::get_point_out);
  788. ClassDB::bind_method(D_METHOD("remove_point", "idx"), &Curve2D::remove_point);
  789. ClassDB::bind_method(D_METHOD("clear_points"), &Curve2D::clear_points);
  790. ClassDB::bind_method(D_METHOD("interpolate", "idx", "t"), &Curve2D::interpolate);
  791. ClassDB::bind_method(D_METHOD("interpolatef", "fofs"), &Curve2D::interpolatef);
  792. //ClassDB::bind_method(D_METHOD("bake","subdivs"),&Curve2D::bake,DEFVAL(10));
  793. ClassDB::bind_method(D_METHOD("set_bake_interval", "distance"), &Curve2D::set_bake_interval);
  794. ClassDB::bind_method(D_METHOD("get_bake_interval"), &Curve2D::get_bake_interval);
  795. ClassDB::bind_method(D_METHOD("get_baked_length"), &Curve2D::get_baked_length);
  796. ClassDB::bind_method(D_METHOD("interpolate_baked", "offset", "cubic"), &Curve2D::interpolate_baked, DEFVAL(false));
  797. ClassDB::bind_method(D_METHOD("get_baked_points"), &Curve2D::get_baked_points);
  798. ClassDB::bind_method(D_METHOD("get_closest_point", "to_point"), &Curve2D::get_closest_point);
  799. ClassDB::bind_method(D_METHOD("get_closest_offset", "to_point"), &Curve2D::get_closest_offset);
  800. ClassDB::bind_method(D_METHOD("tessellate", "max_stages", "tolerance_degrees"), &Curve2D::tessellate, DEFVAL(5), DEFVAL(4));
  801. ClassDB::bind_method(D_METHOD("_get_data"), &Curve2D::_get_data);
  802. ClassDB::bind_method(D_METHOD("_set_data"), &Curve2D::_set_data);
  803. ADD_PROPERTY(PropertyInfo(Variant::REAL, "bake_interval", PROPERTY_HINT_RANGE, "0.01,512,0.01"), "set_bake_interval", "get_bake_interval");
  804. ADD_PROPERTY(PropertyInfo(Variant::INT, "_data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL), "_set_data", "_get_data");
  805. }
  806. Curve2D::Curve2D() {
  807. baked_cache_dirty = false;
  808. baked_max_ofs = 0;
  809. /* add_point(Vector2(-1,0,0));
  810. add_point(Vector2(0,2,0));
  811. add_point(Vector2(0,3,5));*/
  812. bake_interval = 5;
  813. }
  814. /***********************************************************************************/
  815. /***********************************************************************************/
  816. /***********************************************************************************/
  817. /***********************************************************************************/
  818. /***********************************************************************************/
  819. /***********************************************************************************/
  820. int Curve3D::get_point_count() const {
  821. return points.size();
  822. }
  823. void Curve3D::add_point(const Vector3 &p_pos, const Vector3 &p_in, const Vector3 &p_out, int p_atpos) {
  824. Point n;
  825. n.pos = p_pos;
  826. n.in = p_in;
  827. n.out = p_out;
  828. if (p_atpos >= 0 && p_atpos < points.size()) {
  829. points.insert(p_atpos, n);
  830. } else {
  831. points.push_back(n);
  832. }
  833. baked_cache_dirty = true;
  834. emit_signal(CoreStringNames::get_singleton()->changed);
  835. }
  836. void Curve3D::set_point_position(int p_index, const Vector3 &p_pos) {
  837. ERR_FAIL_INDEX(p_index, points.size());
  838. points.write[p_index].pos = p_pos;
  839. baked_cache_dirty = true;
  840. emit_signal(CoreStringNames::get_singleton()->changed);
  841. }
  842. Vector3 Curve3D::get_point_position(int p_index) const {
  843. ERR_FAIL_INDEX_V(p_index, points.size(), Vector3());
  844. return points[p_index].pos;
  845. }
  846. void Curve3D::set_point_tilt(int p_index, float p_tilt) {
  847. ERR_FAIL_INDEX(p_index, points.size());
  848. points.write[p_index].tilt = p_tilt;
  849. baked_cache_dirty = true;
  850. emit_signal(CoreStringNames::get_singleton()->changed);
  851. }
  852. float Curve3D::get_point_tilt(int p_index) const {
  853. ERR_FAIL_INDEX_V(p_index, points.size(), 0);
  854. return points[p_index].tilt;
  855. }
  856. void Curve3D::set_point_in(int p_index, const Vector3 &p_in) {
  857. ERR_FAIL_INDEX(p_index, points.size());
  858. points.write[p_index].in = p_in;
  859. baked_cache_dirty = true;
  860. emit_signal(CoreStringNames::get_singleton()->changed);
  861. }
  862. Vector3 Curve3D::get_point_in(int p_index) const {
  863. ERR_FAIL_INDEX_V(p_index, points.size(), Vector3());
  864. return points[p_index].in;
  865. }
  866. void Curve3D::set_point_out(int p_index, const Vector3 &p_out) {
  867. ERR_FAIL_INDEX(p_index, points.size());
  868. points.write[p_index].out = p_out;
  869. baked_cache_dirty = true;
  870. emit_signal(CoreStringNames::get_singleton()->changed);
  871. }
  872. Vector3 Curve3D::get_point_out(int p_index) const {
  873. ERR_FAIL_INDEX_V(p_index, points.size(), Vector3());
  874. return points[p_index].out;
  875. }
  876. void Curve3D::remove_point(int p_index) {
  877. ERR_FAIL_INDEX(p_index, points.size());
  878. points.remove(p_index);
  879. baked_cache_dirty = true;
  880. emit_signal(CoreStringNames::get_singleton()->changed);
  881. }
  882. void Curve3D::clear_points() {
  883. if (!points.empty()) {
  884. points.clear();
  885. baked_cache_dirty = true;
  886. emit_signal(CoreStringNames::get_singleton()->changed);
  887. }
  888. }
  889. Vector3 Curve3D::interpolate(int p_index, float p_offset) const {
  890. int pc = points.size();
  891. ERR_FAIL_COND_V(pc == 0, Vector3());
  892. if (p_index >= pc - 1) {
  893. return points[pc - 1].pos;
  894. } else if (p_index < 0) {
  895. return points[0].pos;
  896. }
  897. Vector3 p0 = points[p_index].pos;
  898. Vector3 p1 = p0 + points[p_index].out;
  899. Vector3 p3 = points[p_index + 1].pos;
  900. Vector3 p2 = p3 + points[p_index + 1].in;
  901. return _bezier_interp(p_offset, p0, p1, p2, p3);
  902. }
  903. Vector3 Curve3D::interpolatef(real_t p_findex) const {
  904. if (p_findex < 0) {
  905. p_findex = 0;
  906. } else if (p_findex >= points.size()) {
  907. p_findex = points.size();
  908. }
  909. return interpolate((int)p_findex, Math::fmod(p_findex, (real_t)1.0));
  910. }
  911. void Curve3D::_bake_segment3d(Map<float, Vector3> &r_bake, float p_begin, float 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, float p_tol) const {
  912. float mp = p_begin + (p_end - p_begin) * 0.5;
  913. Vector3 beg = _bezier_interp(p_begin, p_a, p_a + p_out, p_b + p_in, p_b);
  914. Vector3 mid = _bezier_interp(mp, p_a, p_a + p_out, p_b + p_in, p_b);
  915. Vector3 end = _bezier_interp(p_end, p_a, p_a + p_out, p_b + p_in, p_b);
  916. Vector3 na = (mid - beg).normalized();
  917. Vector3 nb = (end - mid).normalized();
  918. float dp = na.dot(nb);
  919. if (dp < Math::cos(Math::deg2rad(p_tol))) {
  920. r_bake[mp] = mid;
  921. }
  922. if (p_depth < p_max_depth) {
  923. _bake_segment3d(r_bake, p_begin, mp, p_a, p_out, p_b, p_in, p_depth + 1, p_max_depth, p_tol);
  924. _bake_segment3d(r_bake, mp, p_end, p_a, p_out, p_b, p_in, p_depth + 1, p_max_depth, p_tol);
  925. }
  926. }
  927. void Curve3D::_bake() const {
  928. if (!baked_cache_dirty) {
  929. return;
  930. }
  931. baked_max_ofs = 0;
  932. baked_cache_dirty = false;
  933. if (points.size() == 0) {
  934. baked_point_cache.resize(0);
  935. baked_tilt_cache.resize(0);
  936. baked_up_vector_cache.resize(0);
  937. return;
  938. }
  939. if (points.size() == 1) {
  940. baked_point_cache.resize(1);
  941. baked_point_cache.set(0, points[0].pos);
  942. baked_tilt_cache.resize(1);
  943. baked_tilt_cache.set(0, points[0].tilt);
  944. if (up_vector_enabled) {
  945. baked_up_vector_cache.resize(1);
  946. baked_up_vector_cache.set(0, Vector3(0, 1, 0));
  947. } else {
  948. baked_up_vector_cache.resize(0);
  949. }
  950. return;
  951. }
  952. Vector3 pos = points[0].pos;
  953. List<Plane> pointlist;
  954. pointlist.push_back(Plane(pos, points[0].tilt));
  955. for (int i = 0; i < points.size() - 1; i++) {
  956. float step = 0.1; // at least 10 substeps ought to be enough?
  957. float p = 0;
  958. while (p < 1.0) {
  959. float np = p + step;
  960. if (np > 1.0) {
  961. np = 1.0;
  962. }
  963. Vector3 npp = _bezier_interp(np, points[i].pos, points[i].pos + points[i].out, points[i + 1].pos + points[i + 1].in, points[i + 1].pos);
  964. float d = pos.distance_to(npp);
  965. if (d > bake_interval) {
  966. // OK! between P and NP there _has_ to be Something, let's go searching!
  967. int iterations = 10; //lots of detail!
  968. float low = p;
  969. float hi = np;
  970. float mid = low + (hi - low) * 0.5;
  971. for (int j = 0; j < iterations; j++) {
  972. npp = _bezier_interp(mid, points[i].pos, points[i].pos + points[i].out, points[i + 1].pos + points[i + 1].in, points[i + 1].pos);
  973. d = pos.distance_to(npp);
  974. if (bake_interval < d) {
  975. hi = mid;
  976. } else {
  977. low = mid;
  978. }
  979. mid = low + (hi - low) * 0.5;
  980. }
  981. pos = npp;
  982. p = mid;
  983. Plane post;
  984. post.normal = pos;
  985. post.d = Math::lerp(points[i].tilt, points[i + 1].tilt, mid);
  986. pointlist.push_back(post);
  987. } else {
  988. p = np;
  989. }
  990. }
  991. }
  992. Vector3 lastpos = points[points.size() - 1].pos;
  993. float lastilt = points[points.size() - 1].tilt;
  994. float rem = pos.distance_to(lastpos);
  995. baked_max_ofs = (pointlist.size() - 1) * bake_interval + rem;
  996. pointlist.push_back(Plane(lastpos, lastilt));
  997. baked_point_cache.resize(pointlist.size());
  998. PoolVector3Array::Write w = baked_point_cache.write();
  999. int idx = 0;
  1000. baked_tilt_cache.resize(pointlist.size());
  1001. PoolRealArray::Write wt = baked_tilt_cache.write();
  1002. baked_up_vector_cache.resize(up_vector_enabled ? pointlist.size() : 0);
  1003. PoolVector3Array::Write up_write = baked_up_vector_cache.write();
  1004. Vector3 sideways;
  1005. Vector3 up;
  1006. Vector3 forward;
  1007. Vector3 prev_sideways = Vector3(1, 0, 0);
  1008. Vector3 prev_up = Vector3(0, 1, 0);
  1009. Vector3 prev_forward = Vector3(0, 0, 1);
  1010. for (List<Plane>::Element *E = pointlist.front(); E; E = E->next()) {
  1011. w[idx] = E->get().normal;
  1012. wt[idx] = E->get().d;
  1013. if (!up_vector_enabled) {
  1014. idx++;
  1015. continue;
  1016. }
  1017. forward = idx > 0 ? (w[idx] - w[idx - 1]).normalized() : prev_forward;
  1018. float y_dot = prev_up.dot(forward);
  1019. if (y_dot > (1.0f - CMP_EPSILON)) {
  1020. sideways = prev_sideways;
  1021. up = -prev_forward;
  1022. } else if (y_dot < -(1.0f - CMP_EPSILON)) {
  1023. sideways = prev_sideways;
  1024. up = prev_forward;
  1025. } else {
  1026. sideways = prev_up.cross(forward).normalized();
  1027. up = forward.cross(sideways).normalized();
  1028. }
  1029. if (idx == 1) {
  1030. up_write[0] = up;
  1031. }
  1032. up_write[idx] = up;
  1033. prev_sideways = sideways;
  1034. prev_up = up;
  1035. prev_forward = forward;
  1036. idx++;
  1037. }
  1038. }
  1039. float Curve3D::get_baked_length() const {
  1040. if (baked_cache_dirty) {
  1041. _bake();
  1042. }
  1043. return baked_max_ofs;
  1044. }
  1045. Vector3 Curve3D::interpolate_baked(float p_offset, bool p_cubic) const {
  1046. if (baked_cache_dirty) {
  1047. _bake();
  1048. }
  1049. //validate//
  1050. int pc = baked_point_cache.size();
  1051. ERR_FAIL_COND_V_MSG(pc == 0, Vector3(), "No points in Curve3D.");
  1052. if (pc == 1) {
  1053. return baked_point_cache.get(0);
  1054. }
  1055. int bpc = baked_point_cache.size();
  1056. PoolVector3Array::Read r = baked_point_cache.read();
  1057. if (p_offset < 0) {
  1058. return r[0];
  1059. }
  1060. if (p_offset >= baked_max_ofs) {
  1061. return r[bpc - 1];
  1062. }
  1063. int idx = Math::floor((double)p_offset / (double)bake_interval);
  1064. float frac = Math::fmod(p_offset, bake_interval);
  1065. if (idx >= bpc - 1) {
  1066. return r[bpc - 1];
  1067. } else if (idx == bpc - 2) {
  1068. if (frac > 0) {
  1069. frac /= Math::fmod(baked_max_ofs, bake_interval);
  1070. }
  1071. } else {
  1072. frac /= bake_interval;
  1073. }
  1074. if (p_cubic) {
  1075. Vector3 pre = idx > 0 ? r[idx - 1] : r[idx];
  1076. Vector3 post = (idx < (bpc - 2)) ? r[idx + 2] : r[idx + 1];
  1077. return r[idx].cubic_interpolate(r[idx + 1], pre, post, frac);
  1078. } else {
  1079. return r[idx].linear_interpolate(r[idx + 1], frac);
  1080. }
  1081. }
  1082. float Curve3D::interpolate_baked_tilt(float p_offset) const {
  1083. if (baked_cache_dirty) {
  1084. _bake();
  1085. }
  1086. //validate//
  1087. int pc = baked_tilt_cache.size();
  1088. ERR_FAIL_COND_V_MSG(pc == 0, 0, "No tilts in Curve3D.");
  1089. if (pc == 1) {
  1090. return baked_tilt_cache.get(0);
  1091. }
  1092. int bpc = baked_tilt_cache.size();
  1093. PoolRealArray::Read r = baked_tilt_cache.read();
  1094. if (p_offset < 0) {
  1095. return r[0];
  1096. }
  1097. if (p_offset >= baked_max_ofs) {
  1098. return r[bpc - 1];
  1099. }
  1100. int idx = Math::floor((double)p_offset / (double)bake_interval);
  1101. float frac = Math::fmod(p_offset, bake_interval);
  1102. if (idx >= bpc - 1) {
  1103. return r[bpc - 1];
  1104. } else if (idx == bpc - 2) {
  1105. if (frac > 0) {
  1106. frac /= Math::fmod(baked_max_ofs, bake_interval);
  1107. }
  1108. } else {
  1109. frac /= bake_interval;
  1110. }
  1111. return Math::lerp(r[idx], r[idx + 1], frac);
  1112. }
  1113. Vector3 Curve3D::interpolate_baked_up_vector(float p_offset, bool p_apply_tilt) const {
  1114. if (baked_cache_dirty) {
  1115. _bake();
  1116. }
  1117. //validate//
  1118. // curve may not have baked up vectors
  1119. int count = baked_up_vector_cache.size();
  1120. ERR_FAIL_COND_V_MSG(count == 0, Vector3(0, 1, 0), "No up vectors in Curve3D.");
  1121. if (count == 1) {
  1122. return baked_up_vector_cache.get(0);
  1123. }
  1124. PoolVector3Array::Read r = baked_up_vector_cache.read();
  1125. PoolVector3Array::Read rp = baked_point_cache.read();
  1126. PoolRealArray::Read rt = baked_tilt_cache.read();
  1127. float offset = CLAMP(p_offset, 0.0f, baked_max_ofs);
  1128. int idx = Math::floor((double)offset / (double)bake_interval);
  1129. float frac = Math::fmod(offset, bake_interval) / bake_interval;
  1130. if (idx == count - 1) {
  1131. return p_apply_tilt ? r[idx].rotated((rp[idx] - rp[idx - 1]).normalized(), rt[idx]) : r[idx];
  1132. }
  1133. Vector3 forward = (rp[idx + 1] - rp[idx]).normalized();
  1134. Vector3 up = r[idx];
  1135. Vector3 up1 = r[idx + 1];
  1136. if (p_apply_tilt) {
  1137. up.rotate(forward, rt[idx]);
  1138. up1.rotate(idx + 2 >= count ? forward : (rp[idx + 2] - rp[idx + 1]).normalized(), rt[idx + 1]);
  1139. }
  1140. Vector3 axis = up.cross(up1);
  1141. if (axis.length_squared() < CMP_EPSILON2) {
  1142. axis = forward;
  1143. } else {
  1144. axis.normalize();
  1145. }
  1146. return up.rotated(axis, up.angle_to(up1) * frac);
  1147. }
  1148. PoolVector3Array Curve3D::get_baked_points() const {
  1149. if (baked_cache_dirty) {
  1150. _bake();
  1151. }
  1152. return baked_point_cache;
  1153. }
  1154. PoolRealArray Curve3D::get_baked_tilts() const {
  1155. if (baked_cache_dirty) {
  1156. _bake();
  1157. }
  1158. return baked_tilt_cache;
  1159. }
  1160. PoolVector3Array Curve3D::get_baked_up_vectors() const {
  1161. if (baked_cache_dirty) {
  1162. _bake();
  1163. }
  1164. return baked_up_vector_cache;
  1165. }
  1166. Vector3 Curve3D::get_closest_point(const Vector3 &p_to_point) const {
  1167. // Brute force method
  1168. if (baked_cache_dirty) {
  1169. _bake();
  1170. }
  1171. //validate//
  1172. int pc = baked_point_cache.size();
  1173. ERR_FAIL_COND_V_MSG(pc == 0, Vector3(), "No points in Curve3D.");
  1174. if (pc == 1) {
  1175. return baked_point_cache.get(0);
  1176. }
  1177. PoolVector3Array::Read r = baked_point_cache.read();
  1178. Vector3 nearest;
  1179. float nearest_dist = -1.0f;
  1180. for (int i = 0; i < pc - 1; i++) {
  1181. Vector3 origin = r[i];
  1182. Vector3 direction = (r[i + 1] - origin) / bake_interval;
  1183. float d = CLAMP((p_to_point - origin).dot(direction), 0.0f, bake_interval);
  1184. Vector3 proj = origin + direction * d;
  1185. float dist = proj.distance_squared_to(p_to_point);
  1186. if (nearest_dist < 0.0f || dist < nearest_dist) {
  1187. nearest = proj;
  1188. nearest_dist = dist;
  1189. }
  1190. }
  1191. return nearest;
  1192. }
  1193. float Curve3D::get_closest_offset(const Vector3 &p_to_point) const {
  1194. // Brute force method
  1195. if (baked_cache_dirty) {
  1196. _bake();
  1197. }
  1198. //validate//
  1199. int pc = baked_point_cache.size();
  1200. ERR_FAIL_COND_V_MSG(pc == 0, 0.0f, "No points in Curve3D.");
  1201. if (pc == 1) {
  1202. return 0.0f;
  1203. }
  1204. PoolVector3Array::Read r = baked_point_cache.read();
  1205. float nearest = 0.0f;
  1206. float nearest_dist = -1.0f;
  1207. float offset = 0.0f;
  1208. for (int i = 0; i < pc - 1; i++) {
  1209. Vector3 origin = r[i];
  1210. Vector3 direction = (r[i + 1] - origin) / bake_interval;
  1211. float d = CLAMP((p_to_point - origin).dot(direction), 0.0f, bake_interval);
  1212. Vector3 proj = origin + direction * d;
  1213. float dist = proj.distance_squared_to(p_to_point);
  1214. if (nearest_dist < 0.0f || dist < nearest_dist) {
  1215. nearest = offset + d;
  1216. nearest_dist = dist;
  1217. }
  1218. offset += bake_interval;
  1219. }
  1220. return nearest;
  1221. }
  1222. void Curve3D::set_bake_interval(float p_tolerance) {
  1223. bake_interval = p_tolerance;
  1224. baked_cache_dirty = true;
  1225. emit_signal(CoreStringNames::get_singleton()->changed);
  1226. }
  1227. float Curve3D::get_bake_interval() const {
  1228. return bake_interval;
  1229. }
  1230. void Curve3D::set_up_vector_enabled(bool p_enable) {
  1231. up_vector_enabled = p_enable;
  1232. baked_cache_dirty = true;
  1233. emit_signal(CoreStringNames::get_singleton()->changed);
  1234. }
  1235. bool Curve3D::is_up_vector_enabled() const {
  1236. return up_vector_enabled;
  1237. }
  1238. Dictionary Curve3D::_get_data() const {
  1239. Dictionary dc;
  1240. PoolVector3Array d;
  1241. d.resize(points.size() * 3);
  1242. PoolVector3Array::Write w = d.write();
  1243. PoolRealArray t;
  1244. t.resize(points.size());
  1245. PoolRealArray::Write wt = t.write();
  1246. for (int i = 0; i < points.size(); i++) {
  1247. w[i * 3 + 0] = points[i].in;
  1248. w[i * 3 + 1] = points[i].out;
  1249. w[i * 3 + 2] = points[i].pos;
  1250. wt[i] = points[i].tilt;
  1251. }
  1252. w = PoolVector3Array::Write();
  1253. wt = PoolRealArray::Write();
  1254. dc["points"] = d;
  1255. dc["tilts"] = t;
  1256. return dc;
  1257. }
  1258. void Curve3D::_set_data(const Dictionary &p_data) {
  1259. ERR_FAIL_COND(!p_data.has("points"));
  1260. ERR_FAIL_COND(!p_data.has("tilts"));
  1261. PoolVector3Array rp = p_data["points"];
  1262. int pc = rp.size();
  1263. ERR_FAIL_COND(pc % 3 != 0);
  1264. points.resize(pc / 3);
  1265. PoolVector3Array::Read r = rp.read();
  1266. PoolRealArray rtl = p_data["tilts"];
  1267. PoolRealArray::Read rt = rtl.read();
  1268. for (int i = 0; i < points.size(); i++) {
  1269. points.write[i].in = r[i * 3 + 0];
  1270. points.write[i].out = r[i * 3 + 1];
  1271. points.write[i].pos = r[i * 3 + 2];
  1272. points.write[i].tilt = rt[i];
  1273. }
  1274. baked_cache_dirty = true;
  1275. }
  1276. PoolVector3Array Curve3D::tessellate(int p_max_stages, float p_tolerance) const {
  1277. PoolVector3Array tess;
  1278. if (points.size() == 0) {
  1279. return tess;
  1280. }
  1281. Vector<Map<float, Vector3>> midpoints;
  1282. midpoints.resize(points.size() - 1);
  1283. int pc = 1;
  1284. for (int i = 0; i < points.size() - 1; i++) {
  1285. _bake_segment3d(midpoints.write[i], 0, 1, points[i].pos, points[i].out, points[i + 1].pos, points[i + 1].in, 0, p_max_stages, p_tolerance);
  1286. pc++;
  1287. pc += midpoints[i].size();
  1288. }
  1289. tess.resize(pc);
  1290. PoolVector3Array::Write bpw = tess.write();
  1291. bpw[0] = points[0].pos;
  1292. int pidx = 0;
  1293. for (int i = 0; i < points.size() - 1; i++) {
  1294. for (Map<float, Vector3>::Element *E = midpoints[i].front(); E; E = E->next()) {
  1295. pidx++;
  1296. bpw[pidx] = E->get();
  1297. }
  1298. pidx++;
  1299. bpw[pidx] = points[i + 1].pos;
  1300. }
  1301. bpw = PoolVector3Array::Write();
  1302. return tess;
  1303. }
  1304. void Curve3D::_bind_methods() {
  1305. ClassDB::bind_method(D_METHOD("get_point_count"), &Curve3D::get_point_count);
  1306. ClassDB::bind_method(D_METHOD("add_point", "position", "in", "out", "index"), &Curve3D::add_point, DEFVAL(Vector3()), DEFVAL(Vector3()), DEFVAL(-1));
  1307. ClassDB::bind_method(D_METHOD("set_point_position", "idx", "position"), &Curve3D::set_point_position);
  1308. ClassDB::bind_method(D_METHOD("get_point_position", "idx"), &Curve3D::get_point_position);
  1309. ClassDB::bind_method(D_METHOD("set_point_tilt", "idx", "tilt"), &Curve3D::set_point_tilt);
  1310. ClassDB::bind_method(D_METHOD("get_point_tilt", "idx"), &Curve3D::get_point_tilt);
  1311. ClassDB::bind_method(D_METHOD("set_point_in", "idx", "position"), &Curve3D::set_point_in);
  1312. ClassDB::bind_method(D_METHOD("get_point_in", "idx"), &Curve3D::get_point_in);
  1313. ClassDB::bind_method(D_METHOD("set_point_out", "idx", "position"), &Curve3D::set_point_out);
  1314. ClassDB::bind_method(D_METHOD("get_point_out", "idx"), &Curve3D::get_point_out);
  1315. ClassDB::bind_method(D_METHOD("remove_point", "idx"), &Curve3D::remove_point);
  1316. ClassDB::bind_method(D_METHOD("clear_points"), &Curve3D::clear_points);
  1317. ClassDB::bind_method(D_METHOD("interpolate", "idx", "t"), &Curve3D::interpolate);
  1318. ClassDB::bind_method(D_METHOD("interpolatef", "fofs"), &Curve3D::interpolatef);
  1319. //ClassDB::bind_method(D_METHOD("bake","subdivs"),&Curve3D::bake,DEFVAL(10));
  1320. ClassDB::bind_method(D_METHOD("set_bake_interval", "distance"), &Curve3D::set_bake_interval);
  1321. ClassDB::bind_method(D_METHOD("get_bake_interval"), &Curve3D::get_bake_interval);
  1322. ClassDB::bind_method(D_METHOD("set_up_vector_enabled", "enable"), &Curve3D::set_up_vector_enabled);
  1323. ClassDB::bind_method(D_METHOD("is_up_vector_enabled"), &Curve3D::is_up_vector_enabled);
  1324. ClassDB::bind_method(D_METHOD("get_baked_length"), &Curve3D::get_baked_length);
  1325. ClassDB::bind_method(D_METHOD("interpolate_baked", "offset", "cubic"), &Curve3D::interpolate_baked, DEFVAL(false));
  1326. ClassDB::bind_method(D_METHOD("interpolate_baked_up_vector", "offset", "apply_tilt"), &Curve3D::interpolate_baked_up_vector, DEFVAL(false));
  1327. ClassDB::bind_method(D_METHOD("get_baked_points"), &Curve3D::get_baked_points);
  1328. ClassDB::bind_method(D_METHOD("get_baked_tilts"), &Curve3D::get_baked_tilts);
  1329. ClassDB::bind_method(D_METHOD("get_baked_up_vectors"), &Curve3D::get_baked_up_vectors);
  1330. ClassDB::bind_method(D_METHOD("get_closest_point", "to_point"), &Curve3D::get_closest_point);
  1331. ClassDB::bind_method(D_METHOD("get_closest_offset", "to_point"), &Curve3D::get_closest_offset);
  1332. ClassDB::bind_method(D_METHOD("tessellate", "max_stages", "tolerance_degrees"), &Curve3D::tessellate, DEFVAL(5), DEFVAL(4));
  1333. ClassDB::bind_method(D_METHOD("_get_data"), &Curve3D::_get_data);
  1334. ClassDB::bind_method(D_METHOD("_set_data"), &Curve3D::_set_data);
  1335. ADD_PROPERTY(PropertyInfo(Variant::REAL, "bake_interval", PROPERTY_HINT_RANGE, "0.01,512,0.01"), "set_bake_interval", "get_bake_interval");
  1336. ADD_PROPERTY(PropertyInfo(Variant::INT, "_data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL), "_set_data", "_get_data");
  1337. ADD_GROUP("Up Vector", "up_vector_");
  1338. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "up_vector_enabled"), "set_up_vector_enabled", "is_up_vector_enabled");
  1339. }
  1340. Curve3D::Curve3D() {
  1341. baked_cache_dirty = false;
  1342. baked_max_ofs = 0;
  1343. /* add_point(Vector3(-1,0,0));
  1344. add_point(Vector3(0,2,0));
  1345. add_point(Vector3(0,3,5));*/
  1346. bake_interval = 0.2;
  1347. up_vector_enabled = true;
  1348. }