Debian Codegraph @

modules/webxr/native/library_godot_webxr.js

raw symbols SCIP
1 /**************************************************************************/
2 /* library_godot_webxr.js */
3 /**************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /**************************************************************************/
8 /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9 /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10 /* */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the */
13 /* "Software"), to deal in the Software without restriction, including */
14 /* without limitation the rights to use, copy, modify, merge, publish, */
15 /* distribute, sublicense, and/or sell copies of the Software, and to */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions: */
18 /* */
19 /* The above copyright notice and this permission notice shall be */
20 /* included in all copies or substantial portions of the Software. */
21 /* */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29 /**************************************************************************/
30
31 const GodotWebXR = {
32 $GodotWebXR__deps: ['$MainLoop', '$GL', '$GodotRuntime', '$runtimeKeepalivePush', '$runtimeKeepalivePop'],
33 $GodotWebXR: {
34 gl: null,
35
36 session: null,
37 gl_binding: null,
38 layer: null,
39 space: null,
40 frame: null,
41 pose: null,
42 view_count: 1,
43 input_sources: new Array(16),
44 touches: new Array(5),
45 onsimpleevent: null,
46
47 // Monkey-patch the requestAnimationFrame() used by Emscripten for the main
48 // loop, so that we can swap it out for XRSession.requestAnimationFrame()
49 // when an XR session is started.
50 orig_requestAnimationFrame: null,
51 requestAnimationFrame: (callback) => {
52 if (GodotWebXR.session && GodotWebXR.space) {
53 const onFrame = function (time, frame) {
54 GodotWebXR.frame = frame;
55 GodotWebXR.pose = frame.getViewerPose(GodotWebXR.space);
56 callback(time);
57 GodotWebXR.frame = null;
58 GodotWebXR.pose = null;
59 };
60 GodotWebXR.session.requestAnimationFrame(onFrame);
61 } else {
62 GodotWebXR.orig_requestAnimationFrame(callback);
63 }
64 },
65 monkeyPatchRequestAnimationFrame: (enable) => {
66 if (GodotWebXR.orig_requestAnimationFrame === null) {
67 GodotWebXR.orig_requestAnimationFrame = MainLoop.requestAnimationFrame;
68 }
69 MainLoop.requestAnimationFrame = enable
70 ? GodotWebXR.requestAnimationFrame
71 : GodotWebXR.orig_requestAnimationFrame;
72 },
73 pauseResumeMainLoop: () => {
74 // Once both GodotWebXR.session and GodotWebXR.space are set or
75 // unset, our monkey-patched requestAnimationFrame() should be
76 // enabled or disabled. When using the WebXR API Emulator, this
77 // gets picked up automatically, however, in the Oculus Browser
78 // on the Quest, we need to pause and resume the main loop.
79 MainLoop.pause();
80 runtimeKeepalivePush();
81 window.setTimeout(function () {
82 runtimeKeepalivePop();
83 MainLoop.resume();
84 }, 0);
85 },
86
87 getLayer: () => {
88 const new_view_count = (GodotWebXR.pose) ? GodotWebXR.pose.views.length : 1;
89 let layer = GodotWebXR.layer;
90
91 // If the view count hasn't changed since creating this layer, then
92 // we can simply return it.
93 if (layer && GodotWebXR.view_count === new_view_count) {
94 return layer;
95 }
96
97 if (!GodotWebXR.session || !GodotWebXR.gl_binding || !GodotWebXR.gl_binding.createProjectionLayer) {
98 return null;
99 }
100
101 const gl = GodotWebXR.gl;
102
103 layer = GodotWebXR.gl_binding.createProjectionLayer({
104 textureType: new_view_count > 1 ? 'texture-array' : 'texture',
105 colorFormat: gl.RGBA8,
106 depthFormat: gl.DEPTH_COMPONENT24,
107 });
108 GodotWebXR.session.updateRenderState({ layers: [layer] });
109
110 GodotWebXR.layer = layer;
111 GodotWebXR.view_count = new_view_count;
112 return layer;
113 },
114
115 getSubImage: () => {
116 if (!GodotWebXR.pose) {
117 return null;
118 }
119 const layer = GodotWebXR.getLayer();
120 if (layer === null) {
121 return null;
122 }
123
124 // Because we always use "texture-array" for multiview and "texture"
125 // when there is only 1 view, it should be safe to only grab the
126 // subimage for the first view.
127 return GodotWebXR.gl_binding.getViewSubImage(layer, GodotWebXR.pose.views[0]);
128 },
129
130 getTextureId: (texture) => {
131 if (texture.name !== undefined) {
132 return texture.name;
133 }
134
135 const id = GL.getNewId(GL.textures);
136 texture.name = id;
137 GL.textures[id] = texture;
138
139 return id;
140 },
141
142 addInputSource: (input_source) => {
143 let name = -1;
144 if (input_source.targetRayMode === 'tracked-pointer' && input_source.handedness === 'left') {
145 name = 0;
146 } else if (input_source.targetRayMode === 'tracked-pointer' && input_source.handedness === 'right') {
147 name = 1;
148 } else {
149 for (let i = 2; i < 16; i++) {
150 if (!GodotWebXR.input_sources[i]) {
151 name = i;
152 break;
153 }
154 }
155 }
156 if (name >= 0) {
157 GodotWebXR.input_sources[name] = input_source;
158 input_source.name = name;
159
160 // Find a free touch index for screen sources.
161 if (input_source.targetRayMode === 'screen') {
162 let touch_index = -1;
163 for (let i = 0; i < 5; i++) {
164 if (!GodotWebXR.touches[i]) {
165 touch_index = i;
166 break;
167 }
168 }
169 if (touch_index >= 0) {
170 GodotWebXR.touches[touch_index] = input_source;
171 input_source.touch_index = touch_index;
172 }
173 }
174 }
175 return name;
176 },
177
178 removeInputSource: (input_source) => {
179 if (input_source.name !== undefined) {
180 const name = input_source.name;
181 if (name >= 0 && name < 16) {
182 GodotWebXR.input_sources[name] = null;
183 }
184
185 if (input_source.touch_index !== undefined) {
186 const touch_index = input_source.touch_index;
187 if (touch_index >= 0 && touch_index < 5) {
188 GodotWebXR.touches[touch_index] = null;
189 }
190 }
191 return name;
192 }
193 return -1;
194 },
195
196 getInputSourceId: (input_source) => {
197 if (input_source !== undefined) {
198 return input_source.name;
199 }
200 return -1;
201 },
202
203 getTouchIndex: (input_source) => {
204 if (input_source.touch_index !== undefined) {
205 return input_source.touch_index;
206 }
207 return -1;
208 },
209 },
210
211 godot_webxr_is_supported__proxy: 'sync',
212 godot_webxr_is_supported__sig: 'i',
213 godot_webxr_is_supported: function () {
214 return !!navigator.xr;
215 },
216
217 godot_webxr_is_session_supported__proxy: 'sync',
218 godot_webxr_is_session_supported__sig: 'vii',
219 godot_webxr_is_session_supported: function (p_session_mode, p_callback) {
220 const session_mode = GodotRuntime.parseString(p_session_mode);
221 const cb = GodotRuntime.get_func(p_callback);
222 if (navigator.xr) {
223 navigator.xr.isSessionSupported(session_mode).then(function (supported) {
224 const c_str = GodotRuntime.allocString(session_mode);
225 cb(c_str, supported ? 1 : 0);
226 GodotRuntime.free(c_str);
227 });
228 } else {
229 const c_str = GodotRuntime.allocString(session_mode);
230 cb(c_str, 0);
231 GodotRuntime.free(c_str);
232 }
233 },
234
235 godot_webxr_initialize__deps: ['emscripten_webgl_get_current_context'],
236 godot_webxr_initialize__proxy: 'sync',
237 godot_webxr_initialize__sig: 'viiiiiiiii',
238 godot_webxr_initialize: function (p_session_mode, p_required_features, p_optional_features, p_requested_reference_spaces, p_on_session_started, p_on_session_ended, p_on_session_failed, p_on_input_event, p_on_simple_event) {
239 GodotWebXR.monkeyPatchRequestAnimationFrame(true);
240
241 const session_mode = GodotRuntime.parseString(p_session_mode);
242 const required_features = GodotRuntime.parseString(p_required_features).split(',').map((s) => s.trim()).filter((s) => s !== '');
243 const optional_features = GodotRuntime.parseString(p_optional_features).split(',').map((s) => s.trim()).filter((s) => s !== '');
244 const requested_reference_space_types = GodotRuntime.parseString(p_requested_reference_spaces).split(',').map((s) => s.trim());
245 const onstarted = GodotRuntime.get_func(p_on_session_started);
246 const onended = GodotRuntime.get_func(p_on_session_ended);
247 const onfailed = GodotRuntime.get_func(p_on_session_failed);
248 const oninputevent = GodotRuntime.get_func(p_on_input_event);
249 const onsimpleevent = GodotRuntime.get_func(p_on_simple_event);
250
251 const session_init = {};
252 if (required_features.length > 0) {
253 session_init['requiredFeatures'] = required_features;
254 }
255 if (optional_features.length > 0) {
256 session_init['optionalFeatures'] = optional_features;
257 }
258
259 navigator.xr.requestSession(session_mode, session_init).then(function (session) {
260 GodotWebXR.session = session;
261
262 session.addEventListener('end', function (evt) {
263 onended();
264 });
265
266 session.addEventListener('inputsourceschange', function (evt) {
267 evt.added.forEach(GodotWebXR.addInputSource);
268 evt.removed.forEach(GodotWebXR.removeInputSource);
269 });
270
271 ['selectstart', 'selectend', 'squeezestart', 'squeezeend'].forEach((input_event, index) => {
272 session.addEventListener(input_event, function (evt) {
273 // Since this happens in-between normal frames, we need to
274 // grab the frame from the event in order to get poses for
275 // the input sources.
276 GodotWebXR.frame = evt.frame;
277 oninputevent(index, GodotWebXR.getInputSourceId(evt.inputSource));
278 GodotWebXR.frame = null;
279 });
280 });
281
282 session.addEventListener('visibilitychange', function (evt) {
283 const c_str = GodotRuntime.allocString('visibility_state_changed');
284 onsimpleevent(c_str);
285 GodotRuntime.free(c_str);
286 });
287
288 // Store onsimpleevent so we can use it later.
289 GodotWebXR.onsimpleevent = onsimpleevent;
290
291 const gl_context_handle = _emscripten_webgl_get_current_context();
292 const gl = GL.getContext(gl_context_handle).GLctx;
293 GodotWebXR.gl = gl;
294
295 gl.makeXRCompatible().then(function () {
296 const throwNoWebXRLayersError = () => {
297 throw new Error('This browser doesn\'t support WebXR Layers (which Godot requires) nor is the polyfill in use. If you are the developer of this application, please consider including the polyfill.');
298 };
299
300 try {
301 GodotWebXR.gl_binding = new XRWebGLBinding(session, gl);
302 } catch (error) {
303 // We'll end up here for browsers that don't have XRWebGLBinding at all, or if the browser does support WebXR Layers,
304 // but is using the WebXR polyfill, so calling native XRWebGLBinding with the polyfilled XRSession won't work.
305 throwNoWebXRLayersError();
306 }
307
308 if (!GodotWebXR.gl_binding.createProjectionLayer) {
309 // On other browsers, XRWebGLBinding exists and works, but it doesn't support creating projection layers (which is
310 // contrary to the spec, which says this MUST be supported) and so the polyfill is required.
311 throwNoWebXRLayersError();
312 }
313
314 // This will trigger the layer to get created.
315 const layer = GodotWebXR.getLayer();
316 if (!layer) {
317 throw new Error('Unable to create WebXR Layer.');
318 }
319
320 function onReferenceSpaceSuccess(reference_space, reference_space_type) {
321 GodotWebXR.space = reference_space;
322
323 // Using reference_space.addEventListener() crashes when
324 // using the polyfill with the WebXR Emulator extension,
325 // so we set the event property instead.
326 reference_space.onreset = function (evt) {
327 const c_str = GodotRuntime.allocString('reference_space_reset');
328 onsimpleevent(c_str);
329 GodotRuntime.free(c_str);
330 };
331
332 // Now that both GodotWebXR.session and GodotWebXR.space are
333 // set, we need to pause and resume the main loop for the XR
334 // main loop to kick in.
335 GodotWebXR.pauseResumeMainLoop();
336
337 // Call in setTimeout() so that errors in the onstarted()
338 // callback don't bubble up here and cause Godot to try the
339 // next reference space.
340 window.setTimeout(function () {
341 const reference_space_c_str = GodotRuntime.allocString(reference_space_type);
342 const enabled_features = 'enabledFeatures' in session ? Array.from(session.enabledFeatures) : [];
343 const enabled_features_c_str = GodotRuntime.allocString(enabled_features.join(','));
344 const environment_blend_mode = 'environmentBlendMode' in session ? session.environmentBlendMode : '';
345 const environment_blend_mode_c_str = GodotRuntime.allocString(environment_blend_mode);
346 onstarted(reference_space_c_str, enabled_features_c_str, environment_blend_mode_c_str);
347 GodotRuntime.free(reference_space_c_str);
348 GodotRuntime.free(enabled_features_c_str);
349 GodotRuntime.free(environment_blend_mode_c_str);
350 }, 0);
351 }
352
353 function requestReferenceSpace() {
354 const reference_space_type = requested_reference_space_types.shift();
355 session.requestReferenceSpace(reference_space_type)
356 .then((refSpace) => {
357 onReferenceSpaceSuccess(refSpace, reference_space_type);
358 })
359 .catch(() => {
360 if (requested_reference_space_types.length === 0) {
361 const c_str = GodotRuntime.allocString('Unable to get any of the requested reference space types');
362 onfailed(c_str);
363 GodotRuntime.free(c_str);
364 } else {
365 requestReferenceSpace();
366 }
367 });
368 }
369
370 requestReferenceSpace();
371 }).catch(function (error) {
372 const c_str = GodotRuntime.allocString(`Unable to make WebGL context compatible with WebXR: ${error}`);
373 onfailed(c_str);
374 GodotRuntime.free(c_str);
375 });
376 }).catch(function (error) {
377 const c_str = GodotRuntime.allocString(`Unable to start session: ${error}`);
378 onfailed(c_str);
379 GodotRuntime.free(c_str);
380 });
381 },
382
383 godot_webxr_uninitialize__proxy: 'sync',
384 godot_webxr_uninitialize__sig: 'v',
385 godot_webxr_uninitialize: function () {
386 if (GodotWebXR.session) {
387 GodotWebXR.session.end()
388 // Prevent exception when session has already ended.
389 .catch((e) => { });
390 }
391
392 GodotWebXR.session = null;
393 GodotWebXR.gl_binding = null;
394 GodotWebXR.layer = null;
395 GodotWebXR.space = null;
396 GodotWebXR.frame = null;
397 GodotWebXR.pose = null;
398 GodotWebXR.view_count = 1;
399 GodotWebXR.input_sources = new Array(16);
400 GodotWebXR.touches = new Array(5);
401 GodotWebXR.onsimpleevent = null;
402
403 // Disable the monkey-patched window.requestAnimationFrame() and
404 // pause/restart the main loop to activate it on all platforms.
405 GodotWebXR.monkeyPatchRequestAnimationFrame(false);
406 GodotWebXR.pauseResumeMainLoop();
407 },
408
409 godot_webxr_get_view_count__proxy: 'sync',
410 godot_webxr_get_view_count__sig: 'i',
411 godot_webxr_get_view_count: function () {
412 if (!GodotWebXR.session || !GodotWebXR.pose) {
413 return 1;
414 }
415 const view_count = GodotWebXR.pose.views.length;
416 return view_count > 0 ? view_count : 1;
417 },
418
419 godot_webxr_get_render_target_size__proxy: 'sync',
420 godot_webxr_get_render_target_size__sig: 'ii',
421 godot_webxr_get_render_target_size: function (r_size) {
422 const subimage = GodotWebXR.getSubImage();
423 if (subimage === null) {
424 return false;
425 }
426
427 GodotRuntime.setHeapValue(r_size + 0, subimage.viewport.width, 'i32');
428 GodotRuntime.setHeapValue(r_size + 4, subimage.viewport.height, 'i32');
429
430 return true;
431 },
432
433 godot_webxr_get_transform_for_view__proxy: 'sync',
434 godot_webxr_get_transform_for_view__sig: 'iii',
435 godot_webxr_get_transform_for_view: function (p_view, r_transform) {
436 if (!GodotWebXR.session || !GodotWebXR.pose) {
437 return false;
438 }
439
440 const views = GodotWebXR.pose.views;
441 let matrix;
442 if (p_view >= 0) {
443 matrix = views[p_view].transform.matrix;
444 } else {
445 // For -1 (or any other negative value) return the HMD transform.
446 matrix = GodotWebXR.pose.transform.matrix;
447 }
448
449 for (let i = 0; i < 16; i++) {
450 GodotRuntime.setHeapValue(r_transform + (i * 4), matrix[i], 'float');
451 }
452
453 return true;
454 },
455
456 godot_webxr_get_projection_for_view__proxy: 'sync',
457 godot_webxr_get_projection_for_view__sig: 'iii',
458 godot_webxr_get_projection_for_view: function (p_view, r_transform) {
459 if (!GodotWebXR.session || !GodotWebXR.pose) {
460 return false;
461 }
462
463 const matrix = GodotWebXR.pose.views[p_view].projectionMatrix;
464 for (let i = 0; i < 16; i++) {
465 GodotRuntime.setHeapValue(r_transform + (i * 4), matrix[i], 'float');
466 }
467
468 return true;
469 },
470
471 godot_webxr_get_color_texture__proxy: 'sync',
472 godot_webxr_get_color_texture__sig: 'i',
473 godot_webxr_get_color_texture: function () {
474 const subimage = GodotWebXR.getSubImage();
475 if (subimage === null) {
476 return 0;
477 }
478 return GodotWebXR.getTextureId(subimage.colorTexture);
479 },
480
481 godot_webxr_get_depth_texture__proxy: 'sync',
482 godot_webxr_get_depth_texture__sig: 'i',
483 godot_webxr_get_depth_texture: function () {
484 const subimage = GodotWebXR.getSubImage();
485 if (subimage === null) {
486 return 0;
487 }
488 if (!subimage.depthStencilTexture) {
489 return 0;
490 }
491 return GodotWebXR.getTextureId(subimage.depthStencilTexture);
492 },
493
494 godot_webxr_get_velocity_texture__proxy: 'sync',
495 godot_webxr_get_velocity_texture__sig: 'i',
496 godot_webxr_get_velocity_texture: function () {
497 const subimage = GodotWebXR.getSubImage();
498 if (subimage === null) {
499 return 0;
500 }
501 if (!subimage.motionVectorTexture) {
502 return 0;
503 }
504 return GodotWebXR.getTextureId(subimage.motionVectorTexture);
505 },
506
507 godot_webxr_update_input_source__proxy: 'sync',
508 godot_webxr_update_input_source__sig: 'iiiiiiiiiiiiiii',
509 godot_webxr_update_input_source: function (p_input_source_id, r_target_pose, r_target_ray_mode, r_touch_index, r_has_grip_pose, r_grip_pose, r_has_standard_mapping, r_button_count, r_buttons, r_axes_count, r_axes, r_has_hand_data, r_hand_joints, r_hand_radii) {
510 if (!GodotWebXR.session || !GodotWebXR.frame) {
511 return 0;
512 }
513
514 if (p_input_source_id < 0 || p_input_source_id >= GodotWebXR.input_sources.length || !GodotWebXR.input_sources[p_input_source_id]) {
515 return false;
516 }
517
518 const input_source = GodotWebXR.input_sources[p_input_source_id];
519 const frame = GodotWebXR.frame;
520 const space = GodotWebXR.space;
521
522 // Target pose.
523 const target_pose = frame.getPose(input_source.targetRaySpace, space);
524 if (!target_pose) {
525 // This can mean that the controller lost tracking.
526 return false;
527 }
528 const target_pose_matrix = target_pose.transform.matrix;
529 for (let i = 0; i < 16; i++) {
530 GodotRuntime.setHeapValue(r_target_pose + (i * 4), target_pose_matrix[i], 'float');
531 }
532
533 // Target ray mode.
534 let target_ray_mode = 0;
535 switch (input_source.targetRayMode) {
536 case 'gaze':
537 target_ray_mode = 1;
538 break;
539
540 case 'tracked-pointer':
541 target_ray_mode = 2;
542 break;
543
544 case 'screen':
545 target_ray_mode = 3;
546 break;
547
548 default:
549 }
550 GodotRuntime.setHeapValue(r_target_ray_mode, target_ray_mode, 'i32');
551
552 // Touch index.
553 GodotRuntime.setHeapValue(r_touch_index, GodotWebXR.getTouchIndex(input_source), 'i32');
554
555 // Grip pose.
556 let has_grip_pose = false;
557 if (input_source.gripSpace) {
558 const grip_pose = frame.getPose(input_source.gripSpace, space);
559 if (grip_pose) {
560 const grip_pose_matrix = grip_pose.transform.matrix;
561 for (let i = 0; i < 16; i++) {
562 GodotRuntime.setHeapValue(r_grip_pose + (i * 4), grip_pose_matrix[i], 'float');
563 }
564 has_grip_pose = true;
565 }
566 }
567 GodotRuntime.setHeapValue(r_has_grip_pose, has_grip_pose ? 1 : 0, 'i32');
568
569 // Gamepad data (mapping, buttons and axes).
570 let has_standard_mapping = false;
571 let button_count = 0;
572 let axes_count = 0;
573 if (input_source.gamepad) {
574 if (input_source.gamepad.mapping === 'xr-standard') {
575 has_standard_mapping = true;
576 }
577
578 button_count = Math.min(input_source.gamepad.buttons.length, 10);
579 for (let i = 0; i < button_count; i++) {
580 GodotRuntime.setHeapValue(r_buttons + (i * 4), input_source.gamepad.buttons[i].value, 'float');
581 }
582
583 axes_count = Math.min(input_source.gamepad.axes.length, 10);
584 for (let i = 0; i < axes_count; i++) {
585 GodotRuntime.setHeapValue(r_axes + (i * 4), input_source.gamepad.axes[i], 'float');
586 }
587 }
588 GodotRuntime.setHeapValue(r_has_standard_mapping, has_standard_mapping ? 1 : 0, 'i32');
589 GodotRuntime.setHeapValue(r_button_count, button_count, 'i32');
590 GodotRuntime.setHeapValue(r_axes_count, axes_count, 'i32');
591
592 // Hand tracking data.
593 let has_hand_data = false;
594 if (input_source.hand && r_hand_joints !== 0 && r_hand_radii !== 0) {
595 const hand_joint_array = new Float32Array(25 * 16);
596 const hand_radii_array = new Float32Array(25);
597 if (frame.fillPoses(input_source.hand.values(), space, hand_joint_array) && frame.fillJointRadii(input_source.hand.values(), hand_radii_array)) {
598 GodotRuntime.heapCopy(HEAPF32, hand_joint_array, r_hand_joints);
599 GodotRuntime.heapCopy(HEAPF32, hand_radii_array, r_hand_radii);
600 has_hand_data = true;
601 }
602 }
603 GodotRuntime.setHeapValue(r_has_hand_data, has_hand_data ? 1 : 0, 'i32');
604
605 return true;
606 },
607
608 godot_webxr_get_visibility_state__proxy: 'sync',
609 godot_webxr_get_visibility_state__sig: 'i',
610 godot_webxr_get_visibility_state: function () {
611 if (!GodotWebXR.session || !GodotWebXR.session.visibilityState) {
612 return 0;
613 }
614
615 return GodotRuntime.allocString(GodotWebXR.session.visibilityState);
616 },
617
618 godot_webxr_get_bounds_geometry__proxy: 'sync',
619 godot_webxr_get_bounds_geometry__sig: 'ii',
620 godot_webxr_get_bounds_geometry: function (r_points) {
621 if (!GodotWebXR.space || !GodotWebXR.space.boundsGeometry) {
622 return 0;
623 }
624
625 const point_count = GodotWebXR.space.boundsGeometry.length;
626 if (point_count === 0) {
627 return 0;
628 }
629
630 const buf = GodotRuntime.malloc(point_count * 3 * 4);
631 for (let i = 0; i < point_count; i++) {
632 const point = GodotWebXR.space.boundsGeometry[i];
633 GodotRuntime.setHeapValue(buf + ((i * 3) + 0) * 4, point.x, 'float');
634 GodotRuntime.setHeapValue(buf + ((i * 3) + 1) * 4, point.y, 'float');
635 GodotRuntime.setHeapValue(buf + ((i * 3) + 2) * 4, point.z, 'float');
636 }
637 GodotRuntime.setHeapValue(r_points, buf, 'i32');
638
639 return point_count;
640 },
641
642 godot_webxr_get_frame_rate__proxy: 'sync',
643 godot_webxr_get_frame_rate__sig: 'i',
644 godot_webxr_get_frame_rate: function () {
645 if (!GodotWebXR.session || GodotWebXR.session.frameRate === undefined) {
646 return 0;
647 }
648 return GodotWebXR.session.frameRate;
649 },
650
651 godot_webxr_update_target_frame_rate__proxy: 'sync',
652 godot_webxr_update_target_frame_rate__sig: 'vi',
653 godot_webxr_update_target_frame_rate: function (p_frame_rate) {
654 if (!GodotWebXR.session || GodotWebXR.session.updateTargetFrameRate === undefined) {
655 return;
656 }
657
658 GodotWebXR.session.updateTargetFrameRate(p_frame_rate).then(() => {
659 const c_str = GodotRuntime.allocString('display_refresh_rate_changed');
660 GodotWebXR.onsimpleevent(c_str);
661 GodotRuntime.free(c_str);
662 });
663 },
664
665 godot_webxr_get_supported_frame_rates__proxy: 'sync',
666 godot_webxr_get_supported_frame_rates__sig: 'ii',
667 godot_webxr_get_supported_frame_rates: function (r_frame_rates) {
668 if (!GodotWebXR.session || GodotWebXR.session.supportedFrameRates === undefined) {
669 return 0;
670 }
671
672 const frame_rate_count = GodotWebXR.session.supportedFrameRates.length;
673 if (frame_rate_count === 0) {
674 return 0;
675 }
676
677 const buf = GodotRuntime.malloc(frame_rate_count * 4);
678 for (let i = 0; i < frame_rate_count; i++) {
679 GodotRuntime.setHeapValue(buf + (i * 4), GodotWebXR.session.supportedFrameRates[i], 'float');
680 }
681 GodotRuntime.setHeapValue(r_frame_rates, buf, 'i32');
682
683 return frame_rate_count;
684 },
685
686 };
687
688 autoAddDeps(GodotWebXR, '$GodotWebXR');
689 mergeInto(LibraryManager.library, GodotWebXR);