occluder_shape_polygon.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /**************************************************************************/
  2. /* occluder_shape_polygon.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 "occluder_shape_polygon.h"
  31. #include "servers/visual_server.h"
  32. #ifdef TOOLS_ENABLED
  33. void OccluderShapePolygon::_update_aabb() {
  34. _aabb_local = AABB();
  35. if (_poly_pts_local.size()) {
  36. Vector3 begin = _vec2to3(_poly_pts_local[0]);
  37. Vector3 end = begin;
  38. for (int n = 1; n < _poly_pts_local.size(); n++) {
  39. Vector3 pt = _vec2to3(_poly_pts_local[n]);
  40. begin.x = MIN(begin.x, pt.x);
  41. begin.y = MIN(begin.y, pt.y);
  42. begin.z = MIN(begin.z, pt.z);
  43. end.x = MAX(end.x, pt.x);
  44. end.y = MAX(end.y, pt.y);
  45. end.z = MAX(end.z, pt.z);
  46. }
  47. for (int n = 0; n < _hole_pts_local.size(); n++) {
  48. Vector3 pt = _vec2to3(_hole_pts_local[n]);
  49. begin.x = MIN(begin.x, pt.x);
  50. begin.y = MIN(begin.y, pt.y);
  51. begin.z = MIN(begin.z, pt.z);
  52. end.x = MAX(end.x, pt.x);
  53. end.y = MAX(end.y, pt.y);
  54. end.z = MAX(end.z, pt.z);
  55. }
  56. _aabb_local.position = begin;
  57. _aabb_local.size = end - begin;
  58. }
  59. }
  60. AABB OccluderShapePolygon::get_fallback_gizmo_aabb() const {
  61. return _aabb_local;
  62. }
  63. #endif
  64. void OccluderShapePolygon::_sanitize_points_internal(const PoolVector<Vector2> &p_from, Vector<Vector2> &r_to) {
  65. // remove duplicates? NYI maybe not necessary
  66. Vector<Vector2> raw;
  67. raw.resize(p_from.size());
  68. for (int n = 0; n < p_from.size(); n++) {
  69. raw.set(n, p_from[n]);
  70. }
  71. // this function may get rid of some concave points due to user editing ..
  72. // may not be necessary, no idea how fast it is
  73. r_to = Geometry::convex_hull_2d(raw);
  74. // some peculiarity of convex_hull_2d function, it duplicates the last point for some reason
  75. if (r_to.size() > 1) {
  76. r_to.resize(r_to.size() - 1);
  77. }
  78. // sort winding, the system expects counter clockwise polys
  79. Geometry::sort_polygon_winding(r_to, false);
  80. }
  81. void OccluderShapePolygon::_sanitize_points() {
  82. _sanitize_points_internal(_poly_pts_local_raw, _poly_pts_local);
  83. _sanitize_points_internal(_hole_pts_local_raw, _hole_pts_local);
  84. #ifdef TOOLS_ENABLED
  85. _update_aabb();
  86. #endif
  87. }
  88. void OccluderShapePolygon::set_polygon_point(int p_idx, const Vector2 &p_point) {
  89. if (p_idx >= _poly_pts_local_raw.size()) {
  90. return;
  91. }
  92. _poly_pts_local_raw.set(p_idx, p_point);
  93. _sanitize_points();
  94. update_shape_to_visual_server();
  95. notify_change_to_owners();
  96. }
  97. void OccluderShapePolygon::set_hole_point(int p_idx, const Vector2 &p_point) {
  98. if (p_idx >= _hole_pts_local_raw.size()) {
  99. return;
  100. }
  101. _hole_pts_local_raw.set(p_idx, p_point);
  102. _sanitize_points();
  103. update_shape_to_visual_server();
  104. notify_change_to_owners();
  105. }
  106. void OccluderShapePolygon::set_polygon_points(const PoolVector<Vector2> &p_points) {
  107. _poly_pts_local_raw = p_points;
  108. _sanitize_points();
  109. update_shape_to_visual_server();
  110. notify_change_to_owners();
  111. }
  112. void OccluderShapePolygon::set_hole_points(const PoolVector<Vector2> &p_points) {
  113. _hole_pts_local_raw = p_points;
  114. _sanitize_points();
  115. update_shape_to_visual_server();
  116. notify_change_to_owners();
  117. }
  118. PoolVector<Vector2> OccluderShapePolygon::get_polygon_points() const {
  119. return _poly_pts_local_raw;
  120. }
  121. PoolVector<Vector2> OccluderShapePolygon::get_hole_points() const {
  122. return _hole_pts_local_raw;
  123. }
  124. void OccluderShapePolygon::update_shape_to_visual_server() {
  125. if (_poly_pts_local.size() < 3)
  126. return;
  127. Geometry::OccluderMeshData md;
  128. md.faces.resize(1);
  129. Geometry::OccluderMeshData::Face &face = md.faces[0];
  130. face.two_way = is_two_way();
  131. md.vertices.resize(_poly_pts_local.size() + _hole_pts_local.size());
  132. face.indices.resize(_poly_pts_local.size());
  133. for (int n = 0; n < _poly_pts_local.size(); n++) {
  134. md.vertices[n] = _vec2to3(_poly_pts_local[n]);
  135. face.indices[n] = n;
  136. }
  137. // hole points
  138. if (_hole_pts_local.size()) {
  139. face.holes.resize(1);
  140. Geometry::OccluderMeshData::Hole &hole = face.holes[0];
  141. hole.indices.resize(_hole_pts_local.size());
  142. for (int n = 0; n < _hole_pts_local.size(); n++) {
  143. int dest_idx = n + _poly_pts_local.size();
  144. hole.indices[n] = dest_idx;
  145. md.vertices[dest_idx] = _vec2to3(_hole_pts_local[n]);
  146. }
  147. }
  148. face.plane = Plane(Vector3(0, 0, 0), Vector3(0, 0, -1));
  149. VisualServer::get_singleton()->occluder_resource_mesh_update(get_shape(), md);
  150. }
  151. void OccluderShapePolygon::set_two_way(bool p_two_way) {
  152. _settings_two_way = p_two_way;
  153. update_shape_to_visual_server();
  154. notify_change_to_owners();
  155. }
  156. Transform OccluderShapePolygon::center_node(const Transform &p_global_xform, const Transform &p_parent_xform, real_t p_snap) {
  157. return Transform();
  158. }
  159. void OccluderShapePolygon::clear() {
  160. _poly_pts_local.clear();
  161. _poly_pts_local_raw.resize(0);
  162. _hole_pts_local.clear();
  163. _hole_pts_local_raw.resize(0);
  164. #ifdef TOOLS_ENABLED
  165. _aabb_local = AABB();
  166. #endif
  167. }
  168. void OccluderShapePolygon::_bind_methods() {
  169. ClassDB::bind_method(D_METHOD("set_two_way", "two_way"), &OccluderShapePolygon::set_two_way);
  170. ClassDB::bind_method(D_METHOD("is_two_way"), &OccluderShapePolygon::is_two_way);
  171. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "two_way"), "set_two_way", "is_two_way");
  172. ClassDB::bind_method(D_METHOD("set_polygon_points", "points"), &OccluderShapePolygon::set_polygon_points);
  173. ClassDB::bind_method(D_METHOD("get_polygon_points"), &OccluderShapePolygon::get_polygon_points);
  174. ClassDB::bind_method(D_METHOD("set_polygon_point", "index", "position"), &OccluderShapePolygon::set_polygon_point);
  175. ADD_PROPERTY(PropertyInfo(Variant::POOL_VECTOR2_ARRAY, "polygon_points"), "set_polygon_points", "get_polygon_points");
  176. ClassDB::bind_method(D_METHOD("set_hole_points", "points"), &OccluderShapePolygon::set_hole_points);
  177. ClassDB::bind_method(D_METHOD("get_hole_points"), &OccluderShapePolygon::get_hole_points);
  178. ClassDB::bind_method(D_METHOD("set_hole_point", "index", "position"), &OccluderShapePolygon::set_hole_point);
  179. ADD_PROPERTY(PropertyInfo(Variant::POOL_VECTOR2_ARRAY, "hole_points"), "set_hole_points", "get_hole_points");
  180. }
  181. OccluderShapePolygon::OccluderShapePolygon() {
  182. if (get_shape().is_valid()) {
  183. VisualServer::get_singleton()->occluder_resource_prepare(get_shape(), VisualServer::OCCLUDER_TYPE_MESH);
  184. }
  185. clear();
  186. PoolVector<Vector2> points;
  187. points.resize(4);
  188. points.set(0, Vector2(1, -1));
  189. points.set(1, Vector2(1, 1));
  190. points.set(2, Vector2(-1, 1));
  191. points.set(3, Vector2(-1, -1));
  192. set_polygon_points(points); // default shape
  193. }