Fix WebXRInterface code sample in the class reference

This also fixes the code sample's indentation to look correct in the
editor help.
This commit is contained in:
Hugo Locurcio 2021-01-30 15:58:09 +01:00
parent 46de553473
commit 3434074933
No known key found for this signature in database
GPG key ID: 39E8F8BE30B0A49C

View file

@ -10,77 +10,79 @@
Since WebXR is based on Javascript, it makes extensive use of callbacks, which means that [WebXRInterface] is forced to use signals, where other AR/VR interfaces would instead use functions that return a result immediately. This makes [WebXRInterface] quite a bit more complicated to intialize than other AR/VR interfaces. Since WebXR is based on Javascript, it makes extensive use of callbacks, which means that [WebXRInterface] is forced to use signals, where other AR/VR interfaces would instead use functions that return a result immediately. This makes [WebXRInterface] quite a bit more complicated to intialize than other AR/VR interfaces.
Here's the minimum code required to start an immersive VR session: Here's the minimum code required to start an immersive VR session:
[codeblock] [codeblock]
var webxr_interface extends Node3D
var vr_supported = false
func _ready(): var webxr_interface
# We assume this node has a button as a child. var vr_supported = false
# This button is for the user to consent to entering immersive VR mode.
$Button.connect("pressed", self, "_on_Button_pressed")
webxr_interface = XRServer.find_interface("WebXR") func _ready():
if webxr_interface: # We assume this node has a button as a child.
# WebXR uses a lot of asynchronous callbacks, so we connect to various # This button is for the user to consent to entering immersive VR mode.
# signals in order to receive them. $Button.connect("pressed", self, "_on_Button_pressed")
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 webxr_interface = XRServer.find_interface("WebXR")
# (which we connected to the "session_supported" signal above) will if webxr_interface:
# be called sometime later to let us know if it's supported or not. # WebXR uses a lot of asynchronous callbacks, so we connect to various
webxr_interface.is_session_supported("immersive-vr") # 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")
func _webxr_session_supported(session_mode, supported): # This returns immediately - our _webxr_session_supported() method
if session_mode == 'immersive-vr': # (which we connected to the "session_supported" signal above) will
vr_supported = supported # be called sometime later to let us know if it's supported or not.
webxr_interface.is_session_supported("immersive-vr")
func _on_Button_pressed(): func _webxr_session_supported(session_mode, supported):
if not vr_supported: if session_mode == 'immersive-vr':
OS.alert("Your browser doesn't support VR") vr_supported = supported
return
# We want an immersive VR session, as opposed to AR ('immersive-ar') or a func _on_Button_pressed():
# simple 3DoF viewer ('viewer'). if not vr_supported:
webxr_interface.session_mode = 'immersive-vr' OS.alert("Your browser doesn't support VR")
# 'bounded-floor' is room scale, 'local-floor' is a standing or sitting return
# experience (it puts you 1.6m above the ground if you have 3DoF headset),
# whereas as 'local' puts you down at the XROrigin.
# 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, # We want an immersive VR session, as opposed to AR ('immersive-ar') or a
# however, it can still fail asynchronously later in the process, so we # simple 3DoF viewer ('viewer').
# only know if it's really succeeded or failed when our webxr_interface.session_mode = 'immersive-vr'
# _webxr_session_started() or _webxr_session_failed() methods are called. # 'bounded-floor' is room scale, 'local-floor' is a standing or sitting
if not webxr_interface.initialize(): # experience (it puts you 1.6m above the ground if you have 3DoF headset),
OS.alert("Failed to initialize") # whereas as 'local' puts you down at the XROrigin.
return # 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'
func _webxr_session_started(): # This will return false if we're unable to even request the session,
$Button.visible = false # however, it can still fail asynchronously later in the process, so we
# This tells Godot to start rendering to the headset. # only know if it's really succeeded or failed when our
get_viewport().xr = true # _webxr_session_started() or _webxr_session_failed() methods are called.
# This will be the reference space type you ultimately got, out of the if not webxr_interface.initialize():
# types that you requested above. This is useful if you want the game to OS.alert("Failed to initialize")
# work a little differently in 'bounded-floor' versus 'local-floor'. return
print ("Reference space type: " + webxr_interface.reference_space_type)
func _webxr_session_ended(): func _webxr_session_started():
$Button.visible = true $Button.visible = false
# If the user exits immersive mode, then we tell Godot to render to the web # This tells Godot to start rendering to the headset.
# page again. get_viewport().xr = true
get_viewport().xr = false # 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_failed(message): func _webxr_session_ended():
OS.alert("Failed to initialize: " + message) $Button.visible = true
# If the user exits immersive mode, then we tell Godot to render to the web
# page again.
get_viewport().xr = false
func _webxr_session_failed(message):
OS.alert("Failed to initialize: " + message)
[/codeblock] [/codeblock]
There are several ways to handle "controller" input: There are several ways to handle "controller" input:
- Using [XRController3D] nodes and their [signal XRController3D.button_pressed] and [signal XRController3D.button_released] 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 [url=https://immersive-web.github.io/webxr-gamepads-module/#xr-standard-gamepad-mapping]Section 3.3 of the WebXR Gamepads Module[/url]. - Using [XRController3D] nodes and their [signal XRController3D.button_pressed] and [signal XRController3D.button_released] 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 [url=https://immersive-web.github.io/webxr-gamepads-module/#xr-standard-gamepad-mapping]Section 3.3 of the WebXR Gamepads Module[/url].