Skip to content

Commit

Permalink
Print a warning when using sky with transparent background active
Browse files Browse the repository at this point in the history
  • Loading branch information
matheusmdx committed Oct 27, 2024
1 parent 1015a48 commit 3f095ca
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
37 changes: 33 additions & 4 deletions scene/3d/world_environment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,16 @@

#include "world_environment.h"

#include "core/config/project_settings.h"
#include "scene/3d/node_3d.h"
#include "scene/main/window.h"

void WorldEnvironment::_notification(int p_what) {
switch (p_what) {
case Node3D::NOTIFICATION_ENTER_WORLD:
case Node3D::NOTIFICATION_ENTER_TREE: {
_check_sky_warning();

if (environment.is_valid()) {
add_to_group("_world_environment_" + itos(get_viewport()->find_world_3d()->get_scenario().get_id()));
_update_current_environment();
Expand Down Expand Up @@ -110,14 +113,20 @@ void WorldEnvironment::set_environment(const Ref<Environment> &p_environment) {
if (environment == p_environment) {
return;
}
if (is_inside_tree() && environment.is_valid()) {
remove_from_group("_world_environment_" + itos(get_viewport()->find_world_3d()->get_scenario().get_id()));
if (environment.is_valid()) {
environment->disconnect(StringName("sky_changed"), callable_mp(this, &WorldEnvironment::_check_sky_warning));
if (is_inside_tree()) {
remove_from_group("_world_environment_" + itos(get_viewport()->find_world_3d()->get_scenario().get_id()));
}
}

environment = p_environment;

if (is_inside_tree() && environment.is_valid()) {
add_to_group("_world_environment_" + itos(get_viewport()->find_world_3d()->get_scenario().get_id()));
if (environment.is_valid()) {
environment->connect(StringName("sky_changed"), callable_mp(this, &WorldEnvironment::_check_sky_warning));
if (is_inside_tree()) {
add_to_group("_world_environment_" + itos(get_viewport()->find_world_3d()->get_scenario().get_id()));
}
}

if (is_inside_tree()) {
Expand Down Expand Up @@ -207,6 +216,20 @@ PackedStringArray WorldEnvironment::get_configuration_warnings() const {
return warnings;
}

void WorldEnvironment::_check_sky_warning() {
if ((environment.is_valid() && !environment->get_sky().is_valid()) || !is_inside_tree()) {
return;
}
Viewport *viewport = get_viewport();
if (viewport->get_parent_viewport() == get_tree()->get_root() || String(get_name()).begins_with("@")) {
if (GLOBAL_GET("rendering/viewport/transparent_background")) {
WARN_PRINT("Environment sky will not render when transparent background is active in project settings.");
}
} else if (viewport->has_transparent_background()) {
WARN_PRINT("Environment sky will not render when transparent background is active in subviewport.");
}
}

void WorldEnvironment::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_environment", "env"), &WorldEnvironment::set_environment);
ClassDB::bind_method(D_METHOD("get_environment"), &WorldEnvironment::get_environment);
Expand All @@ -223,3 +246,9 @@ void WorldEnvironment::_bind_methods() {

WorldEnvironment::WorldEnvironment() {
}

WorldEnvironment::~WorldEnvironment() {
if (environment.is_valid() && !environment->get_sky().is_valid()) {
environment->disconnect(StringName("sky_changed"), callable_mp(this, &WorldEnvironment::_check_sky_warning));
}
}
2 changes: 2 additions & 0 deletions scene/3d/world_environment.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class WorldEnvironment : public Node {
void _update_current_environment();
void _update_current_camera_attributes();
void _update_current_compositor();
void _check_sky_warning();

protected:
void _notification(int p_what);
Expand All @@ -64,6 +65,7 @@ class WorldEnvironment : public Node {
PackedStringArray get_configuration_warnings() const override;

WorldEnvironment();
~WorldEnvironment();
};

#endif // WORLD_ENVIRONMENT_H
4 changes: 4 additions & 0 deletions scene/resources/environment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ void Environment::set_sky(const Ref<Sky> &p_sky) {
if (bg_sky.is_valid()) {
sb_rid = bg_sky->get_rid();
}
emit_signal(StringName("sky_changed"));
RS::get_singleton()->environment_set_sky(environment, sb_rid);
}

Expand Down Expand Up @@ -1596,6 +1597,9 @@ void Environment::_bind_methods() {
BIND_ENUM_CONSTANT(SDFGI_Y_SCALE_50_PERCENT);
BIND_ENUM_CONSTANT(SDFGI_Y_SCALE_75_PERCENT);
BIND_ENUM_CONSTANT(SDFGI_Y_SCALE_100_PERCENT);

// Signals
ADD_SIGNAL(MethodInfo("sky_changed"));
}

Environment::Environment() {
Expand Down

0 comments on commit 3f095ca

Please sign in to comment.