multimesh_instance_2d.cpp 9.0 KB

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