Documentation
¶
Overview ¶
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 graphics.gd/classdb/WebXRInterface is forced to use signals, where other XR interfaces would instead use functions that return a result immediately. This makes graphics.gd/classdb/WebXRInterface quite a bit more complicated to initialize than other XR interfaces.
Here's the minimum code required to start an immersive VR session:
package main import ( "graphics.gd/classdb/Button" "graphics.gd/classdb/Node3D" "graphics.gd/classdb/OS" "graphics.gd/classdb/Viewport" "graphics.gd/classdb/WebXRInterface" "graphics.gd/classdb/XRServer" "graphics.gd/variant/Object" ) type NodeWebXR struct { Node3D.Extension[NodeWebXR] Button Button.Instance webxr_interface WebXRInterface.Instance vr_supported bool } func (n *NodeWebXR) Ready() { n.Button.AsBaseButton().OnPressed(func() { if !n.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'). n.webxr_interface.SetSessionMode("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 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. n.webxr_interface.SetRequestedReferenceSpaceTypes("bounded-floor, local-floor, local") // In order to use 'local-floor' or 'bounded-floor' we must also // mark the features as required or optional. By including 'hand-tracking' // as an optional feature, it will be enabled if supported. n.webxr_interface.SetRequiredFeatures("local-floor") n.webxr_interface.SetOptionalFeatures("bounded-floor, hand-tracking") // 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 !n.webxr_interface.AsXRInterface().Initialize() { OS.Alert("Failed to initialize") return } }) n.webxr_interface = Object.To[WebXRInterface.Instance](XRServer.FindInterface("WebXR")) if n.webxr_interface != WebXRInterface.Nil { // WebXR uses a lot of asynchronous callbacks, so we connect to various // signals in order to receive them. n.webxr_interface.OnSessionSupported(func(session_mode string, supported bool) { if session_mode == "immersive-vr" { n.vr_supported = supported } }) n.webxr_interface.OnSessionStarted(func() { n.Button.AsCanvasItem().SetVisible(false) // This tells Godot to start rendering to the headset. Viewport.Get(n.AsNode()).SetUseXr(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: ", n.webxr_interface.ReferenceSpaceType()) // This will be the list of features that were successfully enabled // (except on browsers that don't support this property). print("Enabled features: ", n.webxr_interface.EnabledFeatures()) }) n.webxr_interface.OnSessionEnded(func() { n.Button.AsCanvasItem().SetVisible(true) // If the user exits immersive mode, then we tell Godot to render to the web // page again. Viewport.Get(n.AsNode()).SetUseXr(false) }) n.webxr_interface.OnSessionFailed(func(message string) { OS.Alert("Failed to initialize: " + message) }) } }
There are a couple ways to handle "controller" input:
- Using graphics.gd/classdb/XRController3D nodes and their [Instance.OnXrcontroller3d.ButtonPressed] and [Instance.OnXrcontroller3d.ButtonReleased] 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 Instance.OnSelect, Instance.OnSqueeze 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.
Index ¶
- type Advanced
- type Any
- type Extension
- type ID
- type Instance
- func (self Instance) AsObject() [1]gd.Object
- func (self Instance) AsRefCounted() [1]gd.RefCounted
- func (self Instance) AsWebXRInterface() Instance
- func (self Instance) AsXRInterface() XRInterface.Instance
- func (self Instance) EnabledFeatures() string
- func (self Instance) GetAvailableDisplayRefreshRates() []float32
- func (self Instance) GetDisplayRefreshRate() Float.X
- func (self Instance) GetInputSourceTargetRayMode(input_source_id int) TargetRayMode
- func (self Instance) GetInputSourceTracker(input_source_id int) XRControllerTracker.Instance
- func (self Instance) ID() ID
- func (self Instance) IsInputSourceActive(input_source_id int) bool
- func (self Instance) IsSessionSupported(session_mode string)
- func (self Instance) OnDisplayRefreshRateChanged(cb func(), flags ...Signal.Flags)
- func (self Instance) OnReferenceSpaceReset(cb func(), flags ...Signal.Flags)
- func (self Instance) OnSelect(cb func(input_source_id int), flags ...Signal.Flags)
- func (self Instance) OnSelectend(cb func(input_source_id int), flags ...Signal.Flags)
- func (self Instance) OnSelectstart(cb func(input_source_id int), flags ...Signal.Flags)
- func (self Instance) OnSessionEnded(cb func(), flags ...Signal.Flags)
- func (self Instance) OnSessionFailed(cb func(message string), flags ...Signal.Flags)
- func (self Instance) OnSessionStarted(cb func(), flags ...Signal.Flags)
- func (self Instance) OnSessionSupported(cb func(session_mode string, supported bool), flags ...Signal.Flags)
- func (self Instance) OnSqueeze(cb func(input_source_id int), flags ...Signal.Flags)
- func (self Instance) OnSqueezeend(cb func(input_source_id int), flags ...Signal.Flags)
- func (self Instance) OnSqueezestart(cb func(input_source_id int), flags ...Signal.Flags)
- func (self Instance) OnVisibilityStateChanged(cb func(), flags ...Signal.Flags)
- func (self Instance) OptionalFeatures() string
- func (self Instance) ReferenceSpaceType() string
- func (self Instance) RequestedReferenceSpaceTypes() string
- func (self Instance) RequiredFeatures() string
- func (self Instance) SessionMode() string
- func (self Instance) SetDisplayRefreshRate(refresh_rate Float.X)
- func (self *Instance) SetObject(obj [1]gd.Object) bool
- func (self Instance) SetOptionalFeatures(value string)
- func (self Instance) SetRequestedReferenceSpaceTypes(value string)
- func (self Instance) SetRequiredFeatures(value string)
- func (self Instance) SetSessionMode(value string)
- func (self Instance) Virtual(name string) reflect.Value
- func (self Instance) VisibilityState() string
- type TargetRayMode
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Advanced ¶
type Advanced = class
Advanced exposes a 1:1 low-level instance of the class, undocumented, for those who know what they are doing.
type Extension ¶
Extension can be embedded in a new struct to create an extension of this class. T should be the type that is embedding this Extension
func (*Extension[T]) AsRefCounted ¶
func (self *Extension[T]) AsRefCounted() [1]gd.RefCounted
func (*Extension[T]) AsWebXRInterface ¶
func (*Extension[T]) AsXRInterface ¶
func (self *Extension[T]) AsXRInterface() XRInterface.Instance
type ID ¶
ID is a typed object ID (reference) to an instance of this class, use it to store references to objects with unknown lifetimes, as an ID will not panic on use if the underlying object has been destroyed.
type Instance ¶
type Instance [1]gdclass.WebXRInterface
Instance of the class with convieniently typed arguments and results.
var Nil Instance
Nil is a nil/null instance of the class. Equivalent to the zero value.
func (Instance) AsRefCounted ¶
func (self Instance) AsRefCounted() [1]gd.RefCounted
func (Instance) AsWebXRInterface ¶
func (Instance) AsXRInterface ¶
func (self Instance) AsXRInterface() XRInterface.Instance
func (Instance) EnabledFeatures ¶
func (Instance) GetAvailableDisplayRefreshRates ¶
Returns display refresh rates supported by the current HMD. Only returned if this feature is supported by the web browser and after the interface has been initialized.
func (Instance) GetDisplayRefreshRate ¶
Returns the display refresh rate for the current HMD. Not supported on all HMDs and browsers. It may not report an accurate value until after using Instance.SetDisplayRefreshRate.
func (Instance) GetInputSourceTargetRayMode ¶
func (self Instance) GetInputSourceTargetRayMode(input_source_id int) TargetRayMode
Returns the target ray mode for the given 'input_source_id'.
This can help interpret the input coming from that input source. See XRInputSource.targetRayMode for more information.
func (Instance) GetInputSourceTracker ¶
func (self Instance) GetInputSourceTracker(input_source_id int) XRControllerTracker.Instance
Gets an graphics.gd/classdb/XRControllerTracker for the given 'input_source_id'.
In the context of WebXR, an input source 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 input source is used, interpret the position and orientation of the graphics.gd/classdb/XRPositionalTracker as a ray pointing at the object the user wishes to interact with.
Use this method to get information about the input source that triggered one of these signals:
func (Instance) IsInputSourceActive ¶
Returns true if there is an active input source with the given 'input_source_id'.
func (Instance) IsSessionSupported ¶
Checks if the given 'session_mode' is supported by the user's browser.
Possible values come from WebXR's XRSessionMode, including: "immersive-vr", "immersive-ar", and "inline".
This method returns nothing, instead it emits the Instance.OnSessionSupported signal with the result.
func (Instance) OnDisplayRefreshRateChanged ¶
func (Instance) OnReferenceSpaceReset ¶
func (Instance) OnSelectend ¶
func (Instance) OnSelectstart ¶
func (Instance) OnSessionEnded ¶
func (Instance) OnSessionFailed ¶
func (Instance) OnSessionStarted ¶
func (Instance) OnSessionSupported ¶
func (Instance) OnSqueezeend ¶
func (Instance) OnSqueezestart ¶
func (Instance) OnVisibilityStateChanged ¶
func (Instance) OptionalFeatures ¶
func (Instance) ReferenceSpaceType ¶
func (Instance) RequestedReferenceSpaceTypes ¶
func (Instance) RequiredFeatures ¶
func (Instance) SessionMode ¶
func (Instance) SetDisplayRefreshRate ¶
Sets the display refresh rate for the current HMD. Not supported on all HMDs and browsers. It won't take effect right away until after Instance.OnDisplayRefreshRateChanged is emitted.
func (Instance) SetOptionalFeatures ¶
func (Instance) SetRequestedReferenceSpaceTypes ¶
func (Instance) SetRequiredFeatures ¶
func (Instance) SetSessionMode ¶
func (Instance) VisibilityState ¶
type TargetRayMode ¶
type TargetRayMode int //gd:WebXRInterface.TargetRayMode
const ( // We don't know the target ray mode. TargetRayModeUnknown TargetRayMode = 0 // Target ray originates at the viewer's eyes and points in the direction they are looking. TargetRayModeGaze TargetRayMode = 1 // Target ray from a handheld pointer, most likely a VR touch controller. TargetRayModeTrackedPointer TargetRayMode = 2 // Target ray from touch screen, mouse or other tactile input device. TargetRayModeScreen TargetRayMode = 3 )