diff --git a/modules/webxr/doc_classes/WebXRInterface.xml b/modules/webxr/doc_classes/WebXRInterface.xml index 758c37e2adc..ed162b1da23 100644 --- a/modules/webxr/doc_classes/WebXRInterface.xml +++ b/modules/webxr/doc_classes/WebXRInterface.xml @@ -93,6 +93,18 @@ https://www.snopekgames.com/tutorial/2023/how-make-vr-game-webxr-godot-4 + + + + 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. + + + + + + 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 [method set_display_refresh_rate]. + + @@ -132,6 +144,13 @@ This method returns nothing, instead it emits the [signal session_supported] signal with the result. + + + + + 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 [signal display_refresh_rate_changed] is emitted. + + @@ -167,6 +186,11 @@ + + + Emitted after the display's refresh rate has changed. + + Emitted to indicate that the reference space has been reset or reconfigured. diff --git a/modules/webxr/godot_webxr.h b/modules/webxr/godot_webxr.h index c636be1576d..554ca0398b8 100644 --- a/modules/webxr/godot_webxr.h +++ b/modules/webxr/godot_webxr.h @@ -90,6 +90,10 @@ extern bool godot_webxr_update_input_source( extern char *godot_webxr_get_visibility_state(); extern int godot_webxr_get_bounds_geometry(float **r_points); +extern float godot_webxr_get_frame_rate(); +extern void godot_webxr_update_target_frame_rate(float p_frame_rate); +extern int godot_webxr_get_supported_frame_rates(float **r_frame_rates); + #ifdef __cplusplus } #endif diff --git a/modules/webxr/native/library_godot_webxr.js b/modules/webxr/native/library_godot_webxr.js index 5c01d88a307..fe47de02b05 100644 --- a/modules/webxr/native/library_godot_webxr.js +++ b/modules/webxr/native/library_godot_webxr.js @@ -42,6 +42,7 @@ const GodotWebXR = { view_count: 1, input_sources: new Array(16), touches: new Array(5), + onsimpleevent: null, // Monkey-patch the requestAnimationFrame() used by Emscripten for the main // loop, so that we can swap it out for XRSession.requestAnimationFrame() @@ -283,6 +284,9 @@ const GodotWebXR = { GodotRuntime.free(c_str); }); + // Store onsimpleevent so we can use it later. + GodotWebXR.onsimpleevent = onsimpleevent; + const gl_context_handle = _emscripten_webgl_get_current_context(); // eslint-disable-line no-undef const gl = GL.getContext(gl_context_handle).GLctx; GodotWebXR.gl = gl; @@ -368,6 +372,7 @@ const GodotWebXR = { GodotWebXR.view_count = 1; GodotWebXR.input_sources = new Array(16); GodotWebXR.touches = new Array(5); + GodotWebXR.onsimpleevent = null; // Disable the monkey-patched window.requestAnimationFrame() and // pause/restart the main loop to activate it on all platforms. @@ -594,6 +599,51 @@ const GodotWebXR = { return point_count; }, + + godot_webxr_get_frame_rate__proxy: 'sync', + godot_webxr_get_frame_rate__sig: 'i', + godot_webxr_get_frame_rate: function () { + if (!GodotWebXR.session || GodotWebXR.session.frameRate === undefined) { + return 0; + } + return GodotWebXR.session.frameRate; + }, + + godot_webxr_update_target_frame_rate__proxy: 'sync', + godot_webxr_update_target_frame_rate__sig: 'vi', + godot_webxr_update_target_frame_rate: function (p_frame_rate) { + if (!GodotWebXR.session || GodotWebXR.session.updateTargetFrameRate === undefined) { + return; + } + + GodotWebXR.session.updateTargetFrameRate(p_frame_rate).then(() => { + const c_str = GodotRuntime.allocString('display_refresh_rate_changed'); + GodotWebXR.onsimpleevent(c_str); + GodotRuntime.free(c_str); + }); + }, + + godot_webxr_get_supported_frame_rates__proxy: 'sync', + godot_webxr_get_supported_frame_rates__sig: 'ii', + godot_webxr_get_supported_frame_rates: function (r_frame_rates) { + if (!GodotWebXR.session || GodotWebXR.session.supportedFrameRates === undefined) { + return 0; + } + + const frame_rate_count = GodotWebXR.session.supportedFrameRates.length; + if (frame_rate_count === 0) { + return 0; + } + + const buf = GodotRuntime.malloc(frame_rate_count * 4); + for (let i = 0; i < frame_rate_count; i++) { + GodotRuntime.setHeapValue(buf + (i * 4), GodotWebXR.session.supportedFrameRates[i], 'float'); + } + GodotRuntime.setHeapValue(r_frame_rates, buf, 'i32'); + + return frame_rate_count; + }, + }; autoAddDeps(GodotWebXR, '$GodotWebXR'); diff --git a/modules/webxr/native/webxr.externs.js b/modules/webxr/native/webxr.externs.js index 4b88820b197..7f7c297acc7 100644 --- a/modules/webxr/native/webxr.externs.js +++ b/modules/webxr/native/webxr.externs.js @@ -67,6 +67,16 @@ XRSession.prototype.inputSources; */ XRSession.prototype.visibilityState; +/** + * @type {?number} + */ +XRSession.prototype.frameRate; + +/** + * @type {?Float32Array} + */ +XRSession.prototype.supportedFrameRates; + /** * @type {?function (Event)} */ @@ -141,6 +151,12 @@ XRSession.prototype.end = function () {}; */ XRSession.prototype.requestReferenceSpace = function (referenceSpaceType) {}; +/** + * @param {number} rate + * @return {Promise} + */ +XRSession.prototype.updateTargetFrameRate = function (rate) {}; + /** * @typedef {function(number, XRFrame): undefined} */ diff --git a/modules/webxr/webxr_interface.cpp b/modules/webxr/webxr_interface.cpp index d7f4247768f..5e30d0c9969 100644 --- a/modules/webxr/webxr_interface.cpp +++ b/modules/webxr/webxr_interface.cpp @@ -46,6 +46,9 @@ void WebXRInterface::_bind_methods() { ClassDB::bind_method(D_METHOD("get_input_source_tracker", "input_source_id"), &WebXRInterface::get_input_source_tracker); ClassDB::bind_method(D_METHOD("get_input_source_target_ray_mode", "input_source_id"), &WebXRInterface::get_input_source_target_ray_mode); ClassDB::bind_method(D_METHOD("get_visibility_state"), &WebXRInterface::get_visibility_state); + ClassDB::bind_method(D_METHOD("get_display_refresh_rate"), &WebXRInterface::get_display_refresh_rate); + ClassDB::bind_method(D_METHOD("set_display_refresh_rate", "refresh_rate"), &WebXRInterface::set_display_refresh_rate); + ClassDB::bind_method(D_METHOD("get_available_display_refresh_rates"), &WebXRInterface::get_available_display_refresh_rates); ADD_PROPERTY(PropertyInfo(Variant::STRING, "session_mode", PROPERTY_HINT_NONE), "set_session_mode", "get_session_mode"); ADD_PROPERTY(PropertyInfo(Variant::STRING, "required_features", PROPERTY_HINT_NONE), "set_required_features", "get_required_features"); @@ -68,6 +71,7 @@ void WebXRInterface::_bind_methods() { ADD_SIGNAL(MethodInfo("visibility_state_changed")); ADD_SIGNAL(MethodInfo("reference_space_reset")); + ADD_SIGNAL(MethodInfo("display_refresh_rate_changed")); BIND_ENUM_CONSTANT(TARGET_RAY_MODE_UNKNOWN); BIND_ENUM_CONSTANT(TARGET_RAY_MODE_GAZE); diff --git a/modules/webxr/webxr_interface.h b/modules/webxr/webxr_interface.h index f18b4b40e60..abaa8c01f8a 100644 --- a/modules/webxr/webxr_interface.h +++ b/modules/webxr/webxr_interface.h @@ -66,6 +66,9 @@ public: virtual Ref get_input_source_tracker(int p_input_source_id) const = 0; virtual TargetRayMode get_input_source_target_ray_mode(int p_input_source_id) const = 0; virtual String get_visibility_state() const = 0; + virtual float get_display_refresh_rate() const = 0; + virtual void set_display_refresh_rate(float p_refresh_rate) = 0; + virtual Array get_available_display_refresh_rates() const = 0; }; VARIANT_ENUM_CAST(WebXRInterface::TargetRayMode); diff --git a/modules/webxr/webxr_interface_js.cpp b/modules/webxr/webxr_interface_js.cpp index d3710bd0dff..e1df2ea94e7 100644 --- a/modules/webxr/webxr_interface_js.cpp +++ b/modules/webxr/webxr_interface_js.cpp @@ -202,6 +202,30 @@ PackedVector3Array WebXRInterfaceJS::get_play_area() const { return ret; } +float WebXRInterfaceJS::get_display_refresh_rate() const { + return godot_webxr_get_frame_rate(); +} + +void WebXRInterfaceJS::set_display_refresh_rate(float p_refresh_rate) { + godot_webxr_update_target_frame_rate(p_refresh_rate); +} + +Array WebXRInterfaceJS::get_available_display_refresh_rates() const { + Array ret; + + float *rates; + int rate_count = godot_webxr_get_supported_frame_rates(&rates); + if (rate_count > 0) { + ret.resize(rate_count); + for (int i = 0; i < rate_count; i++) { + ret[i] = rates[i]; + } + free(rates); + } + + return ret; +} + StringName WebXRInterfaceJS::get_name() const { return "WebXR"; }; diff --git a/modules/webxr/webxr_interface_js.h b/modules/webxr/webxr_interface_js.h index 20516e89e29..d85eb8cad74 100644 --- a/modules/webxr/webxr_interface_js.h +++ b/modules/webxr/webxr_interface_js.h @@ -102,6 +102,10 @@ public: virtual String get_visibility_state() const override; virtual PackedVector3Array get_play_area() const override; + virtual float get_display_refresh_rate() const override; + virtual void set_display_refresh_rate(float p_refresh_rate) override; + virtual Array get_available_display_refresh_rates() const override; + virtual StringName get_name() const override; virtual uint32_t get_capabilities() const override;