2021-08-05 16:44:43 +02:00
|
|
|
/*************************************************************************/
|
|
|
|
/* occluder.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/*************************************************************************/
|
2022-01-13 09:45:09 +01:00
|
|
|
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
|
2021-08-05 16:44:43 +02:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
|
|
|
|
|
|
|
#include "occluder.h"
|
|
|
|
|
|
|
|
#include "core/engine.h"
|
2021-08-17 14:40:39 +02:00
|
|
|
#include "servers/visual/portals/portal_occlusion_culler.h"
|
2021-08-05 16:44:43 +02:00
|
|
|
|
|
|
|
void Occluder::resource_changed(RES res) {
|
|
|
|
update_gizmo();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Occluder::set_shape(const Ref<OccluderShape> &p_shape) {
|
|
|
|
if (p_shape == _shape) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!_shape.is_null()) {
|
|
|
|
_shape->unregister_owner(this);
|
|
|
|
}
|
|
|
|
_shape = p_shape;
|
|
|
|
if (_shape.is_valid()) {
|
|
|
|
_shape->register_owner(this);
|
|
|
|
|
|
|
|
if (is_inside_world() && get_world().is_valid()) {
|
2022-02-11 15:36:02 +01:00
|
|
|
if (_occluder_instance.is_valid()) {
|
|
|
|
VisualServer::get_singleton()->occluder_instance_link_resource(_occluder_instance, p_shape->get_rid());
|
2021-08-05 16:44:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
update_gizmo();
|
|
|
|
update_configuration_warning();
|
|
|
|
}
|
|
|
|
Ref<OccluderShape> Occluder::get_shape() const {
|
|
|
|
return _shape;
|
|
|
|
}
|
|
|
|
|
2021-08-17 14:40:39 +02:00
|
|
|
#ifdef TOOLS_ENABLED
|
|
|
|
AABB Occluder::get_fallback_gizmo_aabb() const {
|
|
|
|
if (_shape.is_valid()) {
|
|
|
|
return _shape->get_fallback_gizmo_aabb();
|
|
|
|
}
|
|
|
|
return Spatial::get_fallback_gizmo_aabb();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-08-05 16:44:43 +02:00
|
|
|
String Occluder::get_configuration_warning() const {
|
|
|
|
String warning = Spatial::get_configuration_warning();
|
|
|
|
|
|
|
|
if (!_shape.is_valid()) {
|
|
|
|
if (!warning.empty()) {
|
|
|
|
warning += "\n\n";
|
|
|
|
}
|
|
|
|
warning += TTR("No shape is set.");
|
2021-08-17 14:40:39 +02:00
|
|
|
return warning;
|
2021-08-05 16:44:43 +02:00
|
|
|
}
|
|
|
|
|
2021-08-17 14:40:39 +02:00
|
|
|
#ifdef TOOLS_ENABLED
|
|
|
|
if (_shape.ptr()->requires_uniform_scale()) {
|
|
|
|
Transform tr = get_global_transform();
|
|
|
|
Vector3 scale = tr.basis.get_scale();
|
|
|
|
|
|
|
|
if ((!Math::is_equal_approx(scale.x, scale.y, 0.01f)) ||
|
|
|
|
(!Math::is_equal_approx(scale.x, scale.z, 0.01f))) {
|
|
|
|
if (!warning.empty()) {
|
|
|
|
warning += "\n\n";
|
|
|
|
}
|
|
|
|
warning += TTR("Only uniform scales are supported.");
|
2021-08-05 16:44:43 +02:00
|
|
|
}
|
|
|
|
}
|
2021-08-17 14:40:39 +02:00
|
|
|
#endif
|
2021-08-05 16:44:43 +02:00
|
|
|
|
|
|
|
return warning;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Occluder::_notification(int p_what) {
|
|
|
|
switch (p_what) {
|
|
|
|
case NOTIFICATION_ENTER_WORLD: {
|
|
|
|
ERR_FAIL_COND(get_world().is_null());
|
2022-02-11 15:36:02 +01:00
|
|
|
|
|
|
|
if (_occluder_instance.is_valid()) {
|
|
|
|
VisualServer::get_singleton()->occluder_instance_set_scenario(_occluder_instance, get_world()->get_scenario());
|
|
|
|
if (get_shape().is_valid()) {
|
|
|
|
VisualServer::get_singleton()->occluder_instance_link_resource(_occluder_instance, get_shape()->get_rid());
|
|
|
|
}
|
|
|
|
VisualServer::get_singleton()->occluder_instance_set_active(_occluder_instance, is_visible_in_tree());
|
|
|
|
VisualServer::get_singleton()->occluder_instance_set_transform(_occluder_instance, get_global_transform());
|
2021-08-05 16:44:43 +02:00
|
|
|
}
|
2022-02-11 15:36:02 +01:00
|
|
|
|
2021-08-17 14:40:39 +02:00
|
|
|
#ifdef TOOLS_ENABLED
|
|
|
|
if (Engine::get_singleton()->is_editor_hint()) {
|
|
|
|
set_process_internal(true);
|
|
|
|
}
|
|
|
|
#endif
|
2021-08-05 16:44:43 +02:00
|
|
|
} break;
|
|
|
|
case NOTIFICATION_EXIT_WORLD: {
|
2022-02-11 15:36:02 +01:00
|
|
|
if (_occluder_instance.is_valid()) {
|
|
|
|
VisualServer::get_singleton()->occluder_instance_set_scenario(_occluder_instance, RID());
|
2021-08-05 16:44:43 +02:00
|
|
|
}
|
2021-08-17 14:40:39 +02:00
|
|
|
#ifdef TOOLS_ENABLED
|
|
|
|
if (Engine::get_singleton()->is_editor_hint()) {
|
|
|
|
set_process_internal(false);
|
|
|
|
}
|
|
|
|
#endif
|
2021-08-05 16:44:43 +02:00
|
|
|
} break;
|
|
|
|
case NOTIFICATION_VISIBILITY_CHANGED: {
|
2022-02-11 15:36:02 +01:00
|
|
|
if (_occluder_instance.is_valid() && is_inside_tree()) {
|
|
|
|
VisualServer::get_singleton()->occluder_instance_set_active(_occluder_instance, is_visible_in_tree());
|
2021-08-05 16:44:43 +02:00
|
|
|
}
|
|
|
|
} break;
|
|
|
|
case NOTIFICATION_TRANSFORM_CHANGED: {
|
2022-02-11 15:36:02 +01:00
|
|
|
if (_occluder_instance.is_valid()) {
|
|
|
|
VisualServer::get_singleton()->occluder_instance_set_transform(_occluder_instance, get_global_transform());
|
2021-08-05 16:44:43 +02:00
|
|
|
#ifdef TOOLS_ENABLED
|
|
|
|
if (Engine::get_singleton()->is_editor_hint()) {
|
|
|
|
update_configuration_warning();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
} break;
|
2021-08-17 14:40:39 +02:00
|
|
|
case NOTIFICATION_INTERNAL_PROCESS: {
|
|
|
|
if (PortalOcclusionCuller::_redraw_gizmo) {
|
|
|
|
PortalOcclusionCuller::_redraw_gizmo = false;
|
|
|
|
update_gizmo();
|
|
|
|
}
|
|
|
|
} break;
|
2021-08-05 16:44:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Occluder::_bind_methods() {
|
|
|
|
ClassDB::bind_method(D_METHOD("resource_changed", "resource"), &Occluder::resource_changed);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_shape", "shape"), &Occluder::set_shape);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_shape"), &Occluder::get_shape);
|
|
|
|
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "shape", PROPERTY_HINT_RESOURCE_TYPE, "OccluderShape"), "set_shape", "get_shape");
|
|
|
|
}
|
|
|
|
|
|
|
|
Occluder::Occluder() {
|
2022-02-11 15:36:02 +01:00
|
|
|
_occluder_instance = RID_PRIME(VisualServer::get_singleton()->occluder_instance_create());
|
2021-08-05 16:44:43 +02:00
|
|
|
set_notify_transform(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
Occluder::~Occluder() {
|
2022-02-11 15:36:02 +01:00
|
|
|
if (_occluder_instance != RID()) {
|
|
|
|
VisualServer::get_singleton()->free(_occluder_instance);
|
|
|
|
}
|
|
|
|
|
2021-08-05 16:44:43 +02:00
|
|
|
if (!_shape.is_null()) {
|
|
|
|
_shape->unregister_owner(this);
|
|
|
|
}
|
|
|
|
}
|