WebXRInterface

继承: XRInterface < RefCounted < Object

使用 WebXR 的 AR/VR 接口。

描述

WebXR is an open standard that allows creating VR and AR applications that run in the web browser.

As such, this interface is only available when running in Web exports.

WebXR supports a wide range of devices, from the very capable (like Valve Index, HTC Vive, Oculus Rift and Quest) down to the much less capable (like Google Cardboard, Oculus Go, GearVR, or plain smartphones).

Since WebXR is based on JavaScript, it makes extensive use of callbacks, which means that WebXRInterface is forced to use signals, where other XR interfaces would instead use functions that return a result immediately. This makes WebXRInterface quite a bit more complicated to initialize than other XR interfaces.

Here’s the minimum code required to start an immersive VR session:

  1. extends Node3D
  2. var webxr_interface
  3. var vr_supported = false
  4. func _ready():
  5. # We assume this node has a button as a child.
  6. # This button is for the user to consent to entering immersive VR mode.
  7. $Button.pressed.connect(self._on_button_pressed)
  8. webxr_interface = XRServer.find_interface("WebXR")
  9. if webxr_interface:
  10. # WebXR uses a lot of asynchronous callbacks, so we connect to various
  11. # signals in order to receive them.
  12. webxr_interface.session_supported.connect(self._webxr_session_supported)
  13. webxr_interface.session_started.connect(self._webxr_session_started)
  14. webxr_interface.session_ended.connect(self._webxr_session_ended)
  15. webxr_interface.session_failed.connect(self._webxr_session_failed)
  16. # This returns immediately - our _webxr_session_supported() method
  17. # (which we connected to the "session_supported" signal above) will
  18. # be called sometime later to let us know if it's supported or not.
  19. webxr_interface.is_session_supported("immersive-vr")
  20. func _webxr_session_supported(session_mode, supported):
  21. if session_mode == 'immersive-vr':
  22. vr_supported = supported
  23. func _on_button_pressed():
  24. if not vr_supported:
  25. OS.alert("Your browser doesn't support VR")
  26. return
  27. # We want an immersive VR session, as opposed to AR ('immersive-ar') or a
  28. # simple 3DoF viewer ('viewer').
  29. webxr_interface.session_mode = 'immersive-vr'
  30. # 'bounded-floor' is room scale, 'local-floor' is a standing or sitting
  31. # experience (it puts you 1.6m above the ground if you have 3DoF headset),
  32. # whereas as 'local' puts you down at the XROrigin.
  33. # This list means it'll first try to request 'bounded-floor', then
  34. # fallback on 'local-floor' and ultimately 'local', if nothing else is
  35. # supported.
  36. webxr_interface.requested_reference_space_types = 'bounded-floor, local-floor, local'
  37. # In order to use 'local-floor' or 'bounded-floor' we must also
  38. # mark the features as required or optional. By including 'hand-tracking'
  39. # as an optional feature, it will be enabled if supported.
  40. webxr_interface.required_features = 'local-floor'
  41. webxr_interface.optional_features = 'bounded-floor, hand-tracking'
  42. # This will return false if we're unable to even request the session,
  43. # however, it can still fail asynchronously later in the process, so we
  44. # only know if it's really succeeded or failed when our
  45. # _webxr_session_started() or _webxr_session_failed() methods are called.
  46. if not webxr_interface.initialize():
  47. OS.alert("Failed to initialize")
  48. return
  49. func _webxr_session_started():
  50. $Button.visible = false
  51. # This tells Godot to start rendering to the headset.
  52. get_viewport().use_xr = true
  53. # This will be the reference space type you ultimately got, out of the
  54. # types that you requested above. This is useful if you want the game to
  55. # work a little differently in 'bounded-floor' versus 'local-floor'.
  56. print("Reference space type: ", webxr_interface.reference_space_type)
  57. # This will be the list of features that were successfully enabled
  58. # (except on browsers that don't support this property).
  59. print("Enabled features: ", webxr_interface.enabled_features)
  60. func _webxr_session_ended():
  61. $Button.visible = true
  62. # If the user exits immersive mode, then we tell Godot to render to the web
  63. # page again.
  64. get_viewport().use_xr = false
  65. func _webxr_session_failed(message):
  66. OS.alert("Failed to initialize: " + message)

There are a couple ways to handle “controller” input:

  • Using XRController3D nodes and their XRController3D.button_pressed and XRController3D.button_released signals. This is how controllers are typically handled in XR apps in Godot, however, this will only work with advanced VR controllers like the Oculus Touch or Index controllers, for example.

  • Using the select, squeeze and related signals. This method will work for both advanced VR controllers, and non-traditional input sources like a tap on the screen, a spoken voice command or a button press on the device itself.

You can use both methods to allow your game or app to support a wider or narrower set of devices and input methods, or to allow more advanced interactions with more advanced devices.

教程

属性

String

enabled_features

String

optional_features

String

reference_space_type

String

requested_reference_space_types

String

required_features

String

session_mode

String

visibility_state

方法

Array

get_available_display_refresh_rates() const

float

get_display_refresh_rate() const

TargetRayMode

get_input_source_target_ray_mode(input_source_id: int) const

XRControllerTracker

get_input_source_tracker(input_source_id: int) const

bool

is_input_source_active(input_source_id: int) const

void

is_session_supported(session_mode: String)

void

set_display_refresh_rate(refresh_rate: float)


信号

display_refresh_rate_changed() 🔗

显示器的刷新率发生改变后触发。


reference_space_reset() 🔗

发射以表明参考空间已被重置或重新配置。

何时(或是否)发射取决于用户的浏览器或设备,但可能包括用户改变了他们的游戏空间的大小(可以通过 XRInterface.get_play_area 访问),或按下/按住一个按钮来重新定位他们的位置。

有关详细信息,请参阅 WebXR 的 XRReferenceSpace 重置事件


select(input_source_id: int) 🔗

某个输入源完成其“主要动作”后发出。

请使用 get_input_source_trackerget_input_source_target_ray_mode 获取关于该输入源的更多信息。


selectend(input_source_id: int) 🔗

某个输入源完成其“主要动作”时发出。

请使用 get_input_source_trackerget_input_source_target_ray_mode 获取关于该输入源的更多信息。


selectstart(input_source_id: int) 🔗

某个输入源开始其“主要动作”时发出。

请使用 get_input_source_trackerget_input_source_target_ray_mode 获取关于该输入源的更多信息。


session_ended() 🔗

用户结束 WebXR 会话时发出(可以使用浏览器或设备的 UI 结束会话)。

此时,你应该执行 get_viewport().use_xr = false,让 Godot 继续渲染至屏幕。


session_failed(message: String) 🔗

XRInterface.initialize 在该会话启动失败时发出。

message 可能会包含 WebXR 的错误信息,如果没有可用信息则为空字符串。


session_started() 🔗

XRInterface.initialize 在该会话启动成功时发出。

此时,可以安全地执行 get_viewport().use_xr = true,让 Godot 开始渲染至 XR 设备。


session_supported(session_mode: String, supported: bool) 🔗

is_session_supported 触发,表示是否支持指定的 session_mode


squeeze(input_source_id: int) 🔗

某个输入源完成其“主要紧握动作”后发出。

请使用 get_input_source_trackerget_input_source_target_ray_mode 获取关于该输入源的更多信息。


squeezeend(input_source_id: int) 🔗

某个输入源完成其“主要紧握动作”时发出。

请使用 get_input_source_trackerget_input_source_target_ray_mode 获取关于该输入源的更多信息。


squeezestart(input_source_id: int) 🔗

某个输入源开始其“主要紧握动作”时发出。

请使用 get_input_source_trackerget_input_source_target_ray_mode 获取关于该输入源的更多信息。


visibility_state_changed() 🔗

visibility_state 已更改时触发。


枚举

enum TargetRayMode: 🔗

TargetRayMode TARGET_RAY_MODE_UNKNOWN = 0

不知道目标射线模式。

TargetRayMode TARGET_RAY_MODE_GAZE = 1

目标射线从观察者的眼睛出发,指向所观察的方向。

TargetRayMode TARGET_RAY_MODE_TRACKED_POINTER = 2

目标射线由手持指示器发射,很可能是 VR 触摸控制器。

TargetRayMode TARGET_RAY_MODE_SCREEN = 3

目标射线由触摸屏、鼠标等触觉输入设备发射。


属性说明

String enabled_features 🔗

  • String get_enabled_features()

A comma-separated list of features that were successfully enabled by XRInterface.initialize when setting up the WebXR session.

This may include features requested by setting required_features and optional_features, and will only be available after session_started has been emitted.

Note: This may not be support by all web browsers, in which case it will be an empty string.


String optional_features 🔗

  • void set_optional_features(value: String)

  • String get_optional_features()

A comma-seperated list of optional features used by XRInterface.initialize when setting up the WebXR session.

If a user’s browser or device doesn’t support one of the given features, initialization will continue, but you won’t be able to use the requested feature.

This doesn’t have any effect on the interface when already initialized.

Possible values come from WebXR’s XRReferenceSpaceType, or include other features like "hand-tracking" to enable hand tracking.


String reference_space_type 🔗

  • String get_reference_space_type()

参考空间类型(来自 requested_reference_space_types 属性中设置的请求类型列表),在设置 WebXR 会话时最终由 XRInterface.initialize 使用。

可能的值来自 WebXR 的 XRReferenceSpaceType。 如果想要使用特定的参考空间类型,则它必须列在 required_featuresoptional_features 中。


String requested_reference_space_types 🔗

  • void set_requested_reference_space_types(value: String)

  • String get_requested_reference_space_types()

XRInterface.initialize 在设置 WebXR 会话时使用的以逗号分隔的参考空间类型列表。

按顺序请求参考空间类型,将使用用户设备或浏览器支持的第一个。reference_space_type 属性包含最终选择的参考空间类型。

这对已经初始化的接口没有任何影响。

可能的值来自 WebXR 的 XRReferenceSpaceType。如果想要使用特定的参考空间类型,则它必须列在 required_featuresoptional_features 中。


String required_features 🔗

  • void set_required_features(value: String)

  • String get_required_features()

A comma-seperated list of required features used by XRInterface.initialize when setting up the WebXR session.

If a user’s browser or device doesn’t support one of the given features, initialization will fail and session_failed will be emitted.

This doesn’t have any effect on the interface when already initialized.

Possible values come from WebXR’s XRReferenceSpaceType, or include other features like "hand-tracking" to enable hand tracking.


String session_mode 🔗

  • void set_session_mode(value: String)

  • String get_session_mode()

建立 WebXR 会话时,XRInterface.initialize 使用的会话模式。

这对已经初始化的接口没有任何影响。

可能的值来自 WebXR 的 XRSessionMode,包括:"immersive-vr""immersive-ar""inline"


String visibility_state 🔗

  • String get_visibility_state()

指示用户是否可以看到 WebXR 会话的图像。

可能的值来自 WebXR 的 XRVisibilityState,包括 "hidden""visible""visible-blurred"


方法说明

Array get_available_display_refresh_rates() const 🔗

返回当前 HMD 所支持的显示刷新率。网页浏览器支持该功能,并且该接口已初始化时才会返回。


float get_display_refresh_rate() const 🔗

返回当前 HMD 的显示刷新率。不是所有 HMD 和浏览器都支持。使用 set_display_refresh_rate 前可能不会汇报精确值。


TargetRayMode get_input_source_target_ray_mode(input_source_id: int) const 🔗

返回给定的 input_source_id 的目标射线模式。

可用于帮助解析来自该输入源的输入。详见 XRInputSource.targetRayMode


XRControllerTracker get_input_source_tracker(input_source_id: int) const 🔗

获取给定 input_source_idXRControllerTracker

在 WebXR 上下文中,输入源可以是类似 Oculus Touch 和 Index 控制器的高级 VR 控制器,甚至也可以是屏幕上的点击、语音命令或按下设备本身的按钮。当使用非传统输入源时,会将 XRPositionalTracker 的位置和方向解释为指向用户希望与之交互的对象的射线。

可以使用此方法获取有关触发以下信号之一的输入源的信息:


bool is_input_source_active(input_source_id: int) const 🔗

如果存在具有给定 input_source_id 的活动输入源,则返回 true


void is_session_supported(session_mode: String) 🔗

检查给定的 session_mode 是否被用户的浏览器支持。

可能的值来自 WebXR 的 XRSessionMode,包括:"immersive-vr""immersive-ar""inline"

此方法不返回任何东西,而是将结果发送给 session_supported 信号。


void set_display_refresh_rate(refresh_rate: float) 🔗

为当前的 HMD 设置屏幕刷新率。不是所有 HMD 和浏览器都支持。不会立即生效,发出 display_refresh_rate_changed 信号后才会生效。