world_environment.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. #include "world_environment.h"
  31. #include "scene/3d/node_3d.h"
  32. #include "scene/main/window.h"
  33. void WorldEnvironment::_notification(int p_what) {
  34. switch (p_what) {
  35. case Node3D::NOTIFICATION_ENTER_WORLD:
  36. case Node3D::NOTIFICATION_ENTER_TREE: {
  37. if (environment.is_valid()) {
  38. add_to_group("_world_environment_" + itos(get_viewport()->find_world_3d()->get_scenario().get_id()));
  39. _update_current_environment();
  40. }
  41. if (camera_attributes.is_valid()) {
  42. add_to_group("_world_camera_attributes_" + itos(get_viewport()->find_world_3d()->get_scenario().get_id()));
  43. _update_current_camera_attributes();
  44. }
  45. if (compositor.is_valid()) {
  46. add_to_group("_world_compositor_" + itos(get_viewport()->find_world_3d()->get_scenario().get_id()));
  47. _update_current_compositor();
  48. }
  49. } break;
  50. case Node3D::NOTIFICATION_EXIT_WORLD:
  51. case Node3D::NOTIFICATION_EXIT_TREE: {
  52. if (environment.is_valid()) {
  53. remove_from_group("_world_environment_" + itos(get_viewport()->find_world_3d()->get_scenario().get_id()));
  54. _update_current_environment();
  55. }
  56. if (camera_attributes.is_valid()) {
  57. remove_from_group("_world_camera_attributes_" + itos(get_viewport()->find_world_3d()->get_scenario().get_id()));
  58. _update_current_camera_attributes();
  59. }
  60. if (compositor.is_valid()) {
  61. remove_from_group("_world_compositor_" + itos(get_viewport()->find_world_3d()->get_scenario().get_id()));
  62. _update_current_compositor();
  63. }
  64. } break;
  65. }
  66. }
  67. void WorldEnvironment::_update_current_environment() {
  68. 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())));
  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. void WorldEnvironment::_update_current_camera_attributes() {
  77. 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())));
  78. if (first) {
  79. get_viewport()->find_world_3d()->set_camera_attributes(first->camera_attributes);
  80. } else {
  81. get_viewport()->find_world_3d()->set_camera_attributes(Ref<CameraAttributes>());
  82. }
  83. 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");
  84. }
  85. void WorldEnvironment::_update_current_compositor() {
  86. 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())));
  87. if (first) {
  88. get_viewport()->find_world_3d()->set_compositor(first->compositor);
  89. } else {
  90. get_viewport()->find_world_3d()->set_compositor(Ref<Compositor>());
  91. }
  92. get_tree()->call_group_flags(SceneTree::GROUP_CALL_DEFERRED, "_world_compositor_" + itos(get_viewport()->find_world_3d()->get_scenario().get_id()), "update_configuration_warnings");
  93. }
  94. void WorldEnvironment::set_environment(const Ref<Environment> &p_environment) {
  95. if (environment == p_environment) {
  96. return;
  97. }
  98. if (is_inside_tree() && environment.is_valid()) {
  99. remove_from_group("_world_environment_" + itos(get_viewport()->find_world_3d()->get_scenario().get_id()));
  100. }
  101. environment = p_environment;
  102. if (is_inside_tree() && environment.is_valid()) {
  103. add_to_group("_world_environment_" + itos(get_viewport()->find_world_3d()->get_scenario().get_id()));
  104. }
  105. if (is_inside_tree()) {
  106. _update_current_environment();
  107. } else {
  108. update_configuration_warnings();
  109. }
  110. }
  111. Ref<Environment> WorldEnvironment::get_environment() const {
  112. return environment;
  113. }
  114. void WorldEnvironment::set_camera_attributes(const Ref<CameraAttributes> &p_camera_attributes) {
  115. if (camera_attributes == p_camera_attributes) {
  116. return;
  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. camera_attributes = p_camera_attributes;
  122. if (is_inside_tree() && camera_attributes.is_valid()) {
  123. add_to_group("_world_camera_attributes_" + itos(get_viewport()->find_world_3d()->get_scenario().get_id()));
  124. }
  125. if (is_inside_tree()) {
  126. _update_current_camera_attributes();
  127. } else {
  128. update_configuration_warnings();
  129. }
  130. }
  131. Ref<CameraAttributes> WorldEnvironment::get_camera_attributes() const {
  132. return camera_attributes;
  133. }
  134. void WorldEnvironment::set_compositor(const Ref<Compositor> &p_compositor) {
  135. if (compositor == p_compositor) {
  136. return;
  137. }
  138. if (is_inside_tree() && compositor.is_valid()) {
  139. remove_from_group("_world_compositor_" + itos(get_viewport()->find_world_3d()->get_scenario().get_id()));
  140. }
  141. compositor = p_compositor;
  142. if (is_inside_tree() && compositor.is_valid()) {
  143. add_to_group("_world_compositor_" + itos(get_viewport()->find_world_3d()->get_scenario().get_id()));
  144. }
  145. if (is_inside_tree()) {
  146. _update_current_compositor();
  147. } else {
  148. update_configuration_warnings();
  149. }
  150. }
  151. Ref<Compositor> WorldEnvironment::get_compositor() const {
  152. return compositor;
  153. }
  154. PackedStringArray WorldEnvironment::get_configuration_warnings() const {
  155. PackedStringArray warnings = Node::get_configuration_warnings();
  156. if (!environment.is_valid() && !camera_attributes.is_valid()) {
  157. 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."));
  158. }
  159. if (!is_inside_tree()) {
  160. return warnings;
  161. }
  162. if (environment.is_valid() && get_viewport()->find_world_3d()->get_environment() != environment) {
  163. warnings.push_back(("Only the first Environment has an effect in a scene (or set of instantiated scenes)."));
  164. }
  165. if (camera_attributes.is_valid() && get_viewport()->find_world_3d()->get_camera_attributes() != camera_attributes) {
  166. warnings.push_back(RTR("Only one WorldEnvironment is allowed per scene (or set of instantiated scenes)."));
  167. }
  168. if (compositor.is_valid() && get_viewport()->find_world_3d()->get_compositor() != compositor) {
  169. warnings.push_back(("Only the first Compositor has an effect in a scene (or set of instantiated scenes)."));
  170. }
  171. return warnings;
  172. }
  173. void WorldEnvironment::_bind_methods() {
  174. ClassDB::bind_method(D_METHOD("set_environment", "env"), &WorldEnvironment::set_environment);
  175. ClassDB::bind_method(D_METHOD("get_environment"), &WorldEnvironment::get_environment);
  176. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "environment", PROPERTY_HINT_RESOURCE_TYPE, "Environment"), "set_environment", "get_environment");
  177. ClassDB::bind_method(D_METHOD("set_camera_attributes", "camera_attributes"), &WorldEnvironment::set_camera_attributes);
  178. ClassDB::bind_method(D_METHOD("get_camera_attributes"), &WorldEnvironment::get_camera_attributes);
  179. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "camera_attributes", PROPERTY_HINT_RESOURCE_TYPE, "CameraAttributesPractical,CameraAttributesPhysical"), "set_camera_attributes", "get_camera_attributes");
  180. ClassDB::bind_method(D_METHOD("set_compositor", "compositor"), &WorldEnvironment::set_compositor);
  181. ClassDB::bind_method(D_METHOD("get_compositor"), &WorldEnvironment::get_compositor);
  182. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "compositor", PROPERTY_HINT_RESOURCE_TYPE, "Compositor"), "set_compositor", "get_compositor");
  183. }
  184. WorldEnvironment::WorldEnvironment() {
  185. }