visible_on_screen_notifier_3d.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /**************************************************************************/
  2. /* visible_on_screen_notifier_3d.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 "visible_on_screen_notifier_3d.h"
  31. void VisibleOnScreenNotifier3D::_visibility_enter() {
  32. if (!is_inside_tree() || Engine::get_singleton()->is_editor_hint()) {
  33. return;
  34. }
  35. on_screen = true;
  36. emit_signal(SceneStringName(screen_entered));
  37. _screen_enter();
  38. }
  39. void VisibleOnScreenNotifier3D::_visibility_exit() {
  40. if (!is_inside_tree() || Engine::get_singleton()->is_editor_hint()) {
  41. return;
  42. }
  43. on_screen = false;
  44. emit_signal(SceneStringName(screen_exited));
  45. _screen_exit();
  46. }
  47. void VisibleOnScreenNotifier3D::set_aabb(const AABB &p_aabb) {
  48. if (aabb == p_aabb) {
  49. return;
  50. }
  51. aabb = p_aabb;
  52. RS::get_singleton()->visibility_notifier_set_aabb(get_base(), aabb);
  53. update_gizmos();
  54. }
  55. AABB VisibleOnScreenNotifier3D::get_aabb() const {
  56. return aabb;
  57. }
  58. bool VisibleOnScreenNotifier3D::is_on_screen() const {
  59. return on_screen;
  60. }
  61. void VisibleOnScreenNotifier3D::_notification(int p_what) {
  62. switch (p_what) {
  63. case NOTIFICATION_ENTER_TREE:
  64. case NOTIFICATION_EXIT_TREE: {
  65. on_screen = false;
  66. } break;
  67. }
  68. }
  69. void VisibleOnScreenNotifier3D::_bind_methods() {
  70. ClassDB::bind_method(D_METHOD("set_aabb", "rect"), &VisibleOnScreenNotifier3D::set_aabb);
  71. ClassDB::bind_method(D_METHOD("is_on_screen"), &VisibleOnScreenNotifier3D::is_on_screen);
  72. ADD_PROPERTY(PropertyInfo(Variant::AABB, "aabb", PROPERTY_HINT_NONE, "suffix:m"), "set_aabb", "get_aabb");
  73. ADD_SIGNAL(MethodInfo("screen_entered"));
  74. ADD_SIGNAL(MethodInfo("screen_exited"));
  75. }
  76. VisibleOnScreenNotifier3D::VisibleOnScreenNotifier3D() {
  77. RID notifier = RS::get_singleton()->visibility_notifier_create();
  78. RS::get_singleton()->visibility_notifier_set_aabb(notifier, aabb);
  79. RS::get_singleton()->visibility_notifier_set_callbacks(notifier, callable_mp(this, &VisibleOnScreenNotifier3D::_visibility_enter), callable_mp(this, &VisibleOnScreenNotifier3D::_visibility_exit));
  80. set_base(notifier);
  81. }
  82. VisibleOnScreenNotifier3D::~VisibleOnScreenNotifier3D() {
  83. RID base_old = get_base();
  84. set_base(RID());
  85. ERR_FAIL_NULL(RenderingServer::get_singleton());
  86. RS::get_singleton()->free(base_old);
  87. }
  88. //////////////////////////////////////
  89. void VisibleOnScreenEnabler3D::_screen_enter() {
  90. _update_enable_mode(true);
  91. }
  92. void VisibleOnScreenEnabler3D::_screen_exit() {
  93. _update_enable_mode(false);
  94. }
  95. void VisibleOnScreenEnabler3D::set_enable_mode(EnableMode p_mode) {
  96. enable_mode = p_mode;
  97. if (is_inside_tree()) {
  98. _update_enable_mode(is_on_screen());
  99. }
  100. }
  101. VisibleOnScreenEnabler3D::EnableMode VisibleOnScreenEnabler3D::get_enable_mode() {
  102. return enable_mode;
  103. }
  104. void VisibleOnScreenEnabler3D::set_enable_node_path(NodePath p_path) {
  105. if (enable_node_path == p_path) {
  106. return;
  107. }
  108. enable_node_path = p_path;
  109. if (enable_node_path.is_empty()) {
  110. node_id = ObjectID();
  111. return;
  112. }
  113. if (is_inside_tree() && !Engine::get_singleton()->is_editor_hint()) {
  114. node_id = ObjectID();
  115. Node *node = get_node(enable_node_path);
  116. if (node) {
  117. node_id = node->get_instance_id();
  118. _update_enable_mode(is_on_screen());
  119. }
  120. }
  121. }
  122. NodePath VisibleOnScreenEnabler3D::get_enable_node_path() {
  123. return enable_node_path;
  124. }
  125. void VisibleOnScreenEnabler3D::_update_enable_mode(bool p_enable) {
  126. Node *node = ObjectDB::get_instance<Node>(node_id);
  127. if (node) {
  128. if (p_enable) {
  129. switch (enable_mode) {
  130. case ENABLE_MODE_INHERIT: {
  131. node->set_process_mode(PROCESS_MODE_INHERIT);
  132. } break;
  133. case ENABLE_MODE_ALWAYS: {
  134. node->set_process_mode(PROCESS_MODE_ALWAYS);
  135. } break;
  136. case ENABLE_MODE_WHEN_PAUSED: {
  137. node->set_process_mode(PROCESS_MODE_WHEN_PAUSED);
  138. } break;
  139. }
  140. } else {
  141. node->set_process_mode(PROCESS_MODE_DISABLED);
  142. }
  143. }
  144. }
  145. void VisibleOnScreenEnabler3D::_notification(int p_what) {
  146. switch (p_what) {
  147. case NOTIFICATION_ENTER_TREE: {
  148. if (Engine::get_singleton()->is_editor_hint()) {
  149. return;
  150. }
  151. node_id = ObjectID();
  152. if (enable_node_path.is_empty()) {
  153. return;
  154. }
  155. Node *node = get_node(enable_node_path);
  156. if (node) {
  157. node_id = node->get_instance_id();
  158. node->set_process_mode(PROCESS_MODE_DISABLED);
  159. }
  160. } break;
  161. case NOTIFICATION_EXIT_TREE: {
  162. node_id = ObjectID();
  163. } break;
  164. }
  165. }
  166. void VisibleOnScreenEnabler3D::_bind_methods() {
  167. ClassDB::bind_method(D_METHOD("set_enable_mode", "mode"), &VisibleOnScreenEnabler3D::set_enable_mode);
  168. ClassDB::bind_method(D_METHOD("get_enable_mode"), &VisibleOnScreenEnabler3D::get_enable_mode);
  169. ClassDB::bind_method(D_METHOD("set_enable_node_path", "path"), &VisibleOnScreenEnabler3D::set_enable_node_path);
  170. ClassDB::bind_method(D_METHOD("get_enable_node_path"), &VisibleOnScreenEnabler3D::get_enable_node_path);
  171. ADD_GROUP("Enabling", "enable_");
  172. ADD_PROPERTY(PropertyInfo(Variant::INT, "enable_mode", PROPERTY_HINT_ENUM, "Inherit,Always,When Paused"), "set_enable_mode", "get_enable_mode");
  173. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "enable_node_path"), "set_enable_node_path", "get_enable_node_path");
  174. BIND_ENUM_CONSTANT(ENABLE_MODE_INHERIT);
  175. BIND_ENUM_CONSTANT(ENABLE_MODE_ALWAYS);
  176. BIND_ENUM_CONSTANT(ENABLE_MODE_WHEN_PAUSED);
  177. }
  178. VisibleOnScreenEnabler3D::VisibleOnScreenEnabler3D() {
  179. }