start_vr.gd 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. extends Node3D
  2. signal focus_lost
  3. signal focus_gained
  4. signal pose_recentered
  5. @export var maximum_refresh_rate: int = 90
  6. var xr_interface: OpenXRInterface
  7. var xr_is_focused := false
  8. func _ready() -> void:
  9. xr_interface = XRServer.find_interface("OpenXR")
  10. if xr_interface and xr_interface.is_initialized():
  11. print("OpenXR instantiated successfully.")
  12. var vp: Viewport = get_viewport()
  13. # Enable XR on our viewport.
  14. vp.use_xr = true
  15. # Make sure V-Sync is off, as V-Sync is handled by OpenXR.
  16. DisplayServer.window_set_vsync_mode(DisplayServer.VSYNC_DISABLED)
  17. # Enable variable rate shading.
  18. if RenderingServer.get_rendering_device():
  19. vp.vrs_mode = Viewport.VRS_XR
  20. elif int(ProjectSettings.get_setting("xr/openxr/foveation_level")) == 0:
  21. push_warning("OpenXR: Recommend setting Foveation level to High in Project Settings")
  22. # Connect the OpenXR events.
  23. xr_interface.session_begun.connect(_on_openxr_session_begun)
  24. xr_interface.session_visible.connect(_on_openxr_visible_state)
  25. xr_interface.session_focussed.connect(_on_openxr_focused_state)
  26. xr_interface.session_stopping.connect(_on_openxr_stopping)
  27. xr_interface.pose_recentered.connect(_on_openxr_pose_recentered)
  28. else:
  29. # We couldn't start OpenXR.
  30. print("OpenXR not instantiated!")
  31. get_tree().quit()
  32. # Handle OpenXR session ready.
  33. func _on_openxr_session_begun() -> void:
  34. # Get the reported refresh rate.
  35. var current_refresh_rate := xr_interface.get_display_refresh_rate()
  36. if current_refresh_rate > 0:
  37. print("OpenXR: Refresh rate reported as ", str(current_refresh_rate))
  38. else:
  39. print("OpenXR: No refresh rate given by XR runtime")
  40. # See if we have a better refresh rate available.
  41. var new_rate := current_refresh_rate
  42. var available_rates: Array = xr_interface.get_available_display_refresh_rates()
  43. if available_rates.is_empty():
  44. print("OpenXR: Target does not support refresh rate extension")
  45. elif available_rates.size() == 1:
  46. # Only one available, so use it.
  47. new_rate = available_rates[0]
  48. else:
  49. for rate in available_rates:
  50. if rate > new_rate and rate <= maximum_refresh_rate:
  51. new_rate = rate
  52. # Did we find a better rate?
  53. if current_refresh_rate != new_rate:
  54. print("OpenXR: Setting refresh rate to ", str(new_rate))
  55. xr_interface.set_display_refresh_rate(new_rate)
  56. current_refresh_rate = new_rate
  57. # Now match our physics rate. This is currently needed to avoid jittering,
  58. # due to physics interpolation not being used.
  59. Engine.physics_ticks_per_second = roundi(current_refresh_rate)
  60. # Handle OpenXR visible state
  61. func _on_openxr_visible_state() -> void:
  62. # We always pass this state at startup,
  63. # but the second time we get this, it means our player took off their headset.
  64. if xr_is_focused:
  65. print("OpenXR lost focus")
  66. xr_is_focused = false
  67. # Pause our game.
  68. process_mode = Node.PROCESS_MODE_DISABLED
  69. focus_lost.emit()
  70. # Handle OpenXR focused state
  71. func _on_openxr_focused_state() -> void:
  72. print("OpenXR gained focus")
  73. xr_is_focused = true
  74. # Unpause our game.
  75. process_mode = Node.PROCESS_MODE_INHERIT
  76. focus_gained.emit()
  77. # Handle OpenXR stopping state.
  78. func _on_openxr_stopping() -> void:
  79. # Our session is being stopped.
  80. print("OpenXR is stopping")
  81. # Handle OpenXR pose recentered signal.
  82. func _on_openxr_pose_recentered() -> void:
  83. # User recentered view, we have to react to this by recentering the view.
  84. # This is game implementation dependent.
  85. pose_recentered.emit()