visible_on_screen_notifier_3d.cpp 7.5 KB

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