fog_volume.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /**************************************************************************/
  2. /* fog_volume.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 "fog_volume.h"
  31. #include "scene/resources/environment.h"
  32. ///////////////////////////
  33. void FogVolume::_bind_methods() {
  34. ClassDB::bind_method(D_METHOD("set_size", "size"), &FogVolume::set_size);
  35. ClassDB::bind_method(D_METHOD("get_size"), &FogVolume::get_size);
  36. ClassDB::bind_method(D_METHOD("set_shape", "shape"), &FogVolume::set_shape);
  37. ClassDB::bind_method(D_METHOD("get_shape"), &FogVolume::get_shape);
  38. ClassDB::bind_method(D_METHOD("set_material", "material"), &FogVolume::set_material);
  39. ClassDB::bind_method(D_METHOD("get_material"), &FogVolume::get_material);
  40. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "size", PROPERTY_HINT_RANGE, "0.01,1024,0.01,or_greater,suffix:m"), "set_size", "get_size");
  41. ADD_PROPERTY(PropertyInfo(Variant::INT, "shape", PROPERTY_HINT_ENUM, "Ellipsoid (Local),Cone (Local),Cylinder (Local),Box (Local),World (Global)"), "set_shape", "get_shape");
  42. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "material", PROPERTY_HINT_RESOURCE_TYPE, "FogMaterial,ShaderMaterial"), "set_material", "get_material");
  43. }
  44. void FogVolume::_validate_property(PropertyInfo &p_property) const {
  45. if (p_property.name == "size" && shape == RS::FOG_VOLUME_SHAPE_WORLD) {
  46. p_property.usage = PROPERTY_USAGE_NONE;
  47. return;
  48. }
  49. }
  50. #ifndef DISABLE_DEPRECATED
  51. bool FogVolume::_set(const StringName &p_name, const Variant &p_value) {
  52. if (p_name == "extents") { // Compatibility with Godot 3.x.
  53. set_size((Vector3)p_value * 2);
  54. return true;
  55. }
  56. return false;
  57. }
  58. bool FogVolume::_get(const StringName &p_name, Variant &r_property) const {
  59. if (p_name == "extents") { // Compatibility with Godot 3.x.
  60. r_property = size / 2;
  61. return true;
  62. }
  63. return false;
  64. }
  65. #endif // DISABLE_DEPRECATED
  66. void FogVolume::set_size(const Vector3 &p_size) {
  67. size = p_size;
  68. size = size.maxf(0);
  69. RS::get_singleton()->fog_volume_set_size(_get_volume(), size);
  70. update_gizmos();
  71. }
  72. Vector3 FogVolume::get_size() const {
  73. return size;
  74. }
  75. void FogVolume::set_shape(RS::FogVolumeShape p_type) {
  76. shape = p_type;
  77. RS::get_singleton()->fog_volume_set_shape(_get_volume(), shape);
  78. RS::get_singleton()->instance_set_ignore_culling(get_instance(), shape == RS::FOG_VOLUME_SHAPE_WORLD);
  79. update_gizmos();
  80. notify_property_list_changed();
  81. }
  82. RS::FogVolumeShape FogVolume::get_shape() const {
  83. return shape;
  84. }
  85. void FogVolume::set_material(const Ref<Material> &p_material) {
  86. material = p_material;
  87. RID material_rid;
  88. if (material.is_valid()) {
  89. material_rid = material->get_rid();
  90. }
  91. RS::get_singleton()->fog_volume_set_material(_get_volume(), material_rid);
  92. update_gizmos();
  93. }
  94. Ref<Material> FogVolume::get_material() const {
  95. return material;
  96. }
  97. AABB FogVolume::get_aabb() const {
  98. if (shape != RS::FOG_VOLUME_SHAPE_WORLD) {
  99. return AABB(-size / 2, size);
  100. }
  101. return AABB();
  102. }
  103. PackedStringArray FogVolume::get_configuration_warnings() const {
  104. PackedStringArray warnings = VisualInstance3D::get_configuration_warnings();
  105. Ref<Environment> environment = get_viewport()->find_world_3d()->get_environment();
  106. if (OS::get_singleton()->get_current_rendering_method() != "forward_plus") {
  107. warnings.push_back(RTR("Fog Volumes are only visible when using the Forward+ backend."));
  108. return warnings;
  109. }
  110. if (environment.is_valid() && !environment->is_volumetric_fog_enabled()) {
  111. warnings.push_back(RTR("Fog Volumes need volumetric fog to be enabled in the scene's Environment in order to be visible."));
  112. }
  113. return warnings;
  114. }
  115. FogVolume::FogVolume() {
  116. volume = RS::get_singleton()->fog_volume_create();
  117. RS::get_singleton()->fog_volume_set_shape(volume, RS::FOG_VOLUME_SHAPE_BOX);
  118. set_base(volume);
  119. }
  120. FogVolume::~FogVolume() {
  121. ERR_FAIL_NULL(RenderingServer::get_singleton());
  122. RS::get_singleton()->free(volume);
  123. }