| 1 | /**************************************************************************/ |
| 2 | /* world_environment.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 "world_environment.h" |
| 32 | |
| 33 | #include "scene/3d/node_3d.h" |
| 34 | #include "scene/main/viewport.h" |
| 35 | |
| 36 | void WorldEnvironment::_notification(int p_what) { |
| 37 | switch (p_what) { |
| 38 | case Node3D::NOTIFICATION_ENTER_WORLD: |
| 39 | case Node3D::NOTIFICATION_ENTER_TREE: { |
| 40 | if (environment.is_valid()) { |
| 41 | add_to_group("_world_environment_" + itos(get_viewport()->find_world_3d()->get_scenario().get_id())); |
| 42 | _update_current_environment(); |
| 43 | } |
| 44 | |
| 45 | if (camera_attributes.is_valid()) { |
| 46 | add_to_group("_world_camera_attributes_" + itos(get_viewport()->find_world_3d()->get_scenario().get_id())); |
| 47 | _update_current_camera_attributes(); |
| 48 | } |
| 49 | |
| 50 | if (compositor.is_valid()) { |
| 51 | add_to_group("_world_compositor_" + itos(get_viewport()->find_world_3d()->get_scenario().get_id())); |
| 52 | _update_current_compositor(); |
| 53 | } |
| 54 | } break; |
| 55 | |
| 56 | case Node3D::NOTIFICATION_EXIT_WORLD: |
| 57 | case Node3D::NOTIFICATION_EXIT_TREE: { |
| 58 | if (environment.is_valid()) { |
| 59 | remove_from_group("_world_environment_" + itos(get_viewport()->find_world_3d()->get_scenario().get_id())); |
| 60 | _update_current_environment(); |
| 61 | } |
| 62 | |
| 63 | if (camera_attributes.is_valid()) { |
| 64 | remove_from_group("_world_camera_attributes_" + itos(get_viewport()->find_world_3d()->get_scenario().get_id())); |
| 65 | _update_current_camera_attributes(); |
| 66 | } |
| 67 | |
| 68 | if (compositor.is_valid()) { |
| 69 | remove_from_group("_world_compositor_" + itos(get_viewport()->find_world_3d()->get_scenario().get_id())); |
| 70 | _update_current_compositor(); |
| 71 | } |
| 72 | } break; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | void WorldEnvironment::_update_current_environment() { |
| 77 | WorldEnvironment *first = Object::cast_to<WorldEnvironment>(get_tree()->get_first_node_in_group("_world_environment_" + itos(get_viewport()->find_world_3d()->get_scenario().get_id()))); |
| 78 | |
| 79 | if (first) { |
| 80 | get_viewport()->find_world_3d()->set_environment(first->environment); |
| 81 | } else { |
| 82 | get_viewport()->find_world_3d()->set_environment(Ref<Environment>()); |
| 83 | } |
| 84 | get_tree()->call_group_flags(SceneTree::GROUP_CALL_DEFERRED, "_world_environment_" + itos(get_viewport()->find_world_3d()->get_scenario().get_id()), "update_configuration_warnings"); |
| 85 | } |
| 86 | |
| 87 | void WorldEnvironment::_update_current_camera_attributes() { |
| 88 | WorldEnvironment *first = Object::cast_to<WorldEnvironment>(get_tree()->get_first_node_in_group("_world_camera_attributes_" + itos(get_viewport()->find_world_3d()->get_scenario().get_id()))); |
| 89 | if (first) { |
| 90 | get_viewport()->find_world_3d()->set_camera_attributes(first->camera_attributes); |
| 91 | } else { |
| 92 | get_viewport()->find_world_3d()->set_camera_attributes(Ref<CameraAttributes>()); |
| 93 | } |
| 94 | |
| 95 | get_tree()->call_group_flags(SceneTree::GROUP_CALL_DEFERRED, "_world_camera_attributes_" + itos(get_viewport()->find_world_3d()->get_scenario().get_id()), "update_configuration_warnings"); |
| 96 | } |
| 97 | |
| 98 | void WorldEnvironment::_update_current_compositor() { |
| 99 | WorldEnvironment *first = Object::cast_to<WorldEnvironment>(get_tree()->get_first_node_in_group("_world_compositor_" + itos(get_viewport()->find_world_3d()->get_scenario().get_id()))); |
| 100 | if (first) { |
| 101 | get_viewport()->find_world_3d()->set_compositor(first->compositor); |
| 102 | } else { |
| 103 | get_viewport()->find_world_3d()->set_compositor(Ref<Compositor>()); |
| 104 | } |
| 105 | |
| 106 | get_tree()->call_group_flags(SceneTree::GROUP_CALL_DEFERRED, "_world_compositor_" + itos(get_viewport()->find_world_3d()->get_scenario().get_id()), "update_configuration_warnings"); |
| 107 | } |
| 108 | |
| 109 | void WorldEnvironment::set_environment(const Ref<Environment> &p_environment) { |
| 110 | if (environment == p_environment) { |
| 111 | return; |
| 112 | } |
| 113 | if (is_inside_tree() && environment.is_valid()) { |
| 114 | remove_from_group("_world_environment_" + itos(get_viewport()->find_world_3d()->get_scenario().get_id())); |
| 115 | } |
| 116 | |
| 117 | environment = p_environment; |
| 118 | |
| 119 | if (is_inside_tree() && environment.is_valid()) { |
| 120 | add_to_group("_world_environment_" + itos(get_viewport()->find_world_3d()->get_scenario().get_id())); |
| 121 | } |
| 122 | |
| 123 | if (is_inside_tree()) { |
| 124 | _update_current_environment(); |
| 125 | } else { |
| 126 | update_configuration_warnings(); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | Ref<Environment> WorldEnvironment::get_environment() const { |
| 131 | return environment; |
| 132 | } |
| 133 | |
| 134 | void WorldEnvironment::set_camera_attributes(const Ref<CameraAttributes> &p_camera_attributes) { |
| 135 | if (camera_attributes == p_camera_attributes) { |
| 136 | return; |
| 137 | } |
| 138 | |
| 139 | if (is_inside_tree() && camera_attributes.is_valid() && get_viewport()->find_world_3d()->get_camera_attributes() == camera_attributes) { |
| 140 | remove_from_group("_world_camera_attributes_" + itos(get_viewport()->find_world_3d()->get_scenario().get_id())); |
| 141 | } |
| 142 | |
| 143 | camera_attributes = p_camera_attributes; |
| 144 | if (is_inside_tree() && camera_attributes.is_valid()) { |
| 145 | add_to_group("_world_camera_attributes_" + itos(get_viewport()->find_world_3d()->get_scenario().get_id())); |
| 146 | } |
| 147 | |
| 148 | if (is_inside_tree()) { |
| 149 | _update_current_camera_attributes(); |
| 150 | } else { |
| 151 | update_configuration_warnings(); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | Ref<CameraAttributes> WorldEnvironment::get_camera_attributes() const { |
| 156 | return camera_attributes; |
| 157 | } |
| 158 | |
| 159 | void WorldEnvironment::set_compositor(const Ref<Compositor> &p_compositor) { |
| 160 | if (compositor == p_compositor) { |
| 161 | return; |
| 162 | } |
| 163 | if (is_inside_tree() && compositor.is_valid()) { |
| 164 | remove_from_group("_world_compositor_" + itos(get_viewport()->find_world_3d()->get_scenario().get_id())); |
| 165 | } |
| 166 | |
| 167 | compositor = p_compositor; |
| 168 | |
| 169 | if (is_inside_tree() && compositor.is_valid()) { |
| 170 | add_to_group("_world_compositor_" + itos(get_viewport()->find_world_3d()->get_scenario().get_id())); |
| 171 | } |
| 172 | |
| 173 | if (is_inside_tree()) { |
| 174 | _update_current_compositor(); |
| 175 | } else { |
| 176 | update_configuration_warnings(); |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | Ref<Compositor> WorldEnvironment::get_compositor() const { |
| 181 | return compositor; |
| 182 | } |
| 183 | |
| 184 | PackedStringArray WorldEnvironment::get_configuration_warnings() const { |
| 185 | PackedStringArray warnings = Node::get_configuration_warnings(); |
| 186 | |
| 187 | if (environment.is_null() && camera_attributes.is_null()) { |
| 188 | warnings.push_back(RTR("To have any visible effect, WorldEnvironment requires its \"Environment\" property to contain an Environment, its \"Camera Attributes\" property to contain a CameraAttributes resource, or both.")); |
| 189 | } |
| 190 | |
| 191 | if (!is_inside_tree()) { |
| 192 | return warnings; |
| 193 | } |
| 194 | |
| 195 | if (environment.is_valid() && get_viewport()->find_world_3d()->get_environment() != environment) { |
| 196 | warnings.push_back(("Only the first Environment has an effect in a scene (or set of instantiated scenes).")); |
| 197 | } |
| 198 | |
| 199 | if (camera_attributes.is_valid() && get_viewport()->find_world_3d()->get_camera_attributes() != camera_attributes) { |
| 200 | warnings.push_back(RTR("Only one WorldEnvironment is allowed per scene (or set of instantiated scenes).")); |
| 201 | } |
| 202 | |
| 203 | if (compositor.is_valid() && get_viewport()->find_world_3d()->get_compositor() != compositor) { |
| 204 | warnings.push_back(("Only the first Compositor has an effect in a scene (or set of instantiated scenes).")); |
| 205 | } |
| 206 | |
| 207 | return warnings; |
| 208 | } |
| 209 | |
| 210 | void WorldEnvironment::_bind_methods() { |
| 211 | ClassDB::bind_method(D_METHOD("set_environment", "env"), &WorldEnvironment::set_environment); |
| 212 | ClassDB::bind_method(D_METHOD("get_environment"), &WorldEnvironment::get_environment); |
| 213 | ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "environment", PROPERTY_HINT_RESOURCE_TYPE, "Environment"), "set_environment", "get_environment"); |
| 214 | |
| 215 | ClassDB::bind_method(D_METHOD("set_camera_attributes", "camera_attributes"), &WorldEnvironment::set_camera_attributes); |
| 216 | ClassDB::bind_method(D_METHOD("get_camera_attributes"), &WorldEnvironment::get_camera_attributes); |
| 217 | ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "camera_attributes", PROPERTY_HINT_RESOURCE_TYPE, "CameraAttributesPractical,CameraAttributesPhysical"), "set_camera_attributes", "get_camera_attributes"); |
| 218 | |
| 219 | ClassDB::bind_method(D_METHOD("set_compositor", "compositor"), &WorldEnvironment::set_compositor); |
| 220 | ClassDB::bind_method(D_METHOD("get_compositor"), &WorldEnvironment::get_compositor); |
| 221 | ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "compositor", PROPERTY_HINT_RESOURCE_TYPE, "Compositor"), "set_compositor", "get_compositor"); |
| 222 | } |
| 223 | |
| 224 | WorldEnvironment::WorldEnvironment() { |
| 225 | } |