openxr_visibility_mask.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /**************************************************************************/
  2. /* openxr_visibility_mask.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 "openxr_visibility_mask.h"
  31. #include "../extensions/openxr_visibility_mask_extension.h"
  32. #include "../openxr_interface.h"
  33. #include "scene/3d/xr_nodes.h"
  34. void OpenXRVisibilityMask::_bind_methods() {
  35. }
  36. void OpenXRVisibilityMask::_notification(int p_what) {
  37. switch (p_what) {
  38. case NOTIFICATION_ENTER_TREE: {
  39. OpenXRVisibilityMaskExtension *vis_mask_ext = OpenXRVisibilityMaskExtension::get_singleton();
  40. if (vis_mask_ext && vis_mask_ext->is_available()) {
  41. set_base(vis_mask_ext->get_mesh());
  42. }
  43. } break;
  44. case NOTIFICATION_EXIT_TREE: {
  45. set_base(RID());
  46. } break;
  47. }
  48. }
  49. void OpenXRVisibilityMask::_on_openxr_session_begun() {
  50. if (is_inside_tree()) {
  51. OpenXRVisibilityMaskExtension *vis_mask_ext = OpenXRVisibilityMaskExtension::get_singleton();
  52. if (vis_mask_ext && vis_mask_ext->is_available()) {
  53. set_base(vis_mask_ext->get_mesh());
  54. }
  55. }
  56. }
  57. void OpenXRVisibilityMask::_on_openxr_session_stopping() {
  58. set_base(RID());
  59. }
  60. PackedStringArray OpenXRVisibilityMask::get_configuration_warnings() const {
  61. PackedStringArray warnings = VisualInstance3D::get_configuration_warnings();
  62. if (is_visible() && is_inside_tree()) {
  63. XRCamera3D *camera = Object::cast_to<XRCamera3D>(get_parent());
  64. if (camera == nullptr) {
  65. warnings.push_back(RTR("OpenXR visibility mask must have an XRCamera3D node as their parent."));
  66. }
  67. }
  68. return warnings;
  69. }
  70. AABB OpenXRVisibilityMask::get_aabb() const {
  71. AABB ret;
  72. // Make sure it's always visible, this is positioned through its shader.
  73. ret.position = Vector3(-1000.0, -1000.0, -1000.0);
  74. ret.size = Vector3(2000.0, 2000.0, 2000.0);
  75. return ret;
  76. }
  77. OpenXRVisibilityMask::OpenXRVisibilityMask() {
  78. Ref<OpenXRInterface> openxr_interface = XRServer::get_singleton()->find_interface("OpenXR");
  79. if (openxr_interface.is_valid()) {
  80. openxr_interface->connect("session_begun", callable_mp(this, &OpenXRVisibilityMask::_on_openxr_session_begun));
  81. openxr_interface->connect("session_stopping", callable_mp(this, &OpenXRVisibilityMask::_on_openxr_session_stopping));
  82. }
  83. RS::get_singleton()->instance_geometry_set_cast_shadows_setting(get_instance(), RS::SHADOW_CASTING_SETTING_OFF);
  84. }
  85. OpenXRVisibilityMask::~OpenXRVisibilityMask() {
  86. Ref<OpenXRInterface> openxr_interface = XRServer::get_singleton()->find_interface("OpenXR");
  87. if (openxr_interface.is_valid()) {
  88. openxr_interface->disconnect("session_begun", callable_mp(this, &OpenXRVisibilityMask::_on_openxr_session_begun));
  89. openxr_interface->disconnect("session_stopping", callable_mp(this, &OpenXRVisibilityMask::_on_openxr_session_stopping));
  90. }
  91. }