multimesh_instance_2d.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /**************************************************************************/
  2. /* multimesh_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 "multimesh_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 MultiMeshInstance2D::_navmesh_source_geometry_parsing_callback;
  38. RID MultiMeshInstance2D::_navmesh_source_geometry_parser;
  39. void MultiMeshInstance2D::_notification(int p_what) {
  40. switch (p_what) {
  41. case NOTIFICATION_DRAW: {
  42. if (multimesh.is_valid()) {
  43. draw_multimesh(multimesh, texture);
  44. }
  45. } break;
  46. }
  47. }
  48. void MultiMeshInstance2D::_bind_methods() {
  49. ClassDB::bind_method(D_METHOD("set_multimesh", "multimesh"), &MultiMeshInstance2D::set_multimesh);
  50. ClassDB::bind_method(D_METHOD("get_multimesh"), &MultiMeshInstance2D::get_multimesh);
  51. ClassDB::bind_method(D_METHOD("set_texture", "texture"), &MultiMeshInstance2D::set_texture);
  52. ClassDB::bind_method(D_METHOD("get_texture"), &MultiMeshInstance2D::get_texture);
  53. ADD_SIGNAL(MethodInfo("texture_changed"));
  54. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "multimesh", PROPERTY_HINT_RESOURCE_TYPE, "MultiMesh"), "set_multimesh", "get_multimesh");
  55. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_texture", "get_texture");
  56. }
  57. void MultiMeshInstance2D::set_multimesh(const Ref<MultiMesh> &p_multimesh) {
  58. // Cleanup previous connection if any.
  59. if (multimesh.is_valid()) {
  60. multimesh->disconnect_changed(callable_mp((CanvasItem *)this, &CanvasItem::queue_redraw));
  61. }
  62. multimesh = p_multimesh;
  63. // Connect to the multimesh so the AABB can update when instance transforms are changed.
  64. if (multimesh.is_valid()) {
  65. multimesh->connect_changed(callable_mp((CanvasItem *)this, &CanvasItem::queue_redraw));
  66. }
  67. queue_redraw();
  68. }
  69. Ref<MultiMesh> MultiMeshInstance2D::get_multimesh() const {
  70. return multimesh;
  71. }
  72. void MultiMeshInstance2D::set_texture(const Ref<Texture2D> &p_texture) {
  73. if (p_texture == texture) {
  74. return;
  75. }
  76. texture = p_texture;
  77. queue_redraw();
  78. emit_signal(SceneStringName(texture_changed));
  79. }
  80. Ref<Texture2D> MultiMeshInstance2D::get_texture() const {
  81. return texture;
  82. }
  83. #ifdef DEBUG_ENABLED
  84. Rect2 MultiMeshInstance2D::_edit_get_rect() const {
  85. if (multimesh.is_valid()) {
  86. AABB aabb = multimesh->get_aabb();
  87. return Rect2(aabb.position.x, aabb.position.y, aabb.size.x, aabb.size.y);
  88. }
  89. return Node2D::_edit_get_rect();
  90. }
  91. #endif // DEBUG_ENABLED
  92. void MultiMeshInstance2D::navmesh_parse_init() {
  93. ERR_FAIL_NULL(NavigationServer2D::get_singleton());
  94. if (!_navmesh_source_geometry_parser.is_valid()) {
  95. _navmesh_source_geometry_parsing_callback = callable_mp_static(&MultiMeshInstance2D::navmesh_parse_source_geometry);
  96. _navmesh_source_geometry_parser = NavigationServer2D::get_singleton()->source_geometry_parser_create();
  97. NavigationServer2D::get_singleton()->source_geometry_parser_set_callback(_navmesh_source_geometry_parser, _navmesh_source_geometry_parsing_callback);
  98. }
  99. }
  100. void MultiMeshInstance2D::navmesh_parse_source_geometry(const Ref<NavigationPolygon> &p_navigation_mesh, Ref<NavigationMeshSourceGeometryData2D> p_source_geometry_data, Node *p_node) {
  101. MultiMeshInstance2D *multimesh_instance = Object::cast_to<MultiMeshInstance2D>(p_node);
  102. if (multimesh_instance == nullptr) {
  103. return;
  104. }
  105. NavigationPolygon::ParsedGeometryType parsed_geometry_type = p_navigation_mesh->get_parsed_geometry_type();
  106. if (!(parsed_geometry_type == NavigationPolygon::PARSED_GEOMETRY_MESH_INSTANCES || parsed_geometry_type == NavigationPolygon::PARSED_GEOMETRY_BOTH)) {
  107. return;
  108. }
  109. Ref<MultiMesh> multimesh = multimesh_instance->get_multimesh();
  110. if (!(multimesh.is_valid() && multimesh->get_transform_format() == MultiMesh::TRANSFORM_2D)) {
  111. return;
  112. }
  113. Ref<Mesh> mesh = multimesh->get_mesh();
  114. if (mesh.is_null()) {
  115. return;
  116. }
  117. using namespace Clipper2Lib;
  118. PathsD mesh_subject_paths, dummy_clip_paths;
  119. for (int i = 0; i < mesh->get_surface_count(); i++) {
  120. if (mesh->surface_get_primitive_type(i) != Mesh::PRIMITIVE_TRIANGLES) {
  121. continue;
  122. }
  123. if (!(mesh->surface_get_format(i) & Mesh::ARRAY_FLAG_USE_2D_VERTICES)) {
  124. continue;
  125. }
  126. PathD subject_path;
  127. int index_count = 0;
  128. if (mesh->surface_get_format(i) & Mesh::ARRAY_FORMAT_INDEX) {
  129. index_count = mesh->surface_get_array_index_len(i);
  130. } else {
  131. index_count = mesh->surface_get_array_len(i);
  132. }
  133. ERR_CONTINUE((index_count == 0 || (index_count % 3) != 0));
  134. Array a = mesh->surface_get_arrays(i);
  135. Vector<Vector2> mesh_vertices = a[Mesh::ARRAY_VERTEX];
  136. if (mesh->surface_get_format(i) & Mesh::ARRAY_FORMAT_INDEX) {
  137. Vector<int> mesh_indices = a[Mesh::ARRAY_INDEX];
  138. for (int vertex_index : mesh_indices) {
  139. const Vector2 &vertex = mesh_vertices[vertex_index];
  140. const PointD &point = PointD(vertex.x, vertex.y);
  141. subject_path.push_back(point);
  142. }
  143. } else {
  144. for (const Vector2 &vertex : mesh_vertices) {
  145. const PointD &point = PointD(vertex.x, vertex.y);
  146. subject_path.push_back(point);
  147. }
  148. }
  149. mesh_subject_paths.push_back(subject_path);
  150. }
  151. PathsD mesh_path_solution = Union(mesh_subject_paths, dummy_clip_paths, FillRule::NonZero);
  152. //path_solution = RamerDouglasPeucker(path_solution, 0.025);
  153. int multimesh_instance_count = multimesh->get_visible_instance_count();
  154. if (multimesh_instance_count == -1) {
  155. multimesh_instance_count = multimesh->get_instance_count();
  156. }
  157. const Transform2D multimesh_instance_xform = p_source_geometry_data->root_node_transform * multimesh_instance->get_global_transform();
  158. for (int i = 0; i < multimesh_instance_count; i++) {
  159. const Transform2D multimesh_instance_mesh_instance_xform = multimesh_instance_xform * multimesh->get_instance_transform_2d(i);
  160. for (const PathD &mesh_path : mesh_path_solution) {
  161. Vector<Vector2> shape_outline;
  162. for (const PointD &mesh_path_point : mesh_path) {
  163. shape_outline.push_back(Point2(static_cast<real_t>(mesh_path_point.x), static_cast<real_t>(mesh_path_point.y)));
  164. }
  165. for (int j = 0; j < shape_outline.size(); j++) {
  166. shape_outline.write[j] = multimesh_instance_mesh_instance_xform.xform(shape_outline[j]);
  167. }
  168. p_source_geometry_data->add_obstruction_outline(shape_outline);
  169. }
  170. }
  171. }
  172. MultiMeshInstance2D::MultiMeshInstance2D() {
  173. }
  174. MultiMeshInstance2D::~MultiMeshInstance2D() {
  175. }