/
xr-views.js
293 lines (256 loc) · 7.27 KB
/
xr-views.js
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
import { platform } from '../../core/platform.js';
import { EventHandler } from "../../core/event-handler.js";
import { XrView } from "./xr-view.js";
import { XRTYPE_AR, XRDEPTHSENSINGUSAGE_GPU, XRDEPTHSENSINGFORMAT_L8A8, XRDEPTHSENSINGFORMAT_F32 } from "./constants.js";
import { PIXELFORMAT_LA8, PIXELFORMAT_R32F } from '../../platform/graphics/constants.js';
/**
* Provides access to list of {@link XrView}'s. And information about their capabilities,
* such as support and availability of view's camera color texture, depth texture and other parameters.
*
* @category XR
*/
class XrViews extends EventHandler {
/**
* Fired when view has been added. Views are not available straight away on session start
* and are added mid-session. They can be added/removed mid session by underlyng system. The
* handler is passed the {@link XrView} that has been added.
*
* @event
* @example
* xr.views.on('add', (view) => {
* console.log('View added');
* });
*/
static EVENT_ADD = 'add';
/**
* Fired when view has been removed. They can be added/removed mid session by underlyng system.
* The handler is passed the {@link XrView} that has been removed.
*
* @event
* @example
* xr.views.on('remove', (view) => {
* console.log('View removed');
* });
*/
static EVENT_REMOVE = 'remove';
/**
* @type {import('./xr-manager.js').XrManager}
* @private
*/
_manager;
/**
* @type {Map<string,XrView>}
* @private
*/
_index = new Map();
/**
* @type {Map<string,XrView>}
* @private
*/
_indexTmp = new Map();
/**
* @type {XrView[]}
* @private
*/
_list = [];
/**
* @type {boolean}
* @private
*/
_supportedColor = platform.browser && !!window.XRCamera && !!window.XRWebGLBinding;
/**
* @type {boolean}
* @private
*/
_supportedDepth = platform.browser && !!window.XRDepthInformation;
/**
* @type {boolean}
* @private
*/
_availableColor = false;
/**
* @type {boolean}
* @private
*/
_availableDepth = false;
/**
* @type {string}
* @private
*/
_depthUsage = '';
/**
* @type {string}
* @private
*/
_depthFormat = '';
/**
* @type {object}
* @private
*/
_depthFormats = {
[XRDEPTHSENSINGFORMAT_L8A8]: PIXELFORMAT_LA8,
[XRDEPTHSENSINGFORMAT_F32]: PIXELFORMAT_R32F
};
/**
* @param {import('./xr-manager.js').XrManager} manager - WebXR Manager.
* @ignore
*/
constructor(manager) {
super();
this._manager = manager;
this._manager.on('start', this._onSessionStart, this);
this._manager.on('end', this._onSessionEnd, this);
}
/**
* An array of {@link XrView}'s of this session. Views are not available straight
* away on session start, and can be added/removed mid-session. So use of add/remove
* events is required for accessing views.
*
* @type {XrView[]}
*/
get list() {
return this._list;
}
/**
* Check if Camera Color is supported. It might be still unavailable even if requested,
* based on hardware capabilities and granted permissions.
*
* @type {boolean}
*/
get supportedColor() {
return this._supportedColor;
}
/**
* Check if Camera Depth is supported. It might be still unavailable even if requested,
* based on hardware capabilities and granted permissions.
*
* @type {boolean}
*/
get supportedDepth() {
return this._supportedDepth;
}
/**
* Check if Camera Color is available. This information becomes available only after
* session has started.
*
* @type {boolean}
*/
get availableColor() {
return this._availableColor;
}
/**
* Check if Camera Depth is available. This information becomes available only after
* session has started.
*
* @type {boolean}
*/
get availableDepth() {
return this._availableDepth;
}
/**
* @type {string}
* @ignore
*/
get depthUsage() {
return this._depthUsage;
}
/**
* Whether the depth sensing is GPU optimized.
*
* @type {boolean}
*/
get depthGpuOptimized() {
return this._depthUsage === XRDEPTHSENSINGUSAGE_GPU;
}
/**
* @type {string}
* @ignore
*/
get depthFormat() {
return this._depthFormat;
}
/**
* The depth sensing pixel format. Currently supported either:
* {@link PIXELFORMAT_LA8} or {@link PIXELFORMAT_R32F}
*
* @type {number|null}
*/
get depthPixelFormat() {
return this._depthFormats[this._depthFormat] ?? null;
}
/**
* @param {*} frame - XRFrame from requestAnimationFrame callback.
* @param {XRView} xrView - XRView from WebXR API.
* @ignore
*/
update(frame, xrViews) {
for (let i = 0; i < xrViews.length; i++) {
this._indexTmp.set(xrViews[i].eye, xrViews[i]);
}
for (const [eye, xrView] of this._indexTmp) {
let view = this._index.get(eye);
if (!view) {
// add new view
view = new XrView(this._manager, xrView, xrViews.length);
this._index.set(eye, view);
this._list.push(view);
view.update(frame, xrView);
this.fire('add', view);
} else {
// update existing view0
view.update(frame, xrView);
}
}
// remove views
for (const [eye, view] of this._index) {
if (this._indexTmp.has(eye))
continue;
view.destroy();
this._index.delete(eye);
const ind = this._list.indexOf(view);
if (ind !== -1) this._list.splice(ind, 1);
this.fire('remove', view);
}
this._indexTmp.clear();
}
/**
* Get an {@link XrView} by its associated eye constant.
*
* @param {string} eye - An XREYE_* view is associated with. Can be 'none' for monoscope views.
* @returns {XrView|null} View or null if view of such eye is not available.
*/
get(eye) {
return this._index.get(eye) || null;
}
/**
* @private
*/
_onSessionStart() {
if (this._manager.type !== XRTYPE_AR)
return;
if (!this._manager.session.enabledFeatures)
return;
this._availableColor = this._manager.session.enabledFeatures.indexOf('camera-access') !== -1;
this._availableDepth = this._manager.session.enabledFeatures.indexOf('depth-sensing') !== -1;
if (this._availableDepth) {
const session = this._manager.session;
this._depthUsage = session.depthUsage;
this._depthFormat = session.depthDataFormat;
}
}
/**
* @private
*/
_onSessionEnd() {
for (const view of this._index.values()) {
view.destroy();
}
this._index.clear();
this._availableColor = false;
this._availableDepth = false;
this._depthUsage = '';
this._depthFormat = '';
this._list.length = 0;
}
}
export { XrViews };