visibility_notifier.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /**************************************************************************/
  2. /* visibility_notifier.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 "visibility_notifier.h"
  31. #include "core/engine.h"
  32. #include "scene/3d/camera.h"
  33. #include "scene/3d/physics_body.h"
  34. #include "scene/animation/animation_player.h"
  35. #include "scene/animation/animation_tree.h"
  36. #include "scene/animation/animation_tree_player.h"
  37. #include "scene/scene_string_names.h"
  38. void VisibilityNotifier::_enter_camera(Camera *p_camera) {
  39. ERR_FAIL_COND(cameras.has(p_camera));
  40. cameras.insert(p_camera);
  41. bool in_gameplay = _in_gameplay;
  42. if (!Engine::get_singleton()->are_portals_active()) {
  43. in_gameplay = true;
  44. }
  45. if ((cameras.size() == 1) && in_gameplay) {
  46. emit_signal(SceneStringNames::get_singleton()->screen_entered);
  47. _screen_enter();
  48. }
  49. emit_signal(SceneStringNames::get_singleton()->camera_entered, p_camera);
  50. }
  51. void VisibilityNotifier::_exit_camera(Camera *p_camera) {
  52. ERR_FAIL_COND(!cameras.has(p_camera));
  53. cameras.erase(p_camera);
  54. bool in_gameplay = _in_gameplay;
  55. if (!Engine::get_singleton()->are_portals_active()) {
  56. in_gameplay = true;
  57. }
  58. emit_signal(SceneStringNames::get_singleton()->camera_exited, p_camera);
  59. if ((cameras.size() == 0) && (in_gameplay)) {
  60. emit_signal(SceneStringNames::get_singleton()->screen_exited);
  61. _screen_exit();
  62. }
  63. }
  64. void VisibilityNotifier::set_max_distance(real_t p_max_distance) {
  65. if (p_max_distance > CMP_EPSILON) {
  66. _max_distance = p_max_distance;
  67. _max_distance_squared = _max_distance * _max_distance;
  68. _max_distance_active = true;
  69. // make sure world aabb centre is up to date
  70. if (is_inside_world()) {
  71. AABB world_aabb = get_global_transform().xform(aabb);
  72. _world_aabb_center = world_aabb.get_center();
  73. }
  74. } else {
  75. _max_distance = 0.0;
  76. _max_distance_squared = 0.0;
  77. _max_distance_active = false;
  78. }
  79. }
  80. void VisibilityNotifier::set_aabb(const AABB &p_aabb) {
  81. if (aabb == p_aabb) {
  82. return;
  83. }
  84. aabb = p_aabb;
  85. if (is_inside_world()) {
  86. AABB world_aabb = get_global_transform().xform(aabb);
  87. get_world()->_update_notifier(this, world_aabb);
  88. _world_aabb_center = world_aabb.get_center();
  89. }
  90. _change_notify("aabb");
  91. update_gizmo();
  92. }
  93. AABB VisibilityNotifier::get_aabb() const {
  94. return aabb;
  95. }
  96. void VisibilityNotifier::_refresh_portal_mode() {
  97. // only create in the visual server if we are roaming.
  98. // All other cases don't require a visual server rep.
  99. // Global and ignore are the same (existing client side functionality only).
  100. // Static and dynamic require only a one off creation at conversion.
  101. if (get_portal_mode() == PORTAL_MODE_ROAMING) {
  102. if (is_inside_world()) {
  103. if (_cull_instance_rid == RID()) {
  104. _cull_instance_rid = RID_PRIME(VisualServer::get_singleton()->ghost_create());
  105. }
  106. if (is_inside_world() && get_world().is_valid() && get_world()->get_scenario().is_valid() && is_inside_tree()) {
  107. AABB world_aabb = get_global_transform().xform(aabb);
  108. VisualServer::get_singleton()->ghost_set_scenario(_cull_instance_rid, get_world()->get_scenario(), get_instance_id(), world_aabb);
  109. }
  110. } else {
  111. if (_cull_instance_rid != RID()) {
  112. VisualServer::get_singleton()->free(_cull_instance_rid);
  113. _cull_instance_rid = RID();
  114. }
  115. }
  116. } else {
  117. if (_cull_instance_rid != RID()) {
  118. VisualServer::get_singleton()->free(_cull_instance_rid);
  119. _cull_instance_rid = RID();
  120. }
  121. }
  122. }
  123. void VisibilityNotifier::_notification(int p_what) {
  124. switch (p_what) {
  125. case NOTIFICATION_ENTER_WORLD: {
  126. world = get_world();
  127. ERR_FAIL_COND(!world.is_valid());
  128. AABB world_aabb = get_global_transform().xform(aabb);
  129. world->_register_notifier(this, world_aabb);
  130. _world_aabb_center = world_aabb.get_center();
  131. _refresh_portal_mode();
  132. } break;
  133. case NOTIFICATION_TRANSFORM_CHANGED: {
  134. AABB world_aabb = get_global_transform().xform(aabb);
  135. world->_update_notifier(this, world_aabb);
  136. if (_max_distance_active) {
  137. _world_aabb_center = world_aabb.get_center();
  138. }
  139. if (_cull_instance_rid != RID()) {
  140. VisualServer::get_singleton()->ghost_update(_cull_instance_rid, world_aabb);
  141. }
  142. } break;
  143. case NOTIFICATION_EXIT_WORLD: {
  144. ERR_FAIL_COND(!world.is_valid());
  145. world->_remove_notifier(this);
  146. if (_cull_instance_rid != RID()) {
  147. VisualServer::get_singleton()->ghost_set_scenario(_cull_instance_rid, RID(), get_instance_id(), AABB());
  148. }
  149. } break;
  150. case NOTIFICATION_ENTER_GAMEPLAY: {
  151. _in_gameplay = true;
  152. if (cameras.size() && Engine::get_singleton()->are_portals_active()) {
  153. emit_signal(SceneStringNames::get_singleton()->screen_entered);
  154. _screen_enter();
  155. }
  156. } break;
  157. case NOTIFICATION_EXIT_GAMEPLAY: {
  158. _in_gameplay = false;
  159. if (cameras.size() && Engine::get_singleton()->are_portals_active()) {
  160. emit_signal(SceneStringNames::get_singleton()->screen_exited);
  161. _screen_exit();
  162. }
  163. } break;
  164. }
  165. }
  166. bool VisibilityNotifier::is_on_screen() const {
  167. if (!Engine::get_singleton()->are_portals_active()) {
  168. return cameras.size() != 0;
  169. }
  170. return (cameras.size() != 0) && _in_gameplay;
  171. }
  172. void VisibilityNotifier::_bind_methods() {
  173. ClassDB::bind_method(D_METHOD("set_aabb", "rect"), &VisibilityNotifier::set_aabb);
  174. ClassDB::bind_method(D_METHOD("get_aabb"), &VisibilityNotifier::get_aabb);
  175. ClassDB::bind_method(D_METHOD("is_on_screen"), &VisibilityNotifier::is_on_screen);
  176. ClassDB::bind_method(D_METHOD("set_max_distance", "distance"), &VisibilityNotifier::set_max_distance);
  177. ClassDB::bind_method(D_METHOD("get_max_distance"), &VisibilityNotifier::get_max_distance);
  178. ADD_PROPERTY(PropertyInfo(Variant::AABB, "aabb"), "set_aabb", "get_aabb");
  179. ADD_PROPERTY(PropertyInfo(Variant::REAL, "max_distance", PROPERTY_HINT_RANGE, "0,32768,0.01"), "set_max_distance", "get_max_distance");
  180. ADD_SIGNAL(MethodInfo("camera_entered", PropertyInfo(Variant::OBJECT, "camera", PROPERTY_HINT_RESOURCE_TYPE, "Camera")));
  181. ADD_SIGNAL(MethodInfo("camera_exited", PropertyInfo(Variant::OBJECT, "camera", PROPERTY_HINT_RESOURCE_TYPE, "Camera")));
  182. ADD_SIGNAL(MethodInfo("screen_entered"));
  183. ADD_SIGNAL(MethodInfo("screen_exited"));
  184. }
  185. VisibilityNotifier::VisibilityNotifier() {
  186. aabb = AABB(Vector3(-1, -1, -1), Vector3(2, 2, 2));
  187. set_notify_transform(true);
  188. _in_gameplay = false;
  189. _max_distance_active = false;
  190. _max_distance = 0.0;
  191. _max_distance_squared = 0.0;
  192. _max_distance_leadin_counter = 1; // this could later be exposed as a property if necessary
  193. }
  194. VisibilityNotifier::~VisibilityNotifier() {
  195. if (_cull_instance_rid != RID()) {
  196. VisualServer::get_singleton()->free(_cull_instance_rid);
  197. }
  198. }
  199. //////////////////////////////////////
  200. void VisibilityEnabler::_screen_enter() {
  201. for (Map<Node *, Variant>::Element *E = nodes.front(); E; E = E->next()) {
  202. _change_node_state(E->key(), true);
  203. }
  204. visible = true;
  205. }
  206. void VisibilityEnabler::_screen_exit() {
  207. for (Map<Node *, Variant>::Element *E = nodes.front(); E; E = E->next()) {
  208. _change_node_state(E->key(), false);
  209. }
  210. visible = false;
  211. }
  212. void VisibilityEnabler::_find_nodes(Node *p_node) {
  213. bool add = false;
  214. Variant meta;
  215. {
  216. RigidBody *rb = Object::cast_to<RigidBody>(p_node);
  217. if (rb && ((rb->get_mode() == RigidBody::MODE_CHARACTER || rb->get_mode() == RigidBody::MODE_RIGID))) {
  218. add = true;
  219. meta = rb->get_mode();
  220. }
  221. }
  222. if (Object::cast_to<AnimationPlayer>(p_node) || Object::cast_to<AnimationTree>(p_node) || Object::cast_to<AnimationTreePlayer>(p_node)) {
  223. add = true;
  224. }
  225. {
  226. AnimationTree *at = Object::cast_to<AnimationTree>(p_node);
  227. if (at) {
  228. add = true;
  229. }
  230. }
  231. {
  232. AnimationTreePlayer *atp = Object::cast_to<AnimationTreePlayer>(p_node);
  233. if (atp) {
  234. add = true;
  235. }
  236. }
  237. if (add) {
  238. p_node->connect(SceneStringNames::get_singleton()->tree_exiting, this, "_node_removed", varray(p_node), CONNECT_ONESHOT);
  239. nodes[p_node] = meta;
  240. _change_node_state(p_node, false);
  241. }
  242. for (int i = 0; i < p_node->get_child_count(); i++) {
  243. Node *c = p_node->get_child(i);
  244. if (c->get_filename() != String()) {
  245. continue; //skip, instance
  246. }
  247. _find_nodes(c);
  248. }
  249. }
  250. void VisibilityEnabler::_notification(int p_what) {
  251. if (p_what == NOTIFICATION_ENTER_TREE) {
  252. if (Engine::get_singleton()->is_editor_hint()) {
  253. return;
  254. }
  255. Node *from = this;
  256. //find where current scene starts
  257. while (from->get_parent() && from->get_filename() == String()) {
  258. from = from->get_parent();
  259. }
  260. _find_nodes(from);
  261. }
  262. if (p_what == NOTIFICATION_EXIT_TREE) {
  263. if (Engine::get_singleton()->is_editor_hint()) {
  264. return;
  265. }
  266. for (Map<Node *, Variant>::Element *E = nodes.front(); E; E = E->next()) {
  267. if (!visible) {
  268. _change_node_state(E->key(), true);
  269. }
  270. E->key()->disconnect(SceneStringNames::get_singleton()->tree_exiting, this, "_node_removed");
  271. }
  272. nodes.clear();
  273. }
  274. }
  275. void VisibilityEnabler::_change_node_state(Node *p_node, bool p_enabled) {
  276. ERR_FAIL_COND(!nodes.has(p_node));
  277. if (enabler[ENABLER_FREEZE_BODIES]) {
  278. RigidBody *rb = Object::cast_to<RigidBody>(p_node);
  279. if (rb) {
  280. rb->set_sleeping(!p_enabled);
  281. }
  282. }
  283. if (enabler[ENABLER_PAUSE_ANIMATIONS]) {
  284. AnimationPlayer *ap = Object::cast_to<AnimationPlayer>(p_node);
  285. if (ap) {
  286. ap->set_active(p_enabled);
  287. } else {
  288. AnimationTree *at = Object::cast_to<AnimationTree>(p_node);
  289. if (at) {
  290. at->set_active(p_enabled);
  291. } else {
  292. AnimationTreePlayer *atp = Object::cast_to<AnimationTreePlayer>(p_node);
  293. if (atp) {
  294. atp->set_active(p_enabled);
  295. }
  296. }
  297. }
  298. }
  299. }
  300. void VisibilityEnabler::_node_removed(Node *p_node) {
  301. if (!visible) {
  302. _change_node_state(p_node, true);
  303. }
  304. nodes.erase(p_node);
  305. }
  306. void VisibilityEnabler::_bind_methods() {
  307. ClassDB::bind_method(D_METHOD("set_enabler", "enabler", "enabled"), &VisibilityEnabler::set_enabler);
  308. ClassDB::bind_method(D_METHOD("is_enabler_enabled", "enabler"), &VisibilityEnabler::is_enabler_enabled);
  309. ClassDB::bind_method(D_METHOD("_node_removed"), &VisibilityEnabler::_node_removed);
  310. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "pause_animations"), "set_enabler", "is_enabler_enabled", ENABLER_PAUSE_ANIMATIONS);
  311. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "freeze_bodies"), "set_enabler", "is_enabler_enabled", ENABLER_FREEZE_BODIES);
  312. BIND_ENUM_CONSTANT(ENABLER_PAUSE_ANIMATIONS);
  313. BIND_ENUM_CONSTANT(ENABLER_FREEZE_BODIES);
  314. BIND_ENUM_CONSTANT(ENABLER_MAX);
  315. }
  316. void VisibilityEnabler::set_enabler(Enabler p_enabler, bool p_enable) {
  317. ERR_FAIL_INDEX(p_enabler, ENABLER_MAX);
  318. enabler[p_enabler] = p_enable;
  319. }
  320. bool VisibilityEnabler::is_enabler_enabled(Enabler p_enabler) const {
  321. ERR_FAIL_INDEX_V(p_enabler, ENABLER_MAX, false);
  322. return enabler[p_enabler];
  323. }
  324. VisibilityEnabler::VisibilityEnabler() {
  325. for (int i = 0; i < ENABLER_MAX; i++) {
  326. enabler[i] = true;
  327. }
  328. visible = false;
  329. }