| 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/window.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 | } break; |
| 50 | |
| 51 | case Node3D::NOTIFICATION_EXIT_WORLD: |
| 52 | case Node3D::NOTIFICATION_EXIT_TREE: { |
| 53 | if (environment.is_valid()) { |
| 54 | remove_from_group("_world_environment_" + itos(get_viewport()->find_world_3d()->get_scenario().get_id())); |
| 55 | _update_current_environment(); |
| 56 | } |
| 57 | |
| 58 | if (camera_attributes.is_valid()) { |
| 59 | remove_from_group("_world_camera_attributes_" + itos(get_viewport()->find_world_3d()->get_scenario().get_id())); |
| 60 | _update_current_camera_attributes(); |
| 61 | } |
| 62 | } break; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | void WorldEnvironment::_update_current_environment() { |
| 67 | 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()))); |
| 68 | |
| 69 | if (first) { |
| 70 | get_viewport()->find_world_3d()->set_environment(first->environment); |
| 71 | } else { |
| 72 | get_viewport()->find_world_3d()->set_environment(Ref<Environment>()); |
| 73 | } |
| 74 | get_tree()->call_group_flags(SceneTree::GROUP_CALL_DEFERRED, "_world_environment_" + itos(get_viewport()->find_world_3d()->get_scenario().get_id()), "update_configuration_warnings" ); |
| 75 | } |
| 76 | |
| 77 | void WorldEnvironment::_update_current_camera_attributes() { |
| 78 | 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()))); |
| 79 | if (first) { |
| 80 | get_viewport()->find_world_3d()->set_camera_attributes(first->camera_attributes); |
| 81 | } else { |
| 82 | get_viewport()->find_world_3d()->set_camera_attributes(Ref<CameraAttributes>()); |
| 83 | } |
| 84 | |
| 85 | 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" ); |
| 86 | } |
| 87 | |
| 88 | void WorldEnvironment::set_environment(const Ref<Environment> &p_environment) { |
| 89 | if (environment == p_environment) { |
| 90 | return; |
| 91 | } |
| 92 | if (is_inside_tree() && environment.is_valid()) { |
| 93 | remove_from_group("_world_environment_" + itos(get_viewport()->find_world_3d()->get_scenario().get_id())); |
| 94 | } |
| 95 | |
| 96 | environment = p_environment; |
| 97 | |
| 98 | if (is_inside_tree() && environment.is_valid()) { |
| 99 | add_to_group("_world_environment_" + itos(get_viewport()->find_world_3d()->get_scenario().get_id())); |
| 100 | } |
| 101 | |
| 102 | if (is_inside_tree()) { |
| 103 | _update_current_environment(); |
| 104 | } else { |
| 105 | update_configuration_warnings(); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | Ref<Environment> WorldEnvironment::get_environment() const { |
| 110 | return environment; |
| 111 | } |
| 112 | |
| 113 | void WorldEnvironment::set_camera_attributes(const Ref<CameraAttributes> &p_camera_attributes) { |
| 114 | if (camera_attributes == p_camera_attributes) { |
| 115 | return; |
| 116 | } |
| 117 | |
| 118 | if (is_inside_tree() && camera_attributes.is_valid() && get_viewport()->find_world_3d()->get_camera_attributes() == camera_attributes) { |
| 119 | remove_from_group("_world_camera_attributes_" + itos(get_viewport()->find_world_3d()->get_scenario().get_id())); |
| 120 | } |
| 121 | |
| 122 | camera_attributes = p_camera_attributes; |
| 123 | if (is_inside_tree() && camera_attributes.is_valid()) { |
| 124 | add_to_group("_world_camera_attributes_" + itos(get_viewport()->find_world_3d()->get_scenario().get_id())); |
| 125 | } |
| 126 | |
| 127 | if (is_inside_tree()) { |
| 128 | _update_current_camera_attributes(); |
| 129 | } else { |
| 130 | update_configuration_warnings(); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | Ref<CameraAttributes> WorldEnvironment::get_camera_attributes() const { |
| 135 | return camera_attributes; |
| 136 | } |
| 137 | |
| 138 | PackedStringArray WorldEnvironment::get_configuration_warnings() const { |
| 139 | PackedStringArray warnings = Node::get_configuration_warnings(); |
| 140 | |
| 141 | if (!environment.is_valid() && !camera_attributes.is_valid()) { |
| 142 | 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." )); |
| 143 | } |
| 144 | |
| 145 | if (!is_inside_tree()) { |
| 146 | return warnings; |
| 147 | } |
| 148 | |
| 149 | if (environment.is_valid() && get_viewport()->find_world_3d()->get_environment() != environment) { |
| 150 | warnings.push_back(("Only the first Environment has an effect in a scene (or set of instantiated scenes)." )); |
| 151 | } |
| 152 | |
| 153 | if (camera_attributes.is_valid() && get_viewport()->find_world_3d()->get_camera_attributes() != camera_attributes) { |
| 154 | warnings.push_back(RTR("Only one WorldEnvironment is allowed per scene (or set of instantiated scenes)." )); |
| 155 | } |
| 156 | |
| 157 | return warnings; |
| 158 | } |
| 159 | |
| 160 | void WorldEnvironment::_bind_methods() { |
| 161 | ClassDB::bind_method(D_METHOD("set_environment" , "env" ), &WorldEnvironment::set_environment); |
| 162 | ClassDB::bind_method(D_METHOD("get_environment" ), &WorldEnvironment::get_environment); |
| 163 | ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "environment" , PROPERTY_HINT_RESOURCE_TYPE, "Environment" ), "set_environment" , "get_environment" ); |
| 164 | |
| 165 | ClassDB::bind_method(D_METHOD("set_camera_attributes" , "camera_attributes" ), &WorldEnvironment::set_camera_attributes); |
| 166 | ClassDB::bind_method(D_METHOD("get_camera_attributes" ), &WorldEnvironment::get_camera_attributes); |
| 167 | ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "camera_attributes" , PROPERTY_HINT_RESOURCE_TYPE, "CameraAttributesPractical,CameraAttributesPhysical" ), "set_camera_attributes" , "get_camera_attributes" ); |
| 168 | } |
| 169 | |
| 170 | WorldEnvironment::WorldEnvironment() { |
| 171 | } |
| 172 | |