room.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /**************************************************************************/
  2. /* room.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 "room.h"
  31. #include "portal.h"
  32. #include "room_group.h"
  33. #include "room_manager.h"
  34. #include "servers/visual_server.h"
  35. void Room::SimplifyInfo::set_simplify(real_t p_value, real_t p_room_size) {
  36. _plane_simplify = CLAMP(p_value, 0.0, 1.0);
  37. // just for reference in case we later want to use degrees...
  38. // _plane_simplify_dot = Math::cos(Math::deg2rad(_plane_simplify_degrees));
  39. // _plane_simplify_dot = _plane_simplify;
  40. // _plane_simplify_dot *= _plane_simplify_dot;
  41. // _plane_simplify_dot = 1.0 - _plane_simplify_dot;
  42. // distance based on size of room
  43. // _plane_simplify_dist = p_room_size * 0.1 * _plane_simplify;
  44. // _plane_simplify_dist = MAX(_plane_simplify_dist, 0.08);
  45. // test fix
  46. _plane_simplify_dot = 0.99;
  47. _plane_simplify_dist = 0.08;
  48. // print_verbose("plane simplify dot : " + String(Variant(_plane_simplify_dot)));
  49. // print_verbose("plane simplify dist : " + String(Variant(_plane_simplify_dist)));
  50. }
  51. bool Room::SimplifyInfo::add_plane_if_unique(LocalVector<Plane, int32_t> &r_planes, const Plane &p) const {
  52. for (int n = 0; n < r_planes.size(); n++) {
  53. const Plane &o = r_planes[n];
  54. // this is a fudge factor for how close planes can be to be considered the same ...
  55. // to prevent ridiculous amounts of planes
  56. const real_t d = _plane_simplify_dist; // 0.08f
  57. if (Math::abs(p.d - o.d) > d) {
  58. continue;
  59. }
  60. real_t dot = p.normal.dot(o.normal);
  61. if (dot < _plane_simplify_dot) // 0.98f
  62. {
  63. continue;
  64. }
  65. // match!
  66. return false;
  67. }
  68. r_planes.push_back(p);
  69. return true;
  70. }
  71. void Room::clear() {
  72. _room_ID = -1;
  73. _planes.clear();
  74. _preliminary_planes.clear();
  75. _roomgroups.clear();
  76. _portals.clear();
  77. _bound_mesh_data.edges.clear();
  78. _bound_mesh_data.faces.clear();
  79. _bound_mesh_data.vertices.clear();
  80. _aabb = AABB();
  81. #ifdef TOOLS_ENABLED
  82. _gizmo_overlap_zones.clear();
  83. #endif
  84. }
  85. Room::Room() {
  86. _room_rid = RID_PRIME(VisualServer::get_singleton()->room_create());
  87. }
  88. Room::~Room() {
  89. if (_room_rid != RID()) {
  90. VisualServer::get_singleton()->free(_room_rid);
  91. }
  92. }
  93. bool Room::contains_point(const Vector3 &p_pt) const {
  94. if (!_aabb.has_point(p_pt)) {
  95. return false;
  96. }
  97. for (int n = 0; n < _planes.size(); n++) {
  98. if (_planes[n].is_point_over(p_pt)) {
  99. return false;
  100. }
  101. }
  102. return true;
  103. }
  104. void Room::set_room_simplify(real_t p_value) {
  105. _simplify_info.set_simplify(p_value, _aabb.get_longest_axis_size());
  106. }
  107. void Room::set_use_default_simplify(bool p_use) {
  108. _use_default_simplify = p_use;
  109. }
  110. void Room::set_point(int p_idx, const Vector3 &p_point) {
  111. if (p_idx >= _bound_pts.size()) {
  112. return;
  113. }
  114. _bound_pts.set(p_idx, p_point);
  115. #ifdef TOOLS_ENABLED
  116. _changed(true);
  117. #endif
  118. }
  119. void Room::set_points(const PoolVector<Vector3> &p_points) {
  120. _bound_pts = p_points;
  121. #ifdef TOOLS_ENABLED
  122. if (p_points.size()) {
  123. _changed(true);
  124. }
  125. #endif
  126. }
  127. PoolVector<Vector3> Room::get_points() const {
  128. return _bound_pts;
  129. }
  130. PoolVector<Vector3> Room::generate_points() {
  131. PoolVector<Vector3> pts_returned;
  132. #ifdef TOOLS_ENABLED
  133. // do a rooms convert to make sure the planes are up to date
  134. RoomManager *rm = RoomManager::active_room_manager;
  135. if (rm) {
  136. rm->rooms_convert();
  137. }
  138. if (!_planes.size()) {
  139. return pts_returned;
  140. }
  141. // scale an epsilon using 10.0 for a normal sized room
  142. real_t scaled_epsilon = _aabb.get_longest_axis_size() / 10.0;
  143. scaled_epsilon = MAX(scaled_epsilon * 0.01, 0.001);
  144. LocalVector<Vector3, int32_t> pts;
  145. pts = Geometry::compute_convex_mesh_points(&_planes[0], _planes.size(), scaled_epsilon);
  146. // eliminate duplicates
  147. for (int n = 0; n < pts.size(); n++) {
  148. const Vector3 &a = pts[n];
  149. for (int m = n + 1; m < pts.size(); m++) {
  150. const Vector3 &b = pts[m];
  151. if (a.is_equal_approx(b, scaled_epsilon)) {
  152. // remove b
  153. pts.remove_unordered(m);
  154. m--; // repeat m as the new m is the old last
  155. }
  156. }
  157. }
  158. // convert vector to poolvector
  159. pts_returned.resize(pts.size());
  160. Transform tr = get_global_transform();
  161. tr.affine_invert();
  162. for (int n = 0; n < pts.size(); n++) {
  163. // the points should be saved in LOCAL space,
  164. // so that if we move the room afterwards, the bound points
  165. // will also move in relation to the room.
  166. pts_returned.set(n, tr.xform(pts[n]));
  167. }
  168. #endif
  169. return pts_returned;
  170. }
  171. String Room::get_configuration_warning() const {
  172. String warning = Spatial::get_configuration_warning();
  173. auto lambda = [](const Node *p_node) {
  174. return static_cast<bool>((Object::cast_to<Room>(p_node) || Object::cast_to<RoomManager>(p_node) || Object::cast_to<RoomGroup>(p_node)));
  175. };
  176. if (detect_nodes_using_lambda(this, lambda)) {
  177. if (detect_nodes_of_type<Room>(this)) {
  178. if (!warning.empty()) {
  179. warning += "\n\n";
  180. }
  181. warning += TTR("A Room cannot have another Room as a child or grandchild.");
  182. }
  183. if (detect_nodes_of_type<RoomManager>(this)) {
  184. if (!warning.empty()) {
  185. warning += "\n\n";
  186. }
  187. warning += TTR("The RoomManager should not be placed inside a Room.");
  188. }
  189. if (detect_nodes_of_type<RoomGroup>(this)) {
  190. if (!warning.empty()) {
  191. warning += "\n\n";
  192. }
  193. warning += TTR("A RoomGroup should not be placed inside a Room.");
  194. }
  195. }
  196. if (_planes.size() > 80) {
  197. if (!warning.empty()) {
  198. warning += "\n\n";
  199. }
  200. warning += TTR("Room convex hull contains a large number of planes.\nConsider simplifying the room bound in order to increase performance.");
  201. }
  202. return warning;
  203. }
  204. // extra editor links to the room manager to allow unloading
  205. // on change, or re-converting
  206. void Room::_changed(bool p_regenerate_bounds) {
  207. #ifdef TOOLS_ENABLED
  208. RoomManager *rm = RoomManager::active_room_manager;
  209. if (!rm) {
  210. return;
  211. }
  212. if (p_regenerate_bounds) {
  213. rm->_room_regenerate_bound(this);
  214. }
  215. rm->_rooms_changed("changed Room " + get_name());
  216. #endif
  217. }
  218. void Room::_notification(int p_what) {
  219. switch (p_what) {
  220. case NOTIFICATION_ENTER_WORLD: {
  221. ERR_FAIL_COND(get_world().is_null());
  222. VisualServer::get_singleton()->room_set_scenario(_room_rid, get_world()->get_scenario());
  223. } break;
  224. case NOTIFICATION_EXIT_WORLD: {
  225. VisualServer::get_singleton()->room_set_scenario(_room_rid, RID());
  226. } break;
  227. }
  228. }
  229. void Room::_bind_methods() {
  230. ClassDB::bind_method(D_METHOD("set_use_default_simplify", "p_use"), &Room::set_use_default_simplify);
  231. ClassDB::bind_method(D_METHOD("get_use_default_simplify"), &Room::get_use_default_simplify);
  232. ClassDB::bind_method(D_METHOD("set_room_simplify", "p_value"), &Room::set_room_simplify);
  233. ClassDB::bind_method(D_METHOD("get_room_simplify"), &Room::get_room_simplify);
  234. ClassDB::bind_method(D_METHOD("set_points", "points"), &Room::set_points);
  235. ClassDB::bind_method(D_METHOD("get_points"), &Room::get_points);
  236. ClassDB::bind_method(D_METHOD("set_point", "index", "position"), &Room::set_point);
  237. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_default_simplify"), "set_use_default_simplify", "get_use_default_simplify");
  238. ADD_PROPERTY(PropertyInfo(Variant::REAL, "room_simplify", PROPERTY_HINT_RANGE, "0.0,1.0,0.005"), "set_room_simplify", "get_room_simplify");
  239. ADD_GROUP("Bound", "");
  240. ADD_PROPERTY(PropertyInfo(Variant::POOL_VECTOR3_ARRAY, "points"), "set_points", "get_points");
  241. }