room_manager.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /**************************************************************************/
  2. /* room_manager.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 ROOM_MANAGER_H
  31. #define ROOM_MANAGER_H
  32. #include "core/local_vector.h"
  33. #include "room.h"
  34. #include "spatial.h"
  35. class Portal;
  36. class RoomGroup;
  37. class MeshInstance;
  38. class GeometryInstance;
  39. class VisualInstance;
  40. #define GODOT_PORTAL_WILDCARD ('*')
  41. class RoomManager : public Spatial {
  42. GDCLASS(RoomManager, Spatial);
  43. public:
  44. enum PVSMode {
  45. PVS_MODE_DISABLED,
  46. PVS_MODE_PARTIAL,
  47. PVS_MODE_FULL,
  48. };
  49. void set_roomlist_path(const NodePath &p_path);
  50. NodePath get_roomlist_path() const {
  51. return _settings_path_roomlist;
  52. }
  53. void set_preview_camera_path(const NodePath &p_path);
  54. NodePath get_preview_camera_path() const {
  55. return _settings_path_preview_camera;
  56. }
  57. void rooms_set_active(bool p_active);
  58. bool rooms_get_active() const;
  59. void set_show_margins(bool p_show);
  60. bool get_show_margins() const;
  61. void set_debug_sprawl(bool p_enable);
  62. bool get_debug_sprawl() const;
  63. void set_merge_meshes(bool p_enable);
  64. bool get_merge_meshes() const;
  65. void set_room_simplify(real_t p_value);
  66. real_t get_room_simplify() const;
  67. void set_default_portal_margin(real_t p_dist);
  68. real_t get_default_portal_margin() const;
  69. void set_overlap_warning_threshold(int p_value) { _overlap_warning_threshold = p_value; }
  70. int get_overlap_warning_threshold() const { return (int)_overlap_warning_threshold; }
  71. void set_portal_depth_limit(int p_limit);
  72. int get_portal_depth_limit() const { return _settings_portal_depth_limit; }
  73. void set_roaming_expansion_margin(real_t p_dist);
  74. real_t get_roaming_expansion_margin() const { return _settings_roaming_expansion_margin; }
  75. void set_pvs_mode(PVSMode p_mode);
  76. PVSMode get_pvs_mode() const;
  77. void set_pvs_filename(String p_filename);
  78. String get_pvs_filename() const;
  79. void set_use_secondary_pvs(bool p_enable) { _settings_use_secondary_pvs = p_enable; }
  80. bool get_use_secondary_pvs() const { return _settings_use_secondary_pvs; }
  81. void set_gameplay_monitor_enabled(bool p_enable) { _settings_gameplay_monitor_enabled = p_enable; }
  82. bool get_gameplay_monitor_enabled() const { return _settings_gameplay_monitor_enabled; }
  83. void rooms_convert();
  84. void rooms_clear();
  85. void rooms_flip_portals();
  86. String get_configuration_warning() const;
  87. // for internal use in the editor..
  88. // either we can clear the rooms and unload,
  89. // or reconvert.
  90. void _rooms_changed(String p_reason);
  91. #ifdef TOOLS_ENABLED
  92. // for a preview, we allow the editor to change the bound
  93. bool _room_regenerate_bound(Room *p_room);
  94. #endif
  95. RoomManager();
  96. ~RoomManager();
  97. // an easy way of grabbing the active room manager for tools purposes
  98. #ifdef TOOLS_ENABLED
  99. static RoomManager *active_room_manager;
  100. // static versions of functions for use from editor toolbars
  101. static void static_rooms_set_active(bool p_active);
  102. static bool static_rooms_get_active();
  103. static bool static_rooms_get_active_and_loaded();
  104. static void static_rooms_convert();
  105. #endif
  106. private:
  107. // funcs
  108. bool resolve_preview_camera_path();
  109. void _preview_camera_update();
  110. // conversion
  111. // FIRST PASS
  112. void _convert_rooms_recursive(Spatial *p_node, LocalVector<Portal *> &r_portals, LocalVector<RoomGroup *> &r_roomgroups, int p_roomgroup = -1);
  113. void _convert_room(Spatial *p_node, LocalVector<Portal *> &r_portals, const LocalVector<RoomGroup *> &p_roomgroups, int p_roomgroup);
  114. int _convert_roomgroup(Spatial *p_node, LocalVector<RoomGroup *> &r_roomgroups);
  115. void _find_portals_recursive(Spatial *p_node, Room *p_room, LocalVector<Portal *> &r_portals);
  116. void _convert_portal(Room *p_room, Spatial *p_node, LocalVector<Portal *> &portals);
  117. // SECOND PASS
  118. void _second_pass_portals(Spatial *p_roomlist, LocalVector<Portal *> &r_portals);
  119. void _second_pass_rooms(const LocalVector<RoomGroup *> &p_roomgroups, const LocalVector<Portal *> &p_portals);
  120. void _second_pass_room(Room *p_room, const LocalVector<RoomGroup *> &p_roomgroups, const LocalVector<Portal *> &p_portals);
  121. bool _convert_manual_bound(Room *p_room, Spatial *p_node, const LocalVector<Portal *> &p_portals);
  122. void _check_portal_for_warnings(Portal *p_portal, const AABB &p_room_aabb_without_portals);
  123. void _process_static(Room *p_room, Spatial *p_node, Vector<Vector3> &r_room_pts, bool p_add_to_portal_renderer);
  124. void _find_statics_recursive(Room *p_room, Spatial *p_node, Vector<Vector3> &r_room_pts, bool p_add_to_portal_renderer);
  125. bool _convert_room_hull_preliminary(Room *p_room, const Vector<Vector3> &p_room_pts, const LocalVector<Portal *> &p_portals);
  126. bool _bound_findpoints_mesh_instance(MeshInstance *p_mi, Vector<Vector3> &r_room_pts, AABB &r_aabb);
  127. bool _bound_findpoints_geom_instance(GeometryInstance *p_gi, Vector<Vector3> &r_room_pts, AABB &r_aabb);
  128. // THIRD PASS
  129. void _autolink_portals(Spatial *p_roomlist, LocalVector<Portal *> &r_portals);
  130. void _third_pass_rooms(const LocalVector<Portal *> &p_portals);
  131. bool _convert_room_hull_final(Room *p_room, const LocalVector<Portal *> &p_portals);
  132. void _build_simplified_bound(const Room *p_room, Geometry::MeshData &r_md, LocalVector<Plane, int32_t> &r_planes, int p_num_portal_planes);
  133. // AUTOPLACE - automatically place STATIC and DYNAMICs that are not within a room
  134. // into the most appropriate room, and sprawl
  135. void _autoplace_recursive(Spatial *p_node);
  136. bool _autoplace_object(VisualInstance *p_vi);
  137. // misc
  138. bool _add_plane_if_unique(const Room *p_room, LocalVector<Plane, int32_t> &r_planes, const Plane &p);
  139. void _update_portal_gizmos(Spatial *p_node);
  140. bool _check_roomlist_validity(Node *p_node);
  141. void _cleanup_after_conversion();
  142. Error _build_room_convex_hull(const Room *p_room, const Vector<Vector3> &p_points, Geometry::MeshData &r_mesh);
  143. #ifdef TOOLS_ENABLED
  144. void _generate_room_overlap_zones();
  145. #endif
  146. // merging
  147. void _merge_meshes_in_room(Room *p_room);
  148. void _list_mergeable_mesh_instances(Spatial *p_node, LocalVector<MeshInstance *, int32_t> &r_list);
  149. void _merge_log(String p_string) { debug_print_line(p_string); }
  150. bool _remove_redundant_dangling_nodes(Spatial *p_node);
  151. // helper funcs
  152. bool _name_ends_with(const Node *p_node, String p_postfix) const;
  153. template <class NODE_TYPE>
  154. NODE_TYPE *_resolve_path(NodePath p_path) const;
  155. template <class NODE_TYPE>
  156. bool _node_is_type(Node *p_node) const;
  157. template <class T>
  158. T *_change_node_type(Spatial *p_node, String p_prefix, bool p_delete = true);
  159. void _update_gizmos_recursive(Node *p_node);
  160. void _set_owner_recursive(Node *p_node, Node *p_owner);
  161. void _flip_portals_recursive(Spatial *p_node);
  162. Error _build_convex_hull(const Vector<Vector3> &p_points, Geometry::MeshData &r_mesh, real_t p_epsilon = 3.0 * UNIT_EPSILON);
  163. // output strings during conversion process
  164. void convert_log(String p_string, int p_priority = 0) { debug_print_line(p_string, 1); }
  165. // only prints when user has set 'debug' in the room manager inspector
  166. // also does not show in non editor builds
  167. void debug_print_line(String p_string, int p_priority = 0);
  168. void show_warning(const String &p_string, bool p_skippable = false, bool p_alert = true);
  169. public:
  170. static String _find_name_before(Node *p_node, String p_postfix, bool p_allow_no_postfix = false);
  171. static real_t _get_default_portal_margin() { return _default_portal_margin; }
  172. private:
  173. // accessible from UI
  174. NodePath _settings_path_roomlist;
  175. NodePath _settings_path_preview_camera;
  176. // resolved node
  177. Spatial *_roomlist = nullptr;
  178. bool _warning_misnamed_nodes_detected = false;
  179. bool _warning_portal_link_room_not_found = false;
  180. bool _warning_portal_autolink_failed = false;
  181. bool _warning_room_overlap_detected = false;
  182. // merge suitable meshes in rooms?
  183. bool _settings_merge_meshes = false;
  184. // remove redundant childless spatials after merging
  185. bool _settings_remove_danglers = true;
  186. bool _active = true;
  187. // portals, room hulls etc
  188. bool _show_debug = true;
  189. bool _debug_sprawl = false;
  190. // pvs
  191. PVSMode _pvs_mode = PVS_MODE_PARTIAL;
  192. String _pvs_filename;
  193. bool _settings_use_secondary_pvs = false;
  194. bool _settings_use_simple_pvs = false;
  195. bool _settings_log_pvs_generation = false;
  196. bool _settings_use_signals = true;
  197. bool _settings_gameplay_monitor_enabled = false;
  198. int _conversion_tick = 0;
  199. // just used during conversion, could be invalidated
  200. // later by user deleting rooms etc.
  201. LocalVector<Room *, int32_t> _rooms;
  202. // advanced params
  203. static real_t _default_portal_margin;
  204. real_t _overlap_warning_threshold = 1.0;
  205. Room::SimplifyInfo _room_simplify_info;
  206. int _settings_portal_depth_limit = 16;
  207. real_t _settings_roaming_expansion_margin = 1.0;
  208. // debug override camera
  209. ObjectID _godot_preview_camera_ID = -1;
  210. // local version of the godot camera frustum,
  211. // to prevent updating the visual server (and causing
  212. // a screen refresh) where not necessary.
  213. Vector3 _godot_camera_pos;
  214. Vector<Plane> _godot_camera_planes;
  215. protected:
  216. static void _bind_methods();
  217. void _notification(int p_what);
  218. void _refresh_from_project_settings();
  219. };
  220. VARIANT_ENUM_CAST(RoomManager::PVSMode);
  221. #endif // ROOM_MANAGER_H