webxr_interface_js.h 9.5 KB

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