library_godot_webxr.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671
  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. const GodotWebXR = {
  31. $GodotWebXR__deps: ['$Browser', '$GL', '$GodotRuntime', '$runtimeKeepalivePush', '$runtimeKeepalivePop'],
  32. $GodotWebXR: {
  33. gl: null,
  34. session: null,
  35. gl_binding: null,
  36. layer: null,
  37. space: null,
  38. frame: null,
  39. pose: null,
  40. view_count: 1,
  41. input_sources: new Array(16),
  42. touches: new Array(5),
  43. onsimpleevent: null,
  44. // Monkey-patch the requestAnimationFrame() used by Emscripten for the main
  45. // loop, so that we can swap it out for XRSession.requestAnimationFrame()
  46. // when an XR session is started.
  47. orig_requestAnimationFrame: null,
  48. requestAnimationFrame: (callback) => {
  49. if (GodotWebXR.session && GodotWebXR.space) {
  50. const onFrame = function (time, frame) {
  51. GodotWebXR.frame = frame;
  52. GodotWebXR.pose = frame.getViewerPose(GodotWebXR.space);
  53. callback(time);
  54. GodotWebXR.frame = null;
  55. GodotWebXR.pose = null;
  56. };
  57. GodotWebXR.session.requestAnimationFrame(onFrame);
  58. } else {
  59. GodotWebXR.orig_requestAnimationFrame(callback);
  60. }
  61. },
  62. monkeyPatchRequestAnimationFrame: (enable) => {
  63. if (GodotWebXR.orig_requestAnimationFrame === null) {
  64. GodotWebXR.orig_requestAnimationFrame = Browser.requestAnimationFrame;
  65. }
  66. Browser.requestAnimationFrame = enable
  67. ? GodotWebXR.requestAnimationFrame
  68. : GodotWebXR.orig_requestAnimationFrame;
  69. },
  70. pauseResumeMainLoop: () => {
  71. // Once both GodotWebXR.session and GodotWebXR.space are set or
  72. // unset, our monkey-patched requestAnimationFrame() should be
  73. // enabled or disabled. When using the WebXR API Emulator, this
  74. // gets picked up automatically, however, in the Oculus Browser
  75. // on the Quest, we need to pause and resume the main loop.
  76. Browser.mainLoop.pause();
  77. runtimeKeepalivePush();
  78. window.setTimeout(function () {
  79. runtimeKeepalivePop();
  80. Browser.mainLoop.resume();
  81. }, 0);
  82. },
  83. getLayer: () => {
  84. const new_view_count = (GodotWebXR.pose) ? GodotWebXR.pose.views.length : 1;
  85. let layer = GodotWebXR.layer;
  86. // If the view count hasn't changed since creating this layer, then
  87. // we can simply return it.
  88. if (layer && GodotWebXR.view_count === new_view_count) {
  89. return layer;
  90. }
  91. if (!GodotWebXR.session || !GodotWebXR.gl_binding) {
  92. return null;
  93. }
  94. const gl = GodotWebXR.gl;
  95. layer = GodotWebXR.gl_binding.createProjectionLayer({
  96. textureType: new_view_count > 1 ? 'texture-array' : 'texture',
  97. colorFormat: gl.RGBA8,
  98. depthFormat: gl.DEPTH_COMPONENT24,
  99. });
  100. GodotWebXR.session.updateRenderState({ layers: [layer] });
  101. GodotWebXR.layer = layer;
  102. GodotWebXR.view_count = new_view_count;
  103. return layer;
  104. },
  105. getSubImage: () => {
  106. if (!GodotWebXR.pose) {
  107. return null;
  108. }
  109. const layer = GodotWebXR.getLayer();
  110. if (layer === null) {
  111. return null;
  112. }
  113. // Because we always use "texture-array" for multiview and "texture"
  114. // when there is only 1 view, it should be safe to only grab the
  115. // subimage for the first view.
  116. return GodotWebXR.gl_binding.getViewSubImage(layer, GodotWebXR.pose.views[0]);
  117. },
  118. getTextureId: (texture) => {
  119. if (texture.name !== undefined) {
  120. return texture.name;
  121. }
  122. const id = GL.getNewId(GL.textures);
  123. texture.name = id;
  124. GL.textures[id] = texture;
  125. return id;
  126. },
  127. addInputSource: (input_source) => {
  128. let name = -1;
  129. if (input_source.targetRayMode === 'tracked-pointer' && input_source.handedness === 'left') {
  130. name = 0;
  131. } else if (input_source.targetRayMode === 'tracked-pointer' && input_source.handedness === 'right') {
  132. name = 1;
  133. } else {
  134. for (let i = 2; i < 16; i++) {
  135. if (!GodotWebXR.input_sources[i]) {
  136. name = i;
  137. break;
  138. }
  139. }
  140. }
  141. if (name >= 0) {
  142. GodotWebXR.input_sources[name] = input_source;
  143. input_source.name = name;
  144. // Find a free touch index for screen sources.
  145. if (input_source.targetRayMode === 'screen') {
  146. let touch_index = -1;
  147. for (let i = 0; i < 5; i++) {
  148. if (!GodotWebXR.touches[i]) {
  149. touch_index = i;
  150. break;
  151. }
  152. }
  153. if (touch_index >= 0) {
  154. GodotWebXR.touches[touch_index] = input_source;
  155. input_source.touch_index = touch_index;
  156. }
  157. }
  158. }
  159. return name;
  160. },
  161. removeInputSource: (input_source) => {
  162. if (input_source.name !== undefined) {
  163. const name = input_source.name;
  164. if (name >= 0 && name < 16) {
  165. GodotWebXR.input_sources[name] = null;
  166. }
  167. if (input_source.touch_index !== undefined) {
  168. const touch_index = input_source.touch_index;
  169. if (touch_index >= 0 && touch_index < 5) {
  170. GodotWebXR.touches[touch_index] = null;
  171. }
  172. }
  173. return name;
  174. }
  175. return -1;
  176. },
  177. getInputSourceId: (input_source) => {
  178. if (input_source !== undefined) {
  179. return input_source.name;
  180. }
  181. return -1;
  182. },
  183. getTouchIndex: (input_source) => {
  184. if (input_source.touch_index !== undefined) {
  185. return input_source.touch_index;
  186. }
  187. return -1;
  188. },
  189. },
  190. godot_webxr_is_supported__proxy: 'sync',
  191. godot_webxr_is_supported__sig: 'i',
  192. godot_webxr_is_supported: function () {
  193. return !!navigator.xr;
  194. },
  195. godot_webxr_is_session_supported__proxy: 'sync',
  196. godot_webxr_is_session_supported__sig: 'vii',
  197. godot_webxr_is_session_supported: function (p_session_mode, p_callback) {
  198. const session_mode = GodotRuntime.parseString(p_session_mode);
  199. const cb = GodotRuntime.get_func(p_callback);
  200. if (navigator.xr) {
  201. navigator.xr.isSessionSupported(session_mode).then(function (supported) {
  202. const c_str = GodotRuntime.allocString(session_mode);
  203. cb(c_str, supported ? 1 : 0);
  204. GodotRuntime.free(c_str);
  205. });
  206. } else {
  207. const c_str = GodotRuntime.allocString(session_mode);
  208. cb(c_str, 0);
  209. GodotRuntime.free(c_str);
  210. }
  211. },
  212. godot_webxr_initialize__deps: ['emscripten_webgl_get_current_context'],
  213. godot_webxr_initialize__proxy: 'sync',
  214. godot_webxr_initialize__sig: 'viiiiiiiii',
  215. 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) {
  216. GodotWebXR.monkeyPatchRequestAnimationFrame(true);
  217. const session_mode = GodotRuntime.parseString(p_session_mode);
  218. const required_features = GodotRuntime.parseString(p_required_features).split(',').map((s) => s.trim()).filter((s) => s !== '');
  219. const optional_features = GodotRuntime.parseString(p_optional_features).split(',').map((s) => s.trim()).filter((s) => s !== '');
  220. const requested_reference_space_types = GodotRuntime.parseString(p_requested_reference_spaces).split(',').map((s) => s.trim());
  221. const onstarted = GodotRuntime.get_func(p_on_session_started);
  222. const onended = GodotRuntime.get_func(p_on_session_ended);
  223. const onfailed = GodotRuntime.get_func(p_on_session_failed);
  224. const oninputevent = GodotRuntime.get_func(p_on_input_event);
  225. const onsimpleevent = GodotRuntime.get_func(p_on_simple_event);
  226. const session_init = {};
  227. if (required_features.length > 0) {
  228. session_init['requiredFeatures'] = required_features;
  229. }
  230. if (optional_features.length > 0) {
  231. session_init['optionalFeatures'] = optional_features;
  232. }
  233. navigator.xr.requestSession(session_mode, session_init).then(function (session) {
  234. GodotWebXR.session = session;
  235. session.addEventListener('end', function (evt) {
  236. onended();
  237. });
  238. session.addEventListener('inputsourceschange', function (evt) {
  239. evt.added.forEach(GodotWebXR.addInputSource);
  240. evt.removed.forEach(GodotWebXR.removeInputSource);
  241. });
  242. ['selectstart', 'selectend', 'squeezestart', 'squeezeend'].forEach((input_event, index) => {
  243. session.addEventListener(input_event, function (evt) {
  244. // Since this happens in-between normal frames, we need to
  245. // grab the frame from the event in order to get poses for
  246. // the input sources.
  247. GodotWebXR.frame = evt.frame;
  248. oninputevent(index, GodotWebXR.getInputSourceId(evt.inputSource));
  249. GodotWebXR.frame = null;
  250. });
  251. });
  252. session.addEventListener('visibilitychange', function (evt) {
  253. const c_str = GodotRuntime.allocString('visibility_state_changed');
  254. onsimpleevent(c_str);
  255. GodotRuntime.free(c_str);
  256. });
  257. // Store onsimpleevent so we can use it later.
  258. GodotWebXR.onsimpleevent = onsimpleevent;
  259. const gl_context_handle = _emscripten_webgl_get_current_context();
  260. const gl = GL.getContext(gl_context_handle).GLctx;
  261. GodotWebXR.gl = gl;
  262. gl.makeXRCompatible().then(function () {
  263. GodotWebXR.gl_binding = new XRWebGLBinding(session, gl);
  264. // This will trigger the layer to get created.
  265. GodotWebXR.getLayer();
  266. function onReferenceSpaceSuccess(reference_space, reference_space_type) {
  267. GodotWebXR.space = reference_space;
  268. // Using reference_space.addEventListener() crashes when
  269. // using the polyfill with the WebXR Emulator extension,
  270. // so we set the event property instead.
  271. reference_space.onreset = function (evt) {
  272. const c_str = GodotRuntime.allocString('reference_space_reset');
  273. onsimpleevent(c_str);
  274. GodotRuntime.free(c_str);
  275. };
  276. // Now that both GodotWebXR.session and GodotWebXR.space are
  277. // set, we need to pause and resume the main loop for the XR
  278. // main loop to kick in.
  279. GodotWebXR.pauseResumeMainLoop();
  280. // Call in setTimeout() so that errors in the onstarted()
  281. // callback don't bubble up here and cause Godot to try the
  282. // next reference space.
  283. window.setTimeout(function () {
  284. const reference_space_c_str = GodotRuntime.allocString(reference_space_type);
  285. const enabled_features = 'enabledFeatures' in session ? Array.from(session.enabledFeatures) : [];
  286. const enabled_features_c_str = GodotRuntime.allocString(enabled_features.join(','));
  287. const environment_blend_mode = 'environmentBlendMode' in session ? session.environmentBlendMode : '';
  288. const environment_blend_mode_c_str = GodotRuntime.allocString(environment_blend_mode);
  289. onstarted(reference_space_c_str, enabled_features_c_str, environment_blend_mode_c_str);
  290. GodotRuntime.free(reference_space_c_str);
  291. GodotRuntime.free(enabled_features_c_str);
  292. GodotRuntime.free(environment_blend_mode_c_str);
  293. }, 0);
  294. }
  295. function requestReferenceSpace() {
  296. const reference_space_type = requested_reference_space_types.shift();
  297. session.requestReferenceSpace(reference_space_type)
  298. .then((refSpace) => {
  299. onReferenceSpaceSuccess(refSpace, reference_space_type);
  300. })
  301. .catch(() => {
  302. if (requested_reference_space_types.length === 0) {
  303. const c_str = GodotRuntime.allocString('Unable to get any of the requested reference space types');
  304. onfailed(c_str);
  305. GodotRuntime.free(c_str);
  306. } else {
  307. requestReferenceSpace();
  308. }
  309. });
  310. }
  311. requestReferenceSpace();
  312. }).catch(function (error) {
  313. const c_str = GodotRuntime.allocString(`Unable to make WebGL context compatible with WebXR: ${error}`);
  314. onfailed(c_str);
  315. GodotRuntime.free(c_str);
  316. });
  317. }).catch(function (error) {
  318. const c_str = GodotRuntime.allocString(`Unable to start session: ${error}`);
  319. onfailed(c_str);
  320. GodotRuntime.free(c_str);
  321. });
  322. },
  323. godot_webxr_uninitialize__proxy: 'sync',
  324. godot_webxr_uninitialize__sig: 'v',
  325. godot_webxr_uninitialize: function () {
  326. if (GodotWebXR.session) {
  327. GodotWebXR.session.end()
  328. // Prevent exception when session has already ended.
  329. .catch((e) => { });
  330. }
  331. GodotWebXR.session = null;
  332. GodotWebXR.gl_binding = null;
  333. GodotWebXR.layer = null;
  334. GodotWebXR.space = null;
  335. GodotWebXR.frame = null;
  336. GodotWebXR.pose = null;
  337. GodotWebXR.view_count = 1;
  338. GodotWebXR.input_sources = new Array(16);
  339. GodotWebXR.touches = new Array(5);
  340. GodotWebXR.onsimpleevent = null;
  341. // Disable the monkey-patched window.requestAnimationFrame() and
  342. // pause/restart the main loop to activate it on all platforms.
  343. GodotWebXR.monkeyPatchRequestAnimationFrame(false);
  344. GodotWebXR.pauseResumeMainLoop();
  345. },
  346. godot_webxr_get_view_count__proxy: 'sync',
  347. godot_webxr_get_view_count__sig: 'i',
  348. godot_webxr_get_view_count: function () {
  349. if (!GodotWebXR.session || !GodotWebXR.pose) {
  350. return 1;
  351. }
  352. const view_count = GodotWebXR.pose.views.length;
  353. return view_count > 0 ? view_count : 1;
  354. },
  355. godot_webxr_get_render_target_size__proxy: 'sync',
  356. godot_webxr_get_render_target_size__sig: 'ii',
  357. godot_webxr_get_render_target_size: function (r_size) {
  358. const subimage = GodotWebXR.getSubImage();
  359. if (subimage === null) {
  360. return false;
  361. }
  362. GodotRuntime.setHeapValue(r_size + 0, subimage.viewport.width, 'i32');
  363. GodotRuntime.setHeapValue(r_size + 4, subimage.viewport.height, 'i32');
  364. return true;
  365. },
  366. godot_webxr_get_transform_for_view__proxy: 'sync',
  367. godot_webxr_get_transform_for_view__sig: 'iii',
  368. godot_webxr_get_transform_for_view: function (p_view, r_transform) {
  369. if (!GodotWebXR.session || !GodotWebXR.pose) {
  370. return false;
  371. }
  372. const views = GodotWebXR.pose.views;
  373. let matrix;
  374. if (p_view >= 0) {
  375. matrix = views[p_view].transform.matrix;
  376. } else {
  377. // For -1 (or any other negative value) return the HMD transform.
  378. matrix = GodotWebXR.pose.transform.matrix;
  379. }
  380. for (let i = 0; i < 16; i++) {
  381. GodotRuntime.setHeapValue(r_transform + (i * 4), matrix[i], 'float');
  382. }
  383. return true;
  384. },
  385. godot_webxr_get_projection_for_view__proxy: 'sync',
  386. godot_webxr_get_projection_for_view__sig: 'iii',
  387. godot_webxr_get_projection_for_view: function (p_view, r_transform) {
  388. if (!GodotWebXR.session || !GodotWebXR.pose) {
  389. return false;
  390. }
  391. const matrix = GodotWebXR.pose.views[p_view].projectionMatrix;
  392. for (let i = 0; i < 16; i++) {
  393. GodotRuntime.setHeapValue(r_transform + (i * 4), matrix[i], 'float');
  394. }
  395. return true;
  396. },
  397. godot_webxr_get_color_texture__proxy: 'sync',
  398. godot_webxr_get_color_texture__sig: 'i',
  399. godot_webxr_get_color_texture: function () {
  400. const subimage = GodotWebXR.getSubImage();
  401. if (subimage === null) {
  402. return 0;
  403. }
  404. return GodotWebXR.getTextureId(subimage.colorTexture);
  405. },
  406. godot_webxr_get_depth_texture__proxy: 'sync',
  407. godot_webxr_get_depth_texture__sig: 'i',
  408. godot_webxr_get_depth_texture: function () {
  409. const subimage = GodotWebXR.getSubImage();
  410. if (subimage === null) {
  411. return 0;
  412. }
  413. if (!subimage.depthStencilTexture) {
  414. return 0;
  415. }
  416. return GodotWebXR.getTextureId(subimage.depthStencilTexture);
  417. },
  418. godot_webxr_get_velocity_texture__proxy: 'sync',
  419. godot_webxr_get_velocity_texture__sig: 'i',
  420. godot_webxr_get_velocity_texture: function () {
  421. const subimage = GodotWebXR.getSubImage();
  422. if (subimage === null) {
  423. return 0;
  424. }
  425. if (!subimage.motionVectorTexture) {
  426. return 0;
  427. }
  428. return GodotWebXR.getTextureId(subimage.motionVectorTexture);
  429. },
  430. godot_webxr_update_input_source__proxy: 'sync',
  431. godot_webxr_update_input_source__sig: 'iiiiiiiiiiiiiii',
  432. 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) {
  433. if (!GodotWebXR.session || !GodotWebXR.frame) {
  434. return 0;
  435. }
  436. if (p_input_source_id < 0 || p_input_source_id >= GodotWebXR.input_sources.length || !GodotWebXR.input_sources[p_input_source_id]) {
  437. return false;
  438. }
  439. const input_source = GodotWebXR.input_sources[p_input_source_id];
  440. const frame = GodotWebXR.frame;
  441. const space = GodotWebXR.space;
  442. // Target pose.
  443. const target_pose = frame.getPose(input_source.targetRaySpace, space);
  444. if (!target_pose) {
  445. // This can mean that the controller lost tracking.
  446. return false;
  447. }
  448. const target_pose_matrix = target_pose.transform.matrix;
  449. for (let i = 0; i < 16; i++) {
  450. GodotRuntime.setHeapValue(r_target_pose + (i * 4), target_pose_matrix[i], 'float');
  451. }
  452. // Target ray mode.
  453. let target_ray_mode = 0;
  454. switch (input_source.targetRayMode) {
  455. case 'gaze':
  456. target_ray_mode = 1;
  457. break;
  458. case 'tracked-pointer':
  459. target_ray_mode = 2;
  460. break;
  461. case 'screen':
  462. target_ray_mode = 3;
  463. break;
  464. default:
  465. }
  466. GodotRuntime.setHeapValue(r_target_ray_mode, target_ray_mode, 'i32');
  467. // Touch index.
  468. GodotRuntime.setHeapValue(r_touch_index, GodotWebXR.getTouchIndex(input_source), 'i32');
  469. // Grip pose.
  470. let has_grip_pose = false;
  471. if (input_source.gripSpace) {
  472. const grip_pose = frame.getPose(input_source.gripSpace, space);
  473. if (grip_pose) {
  474. const grip_pose_matrix = grip_pose.transform.matrix;
  475. for (let i = 0; i < 16; i++) {
  476. GodotRuntime.setHeapValue(r_grip_pose + (i * 4), grip_pose_matrix[i], 'float');
  477. }
  478. has_grip_pose = true;
  479. }
  480. }
  481. GodotRuntime.setHeapValue(r_has_grip_pose, has_grip_pose ? 1 : 0, 'i32');
  482. // Gamepad data (mapping, buttons and axes).
  483. let has_standard_mapping = false;
  484. let button_count = 0;
  485. let axes_count = 0;
  486. if (input_source.gamepad) {
  487. if (input_source.gamepad.mapping === 'xr-standard') {
  488. has_standard_mapping = true;
  489. }
  490. button_count = Math.min(input_source.gamepad.buttons.length, 10);
  491. for (let i = 0; i < button_count; i++) {
  492. GodotRuntime.setHeapValue(r_buttons + (i * 4), input_source.gamepad.buttons[i].value, 'float');
  493. }
  494. axes_count = Math.min(input_source.gamepad.axes.length, 10);
  495. for (let i = 0; i < axes_count; i++) {
  496. GodotRuntime.setHeapValue(r_axes + (i * 4), input_source.gamepad.axes[i], 'float');
  497. }
  498. }
  499. GodotRuntime.setHeapValue(r_has_standard_mapping, has_standard_mapping ? 1 : 0, 'i32');
  500. GodotRuntime.setHeapValue(r_button_count, button_count, 'i32');
  501. GodotRuntime.setHeapValue(r_axes_count, axes_count, 'i32');
  502. // Hand tracking data.
  503. let has_hand_data = false;
  504. if (input_source.hand && r_hand_joints !== 0 && r_hand_radii !== 0) {
  505. const hand_joint_array = new Float32Array(25 * 16);
  506. const hand_radii_array = new Float32Array(25);
  507. if (frame.fillPoses(input_source.hand.values(), space, hand_joint_array) && frame.fillJointRadii(input_source.hand.values(), hand_radii_array)) {
  508. GodotRuntime.heapCopy(HEAPF32, hand_joint_array, r_hand_joints);
  509. GodotRuntime.heapCopy(HEAPF32, hand_radii_array, r_hand_radii);
  510. has_hand_data = true;
  511. }
  512. }
  513. GodotRuntime.setHeapValue(r_has_hand_data, has_hand_data ? 1 : 0, 'i32');
  514. return true;
  515. },
  516. godot_webxr_get_visibility_state__proxy: 'sync',
  517. godot_webxr_get_visibility_state__sig: 'i',
  518. godot_webxr_get_visibility_state: function () {
  519. if (!GodotWebXR.session || !GodotWebXR.session.visibilityState) {
  520. return 0;
  521. }
  522. return GodotRuntime.allocString(GodotWebXR.session.visibilityState);
  523. },
  524. godot_webxr_get_bounds_geometry__proxy: 'sync',
  525. godot_webxr_get_bounds_geometry__sig: 'ii',
  526. godot_webxr_get_bounds_geometry: function (r_points) {
  527. if (!GodotWebXR.space || !GodotWebXR.space.boundsGeometry) {
  528. return 0;
  529. }
  530. const point_count = GodotWebXR.space.boundsGeometry.length;
  531. if (point_count === 0) {
  532. return 0;
  533. }
  534. const buf = GodotRuntime.malloc(point_count * 3 * 4);
  535. for (let i = 0; i < point_count; i++) {
  536. const point = GodotWebXR.space.boundsGeometry[i];
  537. GodotRuntime.setHeapValue(buf + ((i * 3) + 0) * 4, point.x, 'float');
  538. GodotRuntime.setHeapValue(buf + ((i * 3) + 1) * 4, point.y, 'float');
  539. GodotRuntime.setHeapValue(buf + ((i * 3) + 2) * 4, point.z, 'float');
  540. }
  541. GodotRuntime.setHeapValue(r_points, buf, 'i32');
  542. return point_count;
  543. },
  544. godot_webxr_get_frame_rate__proxy: 'sync',
  545. godot_webxr_get_frame_rate__sig: 'i',
  546. godot_webxr_get_frame_rate: function () {
  547. if (!GodotWebXR.session || GodotWebXR.session.frameRate === undefined) {
  548. return 0;
  549. }
  550. return GodotWebXR.session.frameRate;
  551. },
  552. godot_webxr_update_target_frame_rate__proxy: 'sync',
  553. godot_webxr_update_target_frame_rate__sig: 'vi',
  554. godot_webxr_update_target_frame_rate: function (p_frame_rate) {
  555. if (!GodotWebXR.session || GodotWebXR.session.updateTargetFrameRate === undefined) {
  556. return;
  557. }
  558. GodotWebXR.session.updateTargetFrameRate(p_frame_rate).then(() => {
  559. const c_str = GodotRuntime.allocString('display_refresh_rate_changed');
  560. GodotWebXR.onsimpleevent(c_str);
  561. GodotRuntime.free(c_str);
  562. });
  563. },
  564. godot_webxr_get_supported_frame_rates__proxy: 'sync',
  565. godot_webxr_get_supported_frame_rates__sig: 'ii',
  566. godot_webxr_get_supported_frame_rates: function (r_frame_rates) {
  567. if (!GodotWebXR.session || GodotWebXR.session.supportedFrameRates === undefined) {
  568. return 0;
  569. }
  570. const frame_rate_count = GodotWebXR.session.supportedFrameRates.length;
  571. if (frame_rate_count === 0) {
  572. return 0;
  573. }
  574. const buf = GodotRuntime.malloc(frame_rate_count * 4);
  575. for (let i = 0; i < frame_rate_count; i++) {
  576. GodotRuntime.setHeapValue(buf + (i * 4), GodotWebXR.session.supportedFrameRates[i], 'float');
  577. }
  578. GodotRuntime.setHeapValue(r_frame_rates, buf, 'i32');
  579. return frame_rate_count;
  580. },
  581. };
  582. autoAddDeps(GodotWebXR, '$GodotWebXR');
  583. mergeInto(LibraryManager.library, GodotWebXR);