polygon_2d.cpp 24 KB

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