line_builder.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  1. /**************************************************************************/
  2. /* line_builder.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 "line_builder.h"
  31. //----------------------------------------------------------------------------
  32. // Util
  33. //----------------------------------------------------------------------------
  34. enum SegmentIntersectionResult {
  35. SEGMENT_PARALLEL = 0,
  36. SEGMENT_NO_INTERSECT = 1,
  37. SEGMENT_INTERSECT = 2
  38. };
  39. static SegmentIntersectionResult segment_intersection(
  40. Vector2 a, Vector2 b, Vector2 c, Vector2 d,
  41. Vector2 *out_intersection) {
  42. // http://paulbourke.net/geometry/pointlineplane/ <-- Good stuff
  43. Vector2 cd = d - c;
  44. Vector2 ab = b - a;
  45. float div = cd.y * ab.x - cd.x * ab.y;
  46. if (Math::abs(div) > 0.001f) {
  47. float ua = (cd.x * (a.y - c.y) - cd.y * (a.x - c.x)) / div;
  48. float ub = (ab.x * (a.y - c.y) - ab.y * (a.x - c.x)) / div;
  49. *out_intersection = a + ua * ab;
  50. if (ua >= 0.f && ua <= 1.f &&
  51. ub >= 0.f && ub <= 1.f) {
  52. return SEGMENT_INTERSECT;
  53. }
  54. return SEGMENT_NO_INTERSECT;
  55. }
  56. return SEGMENT_PARALLEL;
  57. }
  58. static float calculate_total_distance(const Vector<Vector2> &points) {
  59. float d = 0.f;
  60. for (int i = 1; i < points.size(); ++i) {
  61. d += points[i].distance_to(points[i - 1]);
  62. }
  63. return d;
  64. }
  65. static inline Vector2 rotate90(const Vector2 &v) {
  66. // Note: the 2D referential is X-right, Y-down
  67. return Vector2(v.y, -v.x);
  68. }
  69. static inline Vector2 interpolate(const Rect2 &r, const Vector2 &v) {
  70. return Vector2(
  71. Math::lerp(r.position.x, r.position.x + r.get_size().x, v.x),
  72. Math::lerp(r.position.y, r.position.y + r.get_size().y, v.y));
  73. }
  74. //----------------------------------------------------------------------------
  75. // LineBuilder
  76. //----------------------------------------------------------------------------
  77. LineBuilder::LineBuilder() {
  78. joint_mode = Line2D::LINE_JOINT_SHARP;
  79. width = 10;
  80. curve = nullptr;
  81. default_color = Color(0.4, 0.5, 1);
  82. gradient = nullptr;
  83. sharp_limit = 2.f;
  84. round_precision = 8;
  85. begin_cap_mode = Line2D::LINE_CAP_NONE;
  86. end_cap_mode = Line2D::LINE_CAP_NONE;
  87. tile_aspect = 1.f;
  88. _interpolate_color = false;
  89. _last_index[0] = 0;
  90. _last_index[1] = 0;
  91. }
  92. void LineBuilder::clear_output() {
  93. vertices.clear();
  94. colors.clear();
  95. indices.clear();
  96. uvs.clear();
  97. }
  98. void LineBuilder::build() {
  99. // Need at least 2 points to draw a line
  100. if (points.size() < 2) {
  101. clear_output();
  102. return;
  103. }
  104. ERR_FAIL_COND(tile_aspect <= 0.f);
  105. const float hw = width / 2.f;
  106. const float hw_sq = hw * hw;
  107. const float sharp_limit_sq = sharp_limit * sharp_limit;
  108. const int len = points.size();
  109. // Initial values
  110. Vector2 pos0 = points[0];
  111. Vector2 pos1 = points[1];
  112. Vector2 f0 = (pos1 - pos0).normalized();
  113. Vector2 u0 = rotate90(f0);
  114. Vector2 pos_up0 = pos0;
  115. Vector2 pos_down0 = pos0;
  116. Color color0;
  117. Color color1;
  118. float current_distance0 = 0.f;
  119. float current_distance1 = 0.f;
  120. float total_distance = 0.f;
  121. float width_factor = 1.f;
  122. _interpolate_color = gradient != nullptr;
  123. bool retrieve_curve = curve != nullptr;
  124. bool distance_required = _interpolate_color ||
  125. retrieve_curve ||
  126. texture_mode == Line2D::LINE_TEXTURE_TILE ||
  127. texture_mode == Line2D::LINE_TEXTURE_STRETCH;
  128. if (distance_required) {
  129. total_distance = calculate_total_distance(points);
  130. //Adjust totalDistance.
  131. // The line's outer length will be a little higher due to begin and end caps
  132. if (begin_cap_mode == Line2D::LINE_CAP_BOX || begin_cap_mode == Line2D::LINE_CAP_ROUND) {
  133. if (retrieve_curve) {
  134. total_distance += width * curve->interpolate_baked(0.f) * 0.5f;
  135. } else {
  136. total_distance += width * 0.5f;
  137. }
  138. }
  139. if (end_cap_mode == Line2D::LINE_CAP_BOX || end_cap_mode == Line2D::LINE_CAP_ROUND) {
  140. if (retrieve_curve) {
  141. total_distance += width * curve->interpolate_baked(1.f) * 0.5f;
  142. } else {
  143. total_distance += width * 0.5f;
  144. }
  145. }
  146. }
  147. if (_interpolate_color) {
  148. color0 = gradient->get_color(0);
  149. } else {
  150. colors.push_back(default_color);
  151. }
  152. float uvx0 = 0.f;
  153. float uvx1 = 0.f;
  154. if (retrieve_curve) {
  155. width_factor = curve->interpolate_baked(0.f);
  156. }
  157. pos_up0 += u0 * hw * width_factor;
  158. pos_down0 -= u0 * hw * width_factor;
  159. // Begin cap
  160. if (begin_cap_mode == Line2D::LINE_CAP_BOX) {
  161. // Push back first vertices a little bit
  162. pos_up0 -= f0 * hw * width_factor;
  163. pos_down0 -= f0 * hw * width_factor;
  164. current_distance0 += hw * width_factor;
  165. current_distance1 = current_distance0;
  166. } else if (begin_cap_mode == Line2D::LINE_CAP_ROUND) {
  167. if (texture_mode == Line2D::LINE_TEXTURE_TILE) {
  168. uvx0 = width_factor * 0.5f / tile_aspect;
  169. } else if (texture_mode == Line2D::LINE_TEXTURE_STRETCH) {
  170. uvx0 = width * width_factor / total_distance;
  171. }
  172. new_arc(pos0, pos_up0 - pos0, -Math_PI, color0, Rect2(0.f, 0.f, uvx0 * 2, 1.f));
  173. current_distance0 += hw * width_factor;
  174. current_distance1 = current_distance0;
  175. }
  176. strip_begin(pos_up0, pos_down0, color0, uvx0);
  177. /*
  178. * pos_up0 ------------- pos_up1 --------------------
  179. * | |
  180. * pos0 - - - - - - - - - pos1 - - - - - - - - - pos2
  181. * | |
  182. * pos_down0 ------------ pos_down1 ------------------
  183. *
  184. * i-1 i i+1
  185. */
  186. // http://labs.hyperandroid.com/tag/opengl-lines
  187. // (not the same implementation but visuals help a lot)
  188. // For each additional segment
  189. for (int i = 1; i < len - 1; ++i) {
  190. pos1 = points[i];
  191. Vector2 pos2 = points[i + 1];
  192. Vector2 f1 = (pos2 - pos1).normalized();
  193. Vector2 u1 = rotate90(f1);
  194. // Determine joint orientation
  195. const float dp = u0.dot(f1);
  196. const Orientation orientation = (dp > 0.f ? UP : DOWN);
  197. if (distance_required) {
  198. current_distance1 += pos0.distance_to(pos1);
  199. }
  200. if (_interpolate_color) {
  201. color1 = gradient->get_color_at_offset(current_distance1 / total_distance);
  202. }
  203. if (retrieve_curve) {
  204. width_factor = curve->interpolate_baked(current_distance1 / total_distance);
  205. }
  206. Vector2 inner_normal0, inner_normal1;
  207. if (orientation == UP) {
  208. inner_normal0 = u0 * hw * width_factor;
  209. inner_normal1 = u1 * hw * width_factor;
  210. } else {
  211. inner_normal0 = -u0 * hw * width_factor;
  212. inner_normal1 = -u1 * hw * width_factor;
  213. }
  214. /*
  215. * ---------------------------
  216. * /
  217. * 0 / 1
  218. * / /
  219. * --------------------x------ /
  220. * / / (here shown with orientation == DOWN)
  221. * / /
  222. * / /
  223. * / /
  224. * 2 /
  225. * /
  226. */
  227. // Find inner intersection at the joint
  228. Vector2 corner_pos_in, corner_pos_out;
  229. SegmentIntersectionResult intersection_result = segment_intersection(
  230. pos0 + inner_normal0, pos1 + inner_normal0,
  231. pos1 + inner_normal1, pos2 + inner_normal1,
  232. &corner_pos_in);
  233. if (intersection_result == SEGMENT_INTERSECT) {
  234. // Inner parts of the segments intersect
  235. corner_pos_out = 2.f * pos1 - corner_pos_in;
  236. } else {
  237. // No intersection, segments are either parallel or too sharp
  238. corner_pos_in = pos1 + inner_normal0;
  239. corner_pos_out = pos1 - inner_normal0;
  240. }
  241. Vector2 corner_pos_up, corner_pos_down;
  242. if (orientation == UP) {
  243. corner_pos_up = corner_pos_in;
  244. corner_pos_down = corner_pos_out;
  245. } else {
  246. corner_pos_up = corner_pos_out;
  247. corner_pos_down = corner_pos_in;
  248. }
  249. Line2D::LineJointMode current_joint_mode = joint_mode;
  250. Vector2 pos_up1, pos_down1;
  251. if (intersection_result == SEGMENT_INTERSECT) {
  252. // Fallback on bevel if sharp angle is too high (because it would produce very long miters)
  253. float width_factor_sq = width_factor * width_factor;
  254. if (current_joint_mode == Line2D::LINE_JOINT_SHARP && corner_pos_out.distance_squared_to(pos1) / (hw_sq * width_factor_sq) > sharp_limit_sq) {
  255. current_joint_mode = Line2D::LINE_JOINT_BEVEL;
  256. }
  257. if (current_joint_mode == Line2D::LINE_JOINT_SHARP) {
  258. // In this case, we won't create joint geometry,
  259. // The previous and next line quads will directly share an edge.
  260. pos_up1 = corner_pos_up;
  261. pos_down1 = corner_pos_down;
  262. } else {
  263. // Bevel or round
  264. if (orientation == UP) {
  265. pos_up1 = corner_pos_up;
  266. pos_down1 = pos1 - u0 * hw * width_factor;
  267. } else {
  268. pos_up1 = pos1 + u0 * hw * width_factor;
  269. pos_down1 = corner_pos_down;
  270. }
  271. }
  272. } else {
  273. // No intersection: fallback
  274. if (current_joint_mode == Line2D::LINE_JOINT_SHARP) {
  275. // There is no fallback implementation for LINE_JOINT_SHARP so switch to the LINE_JOINT_BEVEL
  276. current_joint_mode = Line2D::LINE_JOINT_BEVEL;
  277. }
  278. pos_up1 = corner_pos_up;
  279. pos_down1 = corner_pos_down;
  280. }
  281. // Add current line body quad
  282. // Triangles are clockwise
  283. if (texture_mode == Line2D::LINE_TEXTURE_TILE) {
  284. uvx1 = current_distance1 / (width * tile_aspect);
  285. } else if (texture_mode == Line2D::LINE_TEXTURE_STRETCH) {
  286. uvx1 = current_distance1 / total_distance;
  287. }
  288. strip_add_quad(pos_up1, pos_down1, color1, uvx1);
  289. // Swap vars for use in the next line
  290. color0 = color1;
  291. u0 = u1;
  292. f0 = f1;
  293. pos0 = pos1;
  294. if (intersection_result == SEGMENT_INTERSECT) {
  295. if (current_joint_mode == Line2D::LINE_JOINT_SHARP) {
  296. pos_up0 = pos_up1;
  297. pos_down0 = pos_down1;
  298. } else {
  299. if (orientation == UP) {
  300. pos_up0 = corner_pos_up;
  301. pos_down0 = pos1 - u1 * hw * width_factor;
  302. } else {
  303. pos_up0 = pos1 + u1 * hw * width_factor;
  304. pos_down0 = corner_pos_down;
  305. }
  306. }
  307. } else {
  308. pos_up0 = pos1 + u1 * hw * width_factor;
  309. pos_down0 = pos1 - u1 * hw * width_factor;
  310. }
  311. // From this point, bu0 and bd0 concern the next segment
  312. // Add joint geometry
  313. if (current_joint_mode != Line2D::LINE_JOINT_SHARP) {
  314. /* ________________ cbegin
  315. * / \
  316. * / \
  317. * ____________/_ _ _\ cend
  318. * | |
  319. * | |
  320. * | |
  321. */
  322. Vector2 cbegin, cend;
  323. if (orientation == UP) {
  324. cbegin = pos_down1;
  325. cend = pos_down0;
  326. } else {
  327. cbegin = pos_up1;
  328. cend = pos_up0;
  329. }
  330. if (current_joint_mode == Line2D::LINE_JOINT_BEVEL) {
  331. strip_add_tri(cend, orientation);
  332. } else if (current_joint_mode == Line2D::LINE_JOINT_ROUND) {
  333. Vector2 vbegin = cbegin - pos1;
  334. Vector2 vend = cend - pos1;
  335. strip_add_arc(pos1, vbegin.angle_to(vend), orientation);
  336. }
  337. if (intersection_result != SEGMENT_INTERSECT) {
  338. // In this case the joint is too corrputed to be re-used,
  339. // start again the strip with fallback points
  340. strip_begin(pos_up0, pos_down0, color1, uvx1);
  341. }
  342. }
  343. }
  344. // Last (or only) segment
  345. pos1 = points[points.size() - 1];
  346. if (distance_required) {
  347. current_distance1 += pos0.distance_to(pos1);
  348. }
  349. if (_interpolate_color) {
  350. color1 = gradient->get_color(gradient->get_points_count() - 1);
  351. }
  352. if (retrieve_curve) {
  353. width_factor = curve->interpolate_baked(1.f);
  354. }
  355. Vector2 pos_up1 = pos1 + u0 * hw * width_factor;
  356. Vector2 pos_down1 = pos1 - u0 * hw * width_factor;
  357. // End cap (box)
  358. if (end_cap_mode == Line2D::LINE_CAP_BOX) {
  359. pos_up1 += f0 * hw * width_factor;
  360. pos_down1 += f0 * hw * width_factor;
  361. }
  362. if (texture_mode == Line2D::LINE_TEXTURE_TILE) {
  363. uvx1 = current_distance1 / (width * tile_aspect);
  364. } else if (texture_mode == Line2D::LINE_TEXTURE_STRETCH) {
  365. uvx1 = current_distance1 / total_distance;
  366. }
  367. strip_add_quad(pos_up1, pos_down1, color1, uvx1);
  368. // End cap (round)
  369. if (end_cap_mode == Line2D::LINE_CAP_ROUND) {
  370. // Note: color is not used in case we don't interpolate...
  371. Color color = _interpolate_color ? gradient->get_color(gradient->get_points_count() - 1) : Color(0, 0, 0);
  372. float dist = 0;
  373. if (texture_mode == Line2D::LINE_TEXTURE_TILE) {
  374. dist = width_factor / tile_aspect;
  375. } else if (texture_mode == Line2D::LINE_TEXTURE_STRETCH) {
  376. dist = width * width_factor / total_distance;
  377. }
  378. new_arc(pos1, pos_up1 - pos1, Math_PI, color, Rect2(uvx1 - 0.5f * dist, 0.f, dist, 1.f));
  379. }
  380. }
  381. void LineBuilder::strip_begin(Vector2 up, Vector2 down, Color color, float uvx) {
  382. int vi = vertices.size();
  383. vertices.push_back(up);
  384. vertices.push_back(down);
  385. if (_interpolate_color) {
  386. colors.push_back(color);
  387. colors.push_back(color);
  388. }
  389. if (texture_mode != Line2D::LINE_TEXTURE_NONE) {
  390. uvs.push_back(Vector2(uvx, 0.f));
  391. uvs.push_back(Vector2(uvx, 1.f));
  392. }
  393. _last_index[UP] = vi;
  394. _last_index[DOWN] = vi + 1;
  395. }
  396. void LineBuilder::strip_new_quad(Vector2 up, Vector2 down, Color color, float uvx) {
  397. int vi = vertices.size();
  398. vertices.push_back(vertices[_last_index[UP]]);
  399. vertices.push_back(vertices[_last_index[DOWN]]);
  400. vertices.push_back(up);
  401. vertices.push_back(down);
  402. if (_interpolate_color) {
  403. colors.push_back(color);
  404. colors.push_back(color);
  405. colors.push_back(color);
  406. colors.push_back(color);
  407. }
  408. if (texture_mode != Line2D::LINE_TEXTURE_NONE) {
  409. uvs.push_back(uvs[_last_index[UP]]);
  410. uvs.push_back(uvs[_last_index[DOWN]]);
  411. uvs.push_back(Vector2(uvx, UP));
  412. uvs.push_back(Vector2(uvx, DOWN));
  413. }
  414. indices.push_back(vi);
  415. indices.push_back(vi + 3);
  416. indices.push_back(vi + 1);
  417. indices.push_back(vi);
  418. indices.push_back(vi + 2);
  419. indices.push_back(vi + 3);
  420. _last_index[UP] = vi + 2;
  421. _last_index[DOWN] = vi + 3;
  422. }
  423. void LineBuilder::strip_add_quad(Vector2 up, Vector2 down, Color color, float uvx) {
  424. int vi = vertices.size();
  425. vertices.push_back(up);
  426. vertices.push_back(down);
  427. if (_interpolate_color) {
  428. colors.push_back(color);
  429. colors.push_back(color);
  430. }
  431. if (texture_mode != Line2D::LINE_TEXTURE_NONE) {
  432. uvs.push_back(Vector2(uvx, 0.f));
  433. uvs.push_back(Vector2(uvx, 1.f));
  434. }
  435. indices.push_back(_last_index[UP]);
  436. indices.push_back(vi + 1);
  437. indices.push_back(_last_index[DOWN]);
  438. indices.push_back(_last_index[UP]);
  439. indices.push_back(vi);
  440. indices.push_back(vi + 1);
  441. _last_index[UP] = vi;
  442. _last_index[DOWN] = vi + 1;
  443. }
  444. void LineBuilder::strip_add_tri(Vector2 up, Orientation orientation) {
  445. int vi = vertices.size();
  446. vertices.push_back(up);
  447. if (_interpolate_color) {
  448. colors.push_back(colors[colors.size() - 1]);
  449. }
  450. Orientation opposite_orientation = orientation == UP ? DOWN : UP;
  451. if (texture_mode != Line2D::LINE_TEXTURE_NONE) {
  452. // UVs are just one slice of the texture all along
  453. // (otherwise we can't share the bottom vertice)
  454. uvs.push_back(uvs[_last_index[opposite_orientation]]);
  455. }
  456. indices.push_back(_last_index[opposite_orientation]);
  457. indices.push_back(vi);
  458. indices.push_back(_last_index[orientation]);
  459. _last_index[opposite_orientation] = vi;
  460. }
  461. void LineBuilder::strip_add_arc(Vector2 center, float angle_delta, Orientation orientation) {
  462. // Take the two last vertices and extrude an arc made of triangles
  463. // that all share one of the initial vertices
  464. Orientation opposite_orientation = orientation == UP ? DOWN : UP;
  465. Vector2 vbegin = vertices[_last_index[opposite_orientation]] - center;
  466. float radius = vbegin.length();
  467. float angle_step = Math_PI / static_cast<float>(round_precision);
  468. float steps = Math::abs(angle_delta) / angle_step;
  469. if (angle_delta < 0.f) {
  470. angle_step = -angle_step;
  471. }
  472. float t = Vector2(1, 0).angle_to(vbegin);
  473. float end_angle = t + angle_delta;
  474. Vector2 rpos(0, 0);
  475. // Arc vertices
  476. for (int ti = 0; ti < steps; ++ti, t += angle_step) {
  477. rpos = center + Vector2(Math::cos(t), Math::sin(t)) * radius;
  478. strip_add_tri(rpos, orientation);
  479. }
  480. // Last arc vertice
  481. rpos = center + Vector2(Math::cos(end_angle), Math::sin(end_angle)) * radius;
  482. strip_add_tri(rpos, orientation);
  483. }
  484. void LineBuilder::new_arc(Vector2 center, Vector2 vbegin, float angle_delta, Color color, Rect2 uv_rect) {
  485. // Make a standalone arc that doesn't use existing vertices,
  486. // with undistorted UVs from within a square section
  487. float radius = vbegin.length();
  488. float angle_step = Math_PI / static_cast<float>(round_precision);
  489. float steps = Math::abs(angle_delta) / angle_step;
  490. if (angle_delta < 0.f) {
  491. angle_step = -angle_step;
  492. }
  493. float t = Vector2(1, 0).angle_to(vbegin);
  494. float end_angle = t + angle_delta;
  495. Vector2 rpos(0, 0);
  496. float tt_begin = -Math_PI / 2.f;
  497. float tt = tt_begin;
  498. // Center vertice
  499. int vi = vertices.size();
  500. vertices.push_back(center);
  501. if (_interpolate_color) {
  502. colors.push_back(color);
  503. }
  504. if (texture_mode != Line2D::LINE_TEXTURE_NONE) {
  505. uvs.push_back(interpolate(uv_rect, Vector2(0.5f, 0.5f)));
  506. }
  507. // Arc vertices
  508. for (int ti = 0; ti < steps; ++ti, t += angle_step) {
  509. Vector2 sc = Vector2(Math::cos(t), Math::sin(t));
  510. rpos = center + sc * radius;
  511. vertices.push_back(rpos);
  512. if (_interpolate_color) {
  513. colors.push_back(color);
  514. }
  515. if (texture_mode != Line2D::LINE_TEXTURE_NONE) {
  516. Vector2 tsc = Vector2(Math::cos(tt), Math::sin(tt));
  517. uvs.push_back(interpolate(uv_rect, 0.5f * (tsc + Vector2(1.f, 1.f))));
  518. tt += angle_step;
  519. }
  520. }
  521. // Last arc vertice
  522. Vector2 sc = Vector2(Math::cos(end_angle), Math::sin(end_angle));
  523. rpos = center + sc * radius;
  524. vertices.push_back(rpos);
  525. if (_interpolate_color) {
  526. colors.push_back(color);
  527. }
  528. if (texture_mode != Line2D::LINE_TEXTURE_NONE) {
  529. tt = tt_begin + angle_delta;
  530. Vector2 tsc = Vector2(Math::cos(tt), Math::sin(tt));
  531. uvs.push_back(interpolate(uv_rect, 0.5f * (tsc + Vector2(1.f, 1.f))));
  532. }
  533. // Make up triangles
  534. int vi0 = vi;
  535. for (int ti = 0; ti < steps; ++ti) {
  536. indices.push_back(vi0);
  537. indices.push_back(++vi);
  538. indices.push_back(vi + 1);
  539. }
  540. }