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