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