Debian Codegraph @

modules/webxr/webxr_interface_js.h

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