occluder.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /**************************************************************************/
  2. /* occluder.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 "occluder.h"
  31. #include "core/engine.h"
  32. #include "servers/visual/portals/portal_occlusion_culler.h"
  33. void Occluder::resource_changed(RES res) {
  34. update_gizmo();
  35. }
  36. void Occluder::set_shape(const Ref<OccluderShape> &p_shape) {
  37. if (p_shape == _shape) {
  38. return;
  39. }
  40. if (!_shape.is_null()) {
  41. _shape->unregister_owner(this);
  42. }
  43. _shape = p_shape;
  44. if (_shape.is_valid()) {
  45. _shape->register_owner(this);
  46. if (is_inside_world() && get_world().is_valid()) {
  47. if (_occluder_instance.is_valid()) {
  48. VisualServer::get_singleton()->occluder_instance_link_resource(_occluder_instance, p_shape->get_rid());
  49. }
  50. }
  51. }
  52. update_gizmo();
  53. update_configuration_warning();
  54. }
  55. Ref<OccluderShape> Occluder::get_shape() const {
  56. return _shape;
  57. }
  58. #ifdef TOOLS_ENABLED
  59. AABB Occluder::get_fallback_gizmo_aabb() const {
  60. if (_shape.is_valid()) {
  61. return _shape->get_fallback_gizmo_aabb();
  62. }
  63. return Spatial::get_fallback_gizmo_aabb();
  64. }
  65. #endif
  66. String Occluder::get_configuration_warning() const {
  67. String warning = Spatial::get_configuration_warning();
  68. if (!_shape.is_valid()) {
  69. if (!warning.empty()) {
  70. warning += "\n\n";
  71. }
  72. warning += TTR("No shape is set.");
  73. return warning;
  74. }
  75. #ifdef TOOLS_ENABLED
  76. if (_shape.ptr()->requires_uniform_scale()) {
  77. Transform tr = get_global_transform();
  78. Vector3 scale = tr.basis.get_scale();
  79. if ((!Math::is_equal_approx(scale.x, scale.y, 0.01f)) ||
  80. (!Math::is_equal_approx(scale.x, scale.z, 0.01f))) {
  81. if (!warning.empty()) {
  82. warning += "\n\n";
  83. }
  84. warning += TTR("Only uniform scales are supported.");
  85. }
  86. }
  87. #endif
  88. return warning;
  89. }
  90. void Occluder::_notification(int p_what) {
  91. switch (p_what) {
  92. case NOTIFICATION_ENTER_WORLD: {
  93. ERR_FAIL_COND(get_world().is_null());
  94. if (_occluder_instance.is_valid()) {
  95. VisualServer::get_singleton()->occluder_instance_set_scenario(_occluder_instance, get_world()->get_scenario());
  96. if (get_shape().is_valid()) {
  97. VisualServer::get_singleton()->occluder_instance_link_resource(_occluder_instance, get_shape()->get_rid());
  98. }
  99. VisualServer::get_singleton()->occluder_instance_set_active(_occluder_instance, is_visible_in_tree());
  100. VisualServer::get_singleton()->occluder_instance_set_transform(_occluder_instance, get_global_transform());
  101. }
  102. #ifdef TOOLS_ENABLED
  103. if (Engine::get_singleton()->is_editor_hint()) {
  104. set_process_internal(true);
  105. }
  106. #endif
  107. } break;
  108. case NOTIFICATION_EXIT_WORLD: {
  109. if (_occluder_instance.is_valid()) {
  110. VisualServer::get_singleton()->occluder_instance_set_scenario(_occluder_instance, RID());
  111. }
  112. #ifdef TOOLS_ENABLED
  113. if (Engine::get_singleton()->is_editor_hint()) {
  114. set_process_internal(false);
  115. }
  116. #endif
  117. } break;
  118. case NOTIFICATION_VISIBILITY_CHANGED: {
  119. if (_occluder_instance.is_valid() && is_inside_tree()) {
  120. VisualServer::get_singleton()->occluder_instance_set_active(_occluder_instance, is_visible_in_tree());
  121. }
  122. } break;
  123. case NOTIFICATION_TRANSFORM_CHANGED: {
  124. if (_occluder_instance.is_valid()) {
  125. VisualServer::get_singleton()->occluder_instance_set_transform(_occluder_instance, get_global_transform());
  126. #ifdef TOOLS_ENABLED
  127. if (Engine::get_singleton()->is_editor_hint()) {
  128. update_configuration_warning();
  129. }
  130. #endif
  131. }
  132. } break;
  133. case NOTIFICATION_INTERNAL_PROCESS: {
  134. if (PortalOcclusionCuller::_redraw_gizmo) {
  135. PortalOcclusionCuller::_redraw_gizmo = false;
  136. update_gizmo();
  137. }
  138. } break;
  139. }
  140. }
  141. void Occluder::_bind_methods() {
  142. ClassDB::bind_method(D_METHOD("resource_changed", "resource"), &Occluder::resource_changed);
  143. ClassDB::bind_method(D_METHOD("set_shape", "shape"), &Occluder::set_shape);
  144. ClassDB::bind_method(D_METHOD("get_shape"), &Occluder::get_shape);
  145. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "shape", PROPERTY_HINT_RESOURCE_TYPE, "OccluderShape"), "set_shape", "get_shape");
  146. }
  147. Occluder::Occluder() {
  148. _occluder_instance = RID_PRIME(VisualServer::get_singleton()->occluder_instance_create());
  149. set_notify_transform(true);
  150. }
  151. Occluder::~Occluder() {
  152. if (_occluder_instance != RID()) {
  153. VisualServer::get_singleton()->free(_occluder_instance);
  154. }
  155. if (!_shape.is_null()) {
  156. _shape->unregister_owner(this);
  157. }
  158. }