multimesh_instance_2d.cpp 8.5 KB

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