library_godot_webxr.js 20 KB

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