vrs.gd 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. extends Node3D
  2. @onready var option_button: OptionButton = $CanvasLayer/VBoxContainer/HBoxContainer/OptionButton
  3. @onready var texture_rect: TextureRect = $CanvasLayer/VBoxContainer/TextureRect
  4. @onready var camera: Camera3D = $Camera3D
  5. @onready var xr_camera: Camera3D = $XROrigin3D/XRCamera3D
  6. @export var texture: Texture
  7. var xr_interface: MobileVRInterface
  8. func _set_xr_mode() -> void:
  9. var vrs_mode = get_viewport().vrs_mode
  10. if vrs_mode == Viewport.VRS_XR:
  11. xr_interface = XRServer.find_interface("Native mobile")
  12. if xr_interface and xr_interface.initialize():
  13. # Disable a lot of VR-specific stuff like lens distortion.
  14. xr_interface.eye_height = 0.0
  15. xr_interface.k1 = 0.0
  16. xr_interface.k2 = 0.0
  17. xr_interface.oversample = 1.0
  18. get_viewport().use_xr = true
  19. xr_camera.current = true
  20. # Reposition our origin point to work around an engine bug.
  21. $XROrigin3D.global_transform = camera.global_transform
  22. else:
  23. if xr_interface:
  24. xr_interface.uninitialize()
  25. get_viewport().use_xr = false
  26. camera.current = true
  27. func _update_texture() -> void:
  28. var vrs_mode = get_viewport().vrs_mode
  29. if vrs_mode == Viewport.VRS_DISABLED:
  30. texture_rect.visible = false
  31. elif vrs_mode == Viewport.VRS_TEXTURE:
  32. get_viewport().vrs_texture = texture
  33. texture_rect.texture = texture
  34. texture_rect.visible = true
  35. elif vrs_mode == Viewport.VRS_XR:
  36. # Doesn't seem to be supported yet. This should be exposed in a future engine version.
  37. #if xr_interface:
  38. # texture_rect.texture = xr_interface.get_vrs_texture()
  39. # texture_rect.visible = true
  40. #else:
  41. # texture_rect.visible = false
  42. texture_rect.visible = false
  43. func _ready() -> void:
  44. var vrs_mode = get_viewport().vrs_mode
  45. option_button.selected = vrs_mode
  46. _update_texture()
  47. func _on_option_button_item_selected(index: int) -> void:
  48. get_viewport().vrs_mode = index as Viewport.VRSMode
  49. _set_xr_mode()
  50. _update_texture()