summaryrefslogtreecommitdiff
path: root/modules/webxr
diff options
context:
space:
mode:
Diffstat (limited to 'modules/webxr')
-rw-r--r--modules/webxr/doc_classes/WebXRInterface.xml122
-rw-r--r--modules/webxr/godot_webxr.h1
-rw-r--r--modules/webxr/native/library_godot_webxr.js31
-rw-r--r--modules/webxr/webxr_interface_js.cpp37
-rw-r--r--modules/webxr/webxr_interface_js.h2
5 files changed, 117 insertions, 76 deletions
diff --git a/modules/webxr/doc_classes/WebXRInterface.xml b/modules/webxr/doc_classes/WebXRInterface.xml
index bddffd910e..2407d44496 100644
--- a/modules/webxr/doc_classes/WebXRInterface.xml
+++ b/modules/webxr/doc_classes/WebXRInterface.xml
@@ -10,75 +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.
Here's the minimum code required to start an immersive VR session:
[codeblock]
- var webxr_interface
- var vr_supported = false
+ extends Node3D
- func _ready():
- # We assume this node has a canvas layer with a button on it as a child.
- # This button is for the user to consent to entering immersive VR mode.
- $CanvasLayer/Button.connect("pressed", self, "_on_Button_pressed")
+ var webxr_interface
+ var vr_supported = false
- webxr_interface = XRServer.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")
+ 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")
- # 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")
+ webxr_interface = XRServer.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")
- func _webxr_session_supported(session_mode, supported):
- if session_mode == 'immersive-vr':
- vr_supported = supported
+ # 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 _on_Button_pressed():
- if not vr_supported:
- OS.alert("Your browser doesn't support VR")
- return
+ func _webxr_session_supported(session_mode, supported):
+ if session_mode == 'immersive-vr':
+ vr_supported = supported
- # 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 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'
+ func _on_Button_pressed():
+ if not vr_supported:
+ OS.alert("Your browser doesn't support VR")
+ return
- # 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
+ # 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 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'
- func _webxr_session_started():
- # This tells Godot to start rendering to the headset.
- get_viewport().xr = 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)
+ # 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_ended():
- # If the user exits immersive mode, then we tell Godot to render to the web
- # page again.
- get_viewport().xr = false
+ func _webxr_session_started():
+ $Button.visible = false
+ # This tells Godot to start rendering to the headset.
+ get_viewport().xr = 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_failed(message):
- OS.alert("Failed to initialize: " + message)
+ 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().xr = false
+
+ func _webxr_session_failed(message):
+ OS.alert("Failed to initialize: " + message)
[/codeblock]
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].
diff --git a/modules/webxr/godot_webxr.h b/modules/webxr/godot_webxr.h
index 5e50ffde28..41a690f473 100644
--- a/modules/webxr/godot_webxr.h
+++ b/modules/webxr/godot_webxr.h
@@ -61,6 +61,7 @@ extern void godot_webxr_initialize(
GodotWebXRSimpleEventCallback p_on_simple_event);
extern void godot_webxr_uninitialize();
+extern int godot_webxr_get_view_count();
extern int *godot_webxr_get_render_targetsize();
extern float *godot_webxr_get_transform_for_eye(int p_eye);
extern float *godot_webxr_get_projection_for_eye(int p_eye);
diff --git a/modules/webxr/native/library_godot_webxr.js b/modules/webxr/native/library_godot_webxr.js
index 447045ed27..8e9ef8a73c 100644
--- a/modules/webxr/native/library_godot_webxr.js
+++ b/modules/webxr/native/library_godot_webxr.js
@@ -380,6 +380,11 @@ const GodotWebXR = {
gl.deleteTexture(texture);
}
GodotWebXR.textures[i] = null;
+
+ const texture_id = GodotWebXR.texture_ids[i];
+ if (texture_id !== null) {
+ GL.textures[texture_id] = null;
+ }
GodotWebXR.texture_ids[i] = null;
}
@@ -394,6 +399,15 @@ const GodotWebXR = {
GodotWebXR.pauseResumeMainLoop();
},
+ godot_webxr_get_view_count__proxy: 'sync',
+ godot_webxr_get_view_count__sig: 'i',
+ godot_webxr_get_view_count: function () {
+ if (!GodotWebXR.session || !GodotWebXR.pose) {
+ return 0;
+ }
+ return GodotWebXR.pose.views.length;
+ },
+
godot_webxr_get_render_targetsize__proxy: 'sync',
godot_webxr_get_render_targetsize__sig: 'i',
godot_webxr_get_render_targetsize: function () {
@@ -451,7 +465,7 @@ const GodotWebXR = {
godot_webxr_get_external_texture_for_eye__proxy: 'sync',
godot_webxr_get_external_texture_for_eye__sig: 'ii',
godot_webxr_get_external_texture_for_eye: function (p_eye) {
- if (!GodotWebXR.session || !GodotWebXR.pose) {
+ if (!GodotWebXR.session) {
return 0;
}
@@ -460,6 +474,13 @@ const GodotWebXR = {
return GodotWebXR.texture_ids[view_index];
}
+ // Check pose separately and after returning the cached texture id,
+ // because we won't get a pose in some cases if we lose tracking, and
+ // we don't want to return 0 just because tracking was lost.
+ if (!GodotWebXR.pose) {
+ return 0;
+ }
+
const glLayer = GodotWebXR.session.renderState.baseLayer;
const view = GodotWebXR.pose.views[view_index];
const viewport = glLayer.getViewport(view);
@@ -601,7 +622,13 @@ const GodotWebXR = {
const buf = GodotRuntime.malloc((axes_count + 1) * 4);
GodotRuntime.setHeapValue(buf, axes_count, 'i32');
for (let i = 0; i < axes_count; i++) {
- GodotRuntime.setHeapValue(buf + 4 + (i * 4), controller.gamepad.axes[i], 'float');
+ let value = controller.gamepad.axes[i];
+ if (i === 1 || i === 3) {
+ // Invert the Y-axis on thumbsticks and trackpads, in order to
+ // match OpenXR and other XR platform SDKs.
+ value *= -1.0;
+ }
+ GodotRuntime.setHeapValue(buf + 4 + (i * 4), value, 'float');
}
return buf;
},
diff --git a/modules/webxr/webxr_interface_js.cpp b/modules/webxr/webxr_interface_js.cpp
index 72dc4790ac..74789fc98e 100644
--- a/modules/webxr/webxr_interface_js.cpp
+++ b/modules/webxr/webxr_interface_js.cpp
@@ -78,6 +78,8 @@ void _emwebxr_on_session_failed(char *p_message) {
Ref<XRInterface> interface = xr_server->find_interface("WebXR");
ERR_FAIL_COND(interface.is_null());
+ interface->uninitialize();
+
String message = String(p_message);
interface->emit_signal("session_failed", message);
}
@@ -197,12 +199,11 @@ StringName WebXRInterfaceJS::get_name() const {
};
int WebXRInterfaceJS::get_capabilities() const {
- return XRInterface::XR_STEREO;
+ return XRInterface::XR_STEREO | XRInterface::XR_MONO;
};
bool WebXRInterfaceJS::is_stereo() {
- // @todo WebXR can be mono! So, how do we know? Count the views in the frame?
- return true;
+ return godot_webxr_get_view_count() == 2;
};
bool WebXRInterfaceJS::is_initialized() const {
@@ -225,6 +226,12 @@ bool WebXRInterfaceJS::initialize() {
// make this our primary interface
xr_server->set_primary_interface(this);
+ // Clear render_targetsize to make sure it gets reset to the new size.
+ // Clearing in uninitialize() doesn't work because a frame can still be
+ // rendered after it's called, which will fill render_targetsize again.
+ render_targetsize.width = 0;
+ render_targetsize.height = 0;
+
initialized = true;
godot_webxr_initialize(
@@ -278,22 +285,24 @@ Transform WebXRInterfaceJS::_js_matrix_to_transform(float *p_js_matrix) {
}
Size2 WebXRInterfaceJS::get_render_targetsize() {
- Size2 target_size;
+ if (render_targetsize.width != 0 && render_targetsize.height != 0) {
+ return render_targetsize;
+ }
int *js_size = godot_webxr_get_render_targetsize();
if (!initialized || js_size == nullptr) {
- // As a default, use half the window size.
- target_size = DisplayServer::get_singleton()->window_get_size();
- target_size.width /= 2.0;
- return target_size;
+ // As a temporary default (until WebXR is fully initialized), use half the window size.
+ Size2 temp = DisplayServer::get_singleton()->window_get_size();
+ temp.width /= 2.0;
+ return temp;
}
- target_size.width = js_size[0];
- target_size.height = js_size[1];
+ render_targetsize.width = js_size[0];
+ render_targetsize.height = js_size[1];
free(js_size);
- return target_size;
+ return render_targetsize;
};
Transform WebXRInterfaceJS::get_transform_for_eye(XRInterface::Eyes p_eye, const Transform &p_cam_transform) {
@@ -375,11 +384,11 @@ void WebXRInterfaceJS::_update_tracker(int p_controller_id) {
if (godot_webxr_is_controller_connected(p_controller_id)) {
if (tracker == nullptr) {
tracker = memnew(XRPositionalTracker);
- tracker->set_type(XRServer::TRACKER_CONTROLLER);
+ tracker->set_tracker_type(XRServer::TRACKER_CONTROLLER);
// Controller id's 0 and 1 are always the left and right hands.
if (p_controller_id < 2) {
- tracker->set_name(p_controller_id == 0 ? "Left" : "Right");
- tracker->set_hand(p_controller_id == 0 ? XRPositionalTracker::TRACKER_LEFT_HAND : XRPositionalTracker::TRACKER_RIGHT_HAND);
+ tracker->set_tracker_name(p_controller_id == 0 ? "Left" : "Right");
+ tracker->set_tracker_hand(p_controller_id == 0 ? XRPositionalTracker::TRACKER_HAND_LEFT : XRPositionalTracker::TRACKER_HAND_RIGHT);
}
// Use the ids we're giving to our "virtual" gamepads.
tracker->set_joy_id(p_controller_id + 100);
diff --git a/modules/webxr/webxr_interface_js.h b/modules/webxr/webxr_interface_js.h
index 93da9a6d12..49299b252f 100644
--- a/modules/webxr/webxr_interface_js.h
+++ b/modules/webxr/webxr_interface_js.h
@@ -47,7 +47,6 @@ class WebXRInterfaceJS : public WebXRInterface {
private:
bool initialized;
- // @todo Should these really use enums instead of strings?
String session_mode;
String required_features;
String optional_features;
@@ -55,6 +54,7 @@ private:
String reference_space_type;
bool controllers_state[2];
+ Size2 render_targetsize;
Transform _js_matrix_to_transform(float *p_js_matrix);
void _update_tracker(int p_controller_id);