forked from WonderlandEngine/emscripten-webxr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebxr.h
260 lines (208 loc) · 7.51 KB
/
webxr.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#ifndef WEBXR_H_
#define WEBXR_H_
/** @file
* @brief Minimal WebXR Device API wrapper
*/
#ifdef __cplusplus
extern "C"
#endif
{
/** Errors enum */
enum WebXRError {
WEBXR_ERR_WEBXR_UNSUPPORTED = -2,
WEBXR_ERR_WEBGPU_UNSUPPORTED = -3,
WEBXR_ERR_XRGPU_BINDING_UNSUPPORTED = -4,
WEBXR_ERR_IMMERSIVE_XR_UNSUPPORTED = -5
};
/** WebXR handedness */
enum WebXRHandedness {
WEBXR_HANDEDNESS_NONE = -1,
WEBXR_HANDEDNESS_LEFT = 0,
WEBXR_HANDEDNESS_RIGHT = 1,
};
/** WebXR target ray mode */
enum WebXRTargetRayMode {
WEBXR_TARGET_RAY_MODE_GAZE = 0,
WEBXR_TARGET_RAY_MODE_TRACKED_POINTER = 1,
WEBXR_TARGET_RAY_MODE_SCREEN = 2,
};
/** WebXR 'WebXRSessionMode' enum*/
enum WebXRSessionMode {
WEBXR_SESSION_MODE_INLINE = 0, /** "inline" */
WEBXR_SESSION_MODE_IMMERSIVE_VR = 1, /** "immersive-vr" */
WEBXR_SESSION_MODE_IMMERSIVE_AR = 2, /** "immersive-ar" */
};
/** WebXR 'WebXRSessionFeatures' enum*/
enum WebXRSessionFeatures {
WEBXR_SESSION_FEATURE_LOCAL = 0, /** "local" */
WEBXR_SESSION_FEATURE_LOCAL_FLOOR = 1, /** "local-floor" */
WEBXR_SESSION_FEATURE_BOUNDED_FLOOR = 2, /** "bounded-floor" */
WEBXR_SESSION_FEATURE_UNBOUNDED = 3, /** "unbounded" */
WEBXR_SESSION_FEATURE_HIT_TEST = 4, /** "hit-test" */
WEBXR_SESSION_FEATURE_WEBGPU = 5, /** "hit-test" */
};
/** WebXR 'WebXRInputPoseMode' enum*/
enum WebXRInputPoseMode {
WEBXR_INPUT_POSE_GRIP = 0, /** gripSpace */
WEBXR_INPUT_POSE_TARGET_RAY = 1, /** targetRaySpace */
};
/** WebXR rigid transform */
typedef struct WebXRRigidTransform {
float matrix[16];
float position[3];
float orientation[4];
} WebXRRigidTransform;
/** WebXR view */
typedef struct WebXRView {
/* view pose */
WebXRRigidTransform viewPose;
/* projection matrix */
float projectionMatrix[16];
/* x, y, width, height of the eye viewport on target texture */
int viewport[4];
} WebXRView;
typedef struct WebXRInputSource {
int id;
WebXRHandedness handedness;
WebXRTargetRayMode targetRayMode;
} WebXRInputSource;
/** 'WebXRSessionMode' enum*/
enum GamepadButtonActionState {
GAMEPAD_BUTTON_PRESSED_STATE = 0,
GAMEPAD_BUTTON_TOUCHED_STATE = 1,
GAMEPAD_BUTTON_VALUE_STATE = 2,
};
typedef struct GamepadButton {
int pressed = 0;
int touched = 0;
float value = false;
bool changedSinceLastSync[3] = {}; // 3 states
} GamepadButton;
/**
Callback for errors
@param userData User pointer passed to init_webxr()
@param error Error code
*/
typedef void (*webxr_error_callback_func)(void* userData, int error);
/**
Callback for frame rendering
@param userData User pointer passed to init_webxr()
@param time Current frame time
@param modelMatrix Transformation of the XR Device to tracking origin
@param views Array of `viewCount` @ref WebXRView "webxr views"
@param viewCount Size of `views`
*/
typedef void (*webxr_frame_callback_func)(void* userData, int time, WebXRRigidTransform* headPose, WebXRView views[2], WGPUTextureView texture_view_left, WGPUTextureView texture_view_right, int viewCount);
/**
Callback for WebXr binding init
@param userData User pointer passed to set_session_start_callback
*/
typedef void (*webxr_webxr_init_callback_func)(void* userData);
/**
Callback for VR session start
@param userData User pointer passed to set_session_start_callback
@param mode The session mode
*/
typedef void (*webxr_session_callback_func)(void* userData, int mode);
/**
Callback for @ref webxr_is_session_supported
@param mode The session mode that was requested
@param supported Whether given mode is supported by this device
*/
typedef void (*webxr_session_supported_callback_func)(int mode, int supported);
extern void webxr_set_device(
WGPUDevice gpuDevice);
/**
Init WebXR rendering
@param frameCallback Callback called every frame
@param sessionStartCallback Callback called when session is started
@param sessionEndCallback Callback called when session ended
@param errorCallback Callback called every frame
@param userData User data passed to the callbacks
*/
extern void webxr_init(
webxr_frame_callback_func frameCallback,
webxr_webxr_init_callback_func webxrInitCallback,
webxr_session_callback_func sessionStartCallback,
webxr_session_callback_func sessionEndCallback,
webxr_error_callback_func errorCallback,
void* userData);
extern void webxr_set_session_blur_callback(
webxr_session_callback_func sessionBlurCallback, void* userData);
extern void webxr_set_session_focus_callback(
webxr_session_callback_func sessionFocusCallback, void* userData);
/*
Test if session mode is supported
@param mode Session mode to test
@param supportedCallback Callback which will be called once the
result has become available
*/
extern void webxr_is_session_supported(WebXRSessionMode mode,
webxr_session_supported_callback_func supportedCallback);
/*
Request session presentation start
@param mode Session mode from @ref WebXRSessionMode.
@param requiredFeatures Required session features from @ref WebXRSessionFeatures
@param optionalFeatures Required session features from @ref WebXRSessionFeatures
Needs to be called from a [user activation event](https://html.spec.whatwg.org/multipage/interaction.html#triggered-by-user-activation).
*/
extern void webxr_request_session(WebXRSessionMode mode,
WebXRSessionFeatures requiredFeatures);
/*
Request that the webxr presentation exits VR mode
*/
extern void webxr_request_exit();
/**
Set projection matrix parameters for the webxr session
@param near Distance of near clipping plane
@param far Distance of far clipping plane
*/
extern void webxr_set_projection_params(float near, float far);
/**
WebXR Input
*/
/**
Callback for primary input action.
@param userData User pointer passed to @ref webxr_set_select_callback, @ref webxr_set_select_end_callback or @ref webxr_set_select_start_callback.
*/
typedef void (*webxr_input_callback_func)(WebXRInputSource* inputSource, void* userData);
/**
Set callbacks for primary input action.
*/
extern void webxr_set_select_callback(
webxr_input_callback_func callback, void* userData);
extern void webxr_set_select_start_callback(
webxr_input_callback_func callback, void* userData);
extern void webxr_set_select_end_callback(
webxr_input_callback_func callback, void* userData);
/**
Get input sources.
@param outArray @ref WebXRInputSource array to fill.
@param max Size of outArray (in elements).
@param outCount Will receive the number of input sources valid in outArray.
*/
extern void webxr_get_input_sources(
WebXRInputSource* outArray, int max, int* outCount);
/**
Get input pose. Can only be called during the frame callback.
@param source The source to get the pose for.
@param outPose Where to store the pose.
@returns `false` if updating the pose failed, `true` otherwise.
*/
extern int webxr_get_input_pose(WebXRInputSource* source, WebXRRigidTransform* outPose, WebXRInputPoseMode mode=WEBXR_INPUT_POSE_GRIP);
/**
Get input button. Can only be called during the frame callback.
@param source The source to get the pose for.
@param outButton Where to store the button.
@returns `false` if updating the pose failed, `true` otherwise.
*/
extern int webxr_get_input_button(WebXRInputSource* source, int buttonId, GamepadButton* outButton);
/**
Get input axes. Can only be called during the frame callback.
@param source The source to get the pose for.
@param outButton Where to store the axes.
@returns `false` if updating the pose failed, `true` otherwise.
*/
extern int webxr_get_input_axes(WebXRInputSource* source, float* outAxes);
}
#endif