mesh_instance_2d.cpp 7.7 KB

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