webxr_interface_js.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /**************************************************************************/
  2. /* webxr_interface_js.h */
  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. #pragma once
  31. #ifdef WEB_ENABLED
  32. #include "servers/xr/xr_controller_tracker.h"
  33. #include "servers/xr/xr_hand_tracker.h"
  34. #include "webxr_interface.h"
  35. /**
  36. The WebXR interface is a VR/AR interface that can be used on the web.
  37. */
  38. namespace GLES3 {
  39. class TextureStorage;
  40. }
  41. class WebXRInterfaceJS : public WebXRInterface {
  42. GDCLASS(WebXRInterfaceJS, WebXRInterface);
  43. private:
  44. bool initialized;
  45. Ref<XRPositionalTracker> head_tracker;
  46. Transform3D head_transform;
  47. String session_mode;
  48. String required_features;
  49. String optional_features;
  50. String requested_reference_space_types;
  51. String reference_space_type;
  52. String enabled_features;
  53. XRInterface::EnvironmentBlendMode environment_blend_mode = XRInterface::XR_ENV_BLEND_MODE_OPAQUE;
  54. Size2 render_targetsize;
  55. RBMap<unsigned int, RID> texture_cache;
  56. struct Touch {
  57. bool is_touching = false;
  58. Vector2 position;
  59. } touches[5];
  60. static constexpr uint8_t input_source_count = 16;
  61. struct InputSource {
  62. Ref<XRControllerTracker> tracker;
  63. bool active = false;
  64. TargetRayMode target_ray_mode;
  65. int touch_index = -1;
  66. } input_sources[input_source_count];
  67. static const int WEBXR_HAND_JOINT_MAX = 25;
  68. static const int HAND_MAX = 2;
  69. Ref<XRHandTracker> hand_trackers[HAND_MAX];
  70. RID color_texture;
  71. RID depth_texture;
  72. RID _get_color_texture();
  73. RID _get_depth_texture();
  74. RID _get_texture(unsigned int p_texture_id);
  75. Transform3D _js_matrix_to_transform(float *p_js_matrix);
  76. void _update_input_source(int p_input_source_id);
  77. Vector2 _get_screen_position_from_joy_vector(const Vector2 &p_joy_vector);
  78. public:
  79. virtual void is_session_supported(const String &p_session_mode) override;
  80. virtual void set_session_mode(String p_session_mode) override;
  81. virtual String get_session_mode() const override;
  82. virtual void set_required_features(String p_required_features) override;
  83. virtual String get_required_features() const override;
  84. virtual void set_optional_features(String p_optional_features) override;
  85. virtual String get_optional_features() const override;
  86. virtual void set_requested_reference_space_types(String p_requested_reference_space_types) override;
  87. virtual String get_requested_reference_space_types() const override;
  88. virtual String get_reference_space_type() const override;
  89. virtual String get_enabled_features() const override;
  90. virtual bool is_input_source_active(int p_input_source_id) const override;
  91. virtual Ref<XRControllerTracker> get_input_source_tracker(int p_input_source_id) const override;
  92. virtual TargetRayMode get_input_source_target_ray_mode(int p_input_source_id) const override;
  93. virtual String get_visibility_state() const override;
  94. virtual PackedVector3Array get_play_area() const override;
  95. virtual float get_display_refresh_rate() const override;
  96. virtual void set_display_refresh_rate(float p_refresh_rate) override;
  97. virtual Array get_available_display_refresh_rates() const override;
  98. virtual Array get_supported_environment_blend_modes() override;
  99. virtual XRInterface::EnvironmentBlendMode get_environment_blend_mode() const override;
  100. virtual bool set_environment_blend_mode(EnvironmentBlendMode p_new_environment_blend_mode) override;
  101. virtual StringName get_name() const override;
  102. virtual uint32_t get_capabilities() const override;
  103. virtual bool is_initialized() const override;
  104. virtual bool initialize() override;
  105. virtual void uninitialize() override;
  106. virtual Dictionary get_system_info() override;
  107. virtual Size2 get_render_target_size() override;
  108. virtual uint32_t get_view_count() override;
  109. virtual Transform3D get_camera_transform() override;
  110. virtual Transform3D get_transform_for_view(uint32_t p_view, const Transform3D &p_cam_transform) override;
  111. virtual Projection get_projection_for_view(uint32_t p_view, double p_aspect, double p_z_near, double p_z_far) override;
  112. virtual bool pre_draw_viewport(RID p_render_target) override;
  113. virtual Vector<BlitToScreen> post_draw_viewport(RID p_render_target, const Rect2 &p_screen_rect) override;
  114. virtual RID get_color_texture() override;
  115. virtual RID get_depth_texture() override;
  116. virtual RID get_velocity_texture() override;
  117. virtual void process() override;
  118. void _on_input_event(int p_event_type, int p_input_source_id);
  119. // Internal setters used by callbacks from Emscripten.
  120. inline void _set_reference_space_type(String p_reference_space_type) { reference_space_type = p_reference_space_type; }
  121. inline void _set_enabled_features(String p_enabled_features) { enabled_features = p_enabled_features; }
  122. void _set_environment_blend_mode(String p_blend_mode_string);
  123. WebXRInterfaceJS();
  124. ~WebXRInterfaceJS();
  125. private:
  126. StringName tracker_names[16] = {
  127. StringName("left_hand"),
  128. StringName("right_hand"),
  129. StringName("tracker_2"),
  130. StringName("tracker_3"),
  131. StringName("tracker_4"),
  132. StringName("tracker_5"),
  133. StringName("tracker_6"),
  134. StringName("tracker_7"),
  135. StringName("tracker_8"),
  136. StringName("tracker_9"),
  137. StringName("tracker_10"),
  138. StringName("tracker_11"),
  139. StringName("tracker_12"),
  140. StringName("tracker_13"),
  141. StringName("tracker_14"),
  142. StringName("tracker_15"),
  143. };
  144. StringName touch_names[5] = {
  145. StringName("touch_0"),
  146. StringName("touch_1"),
  147. StringName("touch_2"),
  148. StringName("touch_3"),
  149. StringName("touch_4"),
  150. };
  151. StringName standard_axis_names[10] = {
  152. StringName("touchpad_x"),
  153. StringName("touchpad_y"),
  154. StringName("thumbstick_x"),
  155. StringName("thumbstick_y"),
  156. StringName("axis_4"),
  157. StringName("axis_5"),
  158. StringName("axis_6"),
  159. StringName("axis_7"),
  160. StringName("axis_8"),
  161. StringName("axis_9"),
  162. };
  163. StringName standard_vector_names[2] = {
  164. StringName("touchpad"),
  165. StringName("thumbstick"),
  166. };
  167. StringName standard_button_names[10] = {
  168. StringName("trigger_click"),
  169. StringName("grip_click"),
  170. StringName("touchpad_click"),
  171. StringName("thumbstick_click"),
  172. StringName("ax_button"),
  173. StringName("by_button"),
  174. StringName("button_6"),
  175. StringName("button_7"),
  176. StringName("button_8"),
  177. StringName("button_9"),
  178. };
  179. StringName standard_button_pressure_names[10] = {
  180. StringName("trigger"),
  181. StringName("grip"),
  182. StringName("touchpad_click_pressure"),
  183. StringName("thumbstick_click_pressure"),
  184. StringName("ax_button_pressure"),
  185. StringName("by_button_pressure"),
  186. StringName("button_pressure_6"),
  187. StringName("button_pressure_7"),
  188. StringName("button_pressure_8"),
  189. StringName("button_pressure_9"),
  190. };
  191. StringName unknown_button_names[10] = {
  192. StringName("button_0"),
  193. StringName("button_1"),
  194. StringName("button_2"),
  195. StringName("button_3"),
  196. StringName("button_4"),
  197. StringName("button_5"),
  198. StringName("button_6"),
  199. StringName("button_7"),
  200. StringName("button_8"),
  201. StringName("button_9"),
  202. };
  203. StringName unknown_axis_names[10] = {
  204. StringName("axis_0"),
  205. StringName("axis_1"),
  206. StringName("axis_2"),
  207. StringName("axis_3"),
  208. StringName("axis_4"),
  209. StringName("axis_5"),
  210. StringName("axis_6"),
  211. StringName("axis_7"),
  212. StringName("axis_8"),
  213. StringName("axis_9"),
  214. };
  215. StringName unknown_button_pressure_names[10] = {
  216. StringName("button_pressure_0"),
  217. StringName("button_pressure_1"),
  218. StringName("button_pressure_2"),
  219. StringName("button_pressure_3"),
  220. StringName("button_pressure_4"),
  221. StringName("button_pressure_5"),
  222. StringName("button_pressure_6"),
  223. StringName("button_pressure_7"),
  224. StringName("button_pressure_8"),
  225. StringName("button_pressure_9"),
  226. };
  227. };
  228. #endif // WEB_ENABLED