merge_group.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /**************************************************************************/
  2. /* merge_group.h */
  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. #ifndef MERGE_GROUP_H
  31. #define MERGE_GROUP_H
  32. #include "spatial.h"
  33. class MeshInstance;
  34. class CSGShape;
  35. class GridMap;
  36. class MergeGroup : public Spatial {
  37. GDCLASS(MergeGroup, Spatial);
  38. friend class MergeGroupEditorPlugin;
  39. public:
  40. enum ParamEnabled {
  41. PARAM_ENABLED_AUTO_MERGE,
  42. PARAM_ENABLED_SHADOW_PROXY,
  43. PARAM_ENABLED_CONVERT_CSGS,
  44. PARAM_ENABLED_CONVERT_GRIDMAPS,
  45. PARAM_ENABLED_COMBINE_SURFACES,
  46. PARAM_ENABLED_CLEAN_MESHES,
  47. PARAM_ENABLED_MAX,
  48. };
  49. enum Param {
  50. PARAM_GROUP_SIZE,
  51. PARAM_SPLITS_HORIZONTAL,
  52. PARAM_SPLITS_VERTICAL,
  53. PARAM_MIN_SPLIT_POLY_COUNT,
  54. PARAM_MAX,
  55. };
  56. void merge_meshes();
  57. bool merge_meshes_in_editor();
  58. void set_param_enabled(ParamEnabled p_param, bool p_enabled);
  59. bool get_param_enabled(ParamEnabled p_param);
  60. void set_param(Param p_param, int p_value);
  61. int get_param(Param p_param);
  62. // These enable feedback in the Godot UI as we bake.
  63. typedef bool (*BakeStepFunc)(float, const String &, void *, bool); // Progress, step description, userdata, force refresh.
  64. typedef void (*BakeEndFunc)(uint32_t); // time_started
  65. static BakeStepFunc bake_step_function;
  66. static BakeStepFunc bake_substep_function;
  67. static BakeEndFunc bake_end_function;
  68. protected:
  69. static void _bind_methods();
  70. void _notification(int p_what);
  71. private:
  72. struct MeshAABB {
  73. MeshInstance *mi = nullptr;
  74. AABB aabb;
  75. static int _sort_axis;
  76. bool operator<(const MeshAABB &p_b) const {
  77. real_t a_min = aabb.position.coord[_sort_axis];
  78. real_t b_min = p_b.aabb.position.coord[_sort_axis];
  79. return a_min < b_min;
  80. }
  81. };
  82. // Main function.
  83. bool _merge_meshes();
  84. // Merging.
  85. void _find_mesh_instances_recursive(int p_depth, Node *p_node, LocalVector<MeshInstance *> &r_mis, bool p_shadows, bool p_flag_invalid_meshes = false);
  86. bool _merge_similar(LocalVector<MeshInstance *> &r_mis, bool p_shadows);
  87. void _merge_list(const LocalVector<MeshInstance *> &p_mis, bool p_shadows, int p_whittle_group = -1);
  88. void _merge_list_ex(const LocalVector<MeshAABB> &p_mesh_aabbs, bool p_shadows, int p_whittle_group = -1);
  89. bool _join_similar(LocalVector<MeshInstance *> &r_mis);
  90. void _split_mesh_by_surface(MeshInstance *p_mi, int p_num_surfaces);
  91. bool _split_by_locality();
  92. // Helper funcs.
  93. void _convert_source_to_spatial(Spatial *p_node);
  94. void _reset_mesh_instance(MeshInstance *p_mi);
  95. void _move_children(Node *p_from, Node *p_to, bool p_recalculate_transforms = false);
  96. void _recursive_tree_merge(int &r_whittle_group, LocalVector<MeshAABB> p_list);
  97. void _delete_node(Node *p_node);
  98. bool _node_ok_to_delete(Node *p_node);
  99. void _cleanup_source_meshes(LocalVector<MeshInstance *> &r_cleanup_list);
  100. void _delete_dangling_spatials(Node *p_node);
  101. void _node_changed(Node *p_node);
  102. void _node_changed_internal(Node *p_node);
  103. // CSG
  104. void _find_csg_recursive(int p_depth, Node *p_node, LocalVector<CSGShape *> &r_csgs);
  105. void _split_csg_by_surface(CSGShape *p_shape);
  106. bool _terminate_search(Node *p_node);
  107. // Gridmap
  108. void _find_gridmap_recursive(int p_depth, Node *p_node, LocalVector<GridMap *> &r_gridmaps);
  109. void _bake_gridmap(GridMap *p_gridmap);
  110. void _log(String p_string);
  111. void _logt(int p_tabs, String p_string);
  112. struct Data {
  113. bool params_enabled[PARAM_ENABLED_MAX];
  114. uint32_t params[PARAM_MAX];
  115. // Hidden Params.
  116. bool delete_sources = false;
  117. bool convert_sources = false;
  118. bool split_by_surface = false;
  119. // Each merge is an iteration.
  120. uint32_t iteration = 0;
  121. Node *scene_root = nullptr;
  122. Data();
  123. } data;
  124. };
  125. VARIANT_ENUM_CAST(MergeGroup::Param);
  126. VARIANT_ENUM_CAST(MergeGroup::ParamEnabled);
  127. #endif // MERGE_GROUP_H