polygon_2d.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  1. /**************************************************************************/
  2. /* polygon_2d.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 "polygon_2d.h"
  31. #include "core/math/geometry_2d.h"
  32. #ifndef NAVIGATION_2D_DISABLED
  33. #include "scene/resources/2d/navigation_mesh_source_geometry_data_2d.h"
  34. #include "scene/resources/2d/navigation_polygon.h"
  35. #include "servers/navigation_server_2d.h"
  36. #endif // NAVIGATION_2D_DISABLED
  37. #include "skeleton_2d.h"
  38. #ifndef NAVIGATION_2D_DISABLED
  39. Callable Polygon2D::_navmesh_source_geometry_parsing_callback;
  40. RID Polygon2D::_navmesh_source_geometry_parser;
  41. #endif // NAVIGATION_2D_DISABLED
  42. #ifdef TOOLS_ENABLED
  43. Dictionary Polygon2D::_edit_get_state() const {
  44. Dictionary state = Node2D::_edit_get_state();
  45. state["offset"] = offset;
  46. return state;
  47. }
  48. void Polygon2D::_edit_set_state(const Dictionary &p_state) {
  49. Node2D::_edit_set_state(p_state);
  50. set_offset(p_state["offset"]);
  51. }
  52. void Polygon2D::_edit_set_pivot(const Point2 &p_pivot) {
  53. set_position(get_transform().xform(p_pivot));
  54. set_offset(get_offset() - p_pivot);
  55. }
  56. Point2 Polygon2D::_edit_get_pivot() const {
  57. return Vector2();
  58. }
  59. bool Polygon2D::_edit_use_pivot() const {
  60. return true;
  61. }
  62. #endif // TOOLS_ENABLED
  63. #ifdef DEBUG_ENABLED
  64. Rect2 Polygon2D::_edit_get_rect() const {
  65. if (rect_cache_dirty) {
  66. int l = polygon.size();
  67. const Vector2 *r = polygon.ptr();
  68. item_rect = Rect2();
  69. for (int i = 0; i < l; i++) {
  70. Vector2 pos = r[i] + offset;
  71. if (i == 0) {
  72. item_rect.position = pos;
  73. } else {
  74. item_rect.expand_to(pos);
  75. }
  76. }
  77. rect_cache_dirty = false;
  78. }
  79. return item_rect;
  80. }
  81. bool Polygon2D::_edit_use_rect() const {
  82. return polygon.size() > 0;
  83. }
  84. bool Polygon2D::_edit_is_selected_on_click(const Point2 &p_point, double p_tolerance) const {
  85. Vector<Vector2> polygon2d = Variant(polygon);
  86. if (internal_vertices > 0) {
  87. polygon2d.resize(polygon2d.size() - internal_vertices);
  88. }
  89. return Geometry2D::is_point_in_polygon(p_point - get_offset(), polygon2d);
  90. }
  91. #endif // DEBUG_ENABLED
  92. void Polygon2D::_skeleton_bone_setup_changed() {
  93. queue_redraw();
  94. }
  95. void Polygon2D::_notification(int p_what) {
  96. if (p_what == NOTIFICATION_TRANSFORM_CHANGED && !Engine::get_singleton()->is_editor_hint()) {
  97. return; // Mesh recreation for NOTIFICATION_TRANSFORM_CHANGED is only needed in editor.
  98. }
  99. switch (p_what) {
  100. case NOTIFICATION_TRANSFORM_CHANGED:
  101. case NOTIFICATION_DRAW: {
  102. if (polygon.size() < 3) {
  103. return;
  104. }
  105. Skeleton2D *skeleton_node = nullptr;
  106. if (has_node(skeleton)) {
  107. skeleton_node = Object::cast_to<Skeleton2D>(get_node(skeleton));
  108. }
  109. ObjectID new_skeleton_id;
  110. if (skeleton_node && !invert && bone_weights.size()) {
  111. RS::get_singleton()->canvas_item_attach_skeleton(get_canvas_item(), skeleton_node->get_skeleton());
  112. new_skeleton_id = skeleton_node->get_instance_id();
  113. } else {
  114. RS::get_singleton()->canvas_item_attach_skeleton(get_canvas_item(), RID());
  115. }
  116. if (new_skeleton_id != current_skeleton_id) {
  117. Object *old_skeleton = ObjectDB::get_instance(current_skeleton_id);
  118. if (old_skeleton) {
  119. old_skeleton->disconnect("bone_setup_changed", callable_mp(this, &Polygon2D::_skeleton_bone_setup_changed));
  120. }
  121. if (skeleton_node) {
  122. skeleton_node->connect("bone_setup_changed", callable_mp(this, &Polygon2D::_skeleton_bone_setup_changed));
  123. }
  124. current_skeleton_id = new_skeleton_id;
  125. }
  126. Vector<Vector2> points;
  127. Vector<Vector2> uvs;
  128. Vector<int> bones;
  129. Vector<float> weights;
  130. int len = polygon.size();
  131. if ((invert || polygons.is_empty()) && internal_vertices > 0) {
  132. //if no polygons are around, internal vertices must not be drawn, else let them be
  133. len -= internal_vertices;
  134. }
  135. if (len <= 0) {
  136. return;
  137. }
  138. points.resize(len);
  139. {
  140. const Vector2 *polyr = polygon.ptr();
  141. for (int i = 0; i < len; i++) {
  142. points.write[i] = polyr[i] + offset;
  143. }
  144. }
  145. if (invert) {
  146. Rect2 bounds;
  147. int highest_idx = -1;
  148. real_t highest_y = -1e20;
  149. real_t sum = 0.0;
  150. for (int i = 0; i < len; i++) {
  151. if (i == 0) {
  152. bounds.position = points[i];
  153. } else {
  154. bounds.expand_to(points[i]);
  155. }
  156. if (points[i].y > highest_y) {
  157. highest_idx = i;
  158. highest_y = points[i].y;
  159. }
  160. int ni = (i + 1) % len;
  161. sum += (points[ni].x - points[i].x) * (points[ni].y + points[i].y);
  162. }
  163. bounds = bounds.grow(invert_border);
  164. Vector2 ep[7] = {
  165. Vector2(points[highest_idx].x, points[highest_idx].y + invert_border),
  166. Vector2(bounds.position + bounds.size),
  167. Vector2(bounds.position + Vector2(bounds.size.x, 0)),
  168. Vector2(bounds.position),
  169. Vector2(bounds.position + Vector2(0, bounds.size.y)),
  170. Vector2(points[highest_idx].x - CMP_EPSILON, points[highest_idx].y + invert_border),
  171. Vector2(points[highest_idx].x - CMP_EPSILON, points[highest_idx].y),
  172. };
  173. if (sum > 0) {
  174. SWAP(ep[1], ep[4]);
  175. SWAP(ep[2], ep[3]);
  176. SWAP(ep[5], ep[0]);
  177. SWAP(ep[6], points.write[highest_idx]);
  178. }
  179. points.resize(points.size() + 7);
  180. for (int i = points.size() - 1; i >= highest_idx + 7; i--) {
  181. points.write[i] = points[i - 7];
  182. }
  183. for (int i = 0; i < 7; i++) {
  184. points.write[highest_idx + i + 1] = ep[i];
  185. }
  186. len = points.size();
  187. }
  188. if (texture.is_valid()) {
  189. Transform2D texmat(tex_rot, tex_ofs);
  190. texmat.scale(tex_scale);
  191. Size2 tex_size = texture->get_size();
  192. uvs.resize(len);
  193. if (points.size() == uv.size()) {
  194. const Vector2 *uvr = uv.ptr();
  195. for (int i = 0; i < len; i++) {
  196. uvs.write[i] = texmat.xform(uvr[i]) / tex_size;
  197. }
  198. } else {
  199. for (int i = 0; i < len; i++) {
  200. uvs.write[i] = texmat.xform(points[i]) / tex_size;
  201. }
  202. }
  203. }
  204. if (skeleton_node && !invert && bone_weights.size()) {
  205. //a skeleton is set! fill indices and weights
  206. int vc = len;
  207. bones.resize(vc * 4);
  208. weights.resize(vc * 4);
  209. int *bonesw = bones.ptrw();
  210. float *weightsw = weights.ptrw();
  211. for (int i = 0; i < vc * 4; i++) {
  212. bonesw[i] = 0;
  213. weightsw[i] = 0;
  214. }
  215. for (int i = 0; i < bone_weights.size(); i++) {
  216. if (bone_weights[i].weights.size() != points.size()) {
  217. continue; //different number of vertices, sorry not using.
  218. }
  219. if (!skeleton_node->has_node(bone_weights[i].path)) {
  220. continue; //node does not exist
  221. }
  222. Bone2D *bone = Object::cast_to<Bone2D>(skeleton_node->get_node(bone_weights[i].path));
  223. if (!bone) {
  224. continue;
  225. }
  226. int bone_index = bone->get_index_in_skeleton();
  227. const float *r = bone_weights[i].weights.ptr();
  228. for (int j = 0; j < vc; j++) {
  229. if (r[j] == 0.0) {
  230. continue; //weight is unpainted, skip
  231. }
  232. //find an index with a weight
  233. for (int k = 0; k < 4; k++) {
  234. if (weightsw[j * 4 + k] < r[j]) {
  235. //this is less than this weight, insert weight!
  236. for (int l = 3; l > k; l--) {
  237. weightsw[j * 4 + l] = weightsw[j * 4 + l - 1];
  238. bonesw[j * 4 + l] = bonesw[j * 4 + l - 1];
  239. }
  240. weightsw[j * 4 + k] = r[j];
  241. bonesw[j * 4 + k] = bone_index;
  242. break;
  243. }
  244. }
  245. }
  246. }
  247. //normalize the weights
  248. for (int i = 0; i < vc; i++) {
  249. real_t tw = 0.0;
  250. for (int j = 0; j < 4; j++) {
  251. tw += weightsw[i * 4 + j];
  252. }
  253. if (tw == 0) {
  254. continue; //unpainted, do nothing
  255. }
  256. //normalize
  257. for (int j = 0; j < 4; j++) {
  258. weightsw[i * 4 + j] /= tw;
  259. }
  260. }
  261. }
  262. Vector<Color> colors;
  263. colors.resize(len);
  264. if (vertex_colors.size() == points.size()) {
  265. const Color *color_r = vertex_colors.ptr();
  266. for (int i = 0; i < len; i++) {
  267. colors.write[i] = color_r[i];
  268. }
  269. } else {
  270. for (int i = 0; i < len; i++) {
  271. colors.write[i] = color;
  272. }
  273. }
  274. Vector<int> index_array;
  275. if (invert || polygons.is_empty()) {
  276. index_array = Geometry2D::triangulate_polygon(points);
  277. } else {
  278. //draw individual polygons
  279. for (int i = 0; i < polygons.size(); i++) {
  280. Vector<int> src_indices = polygons[i];
  281. int ic = src_indices.size();
  282. if (ic < 3) {
  283. continue;
  284. }
  285. const int *r = src_indices.ptr();
  286. Vector<Vector2> tmp_points;
  287. tmp_points.resize(ic);
  288. for (int j = 0; j < ic; j++) {
  289. int idx = r[j];
  290. ERR_CONTINUE(idx < 0 || idx >= points.size());
  291. tmp_points.write[j] = points[r[j]];
  292. }
  293. Vector<int> indices = Geometry2D::triangulate_polygon(tmp_points);
  294. int ic2 = indices.size();
  295. const int *r2 = indices.ptr();
  296. int bic = index_array.size();
  297. index_array.resize(bic + ic2);
  298. int *w2 = index_array.ptrw();
  299. for (int j = 0; j < ic2; j++) {
  300. w2[j + bic] = r[r2[j]];
  301. }
  302. }
  303. }
  304. RS::get_singleton()->mesh_clear(mesh);
  305. if (index_array.size()) {
  306. Array arr;
  307. arr.resize(RS::ARRAY_MAX);
  308. arr[RS::ARRAY_VERTEX] = points;
  309. if (uvs.size() == points.size()) {
  310. arr[RS::ARRAY_TEX_UV] = uvs;
  311. }
  312. if (colors.size() == points.size()) {
  313. arr[RS::ARRAY_COLOR] = colors;
  314. }
  315. if (bones.size() == points.size() * 4) {
  316. arr[RS::ARRAY_BONES] = bones;
  317. arr[RS::ARRAY_WEIGHTS] = weights;
  318. }
  319. arr[RS::ARRAY_INDEX] = index_array;
  320. RS::SurfaceData sd;
  321. if (skeleton_node) {
  322. // Compute transform between mesh and skeleton for runtime AABB compute.
  323. const Transform2D mesh_transform = get_global_transform();
  324. const Transform2D skeleton_transform = skeleton_node->get_global_transform();
  325. const Transform2D mesh_to_sk2d = skeleton_transform.affine_inverse() * mesh_transform;
  326. // Convert 2d transform to 3d.
  327. sd.mesh_to_skeleton_xform.basis.rows[0][0] = mesh_to_sk2d.columns[0][0];
  328. sd.mesh_to_skeleton_xform.basis.rows[1][0] = mesh_to_sk2d.columns[0][1];
  329. sd.mesh_to_skeleton_xform.origin.x = mesh_to_sk2d.get_origin().x;
  330. sd.mesh_to_skeleton_xform.basis.rows[0][1] = mesh_to_sk2d.columns[1][0];
  331. sd.mesh_to_skeleton_xform.basis.rows[1][1] = mesh_to_sk2d.columns[1][1];
  332. sd.mesh_to_skeleton_xform.origin.y = mesh_to_sk2d.get_origin().y;
  333. }
  334. Error err = RS::get_singleton()->mesh_create_surface_data_from_arrays(&sd, RS::PRIMITIVE_TRIANGLES, arr, Array(), Dictionary(), RS::ARRAY_FLAG_USE_2D_VERTICES);
  335. if (err != OK) {
  336. return;
  337. }
  338. RS::get_singleton()->mesh_add_surface(mesh, sd);
  339. RS::get_singleton()->canvas_item_add_mesh(get_canvas_item(), mesh, Transform2D(), Color(1, 1, 1), texture.is_valid() ? texture->get_rid() : RID());
  340. }
  341. } break;
  342. }
  343. }
  344. void Polygon2D::set_polygon(const Vector<Vector2> &p_polygon) {
  345. polygon = p_polygon;
  346. rect_cache_dirty = true;
  347. queue_redraw();
  348. }
  349. Vector<Vector2> Polygon2D::get_polygon() const {
  350. return polygon;
  351. }
  352. void Polygon2D::set_internal_vertex_count(int p_count) {
  353. internal_vertices = p_count;
  354. }
  355. int Polygon2D::get_internal_vertex_count() const {
  356. return internal_vertices;
  357. }
  358. void Polygon2D::set_uv(const Vector<Vector2> &p_uv) {
  359. uv = p_uv;
  360. queue_redraw();
  361. }
  362. Vector<Vector2> Polygon2D::get_uv() const {
  363. return uv;
  364. }
  365. void Polygon2D::set_polygons(const Array &p_polygons) {
  366. polygons = p_polygons;
  367. queue_redraw();
  368. }
  369. Array Polygon2D::get_polygons() const {
  370. return polygons;
  371. }
  372. void Polygon2D::set_color(const Color &p_color) {
  373. color = p_color;
  374. queue_redraw();
  375. }
  376. Color Polygon2D::get_color() const {
  377. return color;
  378. }
  379. void Polygon2D::set_vertex_colors(const Vector<Color> &p_colors) {
  380. vertex_colors = p_colors;
  381. queue_redraw();
  382. }
  383. Vector<Color> Polygon2D::get_vertex_colors() const {
  384. return vertex_colors;
  385. }
  386. void Polygon2D::set_texture(const Ref<Texture2D> &p_texture) {
  387. texture = p_texture;
  388. queue_redraw();
  389. }
  390. Ref<Texture2D> Polygon2D::get_texture() const {
  391. return texture;
  392. }
  393. void Polygon2D::set_texture_offset(const Vector2 &p_offset) {
  394. tex_ofs = p_offset;
  395. queue_redraw();
  396. }
  397. Vector2 Polygon2D::get_texture_offset() const {
  398. return tex_ofs;
  399. }
  400. void Polygon2D::set_texture_rotation(real_t p_rot) {
  401. tex_rot = p_rot;
  402. queue_redraw();
  403. }
  404. real_t Polygon2D::get_texture_rotation() const {
  405. return tex_rot;
  406. }
  407. void Polygon2D::set_texture_scale(const Size2 &p_scale) {
  408. tex_scale = p_scale;
  409. queue_redraw();
  410. }
  411. Size2 Polygon2D::get_texture_scale() const {
  412. return tex_scale;
  413. }
  414. void Polygon2D::set_invert(bool p_invert) {
  415. invert = p_invert;
  416. queue_redraw();
  417. }
  418. bool Polygon2D::get_invert() const {
  419. return invert;
  420. }
  421. void Polygon2D::set_antialiased(bool p_antialiased) {
  422. antialiased = p_antialiased;
  423. queue_redraw();
  424. }
  425. bool Polygon2D::get_antialiased() const {
  426. return antialiased;
  427. }
  428. void Polygon2D::set_invert_border(real_t p_invert_border) {
  429. invert_border = p_invert_border;
  430. queue_redraw();
  431. }
  432. real_t Polygon2D::get_invert_border() const {
  433. return invert_border;
  434. }
  435. void Polygon2D::set_offset(const Vector2 &p_offset) {
  436. offset = p_offset;
  437. rect_cache_dirty = true;
  438. queue_redraw();
  439. }
  440. Vector2 Polygon2D::get_offset() const {
  441. return offset;
  442. }
  443. void Polygon2D::add_bone(const NodePath &p_path, const Vector<float> &p_weights) {
  444. Bone bone;
  445. bone.path = p_path;
  446. bone.weights = p_weights;
  447. bone_weights.push_back(bone);
  448. }
  449. int Polygon2D::get_bone_count() const {
  450. return bone_weights.size();
  451. }
  452. NodePath Polygon2D::get_bone_path(int p_index) const {
  453. ERR_FAIL_INDEX_V(p_index, bone_weights.size(), NodePath());
  454. return bone_weights[p_index].path;
  455. }
  456. Vector<float> Polygon2D::get_bone_weights(int p_index) const {
  457. ERR_FAIL_INDEX_V(p_index, bone_weights.size(), Vector<float>());
  458. return bone_weights[p_index].weights;
  459. }
  460. void Polygon2D::erase_bone(int p_idx) {
  461. ERR_FAIL_INDEX(p_idx, bone_weights.size());
  462. bone_weights.remove_at(p_idx);
  463. }
  464. void Polygon2D::clear_bones() {
  465. bone_weights.clear();
  466. }
  467. void Polygon2D::set_bone_weights(int p_index, const Vector<float> &p_weights) {
  468. ERR_FAIL_INDEX(p_index, bone_weights.size());
  469. bone_weights.write[p_index].weights = p_weights;
  470. queue_redraw();
  471. }
  472. void Polygon2D::set_bone_path(int p_index, const NodePath &p_path) {
  473. ERR_FAIL_INDEX(p_index, bone_weights.size());
  474. bone_weights.write[p_index].path = p_path;
  475. queue_redraw();
  476. }
  477. Array Polygon2D::_get_bones() const {
  478. Array bones;
  479. for (int i = 0; i < get_bone_count(); i++) {
  480. // Convert path property to String to avoid errors due to invalid node path in editor,
  481. // because it's relative to the Skeleton2D node and not Polygon2D.
  482. bones.push_back(String(get_bone_path(i)));
  483. bones.push_back(get_bone_weights(i));
  484. }
  485. return bones;
  486. }
  487. void Polygon2D::_set_bones(const Array &p_bones) {
  488. ERR_FAIL_COND(p_bones.size() & 1);
  489. clear_bones();
  490. for (int i = 0; i < p_bones.size(); i += 2) {
  491. // Convert back from String to NodePath.
  492. add_bone(NodePath(p_bones[i]), p_bones[i + 1]);
  493. }
  494. }
  495. void Polygon2D::set_skeleton(const NodePath &p_skeleton) {
  496. if (skeleton == p_skeleton) {
  497. return;
  498. }
  499. skeleton = p_skeleton;
  500. queue_redraw();
  501. }
  502. NodePath Polygon2D::get_skeleton() const {
  503. return skeleton;
  504. }
  505. #ifndef NAVIGATION_2D_DISABLED
  506. void Polygon2D::navmesh_parse_init() {
  507. ERR_FAIL_NULL(NavigationServer2D::get_singleton());
  508. if (!_navmesh_source_geometry_parser.is_valid()) {
  509. _navmesh_source_geometry_parsing_callback = callable_mp_static(&Polygon2D::navmesh_parse_source_geometry);
  510. _navmesh_source_geometry_parser = NavigationServer2D::get_singleton()->source_geometry_parser_create();
  511. NavigationServer2D::get_singleton()->source_geometry_parser_set_callback(_navmesh_source_geometry_parser, _navmesh_source_geometry_parsing_callback);
  512. }
  513. }
  514. void Polygon2D::navmesh_parse_source_geometry(const Ref<NavigationPolygon> &p_navigation_mesh, Ref<NavigationMeshSourceGeometryData2D> p_source_geometry_data, Node *p_node) {
  515. Polygon2D *polygon_2d = Object::cast_to<Polygon2D>(p_node);
  516. if (polygon_2d == nullptr) {
  517. return;
  518. }
  519. NavigationPolygon::ParsedGeometryType parsed_geometry_type = p_navigation_mesh->get_parsed_geometry_type();
  520. if (parsed_geometry_type == NavigationPolygon::PARSED_GEOMETRY_MESH_INSTANCES || parsed_geometry_type == NavigationPolygon::PARSED_GEOMETRY_BOTH) {
  521. const Transform2D polygon_2d_xform = p_source_geometry_data->root_node_transform * polygon_2d->get_global_transform();
  522. Vector<Vector2> shape_outline = polygon_2d->get_polygon();
  523. for (int i = 0; i < shape_outline.size(); i++) {
  524. shape_outline.write[i] = polygon_2d_xform.xform(shape_outline[i]);
  525. }
  526. p_source_geometry_data->add_obstruction_outline(shape_outline);
  527. }
  528. }
  529. #endif // NAVIGATION_2D_DISABLED
  530. void Polygon2D::_bind_methods() {
  531. ClassDB::bind_method(D_METHOD("set_polygon", "polygon"), &Polygon2D::set_polygon);
  532. ClassDB::bind_method(D_METHOD("get_polygon"), &Polygon2D::get_polygon);
  533. ClassDB::bind_method(D_METHOD("set_uv", "uv"), &Polygon2D::set_uv);
  534. ClassDB::bind_method(D_METHOD("get_uv"), &Polygon2D::get_uv);
  535. ClassDB::bind_method(D_METHOD("set_color", "color"), &Polygon2D::set_color);
  536. ClassDB::bind_method(D_METHOD("get_color"), &Polygon2D::get_color);
  537. ClassDB::bind_method(D_METHOD("set_polygons", "polygons"), &Polygon2D::set_polygons);
  538. ClassDB::bind_method(D_METHOD("get_polygons"), &Polygon2D::get_polygons);
  539. ClassDB::bind_method(D_METHOD("set_vertex_colors", "vertex_colors"), &Polygon2D::set_vertex_colors);
  540. ClassDB::bind_method(D_METHOD("get_vertex_colors"), &Polygon2D::get_vertex_colors);
  541. ClassDB::bind_method(D_METHOD("set_texture", "texture"), &Polygon2D::set_texture);
  542. ClassDB::bind_method(D_METHOD("get_texture"), &Polygon2D::get_texture);
  543. ClassDB::bind_method(D_METHOD("set_texture_offset", "texture_offset"), &Polygon2D::set_texture_offset);
  544. ClassDB::bind_method(D_METHOD("get_texture_offset"), &Polygon2D::get_texture_offset);
  545. ClassDB::bind_method(D_METHOD("set_texture_rotation", "texture_rotation"), &Polygon2D::set_texture_rotation);
  546. ClassDB::bind_method(D_METHOD("get_texture_rotation"), &Polygon2D::get_texture_rotation);
  547. ClassDB::bind_method(D_METHOD("set_texture_scale", "texture_scale"), &Polygon2D::set_texture_scale);
  548. ClassDB::bind_method(D_METHOD("get_texture_scale"), &Polygon2D::get_texture_scale);
  549. ClassDB::bind_method(D_METHOD("set_invert_enabled", "invert"), &Polygon2D::set_invert);
  550. ClassDB::bind_method(D_METHOD("get_invert_enabled"), &Polygon2D::get_invert);
  551. ClassDB::bind_method(D_METHOD("set_antialiased", "antialiased"), &Polygon2D::set_antialiased);
  552. ClassDB::bind_method(D_METHOD("get_antialiased"), &Polygon2D::get_antialiased);
  553. ClassDB::bind_method(D_METHOD("set_invert_border", "invert_border"), &Polygon2D::set_invert_border);
  554. ClassDB::bind_method(D_METHOD("get_invert_border"), &Polygon2D::get_invert_border);
  555. ClassDB::bind_method(D_METHOD("set_offset", "offset"), &Polygon2D::set_offset);
  556. ClassDB::bind_method(D_METHOD("get_offset"), &Polygon2D::get_offset);
  557. ClassDB::bind_method(D_METHOD("add_bone", "path", "weights"), &Polygon2D::add_bone);
  558. ClassDB::bind_method(D_METHOD("get_bone_count"), &Polygon2D::get_bone_count);
  559. ClassDB::bind_method(D_METHOD("get_bone_path", "index"), &Polygon2D::get_bone_path);
  560. ClassDB::bind_method(D_METHOD("get_bone_weights", "index"), &Polygon2D::get_bone_weights);
  561. ClassDB::bind_method(D_METHOD("erase_bone", "index"), &Polygon2D::erase_bone);
  562. ClassDB::bind_method(D_METHOD("clear_bones"), &Polygon2D::clear_bones);
  563. ClassDB::bind_method(D_METHOD("set_bone_path", "index", "path"), &Polygon2D::set_bone_path);
  564. ClassDB::bind_method(D_METHOD("set_bone_weights", "index", "weights"), &Polygon2D::set_bone_weights);
  565. ClassDB::bind_method(D_METHOD("set_skeleton", "skeleton"), &Polygon2D::set_skeleton);
  566. ClassDB::bind_method(D_METHOD("get_skeleton"), &Polygon2D::get_skeleton);
  567. ClassDB::bind_method(D_METHOD("set_internal_vertex_count", "internal_vertex_count"), &Polygon2D::set_internal_vertex_count);
  568. ClassDB::bind_method(D_METHOD("get_internal_vertex_count"), &Polygon2D::get_internal_vertex_count);
  569. ClassDB::bind_method(D_METHOD("_set_bones", "bones"), &Polygon2D::_set_bones);
  570. ClassDB::bind_method(D_METHOD("_get_bones"), &Polygon2D::_get_bones);
  571. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "color"), "set_color", "get_color");
  572. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "offset"), "set_offset", "get_offset");
  573. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "antialiased"), "set_antialiased", "get_antialiased");
  574. ADD_GROUP("Texture", "texture_");
  575. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_texture", "get_texture");
  576. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "texture_offset", PROPERTY_HINT_NONE, "suffix:px"), "set_texture_offset", "get_texture_offset");
  577. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "texture_scale", PROPERTY_HINT_LINK), "set_texture_scale", "get_texture_scale");
  578. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "texture_rotation", PROPERTY_HINT_RANGE, "-360,360,0.1,or_less,or_greater,radians_as_degrees"), "set_texture_rotation", "get_texture_rotation");
  579. ADD_GROUP("Skeleton", "");
  580. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "skeleton", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Skeleton2D"), "set_skeleton", "get_skeleton");
  581. ADD_GROUP("Invert", "invert_");
  582. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "invert_enabled", PROPERTY_HINT_GROUP_ENABLE), "set_invert_enabled", "get_invert_enabled");
  583. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "invert_border", PROPERTY_HINT_RANGE, "0.1,16384,0.1,suffix:px"), "set_invert_border", "get_invert_border");
  584. ADD_GROUP("Data", "");
  585. ADD_PROPERTY(PropertyInfo(Variant::PACKED_VECTOR2_ARRAY, "polygon"), "set_polygon", "get_polygon");
  586. ADD_PROPERTY(PropertyInfo(Variant::PACKED_VECTOR2_ARRAY, "uv"), "set_uv", "get_uv");
  587. ADD_PROPERTY(PropertyInfo(Variant::PACKED_COLOR_ARRAY, "vertex_colors"), "set_vertex_colors", "get_vertex_colors");
  588. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "polygons", PROPERTY_HINT_TYPE_STRING, "PackedInt32Array"), "set_polygons", "get_polygons");
  589. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "bones", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "_set_bones", "_get_bones");
  590. ADD_PROPERTY(PropertyInfo(Variant::INT, "internal_vertex_count", PROPERTY_HINT_RANGE, "0,1000"), "set_internal_vertex_count", "get_internal_vertex_count");
  591. }
  592. Polygon2D::Polygon2D() {
  593. mesh = RS::get_singleton()->mesh_create();
  594. }
  595. Polygon2D::~Polygon2D() {
  596. // This will free the internally-allocated mesh instance, if allocated.
  597. ERR_FAIL_NULL(RenderingServer::get_singleton());
  598. RS::get_singleton()->canvas_item_attach_skeleton(get_canvas_item(), RID());
  599. RS::get_singleton()->free(mesh);
  600. }