webxr_interface_js.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  1. /**************************************************************************/
  2. /* webxr_interface_js.cpp */
  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. #ifdef WEB_ENABLED
  31. #include "webxr_interface_js.h"
  32. #include "core/input/input.h"
  33. #include "core/os/os.h"
  34. #include "drivers/gles3/storage/texture_storage.h"
  35. #include "emscripten.h"
  36. #include "godot_webxr.h"
  37. #include "scene/main/scene_tree.h"
  38. #include "scene/main/window.h"
  39. #include "servers/rendering/renderer_compositor.h"
  40. #include "servers/rendering/rendering_server_globals.h"
  41. #include <stdlib.h>
  42. void _emwebxr_on_session_supported(char *p_session_mode, int p_supported) {
  43. XRServer *xr_server = XRServer::get_singleton();
  44. ERR_FAIL_NULL(xr_server);
  45. Ref<XRInterface> interface = xr_server->find_interface("WebXR");
  46. ERR_FAIL_COND(interface.is_null());
  47. String session_mode = String(p_session_mode);
  48. interface->emit_signal(SNAME("session_supported"), session_mode, p_supported ? true : false);
  49. }
  50. void _emwebxr_on_session_started(char *p_reference_space_type) {
  51. XRServer *xr_server = XRServer::get_singleton();
  52. ERR_FAIL_NULL(xr_server);
  53. Ref<XRInterface> interface = xr_server->find_interface("WebXR");
  54. ERR_FAIL_COND(interface.is_null());
  55. String reference_space_type = String(p_reference_space_type);
  56. static_cast<WebXRInterfaceJS *>(interface.ptr())->_set_reference_space_type(reference_space_type);
  57. interface->emit_signal(SNAME("session_started"));
  58. }
  59. void _emwebxr_on_session_ended() {
  60. XRServer *xr_server = XRServer::get_singleton();
  61. ERR_FAIL_NULL(xr_server);
  62. Ref<XRInterface> interface = xr_server->find_interface("WebXR");
  63. ERR_FAIL_COND(interface.is_null());
  64. interface->uninitialize();
  65. interface->emit_signal(SNAME("session_ended"));
  66. }
  67. void _emwebxr_on_session_failed(char *p_message) {
  68. XRServer *xr_server = XRServer::get_singleton();
  69. ERR_FAIL_NULL(xr_server);
  70. Ref<XRInterface> interface = xr_server->find_interface("WebXR");
  71. ERR_FAIL_COND(interface.is_null());
  72. interface->uninitialize();
  73. String message = String(p_message);
  74. interface->emit_signal(SNAME("session_failed"), message);
  75. }
  76. extern "C" EMSCRIPTEN_KEEPALIVE void _emwebxr_on_input_event(int p_event_type, int p_input_source_id) {
  77. XRServer *xr_server = XRServer::get_singleton();
  78. ERR_FAIL_NULL(xr_server);
  79. Ref<XRInterface> interface = xr_server->find_interface("WebXR");
  80. ERR_FAIL_COND(interface.is_null());
  81. ((WebXRInterfaceJS *)interface.ptr())->_on_input_event(p_event_type, p_input_source_id);
  82. }
  83. extern "C" EMSCRIPTEN_KEEPALIVE void _emwebxr_on_simple_event(char *p_signal_name) {
  84. XRServer *xr_server = XRServer::get_singleton();
  85. ERR_FAIL_NULL(xr_server);
  86. Ref<XRInterface> interface = xr_server->find_interface("WebXR");
  87. ERR_FAIL_COND(interface.is_null());
  88. StringName signal_name = StringName(p_signal_name);
  89. interface->emit_signal(signal_name);
  90. }
  91. void WebXRInterfaceJS::is_session_supported(const String &p_session_mode) {
  92. godot_webxr_is_session_supported(p_session_mode.utf8().get_data(), &_emwebxr_on_session_supported);
  93. }
  94. void WebXRInterfaceJS::set_session_mode(String p_session_mode) {
  95. session_mode = p_session_mode;
  96. }
  97. String WebXRInterfaceJS::get_session_mode() const {
  98. return session_mode;
  99. }
  100. void WebXRInterfaceJS::set_required_features(String p_required_features) {
  101. required_features = p_required_features;
  102. }
  103. String WebXRInterfaceJS::get_required_features() const {
  104. return required_features;
  105. }
  106. void WebXRInterfaceJS::set_optional_features(String p_optional_features) {
  107. optional_features = p_optional_features;
  108. }
  109. String WebXRInterfaceJS::get_optional_features() const {
  110. return optional_features;
  111. }
  112. void WebXRInterfaceJS::set_requested_reference_space_types(String p_requested_reference_space_types) {
  113. requested_reference_space_types = p_requested_reference_space_types;
  114. }
  115. String WebXRInterfaceJS::get_requested_reference_space_types() const {
  116. return requested_reference_space_types;
  117. }
  118. void WebXRInterfaceJS::_set_reference_space_type(String p_reference_space_type) {
  119. reference_space_type = p_reference_space_type;
  120. }
  121. String WebXRInterfaceJS::get_reference_space_type() const {
  122. return reference_space_type;
  123. }
  124. bool WebXRInterfaceJS::is_input_source_active(int p_input_source_id) const {
  125. ERR_FAIL_INDEX_V(p_input_source_id, input_source_count, false);
  126. return input_sources[p_input_source_id].active;
  127. }
  128. Ref<XRPositionalTracker> WebXRInterfaceJS::get_input_source_tracker(int p_input_source_id) const {
  129. ERR_FAIL_INDEX_V(p_input_source_id, input_source_count, Ref<XRPositionalTracker>());
  130. return input_sources[p_input_source_id].tracker;
  131. }
  132. WebXRInterface::TargetRayMode WebXRInterfaceJS::get_input_source_target_ray_mode(int p_input_source_id) const {
  133. ERR_FAIL_INDEX_V(p_input_source_id, input_source_count, WebXRInterface::TARGET_RAY_MODE_UNKNOWN);
  134. if (!input_sources[p_input_source_id].active) {
  135. return WebXRInterface::TARGET_RAY_MODE_UNKNOWN;
  136. }
  137. return input_sources[p_input_source_id].target_ray_mode;
  138. }
  139. String WebXRInterfaceJS::get_visibility_state() const {
  140. char *c_str = godot_webxr_get_visibility_state();
  141. if (c_str) {
  142. String visibility_state = String(c_str);
  143. free(c_str);
  144. return visibility_state;
  145. }
  146. return String();
  147. }
  148. PackedVector3Array WebXRInterfaceJS::get_play_area() const {
  149. PackedVector3Array ret;
  150. float *points;
  151. int point_count = godot_webxr_get_bounds_geometry(&points);
  152. if (point_count > 0) {
  153. ret.resize(point_count);
  154. for (int i = 0; i < point_count; i++) {
  155. float *js_vector3 = points + (i * 3);
  156. ret.set(i, Vector3(js_vector3[0], js_vector3[1], js_vector3[2]));
  157. }
  158. free(points);
  159. }
  160. return ret;
  161. }
  162. StringName WebXRInterfaceJS::get_name() const {
  163. return "WebXR";
  164. };
  165. uint32_t WebXRInterfaceJS::get_capabilities() const {
  166. return XRInterface::XR_STEREO | XRInterface::XR_MONO | XRInterface::XR_VR | XRInterface::XR_AR;
  167. };
  168. uint32_t WebXRInterfaceJS::get_view_count() {
  169. return godot_webxr_get_view_count();
  170. };
  171. bool WebXRInterfaceJS::is_initialized() const {
  172. return (initialized);
  173. };
  174. bool WebXRInterfaceJS::initialize() {
  175. XRServer *xr_server = XRServer::get_singleton();
  176. ERR_FAIL_NULL_V(xr_server, false);
  177. if (!initialized) {
  178. if (!godot_webxr_is_supported()) {
  179. return false;
  180. }
  181. if (requested_reference_space_types.size() == 0) {
  182. return false;
  183. }
  184. // we must create a tracker for our head
  185. head_transform.basis = Basis();
  186. head_transform.origin = Vector3();
  187. head_tracker.instantiate();
  188. head_tracker->set_tracker_type(XRServer::TRACKER_HEAD);
  189. head_tracker->set_tracker_name("head");
  190. head_tracker->set_tracker_desc("Players head");
  191. xr_server->add_tracker(head_tracker);
  192. // make this our primary interface
  193. xr_server->set_primary_interface(this);
  194. // Clear render_targetsize to make sure it gets reset to the new size.
  195. // Clearing in uninitialize() doesn't work because a frame can still be
  196. // rendered after it's called, which will fill render_targetsize again.
  197. render_targetsize.width = 0;
  198. render_targetsize.height = 0;
  199. initialized = true;
  200. godot_webxr_initialize(
  201. session_mode.utf8().get_data(),
  202. required_features.utf8().get_data(),
  203. optional_features.utf8().get_data(),
  204. requested_reference_space_types.utf8().get_data(),
  205. &_emwebxr_on_session_started,
  206. &_emwebxr_on_session_ended,
  207. &_emwebxr_on_session_failed,
  208. &_emwebxr_on_input_event,
  209. &_emwebxr_on_simple_event);
  210. };
  211. return true;
  212. };
  213. void WebXRInterfaceJS::uninitialize() {
  214. if (initialized) {
  215. XRServer *xr_server = XRServer::get_singleton();
  216. if (xr_server != nullptr) {
  217. if (head_tracker.is_valid()) {
  218. xr_server->remove_tracker(head_tracker);
  219. head_tracker.unref();
  220. }
  221. if (xr_server->get_primary_interface() == this) {
  222. // no longer our primary interface
  223. xr_server->set_primary_interface(nullptr);
  224. }
  225. }
  226. godot_webxr_uninitialize();
  227. GLES3::TextureStorage *texture_storage = dynamic_cast<GLES3::TextureStorage *>(RSG::texture_storage);
  228. if (texture_storage != nullptr) {
  229. for (KeyValue<unsigned int, RID> &E : texture_cache) {
  230. // Forcibly mark as not part of a render target so we can free it.
  231. GLES3::Texture *texture = texture_storage->get_texture(E.value);
  232. texture->is_render_target = false;
  233. texture_storage->texture_free(E.value);
  234. }
  235. }
  236. texture_cache.clear();
  237. reference_space_type = "";
  238. initialized = false;
  239. };
  240. };
  241. Dictionary WebXRInterfaceJS::get_system_info() {
  242. Dictionary dict;
  243. // TODO get actual information from WebXR to return here
  244. dict[SNAME("XRRuntimeName")] = String("WebXR");
  245. dict[SNAME("XRRuntimeVersion")] = String("");
  246. return dict;
  247. }
  248. Transform3D WebXRInterfaceJS::_js_matrix_to_transform(float *p_js_matrix) {
  249. Transform3D transform;
  250. transform.basis.rows[0].x = p_js_matrix[0];
  251. transform.basis.rows[1].x = p_js_matrix[1];
  252. transform.basis.rows[2].x = p_js_matrix[2];
  253. transform.basis.rows[0].y = p_js_matrix[4];
  254. transform.basis.rows[1].y = p_js_matrix[5];
  255. transform.basis.rows[2].y = p_js_matrix[6];
  256. transform.basis.rows[0].z = p_js_matrix[8];
  257. transform.basis.rows[1].z = p_js_matrix[9];
  258. transform.basis.rows[2].z = p_js_matrix[10];
  259. transform.origin.x = p_js_matrix[12];
  260. transform.origin.y = p_js_matrix[13];
  261. transform.origin.z = p_js_matrix[14];
  262. return transform;
  263. }
  264. Size2 WebXRInterfaceJS::get_render_target_size() {
  265. if (render_targetsize.width != 0 && render_targetsize.height != 0) {
  266. return render_targetsize;
  267. }
  268. int js_size[2];
  269. bool has_size = godot_webxr_get_render_target_size(js_size);
  270. if (!initialized || !has_size) {
  271. // As a temporary default (until WebXR is fully initialized), use the
  272. // window size.
  273. return DisplayServer::get_singleton()->window_get_size();
  274. }
  275. render_targetsize.width = (float)js_size[0];
  276. render_targetsize.height = (float)js_size[1];
  277. return render_targetsize;
  278. };
  279. Transform3D WebXRInterfaceJS::get_camera_transform() {
  280. Transform3D camera_transform;
  281. XRServer *xr_server = XRServer::get_singleton();
  282. ERR_FAIL_NULL_V(xr_server, camera_transform);
  283. if (initialized) {
  284. double world_scale = xr_server->get_world_scale();
  285. Transform3D _head_transform = head_transform;
  286. _head_transform.origin *= world_scale;
  287. camera_transform = (xr_server->get_reference_frame()) * _head_transform;
  288. }
  289. return camera_transform;
  290. };
  291. Transform3D WebXRInterfaceJS::get_transform_for_view(uint32_t p_view, const Transform3D &p_cam_transform) {
  292. XRServer *xr_server = XRServer::get_singleton();
  293. ERR_FAIL_NULL_V(xr_server, p_cam_transform);
  294. ERR_FAIL_COND_V(!initialized, p_cam_transform);
  295. float js_matrix[16];
  296. bool has_transform = godot_webxr_get_transform_for_view(p_view, js_matrix);
  297. if (!has_transform) {
  298. return p_cam_transform;
  299. }
  300. Transform3D transform_for_view = _js_matrix_to_transform(js_matrix);
  301. double world_scale = xr_server->get_world_scale();
  302. transform_for_view.origin *= world_scale;
  303. return p_cam_transform * xr_server->get_reference_frame() * transform_for_view;
  304. };
  305. Projection WebXRInterfaceJS::get_projection_for_view(uint32_t p_view, double p_aspect, double p_z_near, double p_z_far) {
  306. Projection view;
  307. ERR_FAIL_COND_V(!initialized, view);
  308. float js_matrix[16];
  309. bool has_projection = godot_webxr_get_projection_for_view(p_view, js_matrix);
  310. if (!has_projection) {
  311. return view;
  312. }
  313. int k = 0;
  314. for (int i = 0; i < 4; i++) {
  315. for (int j = 0; j < 4; j++) {
  316. view.columns[i][j] = js_matrix[k++];
  317. }
  318. }
  319. // Copied from godot_oculus_mobile's ovr_mobile_session.cpp
  320. view.columns[2][2] = -(p_z_far + p_z_near) / (p_z_far - p_z_near);
  321. view.columns[3][2] = -(2.0f * p_z_far * p_z_near) / (p_z_far - p_z_near);
  322. return view;
  323. }
  324. bool WebXRInterfaceJS::pre_draw_viewport(RID p_render_target) {
  325. GLES3::TextureStorage *texture_storage = dynamic_cast<GLES3::TextureStorage *>(RSG::texture_storage);
  326. if (texture_storage == nullptr) {
  327. return false;
  328. }
  329. GLES3::RenderTarget *rt = texture_storage->get_render_target(p_render_target);
  330. if (rt == nullptr) {
  331. return false;
  332. }
  333. // Cache the resources so we don't have to get them from JS twice.
  334. color_texture = _get_color_texture();
  335. depth_texture = _get_depth_texture();
  336. // Per the WebXR spec, it returns "opaque textures" to us, which may be the
  337. // same WebGLTexture object (which would be the same GLuint in C++) but
  338. // represent a different underlying resource (probably the next texture in
  339. // the XR device's swap chain). In order to render to this texture, we need
  340. // to re-attach it to the FBO, otherwise we get an "incomplete FBO" error.
  341. //
  342. // See: https://immersive-web.github.io/layers/#xropaquetextures
  343. //
  344. // This is why we're doing this sort of silly check: if the color and depth
  345. // textures are the same this frame as last frame, we need to attach them
  346. // again, despite the fact that the GLuint for them hasn't changed.
  347. if (rt->overridden.is_overridden && rt->overridden.color == color_texture && rt->overridden.depth == depth_texture) {
  348. GLES3::Config *config = GLES3::Config::get_singleton();
  349. bool use_multiview = rt->view_count > 1 && config->multiview_supported;
  350. glBindFramebuffer(GL_FRAMEBUFFER, rt->fbo);
  351. if (use_multiview) {
  352. glFramebufferTextureMultiviewOVR(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, rt->color, 0, 0, rt->view_count);
  353. glFramebufferTextureMultiviewOVR(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, rt->depth, 0, 0, rt->view_count);
  354. } else {
  355. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, rt->color, 0);
  356. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, rt->depth, 0);
  357. }
  358. glBindFramebuffer(GL_FRAMEBUFFER, texture_storage->system_fbo);
  359. }
  360. return true;
  361. }
  362. Vector<BlitToScreen> WebXRInterfaceJS::post_draw_viewport(RID p_render_target, const Rect2 &p_screen_rect) {
  363. Vector<BlitToScreen> blit_to_screen;
  364. // We don't need to do anything here.
  365. return blit_to_screen;
  366. };
  367. RID WebXRInterfaceJS::_get_color_texture() {
  368. unsigned int texture_id = godot_webxr_get_color_texture();
  369. if (texture_id == 0) {
  370. return RID();
  371. }
  372. return _get_texture(texture_id);
  373. }
  374. RID WebXRInterfaceJS::_get_depth_texture() {
  375. unsigned int texture_id = godot_webxr_get_depth_texture();
  376. if (texture_id == 0) {
  377. return RID();
  378. }
  379. return _get_texture(texture_id);
  380. }
  381. RID WebXRInterfaceJS::_get_texture(unsigned int p_texture_id) {
  382. RBMap<unsigned int, RID>::Element *cache = texture_cache.find(p_texture_id);
  383. if (cache != nullptr) {
  384. return cache->get();
  385. }
  386. GLES3::TextureStorage *texture_storage = dynamic_cast<GLES3::TextureStorage *>(RSG::texture_storage);
  387. if (texture_storage == nullptr) {
  388. return RID();
  389. }
  390. uint32_t view_count = godot_webxr_get_view_count();
  391. Size2 texture_size = get_render_target_size();
  392. RID texture = texture_storage->texture_create_external(
  393. view_count == 1 ? GLES3::Texture::TYPE_2D : GLES3::Texture::TYPE_LAYERED,
  394. Image::FORMAT_RGBA8,
  395. p_texture_id,
  396. (int)texture_size.width,
  397. (int)texture_size.height,
  398. 1,
  399. view_count);
  400. texture_cache.insert(p_texture_id, texture);
  401. return texture;
  402. }
  403. RID WebXRInterfaceJS::get_color_texture() {
  404. return color_texture;
  405. }
  406. RID WebXRInterfaceJS::get_depth_texture() {
  407. return depth_texture;
  408. }
  409. RID WebXRInterfaceJS::get_velocity_texture() {
  410. unsigned int texture_id = godot_webxr_get_velocity_texture();
  411. if (texture_id == 0) {
  412. return RID();
  413. }
  414. return _get_texture(texture_id);
  415. }
  416. void WebXRInterfaceJS::process() {
  417. if (initialized) {
  418. // Get the "head" position.
  419. float js_matrix[16];
  420. if (godot_webxr_get_transform_for_view(-1, js_matrix)) {
  421. head_transform = _js_matrix_to_transform(js_matrix);
  422. }
  423. if (head_tracker.is_valid()) {
  424. head_tracker->set_pose("default", head_transform, Vector3(), Vector3());
  425. }
  426. // Update all input sources.
  427. for (int i = 0; i < input_source_count; i++) {
  428. _update_input_source(i);
  429. }
  430. };
  431. };
  432. void WebXRInterfaceJS::_update_input_source(int p_input_source_id) {
  433. XRServer *xr_server = XRServer::get_singleton();
  434. ERR_FAIL_NULL(xr_server);
  435. InputSource &input_source = input_sources[p_input_source_id];
  436. float target_pose[16];
  437. int tmp_target_ray_mode;
  438. int touch_index;
  439. int has_grip_pose;
  440. float grip_pose[16];
  441. int has_standard_mapping;
  442. int button_count;
  443. float buttons[10];
  444. int axes_count;
  445. float axes[10];
  446. input_source.active = godot_webxr_update_input_source(
  447. p_input_source_id,
  448. target_pose,
  449. &tmp_target_ray_mode,
  450. &touch_index,
  451. &has_grip_pose,
  452. grip_pose,
  453. &has_standard_mapping,
  454. &button_count,
  455. buttons,
  456. &axes_count,
  457. axes);
  458. if (!input_source.active) {
  459. if (input_source.tracker.is_valid()) {
  460. xr_server->remove_tracker(input_source.tracker);
  461. input_source.tracker.unref();
  462. }
  463. return;
  464. }
  465. input_source.target_ray_mode = (WebXRInterface::TargetRayMode)tmp_target_ray_mode;
  466. input_source.touch_index = touch_index;
  467. Ref<XRPositionalTracker> &tracker = input_source.tracker;
  468. if (tracker.is_null()) {
  469. tracker.instantiate();
  470. StringName tracker_name;
  471. if (input_source.target_ray_mode == WebXRInterface::TargetRayMode::TARGET_RAY_MODE_SCREEN) {
  472. tracker_name = touch_names[touch_index];
  473. } else {
  474. tracker_name = tracker_names[p_input_source_id];
  475. }
  476. // Input source id's 0 and 1 are always the left and right hands.
  477. if (p_input_source_id < 2) {
  478. tracker->set_tracker_type(XRServer::TRACKER_CONTROLLER);
  479. tracker->set_tracker_name(tracker_name);
  480. tracker->set_tracker_desc(p_input_source_id == 0 ? "Left hand controller" : "Right hand controller");
  481. tracker->set_tracker_hand(p_input_source_id == 0 ? XRPositionalTracker::TRACKER_HAND_LEFT : XRPositionalTracker::TRACKER_HAND_RIGHT);
  482. } else {
  483. tracker->set_tracker_name(tracker_name);
  484. tracker->set_tracker_desc(tracker_name);
  485. }
  486. xr_server->add_tracker(tracker);
  487. }
  488. Transform3D aim_transform = _js_matrix_to_transform(target_pose);
  489. tracker->set_pose(SNAME("default"), aim_transform, Vector3(), Vector3());
  490. tracker->set_pose(SNAME("aim"), aim_transform, Vector3(), Vector3());
  491. if (has_grip_pose) {
  492. tracker->set_pose(SNAME("grip"), _js_matrix_to_transform(grip_pose), Vector3(), Vector3());
  493. }
  494. for (int i = 0; i < button_count; i++) {
  495. StringName button_name = has_standard_mapping ? standard_button_names[i] : unknown_button_names[i];
  496. StringName button_pressure_name = has_standard_mapping ? standard_button_pressure_names[i] : unknown_button_pressure_names[i];
  497. float value = buttons[i];
  498. bool state = value > 0.0;
  499. tracker->set_input(button_name, state);
  500. tracker->set_input(button_pressure_name, value);
  501. }
  502. for (int i = 0; i < axes_count; i++) {
  503. StringName axis_name = has_standard_mapping ? standard_axis_names[i] : unknown_axis_names[i];
  504. float value = axes[i];
  505. if (has_standard_mapping && (i == 1 || i == 3)) {
  506. // Invert the Y-axis on thumbsticks and trackpads, in order to
  507. // match OpenXR and other XR platform SDKs.
  508. value = -value;
  509. }
  510. tracker->set_input(axis_name, value);
  511. }
  512. // Also create Vector2's for the thumbstick and trackpad when we have the
  513. // standard mapping.
  514. if (has_standard_mapping) {
  515. if (axes_count >= 2) {
  516. tracker->set_input(standard_vector_names[0], Vector2(axes[0], -axes[1]));
  517. }
  518. if (axes_count >= 4) {
  519. tracker->set_input(standard_vector_names[1], Vector2(axes[2], -axes[3]));
  520. }
  521. }
  522. if (input_source.target_ray_mode == WebXRInterface::TARGET_RAY_MODE_SCREEN) {
  523. if (touch_index < 5 && axes_count >= 2) {
  524. Vector2 joy_vector = Vector2(axes[0], axes[1]);
  525. Vector2 position = _get_screen_position_from_joy_vector(joy_vector);
  526. if (touches[touch_index].is_touching) {
  527. Vector2 delta = position - touches[touch_index].position;
  528. // If position has changed by at least 1 pixel, generate a drag event.
  529. if (abs(delta.x) >= 1.0 || abs(delta.y) >= 1.0) {
  530. Ref<InputEventScreenDrag> event;
  531. event.instantiate();
  532. event->set_index(touch_index);
  533. event->set_position(position);
  534. event->set_relative(delta);
  535. Input::get_singleton()->parse_input_event(event);
  536. }
  537. }
  538. touches[touch_index].position = position;
  539. }
  540. }
  541. }
  542. void WebXRInterfaceJS::_on_input_event(int p_event_type, int p_input_source_id) {
  543. // Get the latest data for this input source. For transient input sources,
  544. // we may not have any data at all yet!
  545. _update_input_source(p_input_source_id);
  546. if (p_event_type == WEBXR_INPUT_EVENT_SELECTSTART || p_event_type == WEBXR_INPUT_EVENT_SELECTEND) {
  547. const InputSource &input_source = input_sources[p_input_source_id];
  548. if (input_source.target_ray_mode == WebXRInterface::TARGET_RAY_MODE_SCREEN) {
  549. int touch_index = input_source.touch_index;
  550. if (touch_index >= 0 && touch_index < 5) {
  551. touches[touch_index].is_touching = (p_event_type == WEBXR_INPUT_EVENT_SELECTSTART);
  552. Ref<InputEventScreenTouch> event;
  553. event.instantiate();
  554. event->set_index(touch_index);
  555. event->set_position(touches[touch_index].position);
  556. event->set_pressed(p_event_type == WEBXR_INPUT_EVENT_SELECTSTART);
  557. Input::get_singleton()->parse_input_event(event);
  558. }
  559. }
  560. }
  561. switch (p_event_type) {
  562. case WEBXR_INPUT_EVENT_SELECTSTART:
  563. emit_signal("selectstart", p_input_source_id);
  564. break;
  565. case WEBXR_INPUT_EVENT_SELECTEND:
  566. emit_signal("selectend", p_input_source_id);
  567. // Emit the 'select' event on our own (rather than intercepting the
  568. // one from JavaScript) so that we don't have to needlessly call
  569. // _update_input_source() a second time.
  570. emit_signal("select", p_input_source_id);
  571. break;
  572. case WEBXR_INPUT_EVENT_SQUEEZESTART:
  573. emit_signal("squeezestart", p_input_source_id);
  574. break;
  575. case WEBXR_INPUT_EVENT_SQUEEZEEND:
  576. emit_signal("squeezeend", p_input_source_id);
  577. // Again, we emit the 'squeeze' event on our own to avoid extra work.
  578. emit_signal("squeeze", p_input_source_id);
  579. break;
  580. }
  581. }
  582. Vector2 WebXRInterfaceJS::_get_screen_position_from_joy_vector(const Vector2 &p_joy_vector) {
  583. SceneTree *scene_tree = Object::cast_to<SceneTree>(OS::get_singleton()->get_main_loop());
  584. if (!scene_tree) {
  585. return Vector2();
  586. }
  587. Window *viewport = scene_tree->get_root();
  588. Vector2 position_percentage((p_joy_vector.x + 1.0f) / 2.0f, ((p_joy_vector.y) + 1.0f) / 2.0f);
  589. Vector2 position = (Size2)viewport->get_size() * position_percentage;
  590. return position;
  591. }
  592. WebXRInterfaceJS::WebXRInterfaceJS() {
  593. initialized = false;
  594. session_mode = "inline";
  595. requested_reference_space_types = "local";
  596. };
  597. WebXRInterfaceJS::~WebXRInterfaceJS() {
  598. // and make sure we cleanup if we haven't already
  599. if (initialized) {
  600. uninitialize();
  601. };
  602. };
  603. #endif // WEB_ENABLED