mesh_instance_2d.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /**************************************************************************/
  2. /* mesh_instance_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 "mesh_instance_2d.h"
  31. #ifndef NAVIGATION_2D_DISABLED
  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 "thirdparty/clipper2/include/clipper2/clipper.h"
  36. #include "thirdparty/misc/polypartition.h"
  37. #endif // NAVIGATION_2D_DISABLED
  38. Callable MeshInstance2D::_navmesh_source_geometry_parsing_callback;
  39. RID MeshInstance2D::_navmesh_source_geometry_parser;
  40. void MeshInstance2D::_notification(int p_what) {
  41. switch (p_what) {
  42. case NOTIFICATION_DRAW: {
  43. if (mesh.is_valid()) {
  44. draw_mesh(mesh, texture);
  45. }
  46. } break;
  47. }
  48. }
  49. void MeshInstance2D::_bind_methods() {
  50. ClassDB::bind_method(D_METHOD("set_mesh", "mesh"), &MeshInstance2D::set_mesh);
  51. ClassDB::bind_method(D_METHOD("get_mesh"), &MeshInstance2D::get_mesh);
  52. ClassDB::bind_method(D_METHOD("set_texture", "texture"), &MeshInstance2D::set_texture);
  53. ClassDB::bind_method(D_METHOD("get_texture"), &MeshInstance2D::get_texture);
  54. ADD_SIGNAL(MethodInfo("texture_changed"));
  55. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "mesh", PROPERTY_HINT_RESOURCE_TYPE, "Mesh"), "set_mesh", "get_mesh");
  56. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_texture", "get_texture");
  57. }
  58. void MeshInstance2D::set_mesh(const Ref<Mesh> &p_mesh) {
  59. if (mesh == p_mesh) {
  60. return;
  61. }
  62. if (mesh.is_valid()) {
  63. mesh->disconnect_changed(callable_mp((CanvasItem *)this, &CanvasItem::queue_redraw));
  64. }
  65. mesh = p_mesh;
  66. if (mesh.is_valid()) {
  67. // If mesh is a PrimitiveMesh, calling get_rid on it can trigger a changed callback
  68. // so do this before connecting to the change signal.
  69. mesh->get_rid();
  70. mesh->connect_changed(callable_mp((CanvasItem *)this, &CanvasItem::queue_redraw));
  71. }
  72. queue_redraw();
  73. }
  74. Ref<Mesh> MeshInstance2D::get_mesh() const {
  75. return mesh;
  76. }
  77. void MeshInstance2D::set_texture(const Ref<Texture2D> &p_texture) {
  78. if (p_texture == texture) {
  79. return;
  80. }
  81. texture = p_texture;
  82. queue_redraw();
  83. emit_signal(SceneStringName(texture_changed));
  84. }
  85. Ref<Texture2D> MeshInstance2D::get_texture() const {
  86. return texture;
  87. }
  88. #ifdef DEBUG_ENABLED
  89. Rect2 MeshInstance2D::_edit_get_rect() const {
  90. if (mesh.is_valid()) {
  91. AABB aabb = mesh->get_aabb();
  92. return Rect2(aabb.position.x, aabb.position.y, aabb.size.x, aabb.size.y);
  93. }
  94. return Node2D::_edit_get_rect();
  95. }
  96. bool MeshInstance2D::_edit_use_rect() const {
  97. return mesh.is_valid();
  98. }
  99. #endif // DEBUG_ENABLED
  100. #ifndef NAVIGATION_2D_DISABLED
  101. void MeshInstance2D::navmesh_parse_init() {
  102. ERR_FAIL_NULL(NavigationServer2D::get_singleton());
  103. if (!_navmesh_source_geometry_parser.is_valid()) {
  104. _navmesh_source_geometry_parsing_callback = callable_mp_static(&MeshInstance2D::navmesh_parse_source_geometry);
  105. _navmesh_source_geometry_parser = NavigationServer2D::get_singleton()->source_geometry_parser_create();
  106. NavigationServer2D::get_singleton()->source_geometry_parser_set_callback(_navmesh_source_geometry_parser, _navmesh_source_geometry_parsing_callback);
  107. }
  108. }
  109. void MeshInstance2D::navmesh_parse_source_geometry(const Ref<NavigationPolygon> &p_navigation_mesh, Ref<NavigationMeshSourceGeometryData2D> p_source_geometry_data, Node *p_node) {
  110. MeshInstance2D *mesh_instance = Object::cast_to<MeshInstance2D>(p_node);
  111. if (mesh_instance == nullptr) {
  112. return;
  113. }
  114. NavigationPolygon::ParsedGeometryType parsed_geometry_type = p_navigation_mesh->get_parsed_geometry_type();
  115. if (!(parsed_geometry_type == NavigationPolygon::PARSED_GEOMETRY_MESH_INSTANCES || parsed_geometry_type == NavigationPolygon::PARSED_GEOMETRY_BOTH)) {
  116. return;
  117. }
  118. Ref<Mesh> mesh = mesh_instance->get_mesh();
  119. if (mesh.is_null()) {
  120. return;
  121. }
  122. const Transform2D mesh_instance_xform = p_source_geometry_data->root_node_transform * mesh_instance->get_global_transform();
  123. using namespace Clipper2Lib;
  124. PathsD subject_paths, dummy_clip_paths;
  125. for (int i = 0; i < mesh->get_surface_count(); i++) {
  126. if (mesh->surface_get_primitive_type(i) != Mesh::PRIMITIVE_TRIANGLES) {
  127. continue;
  128. }
  129. if (!(mesh->surface_get_format(i) & Mesh::ARRAY_FLAG_USE_2D_VERTICES)) {
  130. continue;
  131. }
  132. PathD subject_path;
  133. int index_count = 0;
  134. if (mesh->surface_get_format(i) & Mesh::ARRAY_FORMAT_INDEX) {
  135. index_count = mesh->surface_get_array_index_len(i);
  136. } else {
  137. index_count = mesh->surface_get_array_len(i);
  138. }
  139. ERR_CONTINUE((index_count == 0 || (index_count % 3) != 0));
  140. Array a = mesh->surface_get_arrays(i);
  141. Vector<Vector2> mesh_vertices = a[Mesh::ARRAY_VERTEX];
  142. if (mesh->surface_get_format(i) & Mesh::ARRAY_FORMAT_INDEX) {
  143. Vector<int> mesh_indices = a[Mesh::ARRAY_INDEX];
  144. for (int vertex_index : mesh_indices) {
  145. const Vector2 &vertex = mesh_vertices[vertex_index];
  146. const PointD &point = PointD(vertex.x, vertex.y);
  147. subject_path.push_back(point);
  148. }
  149. } else {
  150. for (const Vector2 &vertex : mesh_vertices) {
  151. const PointD &point = PointD(vertex.x, vertex.y);
  152. subject_path.push_back(point);
  153. }
  154. }
  155. subject_paths.push_back(subject_path);
  156. }
  157. PathsD path_solution;
  158. path_solution = Union(subject_paths, dummy_clip_paths, FillRule::NonZero);
  159. //path_solution = RamerDouglasPeucker(path_solution, 0.025);
  160. Vector<Vector<Vector2>> polypaths;
  161. for (const PathD &scaled_path : path_solution) {
  162. Vector<Vector2> shape_outline;
  163. for (const PointD &scaled_point : scaled_path) {
  164. shape_outline.push_back(Point2(static_cast<real_t>(scaled_point.x), static_cast<real_t>(scaled_point.y)));
  165. }
  166. for (int i = 0; i < shape_outline.size(); i++) {
  167. shape_outline.write[i] = mesh_instance_xform.xform(shape_outline[i]);
  168. }
  169. p_source_geometry_data->add_obstruction_outline(shape_outline);
  170. }
  171. }
  172. #endif // NAVIGATION_2D_DISABLED
  173. MeshInstance2D::MeshInstance2D() {
  174. }