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 an HTML5 export.
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 godot.WebXRInterface
is forced to use signals, where other AR/VR interfaces would instead use functions that return a result immediately. This makes godot.WebXRInterface
quite a bit more complicated to initialize than other AR/VR interfaces.
Here's the minimum code required to start an immersive VR session:
extends Spatial
var webxr_interface
var vr_supported = false
func _ready():
# We assume this node has a button as a child.
# This button is for the user to consent to entering immersive VR mode.
$Button.connect("pressed", self, "_on_Button_pressed")
webxr_interface = ARVRServer.find_interface("WebXR")
if webxr_interface:
# WebXR uses a lot of asynchronous callbacks, so we connect to various
# signals in order to receive them.
webxr_interface.connect("session_supported", self, "_webxr_session_supported")
webxr_interface.connect("session_started", self, "_webxr_session_started")
webxr_interface.connect("session_ended", self, "_webxr_session_ended")
webxr_interface.connect("session_failed", self, "_webxr_session_failed")
# This returns immediately - our _webxr_session_supported() method
# (which we connected to the "session_supported" signal above) will
# be called sometime later to let us know if it's supported or not.
webxr_interface.is_session_supported("immersive-vr")
func _webxr_session_supported(session_mode, supported):
if session_mode == 'immersive-vr':
vr_supported = supported
func _on_Button_pressed():
if not vr_supported:
OS.alert("Your browser doesn't support VR")
return
# We want an immersive VR session, as opposed to AR ('immersive-ar') or a
# simple 3DoF viewer ('viewer').
webxr_interface.session_mode = 'immersive-vr'
# 'bounded-floor' is room scale, 'local-floor' is a standing or sitting
# experience (it puts you 1.6m above the ground if you have 3DoF headset),
# whereas as 'local' puts you down at the ARVROrigin.
# This list means it'll first try to request 'bounded-floor', then
# fallback on 'local-floor' and ultimately 'local', if nothing else is
# supported.
webxr_interface.requested_reference_space_types = 'bounded-floor, local-floor, local'
# In order to use 'local-floor' or 'bounded-floor' we must also
# mark the features as required or optional.
webxr_interface.required_features = 'local-floor'
webxr_interface.optional_features = 'bounded-floor'
# This will return false if we're unable to even request the session,
# however, it can still fail asynchronously later in the process, so we
# only know if it's really succeeded or failed when our
# _webxr_session_started() or _webxr_session_failed() methods are called.
if not webxr_interface.initialize():
OS.alert("Failed to initialize")
return
func _webxr_session_started():
$Button.visible = false
# This tells Godot to start rendering to the headset.
get_viewport().arvr = true
# This will be the reference space type you ultimately got, out of the
# types that you requested above. This is useful if you want the game to
# work a little differently in 'bounded-floor' versus 'local-floor'.
print ("Reference space type: " + webxr_interface.reference_space_type)
func _webxr_session_ended():
$Button.visible = true
# If the user exits immersive mode, then we tell Godot to render to the web
# page again.
get_viewport().arvr = false
func _webxr_session_failed(message):
OS.alert("Failed to initialize: " + message)
There are several ways to handle "controller" input:
-
Using
godot.ARVRController
nodes and theirARVRController.button_pressed
andARVRController.button_release
signals. This is how controllers are typically handled in AR/VR apps in Godot, however, this will only work with advanced VR controllers like the Oculus Touch or Index controllers, for example. The buttons codes are defined by [https://immersive-web.github.io/webxr-gamepads-module/#xr-standard-gamepad-mapping](Section 3.3 of the WebXR Gamepads Module). -
Using
godot.Node._UnhandledInput
andgodot.InputEventJoypadButton
orgodot.InputEventJoypadMotion
. This works the same as normal joypads, except thegodot.InputEvent.device
starts at 100, so the left controller is 100 and the right controller is 101, and the button codes are also defined by [https://immersive-web.github.io/webxr-gamepads-module/#xr-standard-gamepad-mapping](Section 3.3 of the WebXR Gamepads Module). -
Using the
select
,squeeze
and related signals. This method will work for both advanced VR controllers, and non-traditional "controllers" like a tap on the screen, a spoken voice command or a button press on the device itself. Thecontroller_id
passed to these signals is the same id as used ingodot.ARVRController.controllerId
.
You can use one or all of these methods to allow your game or app to support a wider or narrower set of devices and input methods, or to allow more advanced interations with more advanced devices.
Variables
read onlyboundsGeometry:NativeArray<Vector3>
The vertices of a polygon which defines the boundaries of the user's play area.
This will only be available if godot.WebXRInterface.referenceSpaceType
is "bounded-floor"
and only on certain browsers and devices that support it.
The reference_space_reset
signal may indicate when this changes.
read onlyonSessionSupported:Signal<(sessionMode:String, supported:Bool) ‑> Void>
session_supported
signal.
optionalFeatures:String
A comma-seperated list of optional features used by godot.ARVRInterface.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 [https://developer.mozilla.org/en-US/docs/Web/API/XRReferenceSpaceType](WebXR's XRReferenceSpaceType). If you want to use a particular reference space type, it must be listed in either godot.WebXRInterface.requiredFeatures
or godot.WebXRInterface.optionalFeatures
.
read onlyreferenceSpaceType:String
The reference space type (from the list of requested types set in the godot.WebXRInterface.requestedReferenceSpaceTypes
property), that was ultimately used by godot.ARVRInterface.initialize
when setting up the WebXR session.
Possible values come from [https://developer.mozilla.org/en-US/docs/Web/API/XRReferenceSpaceType](WebXR's XRReferenceSpaceType). If you want to use a particular reference space type, it must be listed in either godot.WebXRInterface.requiredFeatures
or godot.WebXRInterface.optionalFeatures
.
requestedReferenceSpaceTypes:String
A comma-seperated list of reference space types used by godot.ARVRInterface.initialize
when setting up the WebXR session.
The reference space types are requested in order, and the first on supported by the users device or browser will be used. The godot.WebXRInterface.referenceSpaceType
property contains the reference space type that was ultimately used.
This doesn't have any effect on the interface when already initialized.
Possible values come from [https://developer.mozilla.org/en-US/docs/Web/API/XRReferenceSpaceType](WebXR's XRReferenceSpaceType). If you want to use a particular reference space type, it must be listed in either godot.WebXRInterface.requiredFeatures
or godot.WebXRInterface.optionalFeatures
.
requiredFeatures:String
A comma-seperated list of required features used by godot.ARVRInterface.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 [https://developer.mozilla.org/en-US/docs/Web/API/XRReferenceSpaceType](WebXR's XRReferenceSpaceType). If you want to use a particular reference space type, it must be listed in either godot.WebXRInterface.requiredFeatures
or godot.WebXRInterface.optionalFeatures
.
sessionMode:String
The session mode used by godot.ARVRInterface.initialize
when setting up the WebXR session.
This doesn't have any effect on the interface when already initialized.
Possible values come from [https://developer.mozilla.org/en-US/docs/Web/API/XRSessionMode](WebXR's XRSessionMode), including: "immersive-vr"
, "immersive-ar"
, and "inline"
.
read onlyvisibilityState:String
Indicates if the WebXR session's imagery is visible to the user.
Possible values come from [https://developer.mozilla.org/en-US/docs/Web/API/XRVisibilityState](WebXR's XRVisibilityState), including "hidden"
, "visible"
, and "visible-blurred"
.
Methods
getController(controllerId:Int):ARVRPositionalTracker
Gets an godot.ARVRPositionalTracker
for the given controller_id
.
In the context of WebXR, a "controller" can be an advanced VR controller like the Oculus Touch or Index controllers, or even a tap on the screen, a spoken voice command or a button press on the device itself. When a non-traditional controller is used, interpret the position and orientation of the godot.ARVRPositionalTracker
as a ray pointing at the object the user wishes to interact with.
Use this method to get information about the controller that triggered one of these signals:
-
selectstart
-
select
-
selectend
-
squeezestart
-
squeeze
-
squeezestart
isSessionSupported(sessionMode:String):Void
Checks if the given session_mode
is supported by the user's browser.
Possible values come from [https://developer.mozilla.org/en-US/docs/Web/API/XRSessionMode](WebXR's XRSessionMode), including: "immersive-vr"
, "immersive-ar"
, and "inline"
.
This method returns nothing, instead it emits the session_supported
signal with the result.