Skip to content

Commit

Permalink
Added dummy implementation of EnvironmentRoot
Browse files Browse the repository at this point in the history
  • Loading branch information
PTKay committed Feb 1, 2024
1 parent 34e6826 commit 3a3a7c0
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 0 deletions.
26 changes: 26 additions & 0 deletions doc/classes/EnvironmentRoot.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8" ?>
<class name="EnvironmentRoot" inherits="Node" version="3.3">
<brief_description>
Default environment properties for the entire scene (post-processing effects, lighting and background settings).
</brief_description>
<description>
The [EnvironmentRoot] node is used to configure the default [Environment] for the scene.
The parameters defined in the [EnvironmentRoot] can be overridden by an [Environment] node set on the current [Camera]. Additionally, only one [EnvironmentRoot] may be instanced in a given scene at a time.
The [EnvironmentRoot] allows the user to specify default lighting parameters (e.g. ambient lighting), various post-processing effects (e.g. SSAO, DOF, Tonemapping), and how to draw the background (e.g. solid color, skybox). Usually, these are added in order to improve the realism/color balance of the scene.
</description>
<tutorials>
<link title="Environment and post-processing">https://docs.godotengine.org/en/3.3/tutorials/3d/environment_and_post_processing.html</link>
<link title="3D Material Testers Demo">https://godotengine.org/asset-library/asset/123</link>
<link title="2D HDR Demo">https://godotengine.org/asset-library/asset/110</link>
<link title="Third Person Shooter Demo">https://godotengine.org/asset-library/asset/678</link>
</tutorials>
<methods>
</methods>
<members>
<member name="environment" type="Environment" setter="set_environment" getter="get_environment">
The [Environment] resource used by this [EnvironmentRoot], defining the default properties.
</member>
</members>
<constants>
</constants>
</class>
1 change: 1 addition & 0 deletions scene/3d/SCsub
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ if env["disable_3d"]:
env.add_source_files(env.scene_sources, "particles.cpp")
env.add_source_files(env.scene_sources, "visual_instance.cpp")
env.add_source_files(env.scene_sources, "world_environment.cpp")
env.add_source_files(env.scene_sources, "environment_root.cpp")
else:
env.add_source_files(env.scene_sources, "*.cpp")
98 changes: 98 additions & 0 deletions scene/3d/environment_root.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/****************************************************************************/
/* environment_root.cpp */
/****************************************************************************/
/* !!!! NOTICE !!!! */
/* This type is a copy of the WorldEnvironment type */
/* This type is not a valid implementation of the BSE EnvironmentRoot type */
/****************************************************************************/

#include "environment_root.h"
#include "scene/main/viewport.h"

void EnvironmentRoot::_notification(int p_what) {

if (p_what == Spatial::NOTIFICATION_ENTER_WORLD || p_what == Spatial::NOTIFICATION_ENTER_TREE) {

if (environment.is_valid()) {
if (get_viewport()->find_world()->get_environment().is_valid()) {
WARN_PRINT("World already has an environment (Another EnvironmentRoot?), overriding.");
}
get_viewport()->find_world()->set_environment(environment);
add_to_group("_environment_root_" + itos(get_viewport()->find_world()->get_scenario().get_id()));
}

} else if (p_what == Spatial::NOTIFICATION_EXIT_WORLD || p_what == Spatial::NOTIFICATION_EXIT_TREE) {

if (environment.is_valid() && get_viewport()->find_world()->get_environment() == environment) {
get_viewport()->find_world()->set_environment(Ref<Environment>());
remove_from_group("_environment_root_" + itos(get_viewport()->find_world()->get_scenario().get_id()));
}
}
}

void EnvironmentRoot::set_environment(const Ref<Environment> &p_environment) {

if (is_inside_tree() && environment.is_valid() && get_viewport()->find_world()->get_environment() == environment) {
get_viewport()->find_world()->set_environment(Ref<Environment>());
remove_from_group("_environment_root_" + itos(get_viewport()->find_world()->get_scenario().get_id()));
//clean up
}

environment = p_environment;
if (is_inside_tree() && environment.is_valid()) {
if (get_viewport()->find_world()->get_environment().is_valid()) {
WARN_PRINT("World already has an environment (Another EnvironmentRoot?), overriding.");
}
get_viewport()->find_world()->set_environment(environment);
add_to_group("_environment_root_" + itos(get_viewport()->find_world()->get_scenario().get_id()));
}

update_configuration_warning();
}

Ref<Environment> EnvironmentRoot::get_environment() const {

return environment;
}

String EnvironmentRoot::get_configuration_warning() const {

String warning = Node::get_configuration_warning();
if (!environment.is_valid()) {
if (warning != String()) {
warning += "\n\n";
}
warning += TTR("EnvironmentRoot requires its \"Environment\" property to contain an Environment to have a visible effect.");
return warning;
}

if (/*!is_visible_in_tree() ||*/ !is_inside_tree())
return String();

List<Node *> nodes;
get_tree()->get_nodes_in_group("_environment_root_" + itos(get_viewport()->find_world()->get_scenario().get_id()), &nodes);

if (nodes.size() > 1) {
if (warning != String()) {
warning += "\n\n";
}
warning += TTR("Only one EnvironmentRoot is allowed per scene (or set of instanced scenes).");
}

// Commenting this warning for now, I think it makes no sense. If anyone can figure out what its supposed to do, feedback welcome. Else it should be deprecated.
//if (environment.is_valid() && get_viewport() && !get_viewport()->get_camera() && environment->get_background() != Environment::BG_CANVAS) {
// return TTR("This EnvironmentRoot is ignored. Either add a Camera (for 3D scenes) or set this environment's Background Mode to Canvas (for 2D scenes).");
//}

return warning;
}

void EnvironmentRoot::_bind_methods() {

ClassDB::bind_method(D_METHOD("set_environment", "env"), &EnvironmentRoot::set_environment);
ClassDB::bind_method(D_METHOD("get_environment"), &EnvironmentRoot::get_environment);
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "environment", PROPERTY_HINT_RESOURCE_TYPE, "Environment"), "set_environment", "get_environment");
}

EnvironmentRoot::EnvironmentRoot() {
}
33 changes: 33 additions & 0 deletions scene/3d/environment_root.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/****************************************************************************/
/* environment_root.h */
/****************************************************************************/
/* !!!! NOTICE !!!! */
/* This type is a copy of the WorldEnvironment type */
/* This type is not a valid implementation of the BSE EnvironmentRoot type */
/****************************************************************************/

#ifndef SCENARIO_FX_H2
#define SCENARIO_FX_H2

#include "scene/3d/spatial.h"

class EnvironmentRoot : public Node {

GDCLASS(EnvironmentRoot, Node);

Ref<Environment> environment;

protected:
void _notification(int p_what);
static void _bind_methods();

public:
void set_environment(const Ref<Environment> &p_environment);
Ref<Environment> get_environment() const;

String get_configuration_warning() const;

EnvironmentRoot();
};

#endif
2 changes: 2 additions & 0 deletions scene/register_scene_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@

#include "scene/3d/spatial.h"
#include "scene/3d/world_environment.h"
#include "scene/3d/environment_root.h"

#ifndef _3D_DISABLED
#include "scene/3d/area.h"
Expand Down Expand Up @@ -462,6 +463,7 @@ void register_scene_types() {
ClassDB::register_class<VisibilityNotifier>();
ClassDB::register_class<VisibilityEnabler>();
ClassDB::register_class<WorldEnvironment>();
ClassDB::register_class<EnvironmentRoot>();
ClassDB::register_class<RemoteTransform>();

ClassDB::register_virtual_class<Joint>();
Expand Down

0 comments on commit 3a3a7c0

Please sign in to comment.