curve.cpp 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423
  1. /*************************************************************************/
  2. /* curve.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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_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. }
  49. int Curve::add_point(Vector2 p_pos, real_t left_tangent, real_t right_tangent, TangentMode left_mode, TangentMode right_mode) {
  50. // Add a point and preserve order
  51. // Curve bounds is in 0..1
  52. if (p_pos.x > MAX_X)
  53. p_pos.x = MAX_X;
  54. else if (p_pos.x < MIN_X)
  55. p_pos.x = MIN_X;
  56. int ret = -1;
  57. if (_points.size() == 0) {
  58. _points.push_back(Point(p_pos, left_tangent, right_tangent, left_mode, right_mode));
  59. ret = 0;
  60. } else if (_points.size() == 1) {
  61. // TODO Is the `else` able to handle this block already?
  62. real_t diff = p_pos.x - _points[0].pos.x;
  63. if (diff > 0) {
  64. _points.push_back(Point(p_pos, left_tangent, right_tangent, left_mode, right_mode));
  65. ret = 1;
  66. } else {
  67. _points.insert(0, Point(p_pos, left_tangent, right_tangent, left_mode, right_mode));
  68. ret = 0;
  69. }
  70. } else {
  71. int i = get_index(p_pos.x);
  72. if (i == 0 && p_pos.x < _points[0].pos.x) {
  73. // Insert before anything else
  74. _points.insert(0, Point(p_pos, left_tangent, right_tangent, left_mode, right_mode));
  75. ret = 0;
  76. } else {
  77. // Insert between i and i+1
  78. ++i;
  79. _points.insert(i, Point(p_pos, left_tangent, right_tangent, left_mode, right_mode));
  80. ret = i;
  81. }
  82. }
  83. update_auto_tangents(ret);
  84. mark_dirty();
  85. return ret;
  86. }
  87. int Curve::get_index(real_t offset) const {
  88. // Lower-bound float binary search
  89. int imin = 0;
  90. int imax = _points.size() - 1;
  91. while (imax - imin > 1) {
  92. int m = (imin + imax) / 2;
  93. real_t a = _points[m].pos.x;
  94. real_t b = _points[m + 1].pos.x;
  95. if (a < offset && b < offset) {
  96. imin = m;
  97. } else if (a > offset) {
  98. imax = m;
  99. } else {
  100. return m;
  101. }
  102. }
  103. // Will happen if the offset is out of bounds
  104. if (offset > _points[imax].pos.x)
  105. return imax;
  106. return imin;
  107. }
  108. void Curve::clean_dupes() {
  109. bool dirty = false;
  110. for (int i = 1; i < _points.size(); ++i) {
  111. real_t diff = _points[i - 1].pos.x - _points[i].pos.x;
  112. if (diff <= CMP_EPSILON) {
  113. _points.remove(i);
  114. --i;
  115. dirty = true;
  116. }
  117. }
  118. if (dirty)
  119. mark_dirty();
  120. }
  121. void Curve::set_point_left_tangent(int i, real_t tangent) {
  122. ERR_FAIL_INDEX(i, _points.size());
  123. _points[i].left_tangent = tangent;
  124. _points[i].left_mode = TANGENT_FREE;
  125. mark_dirty();
  126. }
  127. void Curve::set_point_right_tangent(int i, real_t tangent) {
  128. ERR_FAIL_INDEX(i, _points.size());
  129. _points[i].right_tangent = tangent;
  130. _points[i].right_mode = TANGENT_FREE;
  131. mark_dirty();
  132. }
  133. void Curve::set_point_left_mode(int i, TangentMode p_mode) {
  134. ERR_FAIL_INDEX(i, _points.size());
  135. _points[i].left_mode = p_mode;
  136. if (i > 0) {
  137. if (p_mode == TANGENT_LINEAR) {
  138. Vector2 v = (_points[i - 1].pos - _points[i].pos).normalized();
  139. _points[i].left_tangent = v.y / v.x;
  140. }
  141. }
  142. mark_dirty();
  143. }
  144. void Curve::set_point_right_mode(int i, TangentMode p_mode) {
  145. ERR_FAIL_INDEX(i, _points.size());
  146. _points[i].right_mode = p_mode;
  147. if (i + 1 < _points.size()) {
  148. if (p_mode == TANGENT_LINEAR) {
  149. Vector2 v = (_points[i + 1].pos - _points[i].pos).normalized();
  150. _points[i].right_tangent = v.y / v.x;
  151. }
  152. }
  153. mark_dirty();
  154. }
  155. real_t Curve::get_point_left_tangent(int i) const {
  156. ERR_FAIL_INDEX_V(i, _points.size(), 0);
  157. return _points[i].left_tangent;
  158. }
  159. real_t Curve::get_point_right_tangent(int i) const {
  160. ERR_FAIL_INDEX_V(i, _points.size(), 0);
  161. return _points[i].right_tangent;
  162. }
  163. Curve::TangentMode Curve::get_point_left_mode(int i) const {
  164. ERR_FAIL_INDEX_V(i, _points.size(), TANGENT_FREE);
  165. return _points[i].left_mode;
  166. }
  167. Curve::TangentMode Curve::get_point_right_mode(int i) const {
  168. ERR_FAIL_INDEX_V(i, _points.size(), TANGENT_FREE);
  169. return _points[i].right_mode;
  170. }
  171. void Curve::remove_point(int p_index) {
  172. ERR_FAIL_INDEX(p_index, _points.size());
  173. _points.remove(p_index);
  174. mark_dirty();
  175. }
  176. void Curve::clear_points() {
  177. _points.clear();
  178. mark_dirty();
  179. }
  180. void Curve::set_point_value(int p_index, real_t pos) {
  181. ERR_FAIL_INDEX(p_index, _points.size());
  182. _points[p_index].pos.y = pos;
  183. update_auto_tangents(p_index);
  184. mark_dirty();
  185. }
  186. int Curve::set_point_offset(int p_index, float offset) {
  187. ERR_FAIL_INDEX_V(p_index, _points.size(), -1);
  188. Point p = _points[p_index];
  189. remove_point(p_index);
  190. int i = add_point(Vector2(offset, p.pos.y));
  191. _points[i].left_tangent = p.left_tangent;
  192. _points[i].right_tangent = p.right_tangent;
  193. _points[i].left_mode = p.left_mode;
  194. _points[i].right_mode = p.right_mode;
  195. if (p_index != i)
  196. update_auto_tangents(p_index);
  197. update_auto_tangents(i);
  198. return i;
  199. }
  200. Vector2 Curve::get_point_position(int p_index) const {
  201. ERR_FAIL_INDEX_V(p_index, _points.size(), Vector2(0, 0));
  202. return _points[p_index].pos;
  203. }
  204. Curve::Point Curve::get_point(int p_index) const {
  205. ERR_FAIL_INDEX_V(p_index, _points.size(), Point());
  206. return _points[p_index];
  207. }
  208. void Curve::update_auto_tangents(int i) {
  209. Point &p = _points[i];
  210. if (i > 0) {
  211. if (p.left_mode == TANGENT_LINEAR) {
  212. Vector2 v = (_points[i - 1].pos - p.pos).normalized();
  213. p.left_tangent = v.y / v.x;
  214. }
  215. if (_points[i - 1].right_mode == TANGENT_LINEAR) {
  216. Vector2 v = (_points[i - 1].pos - p.pos).normalized();
  217. _points[i - 1].right_tangent = v.y / v.x;
  218. }
  219. }
  220. if (i + 1 < _points.size()) {
  221. if (p.right_mode == TANGENT_LINEAR && i + 1 < _points.size()) {
  222. Vector2 v = (_points[i + 1].pos - p.pos).normalized();
  223. p.right_tangent = v.y / v.x;
  224. }
  225. if (_points[i + 1].left_mode == TANGENT_LINEAR) {
  226. Vector2 v = (_points[i + 1].pos - p.pos).normalized();
  227. _points[i + 1].left_tangent = v.y / v.x;
  228. }
  229. }
  230. }
  231. #define MIN_Y_RANGE 0.01
  232. void Curve::set_min_value(float p_min) {
  233. if (p_min > _max_value - MIN_Y_RANGE)
  234. _min_value = _max_value - MIN_Y_RANGE;
  235. else
  236. _min_value = p_min;
  237. // Note: min and max are indicative values,
  238. // it's still possible that existing points are out of range at this point.
  239. emit_signal(SIGNAL_RANGE_CHANGED);
  240. }
  241. void Curve::set_max_value(float p_max) {
  242. if (p_max < _min_value + MIN_Y_RANGE)
  243. _max_value = _min_value + MIN_Y_RANGE;
  244. else
  245. _max_value = p_max;
  246. emit_signal(SIGNAL_RANGE_CHANGED);
  247. }
  248. real_t Curve::interpolate(real_t offset) const {
  249. if (_points.size() == 0)
  250. return 0;
  251. if (_points.size() == 1)
  252. return _points[0].pos.y;
  253. int i = get_index(offset);
  254. if (i == _points.size() - 1)
  255. return _points[i].pos.y;
  256. real_t local = offset - _points[i].pos.x;
  257. if (i == 0 && local <= 0)
  258. return _points[0].pos.y;
  259. return interpolate_local_nocheck(i, local);
  260. }
  261. real_t Curve::interpolate_local_nocheck(int index, real_t local_offset) const {
  262. const Point a = _points[index];
  263. const Point b = _points[index + 1];
  264. /* Cubic bezier
  265. *
  266. * ac-----bc
  267. * / \
  268. * / \ Here with a.right_tangent > 0
  269. * / \ and b.left_tangent < 0
  270. * / \
  271. * a b
  272. *
  273. * |-d1--|-d2--|-d3--|
  274. *
  275. * d1 == d2 == d3 == d / 3
  276. */
  277. // Control points are chosen at equal distances
  278. real_t d = b.pos.x - a.pos.x;
  279. if (Math::abs(d) <= CMP_EPSILON)
  280. return b.pos.y;
  281. local_offset /= d;
  282. d /= 3.0;
  283. real_t yac = a.pos.y + d * a.right_tangent;
  284. real_t ybc = b.pos.y - d * b.left_tangent;
  285. real_t y = _bezier_interp(local_offset, a.pos.y, yac, ybc, b.pos.y);
  286. return y;
  287. }
  288. void Curve::mark_dirty() {
  289. _baked_cache_dirty = true;
  290. emit_signal(CoreStringNames::get_singleton()->changed);
  291. }
  292. Array Curve::get_data() const {
  293. Array output;
  294. const unsigned int ELEMS = 5;
  295. output.resize(_points.size() * ELEMS);
  296. for (int j = 0; j < _points.size(); ++j) {
  297. const Point p = _points[j];
  298. int i = j * ELEMS;
  299. output[i] = p.pos;
  300. output[i + 1] = p.left_tangent;
  301. output[i + 2] = p.right_tangent;
  302. output[i + 3] = p.left_mode;
  303. output[i + 4] = p.right_mode;
  304. }
  305. return output;
  306. }
  307. void Curve::set_data(Array input) {
  308. const unsigned int ELEMS = 5;
  309. ERR_FAIL_COND(input.size() % ELEMS != 0);
  310. _points.clear();
  311. // Validate input
  312. for (int i = 0; i < input.size(); i += ELEMS) {
  313. ERR_FAIL_COND(input[i].get_type() != Variant::VECTOR2);
  314. ERR_FAIL_COND(!input[i + 1].is_num());
  315. ERR_FAIL_COND(input[i + 2].get_type() != Variant::REAL);
  316. ERR_FAIL_COND(input[i + 3].get_type() != Variant::INT);
  317. int left_mode = input[i + 3];
  318. ERR_FAIL_COND(left_mode < 0 || left_mode >= TANGENT_MODE_COUNT);
  319. ERR_FAIL_COND(input[i + 4].get_type() != Variant::INT);
  320. int right_mode = input[i + 4];
  321. ERR_FAIL_COND(right_mode < 0 || right_mode >= TANGENT_MODE_COUNT);
  322. }
  323. _points.resize(input.size() / ELEMS);
  324. for (int j = 0; j < _points.size(); ++j) {
  325. Point &p = _points[j];
  326. int i = j * ELEMS;
  327. p.pos = input[i];
  328. p.left_tangent = input[i + 1];
  329. p.right_tangent = input[i + 2];
  330. // TODO For some reason the compiler won't convert from Variant to enum
  331. int left_mode = input[i + 3];
  332. int right_mode = input[i + 4];
  333. p.left_mode = (TangentMode)left_mode;
  334. p.right_mode = (TangentMode)right_mode;
  335. }
  336. mark_dirty();
  337. }
  338. void Curve::bake() {
  339. _baked_cache.clear();
  340. _baked_cache.resize(_bake_resolution);
  341. for (int i = 1; i < _bake_resolution - 1; ++i) {
  342. real_t x = i / static_cast<real_t>(_bake_resolution);
  343. real_t y = interpolate(x);
  344. _baked_cache[i] = y;
  345. }
  346. if (_points.size() != 0) {
  347. _baked_cache[0] = _points[0].pos.y;
  348. _baked_cache[_baked_cache.size() - 1] = _points[_points.size() - 1].pos.y;
  349. }
  350. _baked_cache_dirty = false;
  351. }
  352. void Curve::set_bake_resolution(int p_resolution) {
  353. ERR_FAIL_COND(p_resolution < 1);
  354. ERR_FAIL_COND(p_resolution > 1000);
  355. _bake_resolution = p_resolution;
  356. _baked_cache_dirty = true;
  357. }
  358. real_t Curve::interpolate_baked(real_t offset) {
  359. if (_baked_cache_dirty) {
  360. // Last-second bake if not done already
  361. bake();
  362. }
  363. // Special cases if the cache is too small
  364. if (_baked_cache.size() == 0) {
  365. if (_points.size() == 0)
  366. return 0;
  367. return _points[0].pos.y;
  368. } else if (_baked_cache.size() == 1) {
  369. return _baked_cache[0];
  370. }
  371. // Get interpolation index
  372. real_t fi = offset * _baked_cache.size();
  373. int i = Math::floor(fi);
  374. if (i < 0) {
  375. i = 0;
  376. fi = 0;
  377. } else if (i >= _baked_cache.size()) {
  378. i = _baked_cache.size() - 1;
  379. fi = 0;
  380. }
  381. // Interpolate
  382. if (i + 1 < _baked_cache.size()) {
  383. real_t t = fi - i;
  384. return Math::lerp(_baked_cache[i], _baked_cache[i + 1], t);
  385. } else {
  386. return _baked_cache[_baked_cache.size() - 1];
  387. }
  388. }
  389. void Curve::_bind_methods() {
  390. 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));
  391. ClassDB::bind_method(D_METHOD("remove_point", "index"), &Curve::remove_point);
  392. ClassDB::bind_method(D_METHOD("clear_points"), &Curve::clear_points);
  393. ClassDB::bind_method(D_METHOD("get_point_position", "index"), &Curve::get_point_position);
  394. ClassDB::bind_method(D_METHOD("set_point_value", "index", "y"), &Curve::set_point_value);
  395. ClassDB::bind_method(D_METHOD("set_point_offset", "index", "offset"), &Curve::set_point_offset);
  396. ClassDB::bind_method(D_METHOD("interpolate", "offset"), &Curve::interpolate);
  397. ClassDB::bind_method(D_METHOD("interpolate_baked", "offset"), &Curve::interpolate_baked);
  398. ClassDB::bind_method(D_METHOD("get_point_left_tangent", "index"), &Curve::get_point_left_tangent);
  399. ClassDB::bind_method(D_METHOD("get_point_right_tangent", "index"), &Curve::get_point_right_tangent);
  400. ClassDB::bind_method(D_METHOD("get_point_left_mode", "index"), &Curve::get_point_left_mode);
  401. ClassDB::bind_method(D_METHOD("get_point_right_mode", "index"), &Curve::get_point_right_mode);
  402. ClassDB::bind_method(D_METHOD("set_point_left_tangent", "index", "tangent"), &Curve::set_point_left_tangent);
  403. ClassDB::bind_method(D_METHOD("set_point_right_tangent", "index", "tangent"), &Curve::set_point_right_tangent);
  404. ClassDB::bind_method(D_METHOD("set_point_left_mode", "index", "mode"), &Curve::set_point_left_mode);
  405. ClassDB::bind_method(D_METHOD("set_point_right_mode", "index", "mode"), &Curve::set_point_right_mode);
  406. ClassDB::bind_method(D_METHOD("get_min_value"), &Curve::get_min_value);
  407. ClassDB::bind_method(D_METHOD("set_min_value", "min"), &Curve::set_min_value);
  408. ClassDB::bind_method(D_METHOD("get_max_value"), &Curve::get_max_value);
  409. ClassDB::bind_method(D_METHOD("set_max_value", "max"), &Curve::set_max_value);
  410. ClassDB::bind_method(D_METHOD("clean_dupes"), &Curve::clean_dupes);
  411. ClassDB::bind_method(D_METHOD("bake"), &Curve::bake);
  412. ClassDB::bind_method(D_METHOD("get_bake_resolution"), &Curve::get_bake_resolution);
  413. ClassDB::bind_method(D_METHOD("set_bake_resolution", "resolution"), &Curve::set_bake_resolution);
  414. ClassDB::bind_method(D_METHOD("_get_data"), &Curve::get_data);
  415. ClassDB::bind_method(D_METHOD("_set_data", "data"), &Curve::set_data);
  416. ADD_PROPERTY(PropertyInfo(Variant::REAL, "min_value", PROPERTY_HINT_RANGE, "-1024,1024,0.01"), "set_min_value", "get_min_value");
  417. ADD_PROPERTY(PropertyInfo(Variant::REAL, "max_value", PROPERTY_HINT_RANGE, "-1024,1024,0.01"), "set_max_value", "get_max_value");
  418. ADD_PROPERTY(PropertyInfo(Variant::INT, "bake_resolution", PROPERTY_HINT_RANGE, "1,1000,1"), "set_bake_resolution", "get_bake_resolution");
  419. ADD_PROPERTY(PropertyInfo(Variant::INT, "_data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), "_set_data", "_get_data");
  420. ADD_SIGNAL(MethodInfo(SIGNAL_RANGE_CHANGED));
  421. BIND_ENUM_CONSTANT(TANGENT_FREE);
  422. BIND_ENUM_CONSTANT(TANGENT_LINEAR);
  423. BIND_ENUM_CONSTANT(TANGENT_MODE_COUNT);
  424. }
  425. int Curve2D::get_point_count() const {
  426. return points.size();
  427. }
  428. void Curve2D::add_point(const Vector2 &p_pos, const Vector2 &p_in, const Vector2 &p_out, int p_atpos) {
  429. Point n;
  430. n.pos = p_pos;
  431. n.in = p_in;
  432. n.out = p_out;
  433. if (p_atpos >= 0 && p_atpos < points.size())
  434. points.insert(p_atpos, n);
  435. else
  436. points.push_back(n);
  437. baked_cache_dirty = true;
  438. emit_signal(CoreStringNames::get_singleton()->changed);
  439. }
  440. void Curve2D::set_point_position(int p_index, const Vector2 &p_pos) {
  441. ERR_FAIL_INDEX(p_index, points.size());
  442. points[p_index].pos = p_pos;
  443. baked_cache_dirty = true;
  444. emit_signal(CoreStringNames::get_singleton()->changed);
  445. }
  446. Vector2 Curve2D::get_point_position(int p_index) const {
  447. ERR_FAIL_INDEX_V(p_index, points.size(), Vector2());
  448. return points[p_index].pos;
  449. }
  450. void Curve2D::set_point_in(int p_index, const Vector2 &p_in) {
  451. ERR_FAIL_INDEX(p_index, points.size());
  452. points[p_index].in = p_in;
  453. baked_cache_dirty = true;
  454. emit_signal(CoreStringNames::get_singleton()->changed);
  455. }
  456. Vector2 Curve2D::get_point_in(int p_index) const {
  457. ERR_FAIL_INDEX_V(p_index, points.size(), Vector2());
  458. return points[p_index].in;
  459. }
  460. void Curve2D::set_point_out(int p_index, const Vector2 &p_out) {
  461. ERR_FAIL_INDEX(p_index, points.size());
  462. points[p_index].out = p_out;
  463. baked_cache_dirty = true;
  464. emit_signal(CoreStringNames::get_singleton()->changed);
  465. }
  466. Vector2 Curve2D::get_point_out(int p_index) const {
  467. ERR_FAIL_INDEX_V(p_index, points.size(), Vector2());
  468. return points[p_index].out;
  469. }
  470. void Curve2D::remove_point(int p_index) {
  471. ERR_FAIL_INDEX(p_index, points.size());
  472. points.remove(p_index);
  473. baked_cache_dirty = true;
  474. emit_signal(CoreStringNames::get_singleton()->changed);
  475. }
  476. void Curve2D::clear_points() {
  477. if (!points.empty()) {
  478. points.clear();
  479. baked_cache_dirty = true;
  480. emit_signal(CoreStringNames::get_singleton()->changed);
  481. }
  482. }
  483. Vector2 Curve2D::interpolate(int p_index, float p_offset) const {
  484. int pc = points.size();
  485. ERR_FAIL_COND_V(pc == 0, Vector2());
  486. if (p_index >= pc - 1)
  487. return points[pc - 1].pos;
  488. else if (p_index < 0)
  489. return points[0].pos;
  490. Vector2 p0 = points[p_index].pos;
  491. Vector2 p1 = p0 + points[p_index].out;
  492. Vector2 p3 = points[p_index + 1].pos;
  493. Vector2 p2 = p3 + points[p_index + 1].in;
  494. return _bezier_interp(p_offset, p0, p1, p2, p3);
  495. }
  496. Vector2 Curve2D::interpolatef(real_t p_findex) const {
  497. if (p_findex < 0)
  498. p_findex = 0;
  499. else if (p_findex >= points.size())
  500. p_findex = points.size();
  501. return interpolate((int)p_findex, Math::fmod(p_findex, (real_t)1.0));
  502. }
  503. 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 {
  504. float mp = p_begin + (p_end - p_begin) * 0.5;
  505. Vector2 beg = _bezier_interp(p_begin, p_a, p_a + p_out, p_b + p_in, p_b);
  506. Vector2 mid = _bezier_interp(mp, p_a, p_a + p_out, p_b + p_in, p_b);
  507. Vector2 end = _bezier_interp(p_end, p_a, p_a + p_out, p_b + p_in, p_b);
  508. Vector2 na = (mid - beg).normalized();
  509. Vector2 nb = (end - mid).normalized();
  510. float dp = na.dot(nb);
  511. if (dp < Math::cos(Math::deg2rad(p_tol))) {
  512. r_bake[mp] = mid;
  513. }
  514. if (p_depth < p_max_depth) {
  515. _bake_segment2d(r_bake, p_begin, mp, p_a, p_out, p_b, p_in, p_depth + 1, p_max_depth, p_tol);
  516. _bake_segment2d(r_bake, mp, p_end, p_a, p_out, p_b, p_in, p_depth + 1, p_max_depth, p_tol);
  517. }
  518. }
  519. void Curve2D::_bake() const {
  520. if (!baked_cache_dirty)
  521. return;
  522. baked_max_ofs = 0;
  523. baked_cache_dirty = false;
  524. if (points.size() == 0) {
  525. baked_point_cache.resize(0);
  526. return;
  527. }
  528. if (points.size() == 1) {
  529. baked_point_cache.resize(1);
  530. baked_point_cache.set(0, points[0].pos);
  531. return;
  532. }
  533. Vector2 pos = points[0].pos;
  534. List<Vector2> pointlist;
  535. pointlist.push_back(pos); //start always from origin
  536. for (int i = 0; i < points.size() - 1; i++) {
  537. float step = 0.1; // at least 10 substeps ought to be enough?
  538. float p = 0;
  539. while (p < 1.0) {
  540. float np = p + step;
  541. if (np > 1.0)
  542. np = 1.0;
  543. 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);
  544. float d = pos.distance_to(npp);
  545. if (d > bake_interval) {
  546. // OK! between P and NP there _has_ to be Something, let's go searching!
  547. int iterations = 10; //lots of detail!
  548. float low = p;
  549. float hi = np;
  550. float mid = low + (hi - low) * 0.5;
  551. for (int j = 0; j < iterations; j++) {
  552. 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);
  553. d = pos.distance_to(npp);
  554. if (bake_interval < d)
  555. hi = mid;
  556. else
  557. low = mid;
  558. mid = low + (hi - low) * 0.5;
  559. }
  560. pos = npp;
  561. p = mid;
  562. pointlist.push_back(pos);
  563. } else {
  564. p = np;
  565. }
  566. }
  567. }
  568. Vector2 lastpos = points[points.size() - 1].pos;
  569. float rem = pos.distance_to(lastpos);
  570. baked_max_ofs = (pointlist.size() - 1) * bake_interval + rem;
  571. pointlist.push_back(lastpos);
  572. baked_point_cache.resize(pointlist.size());
  573. PoolVector2Array::Write w = baked_point_cache.write();
  574. int idx = 0;
  575. for (List<Vector2>::Element *E = pointlist.front(); E; E = E->next()) {
  576. w[idx] = E->get();
  577. idx++;
  578. }
  579. }
  580. float Curve2D::get_baked_length() const {
  581. if (baked_cache_dirty)
  582. _bake();
  583. return baked_max_ofs;
  584. }
  585. Vector2 Curve2D::interpolate_baked(float p_offset, bool p_cubic) const {
  586. if (baked_cache_dirty)
  587. _bake();
  588. //validate//
  589. int pc = baked_point_cache.size();
  590. if (pc == 0) {
  591. ERR_EXPLAIN("No points in Curve2D");
  592. ERR_FAIL_COND_V(pc == 0, Vector2());
  593. }
  594. if (pc == 1)
  595. return baked_point_cache.get(0);
  596. int bpc = baked_point_cache.size();
  597. PoolVector2Array::Read r = baked_point_cache.read();
  598. if (p_offset < 0)
  599. return r[0];
  600. if (p_offset >= baked_max_ofs)
  601. return r[bpc - 1];
  602. int idx = Math::floor((double)p_offset / (double)bake_interval);
  603. float frac = Math::fmod(p_offset, (float)bake_interval);
  604. if (idx >= bpc - 1) {
  605. return r[bpc - 1];
  606. } else if (idx == bpc - 2) {
  607. frac /= Math::fmod(baked_max_ofs, bake_interval);
  608. } else {
  609. frac /= bake_interval;
  610. }
  611. if (p_cubic) {
  612. Vector2 pre = idx > 0 ? r[idx - 1] : r[idx];
  613. Vector2 post = (idx < (bpc - 2)) ? r[idx + 2] : r[idx + 1];
  614. return r[idx].cubic_interpolate(r[idx + 1], pre, post, frac);
  615. } else {
  616. return r[idx].linear_interpolate(r[idx + 1], frac);
  617. }
  618. }
  619. PoolVector2Array Curve2D::get_baked_points() const {
  620. if (baked_cache_dirty)
  621. _bake();
  622. return baked_point_cache;
  623. }
  624. void Curve2D::set_bake_interval(float p_tolerance) {
  625. bake_interval = p_tolerance;
  626. baked_cache_dirty = true;
  627. emit_signal(CoreStringNames::get_singleton()->changed);
  628. }
  629. float Curve2D::get_bake_interval() const {
  630. return bake_interval;
  631. }
  632. Dictionary Curve2D::_get_data() const {
  633. Dictionary dc;
  634. PoolVector2Array d;
  635. d.resize(points.size() * 3);
  636. PoolVector2Array::Write w = d.write();
  637. for (int i = 0; i < points.size(); i++) {
  638. w[i * 3 + 0] = points[i].in;
  639. w[i * 3 + 1] = points[i].out;
  640. w[i * 3 + 2] = points[i].pos;
  641. }
  642. w = PoolVector2Array::Write();
  643. dc["points"] = d;
  644. return dc;
  645. }
  646. void Curve2D::_set_data(const Dictionary &p_data) {
  647. ERR_FAIL_COND(!p_data.has("points"));
  648. PoolVector2Array rp = p_data["points"];
  649. int pc = rp.size();
  650. ERR_FAIL_COND(pc % 3 != 0);
  651. points.resize(pc / 3);
  652. PoolVector2Array::Read r = rp.read();
  653. for (int i = 0; i < points.size(); i++) {
  654. points[i].in = r[i * 3 + 0];
  655. points[i].out = r[i * 3 + 1];
  656. points[i].pos = r[i * 3 + 2];
  657. }
  658. baked_cache_dirty = true;
  659. }
  660. PoolVector2Array Curve2D::tessellate(int p_max_stages, float p_tolerance) const {
  661. PoolVector2Array tess;
  662. if (points.size() == 0) {
  663. return tess;
  664. }
  665. Vector<Map<float, Vector2> > midpoints;
  666. midpoints.resize(points.size() - 1);
  667. int pc = 1;
  668. for (int i = 0; i < points.size() - 1; i++) {
  669. _bake_segment2d(midpoints[i], 0, 1, points[i].pos, points[i].out, points[i + 1].pos, points[i + 1].in, 0, p_max_stages, p_tolerance);
  670. pc++;
  671. pc += midpoints[i].size();
  672. }
  673. tess.resize(pc);
  674. PoolVector2Array::Write bpw = tess.write();
  675. bpw[0] = points[0].pos;
  676. int pidx = 0;
  677. for (int i = 0; i < points.size() - 1; i++) {
  678. for (Map<float, Vector2>::Element *E = midpoints[i].front(); E; E = E->next()) {
  679. pidx++;
  680. bpw[pidx] = E->get();
  681. }
  682. pidx++;
  683. bpw[pidx] = points[i + 1].pos;
  684. }
  685. bpw = PoolVector2Array::Write();
  686. return tess;
  687. }
  688. void Curve2D::_bind_methods() {
  689. ClassDB::bind_method(D_METHOD("get_point_count"), &Curve2D::get_point_count);
  690. ClassDB::bind_method(D_METHOD("add_point", "position", "in", "out", "at_position"), &Curve2D::add_point, DEFVAL(Vector2()), DEFVAL(Vector2()), DEFVAL(-1));
  691. ClassDB::bind_method(D_METHOD("set_point_position", "idx", "position"), &Curve2D::set_point_position);
  692. ClassDB::bind_method(D_METHOD("get_point_position", "idx"), &Curve2D::get_point_position);
  693. ClassDB::bind_method(D_METHOD("set_point_in", "idx", "position"), &Curve2D::set_point_in);
  694. ClassDB::bind_method(D_METHOD("get_point_in", "idx"), &Curve2D::get_point_in);
  695. ClassDB::bind_method(D_METHOD("set_point_out", "idx", "position"), &Curve2D::set_point_out);
  696. ClassDB::bind_method(D_METHOD("get_point_out", "idx"), &Curve2D::get_point_out);
  697. ClassDB::bind_method(D_METHOD("remove_point", "idx"), &Curve2D::remove_point);
  698. ClassDB::bind_method(D_METHOD("clear_points"), &Curve2D::clear_points);
  699. ClassDB::bind_method(D_METHOD("interpolate", "idx", "t"), &Curve2D::interpolate);
  700. ClassDB::bind_method(D_METHOD("interpolatef", "fofs"), &Curve2D::interpolatef);
  701. //ClassDB::bind_method(D_METHOD("bake","subdivs"),&Curve2D::bake,DEFVAL(10));
  702. ClassDB::bind_method(D_METHOD("set_bake_interval", "distance"), &Curve2D::set_bake_interval);
  703. ClassDB::bind_method(D_METHOD("get_bake_interval"), &Curve2D::get_bake_interval);
  704. ClassDB::bind_method(D_METHOD("get_baked_length"), &Curve2D::get_baked_length);
  705. ClassDB::bind_method(D_METHOD("interpolate_baked", "offset", "cubic"), &Curve2D::interpolate_baked, DEFVAL(false));
  706. ClassDB::bind_method(D_METHOD("get_baked_points"), &Curve2D::get_baked_points);
  707. ClassDB::bind_method(D_METHOD("tessellate", "max_stages", "tolerance_degrees"), &Curve2D::tessellate, DEFVAL(5), DEFVAL(4));
  708. ClassDB::bind_method(D_METHOD("_get_data"), &Curve2D::_get_data);
  709. ClassDB::bind_method(D_METHOD("_set_data"), &Curve2D::_set_data);
  710. ADD_PROPERTY(PropertyInfo(Variant::REAL, "bake_interval", PROPERTY_HINT_RANGE, "0.01,512,0.01"), "set_bake_interval", "get_bake_interval");
  711. ADD_PROPERTY(PropertyInfo(Variant::INT, "_data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), "_set_data", "_get_data");
  712. }
  713. Curve2D::Curve2D() {
  714. baked_cache_dirty = false;
  715. baked_max_ofs = 0;
  716. /* add_point(Vector2(-1,0,0));
  717. add_point(Vector2(0,2,0));
  718. add_point(Vector2(0,3,5));*/
  719. bake_interval = 5;
  720. }
  721. /***********************************************************************************/
  722. /***********************************************************************************/
  723. /***********************************************************************************/
  724. /***********************************************************************************/
  725. /***********************************************************************************/
  726. /***********************************************************************************/
  727. int Curve3D::get_point_count() const {
  728. return points.size();
  729. }
  730. void Curve3D::add_point(const Vector3 &p_pos, const Vector3 &p_in, const Vector3 &p_out, int p_atpos) {
  731. Point n;
  732. n.pos = p_pos;
  733. n.in = p_in;
  734. n.out = p_out;
  735. if (p_atpos >= 0 && p_atpos < points.size())
  736. points.insert(p_atpos, n);
  737. else
  738. points.push_back(n);
  739. baked_cache_dirty = true;
  740. emit_signal(CoreStringNames::get_singleton()->changed);
  741. }
  742. void Curve3D::set_point_position(int p_index, const Vector3 &p_pos) {
  743. ERR_FAIL_INDEX(p_index, points.size());
  744. points[p_index].pos = p_pos;
  745. baked_cache_dirty = true;
  746. emit_signal(CoreStringNames::get_singleton()->changed);
  747. }
  748. Vector3 Curve3D::get_point_position(int p_index) const {
  749. ERR_FAIL_INDEX_V(p_index, points.size(), Vector3());
  750. return points[p_index].pos;
  751. }
  752. void Curve3D::set_point_tilt(int p_index, float p_tilt) {
  753. ERR_FAIL_INDEX(p_index, points.size());
  754. points[p_index].tilt = p_tilt;
  755. baked_cache_dirty = true;
  756. emit_signal(CoreStringNames::get_singleton()->changed);
  757. }
  758. float Curve3D::get_point_tilt(int p_index) const {
  759. ERR_FAIL_INDEX_V(p_index, points.size(), 0);
  760. return points[p_index].tilt;
  761. }
  762. void Curve3D::set_point_in(int p_index, const Vector3 &p_in) {
  763. ERR_FAIL_INDEX(p_index, points.size());
  764. points[p_index].in = p_in;
  765. baked_cache_dirty = true;
  766. emit_signal(CoreStringNames::get_singleton()->changed);
  767. }
  768. Vector3 Curve3D::get_point_in(int p_index) const {
  769. ERR_FAIL_INDEX_V(p_index, points.size(), Vector3());
  770. return points[p_index].in;
  771. }
  772. void Curve3D::set_point_out(int p_index, const Vector3 &p_out) {
  773. ERR_FAIL_INDEX(p_index, points.size());
  774. points[p_index].out = p_out;
  775. baked_cache_dirty = true;
  776. emit_signal(CoreStringNames::get_singleton()->changed);
  777. }
  778. Vector3 Curve3D::get_point_out(int p_index) const {
  779. ERR_FAIL_INDEX_V(p_index, points.size(), Vector3());
  780. return points[p_index].out;
  781. }
  782. void Curve3D::remove_point(int p_index) {
  783. ERR_FAIL_INDEX(p_index, points.size());
  784. points.remove(p_index);
  785. baked_cache_dirty = true;
  786. emit_signal(CoreStringNames::get_singleton()->changed);
  787. }
  788. void Curve3D::clear_points() {
  789. if (!points.empty()) {
  790. points.clear();
  791. baked_cache_dirty = true;
  792. emit_signal(CoreStringNames::get_singleton()->changed);
  793. }
  794. }
  795. Vector3 Curve3D::interpolate(int p_index, float p_offset) const {
  796. int pc = points.size();
  797. ERR_FAIL_COND_V(pc == 0, Vector3());
  798. if (p_index >= pc - 1)
  799. return points[pc - 1].pos;
  800. else if (p_index < 0)
  801. return points[0].pos;
  802. Vector3 p0 = points[p_index].pos;
  803. Vector3 p1 = p0 + points[p_index].out;
  804. Vector3 p3 = points[p_index + 1].pos;
  805. Vector3 p2 = p3 + points[p_index + 1].in;
  806. return _bezier_interp(p_offset, p0, p1, p2, p3);
  807. }
  808. Vector3 Curve3D::interpolatef(real_t p_findex) const {
  809. if (p_findex < 0)
  810. p_findex = 0;
  811. else if (p_findex >= points.size())
  812. p_findex = points.size();
  813. return interpolate((int)p_findex, Math::fmod(p_findex, (real_t)1.0));
  814. }
  815. 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 {
  816. float mp = p_begin + (p_end - p_begin) * 0.5;
  817. Vector3 beg = _bezier_interp(p_begin, p_a, p_a + p_out, p_b + p_in, p_b);
  818. Vector3 mid = _bezier_interp(mp, p_a, p_a + p_out, p_b + p_in, p_b);
  819. Vector3 end = _bezier_interp(p_end, p_a, p_a + p_out, p_b + p_in, p_b);
  820. Vector3 na = (mid - beg).normalized();
  821. Vector3 nb = (end - mid).normalized();
  822. float dp = na.dot(nb);
  823. if (dp < Math::cos(Math::deg2rad(p_tol))) {
  824. r_bake[mp] = mid;
  825. }
  826. if (p_depth < p_max_depth) {
  827. _bake_segment3d(r_bake, p_begin, mp, p_a, p_out, p_b, p_in, p_depth + 1, p_max_depth, p_tol);
  828. _bake_segment3d(r_bake, mp, p_end, p_a, p_out, p_b, p_in, p_depth + 1, p_max_depth, p_tol);
  829. }
  830. }
  831. void Curve3D::_bake() const {
  832. if (!baked_cache_dirty)
  833. return;
  834. baked_max_ofs = 0;
  835. baked_cache_dirty = false;
  836. if (points.size() == 0) {
  837. baked_point_cache.resize(0);
  838. baked_tilt_cache.resize(0);
  839. return;
  840. }
  841. if (points.size() == 1) {
  842. baked_point_cache.resize(1);
  843. baked_point_cache.set(0, points[0].pos);
  844. baked_tilt_cache.resize(1);
  845. baked_tilt_cache.set(0, points[0].tilt);
  846. return;
  847. }
  848. Vector3 pos = points[0].pos;
  849. List<Plane> pointlist;
  850. pointlist.push_back(Plane(pos, points[0].tilt));
  851. for (int i = 0; i < points.size() - 1; i++) {
  852. float step = 0.1; // at least 10 substeps ought to be enough?
  853. float p = 0;
  854. while (p < 1.0) {
  855. float np = p + step;
  856. if (np > 1.0)
  857. np = 1.0;
  858. 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);
  859. float d = pos.distance_to(npp);
  860. if (d > bake_interval) {
  861. // OK! between P and NP there _has_ to be Something, let's go searching!
  862. int iterations = 10; //lots of detail!
  863. float low = p;
  864. float hi = np;
  865. float mid = low + (hi - low) * 0.5;
  866. for (int j = 0; j < iterations; j++) {
  867. 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);
  868. d = pos.distance_to(npp);
  869. if (bake_interval < d)
  870. hi = mid;
  871. else
  872. low = mid;
  873. mid = low + (hi - low) * 0.5;
  874. }
  875. pos = npp;
  876. p = mid;
  877. Plane post;
  878. post.normal = pos;
  879. post.d = Math::lerp(points[i].tilt, points[i + 1].tilt, mid);
  880. pointlist.push_back(post);
  881. } else {
  882. p = np;
  883. }
  884. }
  885. }
  886. Vector3 lastpos = points[points.size() - 1].pos;
  887. float lastilt = points[points.size() - 1].tilt;
  888. float rem = pos.distance_to(lastpos);
  889. baked_max_ofs = (pointlist.size() - 1) * bake_interval + rem;
  890. pointlist.push_back(Plane(lastpos, lastilt));
  891. baked_point_cache.resize(pointlist.size());
  892. PoolVector3Array::Write w = baked_point_cache.write();
  893. int idx = 0;
  894. baked_tilt_cache.resize(pointlist.size());
  895. PoolRealArray::Write wt = baked_tilt_cache.write();
  896. for (List<Plane>::Element *E = pointlist.front(); E; E = E->next()) {
  897. w[idx] = E->get().normal;
  898. wt[idx] = E->get().d;
  899. idx++;
  900. }
  901. }
  902. float Curve3D::get_baked_length() const {
  903. if (baked_cache_dirty)
  904. _bake();
  905. return baked_max_ofs;
  906. }
  907. Vector3 Curve3D::interpolate_baked(float p_offset, bool p_cubic) const {
  908. if (baked_cache_dirty)
  909. _bake();
  910. //validate//
  911. int pc = baked_point_cache.size();
  912. if (pc == 0) {
  913. ERR_EXPLAIN("No points in Curve3D");
  914. ERR_FAIL_COND_V(pc == 0, Vector3());
  915. }
  916. if (pc == 1)
  917. return baked_point_cache.get(0);
  918. int bpc = baked_point_cache.size();
  919. PoolVector3Array::Read r = baked_point_cache.read();
  920. if (p_offset < 0)
  921. return r[0];
  922. if (p_offset >= baked_max_ofs)
  923. return r[bpc - 1];
  924. int idx = Math::floor((double)p_offset / (double)bake_interval);
  925. float frac = Math::fmod(p_offset, bake_interval);
  926. if (idx >= bpc - 1) {
  927. return r[bpc - 1];
  928. } else if (idx == bpc - 2) {
  929. frac /= Math::fmod(baked_max_ofs, bake_interval);
  930. } else {
  931. frac /= bake_interval;
  932. }
  933. if (p_cubic) {
  934. Vector3 pre = idx > 0 ? r[idx - 1] : r[idx];
  935. Vector3 post = (idx < (bpc - 2)) ? r[idx + 2] : r[idx + 1];
  936. return r[idx].cubic_interpolate(r[idx + 1], pre, post, frac);
  937. } else {
  938. return r[idx].linear_interpolate(r[idx + 1], frac);
  939. }
  940. }
  941. float Curve3D::interpolate_baked_tilt(float p_offset) const {
  942. if (baked_cache_dirty)
  943. _bake();
  944. //validate//
  945. int pc = baked_tilt_cache.size();
  946. if (pc == 0) {
  947. ERR_EXPLAIN("No tilts in Curve3D");
  948. ERR_FAIL_COND_V(pc == 0, 0);
  949. }
  950. if (pc == 1)
  951. return baked_tilt_cache.get(0);
  952. int bpc = baked_tilt_cache.size();
  953. PoolRealArray::Read r = baked_tilt_cache.read();
  954. if (p_offset < 0)
  955. return r[0];
  956. if (p_offset >= baked_max_ofs)
  957. return r[bpc - 1];
  958. int idx = Math::floor((double)p_offset / (double)bake_interval);
  959. float frac = Math::fmod(p_offset, bake_interval);
  960. if (idx >= bpc - 1) {
  961. return r[bpc - 1];
  962. } else if (idx == bpc - 2) {
  963. frac /= Math::fmod(baked_max_ofs, bake_interval);
  964. } else {
  965. frac /= bake_interval;
  966. }
  967. return Math::lerp(r[idx], r[idx + 1], frac);
  968. }
  969. PoolVector3Array Curve3D::get_baked_points() const {
  970. if (baked_cache_dirty)
  971. _bake();
  972. return baked_point_cache;
  973. }
  974. PoolRealArray Curve3D::get_baked_tilts() const {
  975. if (baked_cache_dirty)
  976. _bake();
  977. return baked_tilt_cache;
  978. }
  979. void Curve3D::set_bake_interval(float p_tolerance) {
  980. bake_interval = p_tolerance;
  981. baked_cache_dirty = true;
  982. emit_signal(CoreStringNames::get_singleton()->changed);
  983. }
  984. float Curve3D::get_bake_interval() const {
  985. return bake_interval;
  986. }
  987. Dictionary Curve3D::_get_data() const {
  988. Dictionary dc;
  989. PoolVector3Array d;
  990. d.resize(points.size() * 3);
  991. PoolVector3Array::Write w = d.write();
  992. PoolRealArray t;
  993. t.resize(points.size());
  994. PoolRealArray::Write wt = t.write();
  995. for (int i = 0; i < points.size(); i++) {
  996. w[i * 3 + 0] = points[i].in;
  997. w[i * 3 + 1] = points[i].out;
  998. w[i * 3 + 2] = points[i].pos;
  999. wt[i] = points[i].tilt;
  1000. }
  1001. w = PoolVector3Array::Write();
  1002. wt = PoolRealArray::Write();
  1003. dc["points"] = d;
  1004. dc["tilts"] = t;
  1005. return dc;
  1006. }
  1007. void Curve3D::_set_data(const Dictionary &p_data) {
  1008. ERR_FAIL_COND(!p_data.has("points"));
  1009. ERR_FAIL_COND(!p_data.has("tilts"));
  1010. PoolVector3Array rp = p_data["points"];
  1011. int pc = rp.size();
  1012. ERR_FAIL_COND(pc % 3 != 0);
  1013. points.resize(pc / 3);
  1014. PoolVector3Array::Read r = rp.read();
  1015. PoolRealArray rtl = p_data["tilts"];
  1016. PoolRealArray::Read rt = rtl.read();
  1017. for (int i = 0; i < points.size(); i++) {
  1018. points[i].in = r[i * 3 + 0];
  1019. points[i].out = r[i * 3 + 1];
  1020. points[i].pos = r[i * 3 + 2];
  1021. points[i].tilt = rt[i];
  1022. }
  1023. baked_cache_dirty = true;
  1024. }
  1025. PoolVector3Array Curve3D::tessellate(int p_max_stages, float p_tolerance) const {
  1026. PoolVector3Array tess;
  1027. if (points.size() == 0) {
  1028. return tess;
  1029. }
  1030. Vector<Map<float, Vector3> > midpoints;
  1031. midpoints.resize(points.size() - 1);
  1032. int pc = 1;
  1033. for (int i = 0; i < points.size() - 1; i++) {
  1034. _bake_segment3d(midpoints[i], 0, 1, points[i].pos, points[i].out, points[i + 1].pos, points[i + 1].in, 0, p_max_stages, p_tolerance);
  1035. pc++;
  1036. pc += midpoints[i].size();
  1037. }
  1038. tess.resize(pc);
  1039. PoolVector3Array::Write bpw = tess.write();
  1040. bpw[0] = points[0].pos;
  1041. int pidx = 0;
  1042. for (int i = 0; i < points.size() - 1; i++) {
  1043. for (Map<float, Vector3>::Element *E = midpoints[i].front(); E; E = E->next()) {
  1044. pidx++;
  1045. bpw[pidx] = E->get();
  1046. }
  1047. pidx++;
  1048. bpw[pidx] = points[i + 1].pos;
  1049. }
  1050. bpw = PoolVector3Array::Write();
  1051. return tess;
  1052. }
  1053. void Curve3D::_bind_methods() {
  1054. ClassDB::bind_method(D_METHOD("get_point_count"), &Curve3D::get_point_count);
  1055. ClassDB::bind_method(D_METHOD("add_point", "position", "in", "out", "at_position"), &Curve3D::add_point, DEFVAL(Vector3()), DEFVAL(Vector3()), DEFVAL(-1));
  1056. ClassDB::bind_method(D_METHOD("set_point_position", "idx", "position"), &Curve3D::set_point_position);
  1057. ClassDB::bind_method(D_METHOD("get_point_position", "idx"), &Curve3D::get_point_position);
  1058. ClassDB::bind_method(D_METHOD("set_point_tilt", "idx", "tilt"), &Curve3D::set_point_tilt);
  1059. ClassDB::bind_method(D_METHOD("get_point_tilt", "idx"), &Curve3D::get_point_tilt);
  1060. ClassDB::bind_method(D_METHOD("set_point_in", "idx", "position"), &Curve3D::set_point_in);
  1061. ClassDB::bind_method(D_METHOD("get_point_in", "idx"), &Curve3D::get_point_in);
  1062. ClassDB::bind_method(D_METHOD("set_point_out", "idx", "position"), &Curve3D::set_point_out);
  1063. ClassDB::bind_method(D_METHOD("get_point_out", "idx"), &Curve3D::get_point_out);
  1064. ClassDB::bind_method(D_METHOD("remove_point", "idx"), &Curve3D::remove_point);
  1065. ClassDB::bind_method(D_METHOD("clear_points"), &Curve3D::clear_points);
  1066. ClassDB::bind_method(D_METHOD("interpolate", "idx", "t"), &Curve3D::interpolate);
  1067. ClassDB::bind_method(D_METHOD("interpolatef", "fofs"), &Curve3D::interpolatef);
  1068. //ClassDB::bind_method(D_METHOD("bake","subdivs"),&Curve3D::bake,DEFVAL(10));
  1069. ClassDB::bind_method(D_METHOD("set_bake_interval", "distance"), &Curve3D::set_bake_interval);
  1070. ClassDB::bind_method(D_METHOD("get_bake_interval"), &Curve3D::get_bake_interval);
  1071. ClassDB::bind_method(D_METHOD("get_baked_length"), &Curve3D::get_baked_length);
  1072. ClassDB::bind_method(D_METHOD("interpolate_baked", "offset", "cubic"), &Curve3D::interpolate_baked, DEFVAL(false));
  1073. ClassDB::bind_method(D_METHOD("get_baked_points"), &Curve3D::get_baked_points);
  1074. ClassDB::bind_method(D_METHOD("get_baked_tilts"), &Curve3D::get_baked_tilts);
  1075. ClassDB::bind_method(D_METHOD("tessellate", "max_stages", "tolerance_degrees"), &Curve3D::tessellate, DEFVAL(5), DEFVAL(4));
  1076. ClassDB::bind_method(D_METHOD("_get_data"), &Curve3D::_get_data);
  1077. ClassDB::bind_method(D_METHOD("_set_data"), &Curve3D::_set_data);
  1078. ADD_PROPERTY(PropertyInfo(Variant::REAL, "bake_interval", PROPERTY_HINT_RANGE, "0.01,512,0.01"), "set_bake_interval", "get_bake_interval");
  1079. ADD_PROPERTY(PropertyInfo(Variant::INT, "_data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), "_set_data", "_get_data");
  1080. }
  1081. Curve3D::Curve3D() {
  1082. baked_cache_dirty = false;
  1083. baked_max_ofs = 0;
  1084. /* add_point(Vector3(-1,0,0));
  1085. add_point(Vector3(0,2,0));
  1086. add_point(Vector3(0,3,5));*/
  1087. bake_interval = 0.2;
  1088. }