@galacean/engine-xr 1.6.6 → 1.6.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. package/package.json +4 -4
  2. package/dist/browser.js +0 -1630
  3. package/dist/browser.js.map +0 -1
  4. package/dist/browser.min.js +0 -2
  5. package/dist/browser.min.js.map +0 -1
  6. package/dist/main.js +0 -1626
  7. package/dist/main.js.map +0 -1
  8. package/dist/module.js +0 -1598
  9. package/dist/module.js.map +0 -1
  10. package/types/XRManagerExtended.d.ts +0 -62
  11. package/types/XRPose.d.ts +0 -15
  12. package/types/feature/XRFeature.d.ts +0 -16
  13. package/types/feature/XRFeatureType.d.ts +0 -6
  14. package/types/feature/camera/XRCameraManager.d.ts +0 -26
  15. package/types/feature/hitTest/TrackableType.d.ts +0 -10
  16. package/types/feature/hitTest/XRHitResult.d.ts +0 -19
  17. package/types/feature/hitTest/XRHitTest.d.ts +0 -40
  18. package/types/feature/trackable/XRRequestTracking.d.ts +0 -1
  19. package/types/feature/trackable/XRRequestTrackingState.d.ts +0 -8
  20. package/types/feature/trackable/XRTrackableFeature.d.ts +0 -35
  21. package/types/feature/trackable/XRTracked.d.ts +0 -14
  22. package/types/feature/trackable/anchor/XRAnchor.d.ts +0 -6
  23. package/types/feature/trackable/anchor/XRAnchorTracking.d.ts +0 -38
  24. package/types/feature/trackable/anchor/XRRequestAnchor.d.ts +0 -16
  25. package/types/feature/trackable/image/XRImageTracking.d.ts +0 -25
  26. package/types/feature/trackable/image/XRReferenceImage.d.ts +0 -16
  27. package/types/feature/trackable/image/XRRequestImage.d.ts +0 -14
  28. package/types/feature/trackable/image/XRTrackedImage.d.ts +0 -12
  29. package/types/feature/trackable/plane/XRPlaneMode.d.ts +0 -13
  30. package/types/feature/trackable/plane/XRPlaneTracking.d.ts +0 -24
  31. package/types/feature/trackable/plane/XRRequestPlane.d.ts +0 -13
  32. package/types/feature/trackable/plane/XRTrackedPlane.d.ts +0 -21
  33. package/types/index.d.ts +0 -32
  34. package/types/input/XRCamera.d.ts +0 -15
  35. package/types/input/XRController.d.ts +0 -38
  36. package/types/input/XRInput.d.ts +0 -8
  37. package/types/input/XRInputButton.d.ts +0 -19
  38. package/types/input/XRInputEventType.d.ts +0 -8
  39. package/types/input/XRInputManager.d.ts +0 -32
  40. package/types/input/XRTargetRayMode.d.ts +0 -5
  41. package/types/input/XRTrackedInputDevice.d.ts +0 -21
  42. package/types/input/XRTrackingState.d.ts +0 -11
  43. package/types/loader/XRReferenceImageDecoder.d.ts +0 -5
  44. package/types/loader/XRReferenceImageLoader.d.ts +0 -5
  45. package/types/session/XRSessionManager.d.ts +0 -56
  46. package/types/session/XRSessionMode.d.ts +0 -8
  47. package/types/session/XRSessionState.d.ts +0 -15
package/dist/module.js DELETED
@@ -1,1598 +0,0 @@
1
- import { CameraType, Matrix, CameraClearFlags, Vector3, Quaternion, Rect, SafeLoopArray, XRManager, Ray, Plane, Vector2, decoder, AssetPromise, resourceLoader, decode, Loader } from '@galacean/engine';
2
-
3
- function _is_native_reflect_construct() {
4
- // Since Reflect.construct can't be properly polyfilled, some
5
- // implementations (e.g. core-js@2) don't set the correct internal slots.
6
- // Those polyfills don't allow us to subclass built-ins, so we need to
7
- // use our fallback implementation.
8
- try {
9
- // If the internal slots aren't set, this throws an error similar to
10
- // TypeError: this is not a Boolean object.
11
- var result = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function() {}));
12
- } catch (_) {}
13
- return (_is_native_reflect_construct = function() {
14
- return !!result;
15
- })();
16
- }
17
-
18
- function _set_prototype_of(o, p) {
19
- _set_prototype_of = Object.setPrototypeOf || function setPrototypeOf(o, p) {
20
- o.__proto__ = p;
21
-
22
- return o;
23
- };
24
-
25
- return _set_prototype_of(o, p);
26
- }
27
-
28
- function _construct(Parent, args, Class) {
29
- if (_is_native_reflect_construct()) _construct = Reflect.construct;
30
- else {
31
- _construct = function construct(Parent, args, Class) {
32
- var a = [null];
33
- a.push.apply(a, args);
34
- var Constructor = Function.bind.apply(Parent, a);
35
- var instance = new Constructor();
36
-
37
- if (Class) _set_prototype_of(instance, Class.prototype);
38
-
39
- return instance;
40
- };
41
- }
42
-
43
- return _construct.apply(null, arguments);
44
- }
45
-
46
- function _defineProperties(target, props) {
47
- for (var i = 0; i < props.length; i++) {
48
- var descriptor = props[i];
49
- descriptor.enumerable = descriptor.enumerable || false;
50
- descriptor.configurable = true;
51
-
52
- if ("value" in descriptor) descriptor.writable = true;
53
-
54
- Object.defineProperty(target, descriptor.key, descriptor);
55
- }
56
- }
57
- function _create_class(Constructor, protoProps, staticProps) {
58
- if (protoProps) _defineProperties(Constructor.prototype, protoProps);
59
- if (staticProps) _defineProperties(Constructor, staticProps);
60
-
61
- return Constructor;
62
- }
63
-
64
- function _inherits(subClass, superClass) {
65
- if (typeof superClass !== "function" && superClass !== null) {
66
- throw new TypeError("Super expression must either be null or a function");
67
- }
68
-
69
- subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } });
70
-
71
- if (superClass) _set_prototype_of(subClass, superClass);
72
- }
73
-
74
- function _instanceof(left, right) {
75
- if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) {
76
- return !!right[Symbol.hasInstance](left);
77
- } else return left instanceof right;
78
- }
79
-
80
- /**
81
- * Enumerates some input devices that can be tracked.(including status, posture and other information)
82
- */ var XRTrackedInputDevice = /*#__PURE__*/ function(XRTrackedInputDevice) {
83
- /** Controller */ XRTrackedInputDevice[XRTrackedInputDevice["Controller"] = 0] = "Controller";
84
- /** Left controller */ XRTrackedInputDevice[XRTrackedInputDevice["LeftController"] = 1] = "LeftController";
85
- /** Right controller */ XRTrackedInputDevice[XRTrackedInputDevice["RightController"] = 2] = "RightController";
86
- /** Camera */ XRTrackedInputDevice[XRTrackedInputDevice["Camera"] = 3] = "Camera";
87
- /** Left camera */ XRTrackedInputDevice[XRTrackedInputDevice["LeftCamera"] = 4] = "LeftCamera";
88
- /** Right camera */ XRTrackedInputDevice[XRTrackedInputDevice["RightCamera"] = 5] = "RightCamera";
89
- /** Head */ XRTrackedInputDevice[XRTrackedInputDevice["LeftHand"] = 6] = "LeftHand";
90
- /** Right hand */ XRTrackedInputDevice[XRTrackedInputDevice["RightHand"] = 7] = "RightHand";
91
- return XRTrackedInputDevice;
92
- }({});
93
-
94
- /**
95
- * The state of an XRSession.
96
- */ var XRSessionState = /*#__PURE__*/ function(XRSessionState) {
97
- /** Not initialized. */ XRSessionState[XRSessionState["None"] = 0] = "None";
98
- /** Initializing session. */ XRSessionState[XRSessionState["Initializing"] = 1] = "Initializing";
99
- /** Initialized but not started. */ XRSessionState[XRSessionState["Initialized"] = 2] = "Initialized";
100
- /** Running. */ XRSessionState[XRSessionState["Running"] = 3] = "Running";
101
- /** Paused. */ XRSessionState[XRSessionState["Paused"] = 4] = "Paused";
102
- return XRSessionState;
103
- }({});
104
-
105
- /**
106
- * The manager of XR camera.
107
- */ var XRCameraManager = /*#__PURE__*/ function() {
108
- function XRCameraManager(_xrManager) {
109
- this._xrManager = _xrManager;
110
- }
111
- var _proto = XRCameraManager.prototype;
112
- /**
113
- * Attach the camera to the specified input type(Camera, LeftCamera or RightCamera).
114
- * The camera entity need to be moved to the XROrigin entity.
115
- * @param type - The input type
116
- * @param camera - The camera to be attached
117
- */ _proto.attachCamera = function attachCamera(type, camera) {
118
- var xrCamera = this._xrManager.inputManager.getTrackedDevice(type);
119
- var preCamera = xrCamera._camera;
120
- if (preCamera !== camera) {
121
- // @ts-ignore
122
- preCamera && (preCamera._cameraType = CameraType.Normal);
123
- switch(type){
124
- case XRTrackedInputDevice.Camera:
125
- // @ts-ignore
126
- camera._cameraType = CameraType.XRCenterCamera;
127
- break;
128
- case XRTrackedInputDevice.LeftCamera:
129
- // @ts-ignore
130
- camera._cameraType = CameraType.XRLeftCamera;
131
- break;
132
- case XRTrackedInputDevice.RightCamera:
133
- // @ts-ignore
134
- camera._cameraType = CameraType.XRRightCamera;
135
- break;
136
- }
137
- xrCamera._camera = camera;
138
- }
139
- };
140
- /**
141
- * Detach the camera from the specified input type.
142
- * @param type - The input type
143
- * @returns The camera that was detached
144
- */ _proto.detachCamera = function detachCamera(type) {
145
- var xrCamera = this._xrManager.inputManager.getTrackedDevice(type);
146
- var preCamera = xrCamera._camera;
147
- // @ts-ignore
148
- preCamera && (preCamera._cameraType = CameraType.Normal);
149
- xrCamera._camera = null;
150
- return preCamera;
151
- };
152
- /**
153
- * @internal
154
- */ _proto._onSessionStart = function _onSessionStart() {};
155
- /**
156
- * @internal
157
- */ _proto._onUpdate = function _onUpdate() {
158
- var cameras = this._xrManager.inputManager._cameras;
159
- var platformSession = this._xrManager.sessionManager._platformSession;
160
- for(var i = 0, n = cameras.length; i < n; i++){
161
- var cameraDevice = cameras[i];
162
- var camera = cameraDevice._camera;
163
- if (!camera) continue;
164
- // sync position and rotation
165
- var transform = camera.entity.transform;
166
- var pose = cameraDevice.pose;
167
- transform.position = pose.position;
168
- transform.rotationQuaternion = pose.rotation;
169
- // sync viewport
170
- var viewport = camera.viewport;
171
- var _cameraDevice_viewport = cameraDevice.viewport, x = _cameraDevice_viewport.x, y = _cameraDevice_viewport.y, width = _cameraDevice_viewport.width, height = _cameraDevice_viewport.height;
172
- if (!(x === viewport.x && y === viewport.y && width === viewport.z && height === viewport.w)) {
173
- camera.viewport = viewport.set(x, y, width, height);
174
- }
175
- // sync project matrix
176
- if (!Matrix.equals(camera.projectionMatrix, cameraDevice.projectionMatrix)) {
177
- camera.projectionMatrix = cameraDevice.projectionMatrix;
178
- }
179
- // sync pixel viewport (only when rendering to XR main framebuffer)
180
- if (!camera.renderTarget) {
181
- // @ts-ignore
182
- camera._adjustPixelViewport(platformSession.framebufferWidth, platformSession.framebufferHeight);
183
- }
184
- }
185
- };
186
- /**
187
- * @internal
188
- */ _proto._onSessionExit = function _onSessionExit() {
189
- var cameras = this._xrManager.inputManager._cameras;
190
- for(var i = 0, n = cameras.length; i < n; i++){
191
- var // @ts-ignore
192
- _cameras_i__camera;
193
- (_cameras_i__camera = cameras[i]._camera) == null ? void 0 : _cameras_i__camera._updatePixelViewport();
194
- }
195
- };
196
- /**
197
- * @internal
198
- */ _proto._getIgnoreClearFlags = function _getIgnoreClearFlags(cameraType) {
199
- if (cameraType === CameraType.XRCenterCamera) {
200
- if (this._xrManager.sessionManager.state === XRSessionState.Running) {
201
- return CameraClearFlags.Color;
202
- } else {
203
- return CameraClearFlags.None;
204
- }
205
- } else {
206
- return CameraClearFlags.None;
207
- }
208
- };
209
- /**
210
- * @internal
211
- */ _proto._onDestroy = function _onDestroy() {};
212
- _create_class(XRCameraManager, [
213
- {
214
- key: "fixedFoveation",
215
- get: /**
216
- * The fixed foveation of the camera.
217
- */ function get() {
218
- var _this__xrManager_sessionManager = this._xrManager.sessionManager, platformSession = _this__xrManager_sessionManager._platformSession;
219
- if (platformSession) {
220
- return platformSession.getFixedFoveation();
221
- } else {
222
- throw new Error("XR session is not available.");
223
- }
224
- },
225
- set: function set(value) {
226
- var _this__xrManager_sessionManager = this._xrManager.sessionManager, platformSession = _this__xrManager_sessionManager._platformSession;
227
- if (platformSession) {
228
- platformSession.setFixedFoveation(value);
229
- } else {
230
- throw new Error("XR session is not available.");
231
- }
232
- }
233
- }
234
- ]);
235
- return XRCameraManager;
236
- }();
237
-
238
- /**
239
- * Data for describing pose in the XR space.
240
- */ var XRPose = function XRPose() {
241
- /** The position of the pose in XR space. */ this.position = new Vector3();
242
- /** The rotation of the pose in XR space. */ this.rotation = new Quaternion();
243
- /** The matrix of the pose in XR space. */ this.matrix = new Matrix();
244
- /** The inverse matrix of the pose in XR space. */ this.inverseMatrix = new Matrix();
245
- };
246
-
247
- /**
248
- * Enum for XR tracking state.
249
- */ var XRTrackingState = /*#__PURE__*/ function(XRTrackingState) {
250
- /** Not tracking */ XRTrackingState[XRTrackingState["NotTracking"] = 0] = "NotTracking";
251
- /** Tracking */ XRTrackingState[XRTrackingState["Tracking"] = 1] = "Tracking";
252
- /** Lost track */ XRTrackingState[XRTrackingState["TrackingLost"] = 2] = "TrackingLost";
253
- return XRTrackingState;
254
- }({});
255
-
256
- var XRInput = function XRInput(type) {
257
- this.type = type;
258
- this./** The tracking state of xr input. */ trackingState = XRTrackingState.NotTracking;
259
- };
260
-
261
- /**
262
- * The XR camera.
263
- */ var XRCamera = /*#__PURE__*/ function(XRInput) {
264
- _inherits(XRCamera, XRInput);
265
- function XRCamera() {
266
- var _this;
267
- _this = XRInput.apply(this, arguments) || this, /** The pose of the camera in XR space. */ _this.pose = new XRPose(), /** The viewport of the camera. */ _this.viewport = new Rect(), /** The projection matrix of the camera. */ _this.projectionMatrix = new Matrix();
268
- return _this;
269
- }
270
- return XRCamera;
271
- }(XRInput);
272
-
273
- /**
274
- * Enum for XR input button.
275
- */ var XRInputButton = /*#__PURE__*/ function(XRInputButton) {
276
- /** None */ XRInputButton[XRInputButton["None"] = 0] = "None";
277
- /** Select */ XRInputButton[XRInputButton["Select"] = 1] = "Select";
278
- /** Select */ XRInputButton[XRInputButton["Trigger"] = 1] = "Trigger";
279
- /** Squeeze */ XRInputButton[XRInputButton["Squeeze"] = 2] = "Squeeze";
280
- /** TouchPad */ XRInputButton[XRInputButton["TouchPad"] = 4] = "TouchPad";
281
- /** A */ XRInputButton[XRInputButton["AButton"] = 8] = "AButton";
282
- /** B */ XRInputButton[XRInputButton["BButton"] = 16] = "BButton";
283
- return XRInputButton;
284
- }({});
285
-
286
- /**
287
- * The XR controller.
288
- */ var XRController = /*#__PURE__*/ function(XRInput) {
289
- _inherits(XRController, XRInput);
290
- function XRController() {
291
- var _this;
292
- _this = XRInput.apply(this, arguments) || this, /** The grip space pose of the controller in XR space. */ _this.gripPose = new XRPose(), /** The target ray space pose of the controller in XR space. */ _this.targetRayPose = new XRPose(), /** The currently pressed buttons of this controller. */ _this.pressedButtons = XRInputButton.None, /** Record button lifted. */ _this.down = XRInputButton.None, /** Record button pressed. */ _this.up = XRInputButton.None;
293
- return _this;
294
- }
295
- var _proto = XRController.prototype;
296
- /**
297
- *
298
- * Returns whether the button is pressed.
299
- * @param button - The button to check
300
- * @returns Whether the button is pressed
301
- */ _proto.isButtonDown = function isButtonDown(button) {
302
- return (this.down & button) !== 0;
303
- };
304
- /**
305
- * Returns whether the button is lifted.
306
- * @param button - The button to check
307
- * @returns Whether the button is lifted
308
- */ _proto.isButtonUp = function isButtonUp(button) {
309
- return (this.up & button) !== 0;
310
- };
311
- /**
312
- * Returns whether the button is held down.
313
- * @param button - The button to check
314
- * @returns Whether the button is held down
315
- */ _proto.isButtonHeldDown = function isButtonHeldDown(button) {
316
- return (this.pressedButtons & button) !== 0;
317
- };
318
- return XRController;
319
- }(XRInput);
320
-
321
- var XRInputEventType = /*#__PURE__*/ function(XRInputEventType) {
322
- XRInputEventType[XRInputEventType["SelectStart"] = 0] = "SelectStart";
323
- XRInputEventType[XRInputEventType["Select"] = 1] = "Select";
324
- XRInputEventType[XRInputEventType["SelectEnd"] = 2] = "SelectEnd";
325
- XRInputEventType[XRInputEventType["SqueezeStart"] = 3] = "SqueezeStart";
326
- XRInputEventType[XRInputEventType["Squeeze"] = 4] = "Squeeze";
327
- XRInputEventType[XRInputEventType["SqueezeEnd"] = 5] = "SqueezeEnd";
328
- return XRInputEventType;
329
- }({});
330
-
331
- var XRTargetRayMode = /*#__PURE__*/ function(XRTargetRayMode) {
332
- XRTargetRayMode[XRTargetRayMode["Gaze"] = 0] = "Gaze";
333
- XRTargetRayMode[XRTargetRayMode["TrackedPointer"] = 1] = "TrackedPointer";
334
- XRTargetRayMode[XRTargetRayMode["Screen"] = 2] = "Screen";
335
- return XRTargetRayMode;
336
- }({});
337
-
338
- /**
339
- * The manager of XR input.
340
- */ var XRInputManager = /*#__PURE__*/ function() {
341
- function XRInputManager(_xrManager, _engine) {
342
- this._xrManager = _xrManager;
343
- this._engine = _engine;
344
- this./** @internal */ _cameras = [];
345
- this./** @internal */ _controllers = [];
346
- this._added = [];
347
- this._removed = [];
348
- this._trackedDevices = [];
349
- this._statusSnapshot = [];
350
- this._listeners = new SafeLoopArray();
351
- var _this = this, trackedDevices = _this._trackedDevices, controllers = _this._controllers, cameras = _this._cameras;
352
- for(var i = 0; i < 6; i++){
353
- switch(i){
354
- case XRTrackedInputDevice.Camera:
355
- case XRTrackedInputDevice.LeftCamera:
356
- case XRTrackedInputDevice.RightCamera:
357
- cameras.push(trackedDevices[i] = new XRCamera(i));
358
- break;
359
- case XRTrackedInputDevice.Controller:
360
- case XRTrackedInputDevice.LeftController:
361
- case XRTrackedInputDevice.RightController:
362
- controllers.push(trackedDevices[i] = new XRController(i));
363
- break;
364
- }
365
- }
366
- this._statusSnapshot.fill(XRTrackingState.NotTracking, 0, trackedDevices.length);
367
- }
368
- var _proto = XRInputManager.prototype;
369
- /**
370
- * Returns the tracked device instance.
371
- * @param type - The tracked input device type
372
- * @returns The input instance
373
- */ _proto.getTrackedDevice = function getTrackedDevice(type) {
374
- return this._trackedDevices[type];
375
- };
376
- /**
377
- * Add a listener for tracked device changes.
378
- * @param listener - The listener to add
379
- */ _proto.addTrackedDeviceChangedListener = function addTrackedDeviceChangedListener(listener) {
380
- this._listeners.push({
381
- fn: listener
382
- });
383
- };
384
- /**
385
- * Remove a listener of tracked device changes.
386
- * @param listener - The listener to remove
387
- */ _proto.removeTrackedDeviceChangedListener = function removeTrackedDeviceChangedListener(listener) {
388
- this._listeners.findAndRemove(function(value) {
389
- return value.fn === listener ? value.destroyed = true : false;
390
- });
391
- };
392
- /**
393
- * @internal
394
- */ _proto._onUpdate = function _onUpdate() {
395
- var _this = this, added = _this._added, removed = _this._removed, statusSnapshot = _this._statusSnapshot;
396
- var _this1 = this, trackedDevices = _this1._trackedDevices, controllers = _this1._controllers;
397
- // Reset data
398
- added.length = removed.length = 0;
399
- for(var i = 0, n = controllers.length; i < n; i++){
400
- var controller = controllers[i];
401
- controller.down = controller.up = 0;
402
- }
403
- // Handle events and update tracked devices
404
- var _this__xrManager_sessionManager = this._xrManager.sessionManager, platformSession = _this__xrManager_sessionManager._platformSession;
405
- var platformEvents = platformSession.events;
406
- for(var i1 = 0, n1 = platformEvents.length; i1 < n1; i1++){
407
- this._handleEvent(platformEvents[i1]);
408
- }
409
- platformSession.resetEvents();
410
- platformSession.frame.updateInputs(trackedDevices);
411
- for(var i2 = 0, n2 = trackedDevices.length; i2 < n2; i2++){
412
- var input = trackedDevices[i2];
413
- if (!input) continue;
414
- var nowState = input.trackingState;
415
- if (statusSnapshot[i2] === XRTrackingState.Tracking) {
416
- if (nowState !== XRTrackingState.Tracking) {
417
- removed.push(input);
418
- }
419
- } else {
420
- if (nowState === XRTrackingState.Tracking) {
421
- added.push(input);
422
- }
423
- }
424
- statusSnapshot[i2] = nowState;
425
- }
426
- // Dispatch change event
427
- if (added.length > 0 || removed.length > 0) {
428
- var listeners = this._listeners.getLoopArray();
429
- for(var i3 = 0, n3 = listeners.length; i3 < n3; i3++){
430
- var listener = listeners[i3];
431
- !listener.destroyed && listener.fn(added, removed);
432
- }
433
- }
434
- };
435
- /**
436
- * @internal
437
- */ _proto._onDestroy = function _onDestroy() {
438
- this._listeners.findAndRemove(function(value) {
439
- return value.destroyed = true;
440
- });
441
- };
442
- _proto._handleEvent = function _handleEvent(event) {
443
- var input = this._trackedDevices[event.input];
444
- switch(event.targetRayMode){
445
- case XRTargetRayMode.TrackedPointer:
446
- switch(event.type){
447
- case XRInputEventType.SelectStart:
448
- input.down |= XRInputButton.Select;
449
- input.pressedButtons |= XRInputButton.Select;
450
- break;
451
- case XRInputEventType.SelectEnd:
452
- input.up |= XRInputButton.Select;
453
- input.pressedButtons &= ~XRInputButton.Select;
454
- break;
455
- case XRInputEventType.SqueezeStart:
456
- input.down |= XRInputButton.Squeeze;
457
- input.pressedButtons |= XRInputButton.Squeeze;
458
- break;
459
- case XRInputEventType.SqueezeEnd:
460
- input.up |= XRInputButton.Squeeze;
461
- input.pressedButtons &= ~XRInputButton.Squeeze;
462
- break;
463
- }
464
- break;
465
- case XRTargetRayMode.Screen:
466
- var _this = this, engine = _this._engine;
467
- // @ts-ignore
468
- var target = engine.inputManager._pointerManager._target;
469
- // @ts-ignore
470
- var canvas = engine.canvas._webCanvas;
471
- var clientWidth = canvas.clientWidth, clientHeight = canvas.clientHeight;
472
- var clientX = clientWidth * (event.x + 1) * 0.5;
473
- var clientY = clientHeight * (event.y + 1) * 0.5;
474
- // @ts-ignore
475
- switch(event.type){
476
- case XRInputEventType.SelectStart:
477
- target.dispatchEvent(this._makeUpPointerEvent("pointerdown", event.id, clientX, clientY));
478
- break;
479
- case XRInputEventType.Select:
480
- target.dispatchEvent(this._makeUpPointerEvent("pointermove", event.id, clientX, clientY));
481
- break;
482
- case XRInputEventType.SelectEnd:
483
- target.dispatchEvent(this._makeUpPointerEvent("pointerup", event.id, clientX, clientY));
484
- target.dispatchEvent(this._makeUpPointerEvent("pointerleave", event.id, clientX, clientY));
485
- break;
486
- }
487
- break;
488
- }
489
- };
490
- _proto._makeUpPointerEvent = function _makeUpPointerEvent(type, pointerId, clientX, clientY) {
491
- var eventInitDict = {
492
- pointerId: pointerId,
493
- clientX: clientX,
494
- clientY: clientY
495
- };
496
- switch(type){
497
- case "pointerdown":
498
- eventInitDict.button = 0;
499
- eventInitDict.buttons = 1;
500
- break;
501
- case "pointermove":
502
- eventInitDict.button = -1;
503
- eventInitDict.buttons = 1;
504
- break;
505
- case "pointerup":
506
- eventInitDict.button = 0;
507
- eventInitDict.buttons = 0;
508
- break;
509
- case "pointerleave":
510
- eventInitDict.button = 0;
511
- eventInitDict.buttons = 0;
512
- break;
513
- }
514
- return new PointerEvent(type, eventInitDict);
515
- };
516
- return XRInputManager;
517
- }();
518
-
519
- /**
520
- * The type of XR session.
521
- */ var XRSessionMode = /*#__PURE__*/ function(XRSessionMode) {
522
- XRSessionMode[XRSessionMode["None"] = 0] = "None";
523
- XRSessionMode[XRSessionMode["AR"] = 1] = "AR";
524
- XRSessionMode[XRSessionMode["VR"] = 2] = "VR";
525
- return XRSessionMode;
526
- }({});
527
-
528
- /**
529
- * XRSessionManager manages the life cycle of XR sessions.
530
- */ var XRSessionManager = /*#__PURE__*/ function() {
531
- function XRSessionManager(_xrManager, _engine) {
532
- this._xrManager = _xrManager;
533
- this._engine = _engine;
534
- this._mode = XRSessionMode.None;
535
- this._state = XRSessionState.None;
536
- this._listeners = new SafeLoopArray();
537
- // @ts-ignore
538
- this._rhi = _engine._hardwareRenderer;
539
- this._raf = requestAnimationFrame.bind(window);
540
- this._caf = cancelAnimationFrame.bind(window);
541
- this._onSessionExit = this._onSessionExit.bind(this);
542
- }
543
- var _proto = XRSessionManager.prototype;
544
- /**
545
- * Check if the specified mode is supported.
546
- * @param mode - The mode to check
547
- * @returns A promise that resolves if the mode is supported, otherwise rejects
548
- */ _proto.isSupportedMode = function isSupportedMode(mode) {
549
- return this._xrManager._platformDevice.isSupportedSessionMode(mode);
550
- };
551
- /**
552
- * Run the session.
553
- */ _proto.run = function run() {
554
- var _this = this, platformSession = _this._platformSession, engine = _this._engine;
555
- if (!platformSession) {
556
- throw new Error("Without session to run.");
557
- }
558
- platformSession.start();
559
- this._setState(XRSessionState.Running);
560
- this._xrManager._onSessionStart();
561
- if (!engine.isPaused) {
562
- engine.pause();
563
- engine.resume();
564
- }
565
- };
566
- /**
567
- * Stop the session.
568
- */ _proto.stop = function stop() {
569
- var _this = this, platformSession = _this._platformSession, engine = _this._engine, rhi = _this._rhi;
570
- if (!platformSession) {
571
- throw new Error("Without session to stop.");
572
- }
573
- if (this._state !== XRSessionState.Running) {
574
- throw new Error("Session is not running.");
575
- }
576
- rhi._mainFrameBuffer = null;
577
- rhi._mainFrameWidth = rhi._mainFrameHeight = 0;
578
- platformSession.stop();
579
- this._setState(XRSessionState.Paused);
580
- this._xrManager._onSessionStop();
581
- if (!engine.isPaused) {
582
- engine.pause();
583
- engine.resume();
584
- }
585
- };
586
- /**
587
- * Add a listening function for session state changes.
588
- * @param listener - The listening function
589
- */ _proto.addStateChangedListener = function addStateChangedListener(listener) {
590
- this._listeners.push({
591
- fn: listener
592
- });
593
- };
594
- /**
595
- * Remove a listening function of session state changes.
596
- * @param listener - The listening function
597
- */ _proto.removeStateChangedListener = function removeStateChangedListener(listener) {
598
- this._listeners.findAndRemove(function(value) {
599
- return value.fn === listener ? value.destroyed = true : false;
600
- });
601
- };
602
- /**
603
- * @internal
604
- */ _proto._setState = function _setState(value) {
605
- this._state = value;
606
- var listeners = this._listeners.getLoopArray();
607
- for(var i = 0, n = listeners.length; i < n; i++){
608
- var listener = listeners[i];
609
- !listener.destroyed && listener.fn(value);
610
- }
611
- };
612
- /**
613
- * @internal
614
- */ _proto._initialize = function _initialize(mode, features) {
615
- var _this = this;
616
- return new Promise(function(resolve, reject) {
617
- var xrManager = _this._xrManager;
618
- // Initialize all features
619
- var platformFeatures = [];
620
- for(var i = 0, n = features.length; i < n; i++){
621
- var _features_i = features[i], platformFeature = _features_i._platformFeature;
622
- platformFeature && platformFeatures.push(platformFeature);
623
- }
624
- xrManager._platformDevice.requestSession(_this._rhi, mode, platformFeatures).then(function(platformSession) {
625
- _this._mode = mode;
626
- _this._platformSession = platformSession;
627
- _this._setState(XRSessionState.Initialized);
628
- platformSession.setSessionExitCallBack(_this._onSessionExit);
629
- platformSession.addEventListener();
630
- xrManager._onSessionInit();
631
- resolve();
632
- }, reject);
633
- });
634
- };
635
- /**
636
- * @internal
637
- */ _proto._onUpdate = function _onUpdate() {
638
- var _this = this, rhi = _this._rhi, platformSession = _this._platformSession;
639
- rhi._mainFrameBuffer = platformSession.framebuffer;
640
- rhi._mainFrameWidth = platformSession.framebufferWidth;
641
- rhi._mainFrameHeight = platformSession.framebufferHeight;
642
- };
643
- /**
644
- * @internal
645
- */ _proto._getRequestAnimationFrame = function _getRequestAnimationFrame() {
646
- if (this._state === XRSessionState.Running) {
647
- return this._platformSession.requestAnimationFrame;
648
- } else {
649
- return this._raf;
650
- }
651
- };
652
- /**
653
- * @internal
654
- */ _proto._getCancelAnimationFrame = function _getCancelAnimationFrame() {
655
- if (this._state === XRSessionState.Running) {
656
- return this._platformSession.cancelAnimationFrame;
657
- } else {
658
- return this._caf;
659
- }
660
- };
661
- /**
662
- * @internal
663
- */ _proto._exit = function _exit() {
664
- var _this = this, platformSession = _this._platformSession;
665
- if (!platformSession) {
666
- return Promise.reject("Without session to stop.");
667
- }
668
- return platformSession.end();
669
- };
670
- _proto._onSessionExit = function _onSessionExit() {
671
- var _this = this, rhi = _this._rhi, platformSession = _this._platformSession, engine = _this._engine;
672
- rhi._mainFrameBuffer = null;
673
- rhi._mainFrameWidth = rhi._mainFrameHeight = 0;
674
- platformSession.removeEventListener();
675
- this._platformSession = null;
676
- this._setState(XRSessionState.None);
677
- this._xrManager._onSessionExit();
678
- if (!engine.isPaused) {
679
- engine.pause();
680
- engine.resume();
681
- }
682
- };
683
- /**
684
- * @internal
685
- */ _proto._onDestroy = function _onDestroy() {
686
- this._listeners.findAndRemove(function(value) {
687
- return value.destroyed = true;
688
- });
689
- this._raf = this._caf = null;
690
- };
691
- _create_class(XRSessionManager, [
692
- {
693
- key: "mode",
694
- get: /**
695
- * The current session mode( AR or VR ).
696
- */ function get() {
697
- return this._mode;
698
- }
699
- },
700
- {
701
- key: "state",
702
- get: /**
703
- * Return the current session state.
704
- */ function get() {
705
- return this._state;
706
- }
707
- },
708
- {
709
- key: "supportedFrameRate",
710
- get: /**
711
- * Return a list of supported frame rates.(only available in-session)
712
- */ function get() {
713
- return this._platformSession.supportedFrameRates;
714
- }
715
- },
716
- {
717
- key: "frameRate",
718
- get: /**
719
- * Return the current frame rate as reported by the device.
720
- */ function get() {
721
- return this._platformSession.frameRate;
722
- }
723
- }
724
- ]);
725
- return XRSessionManager;
726
- }();
727
-
728
- /**
729
- * @internal
730
- */ var XRManagerExtended = /*#__PURE__*/ function(XRManager) {
731
- _inherits(XRManagerExtended, XRManager);
732
- function XRManagerExtended() {
733
- return XRManager.apply(this, arguments) || this;
734
- }
735
- var _proto = XRManagerExtended.prototype;
736
- _proto.isSupportedFeature = function isSupportedFeature(feature) {
737
- return this._platformDevice.isSupportedFeature(XRManagerExtended._featureMap.get(feature));
738
- };
739
- _proto.addFeature = function addFeature(type) {
740
- for(var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++){
741
- args[_key - 1] = arguments[_key];
742
- }
743
- if (this.sessionManager._platformSession) {
744
- throw new Error("Cannot add feature when the session is initialized.");
745
- }
746
- if (!this._platformDevice.isSupportedFeature(XRManagerExtended._featureMap.get(type))) {
747
- throw new Error("The feature is not supported");
748
- }
749
- var features = this.features;
750
- for(var i = 0, n = features.length; i < n; i++){
751
- if (_instanceof(features[i], type)) throw new Error("The feature has been added");
752
- }
753
- var feature = _construct(type, [].concat([
754
- this
755
- ], args));
756
- features.push(feature);
757
- return feature;
758
- };
759
- _proto.getFeature = function getFeature(type) {
760
- var features = this.features;
761
- for(var i = 0, n = features.length; i < n; i++){
762
- var feature = features[i];
763
- if (_instanceof(feature, type)) {
764
- return feature;
765
- }
766
- }
767
- };
768
- _proto.enterXR = function enterXR(sessionMode, autoRun) {
769
- var _this = this;
770
- if (autoRun === void 0) autoRun = true;
771
- var sessionManager = this.sessionManager;
772
- if (sessionManager._platformSession) {
773
- throw new Error("Please exit XR immersive mode first.");
774
- }
775
- if (!this._origin) {
776
- throw new Error("Please set origin before enter XR.");
777
- }
778
- return new Promise(function(resolve, reject) {
779
- // 1. Check if this xr mode is supported
780
- sessionManager.isSupportedMode(sessionMode).then(function() {
781
- sessionManager._setState(XRSessionState.Initializing);
782
- // 2. Initialize session
783
- sessionManager._initialize(sessionMode, _this.features).then(function() {
784
- autoRun && sessionManager.run();
785
- resolve();
786
- }, reject);
787
- }, reject);
788
- });
789
- };
790
- _proto.exitXR = function exitXR() {
791
- var _this = this;
792
- return new Promise(function(resolve, reject) {
793
- _this.sessionManager._exit().then(function() {
794
- resolve();
795
- }, reject);
796
- });
797
- };
798
- _proto._initialize = function _initialize(engine, xrDevice) {
799
- this._features = [];
800
- this._platformDevice = xrDevice;
801
- this.sessionManager = new XRSessionManager(this, engine);
802
- this.inputManager = new XRInputManager(this, engine);
803
- this.cameraManager = new XRCameraManager(this);
804
- };
805
- _proto._update = function _update() {
806
- var sessionManager = this.sessionManager;
807
- if (sessionManager.state !== XRSessionState.Running) return;
808
- sessionManager._onUpdate();
809
- this.inputManager._onUpdate();
810
- this.cameraManager._onUpdate();
811
- var features = this.features;
812
- for(var i = 0, n = features.length; i < n; i++){
813
- var feature = features[i];
814
- feature.enabled && feature._onUpdate();
815
- }
816
- };
817
- _proto._destroy = function _destroy() {
818
- var _this = this;
819
- if (this.sessionManager._platformSession) {
820
- this.exitXR().then(function() {
821
- _this.sessionManager._onDestroy();
822
- _this.inputManager._onDestroy();
823
- _this.cameraManager._onDestroy();
824
- });
825
- } else {
826
- this.sessionManager._onDestroy();
827
- this.inputManager._onDestroy();
828
- this.cameraManager._onDestroy();
829
- }
830
- };
831
- _proto._getRequestAnimationFrame = function _getRequestAnimationFrame() {
832
- return this.sessionManager._getRequestAnimationFrame();
833
- };
834
- _proto._getCancelAnimationFrame = function _getCancelAnimationFrame() {
835
- return this.sessionManager._getCancelAnimationFrame();
836
- };
837
- _proto._getCameraIgnoreClearFlags = function _getCameraIgnoreClearFlags(type) {
838
- return this.cameraManager._getIgnoreClearFlags(type);
839
- };
840
- /**
841
- * @internal
842
- */ _proto._onSessionStop = function _onSessionStop() {
843
- var features = this.features;
844
- for(var i = 0, n = features.length; i < n; i++){
845
- var feature = features[i];
846
- feature.enabled && feature._onSessionStop();
847
- }
848
- };
849
- /**
850
- * @internal
851
- */ _proto._onSessionInit = function _onSessionInit() {
852
- var features = this.features;
853
- for(var i = 0, n = features.length; i < n; i++){
854
- var feature = features[i];
855
- feature.enabled && feature._onSessionInit();
856
- }
857
- };
858
- /**
859
- * @internal
860
- */ _proto._onSessionStart = function _onSessionStart() {
861
- this.cameraManager._onSessionStart();
862
- var features = this.features;
863
- for(var i = 0, n = features.length; i < n; i++){
864
- var feature = features[i];
865
- feature.enabled && feature._onSessionStart();
866
- }
867
- };
868
- /**
869
- * @internal
870
- */ _proto._onSessionExit = function _onSessionExit() {
871
- this.cameraManager._onSessionExit();
872
- var features = this.features;
873
- for(var i = 0, n = features.length; i < n; i++){
874
- var feature = features[i];
875
- feature.enabled && feature._onSessionExit();
876
- feature._onDestroy();
877
- }
878
- features.length = 0;
879
- };
880
- _create_class(XRManagerExtended, [
881
- {
882
- key: "features",
883
- get: function get() {
884
- return this._features;
885
- }
886
- },
887
- {
888
- key: "origin",
889
- get: function get() {
890
- return this._origin;
891
- },
892
- set: function set(value) {
893
- if (this.sessionManager._platformSession) {
894
- throw new Error("Cannot set origin when the session is initialized.");
895
- }
896
- this._origin = value;
897
- }
898
- }
899
- ]);
900
- return XRManagerExtended;
901
- }(XRManager);
902
- /** @internal */ XRManagerExtended._featureMap = new Map();
903
- /**
904
- * @internal
905
- */ function registerXRFeature(type) {
906
- return function(feature) {
907
- XRManagerExtended._featureMap.set(feature, type);
908
- };
909
- }
910
- // 实现混入的函数
911
- function ApplyMixins(derivedCtor, baseCtors) {
912
- baseCtors.forEach(function(baseCtor) {
913
- Object.getOwnPropertyNames(baseCtor.prototype).forEach(function(name) {
914
- Object.defineProperty(derivedCtor.prototype, name, Object.getOwnPropertyDescriptor(baseCtor.prototype, name) || Object.create(null));
915
- });
916
- });
917
- }
918
- ApplyMixins(XRManager, [
919
- XRManagerExtended
920
- ]);
921
-
922
- /**
923
- * The base class of XR feature manager.
924
- */ var XRFeature = /*#__PURE__*/ function() {
925
- function XRFeature(_xrManager, _type) {
926
- for(var _len = arguments.length, args = new Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++){
927
- args[_key - 2] = arguments[_key];
928
- }
929
- var _xrManager__platformDevice;
930
- this._xrManager = _xrManager;
931
- this._type = _type;
932
- this._enabled = true;
933
- this._platformFeature = (_xrManager__platformDevice = _xrManager._platformDevice).createPlatformFeature.apply(_xrManager__platformDevice, [].concat([
934
- _type
935
- ], args));
936
- this._onEnable();
937
- }
938
- var _proto = XRFeature.prototype;
939
- /**
940
- * @internal
941
- */ _proto._onEnable = function _onEnable() {};
942
- /**
943
- * @internal
944
- */ _proto._onDisable = function _onDisable() {};
945
- /**
946
- * @internal
947
- */ _proto._onUpdate = function _onUpdate() {};
948
- /**
949
- * @internal
950
- */ _proto._onSessionInit = function _onSessionInit() {};
951
- /**
952
- * @internal
953
- */ _proto._onSessionStart = function _onSessionStart() {};
954
- /**
955
- * @internal
956
- */ _proto._onSessionStop = function _onSessionStop() {};
957
- /**
958
- * @internal
959
- */ _proto._onSessionExit = function _onSessionExit() {};
960
- /**
961
- * @internal
962
- */ _proto._onDestroy = function _onDestroy() {};
963
- _create_class(XRFeature, [
964
- {
965
- key: "enabled",
966
- get: /**
967
- * Returns whether the feature is enabled.
968
- */ function get() {
969
- return this._enabled;
970
- },
971
- set: function set(value) {
972
- if (this.enabled !== value) {
973
- this._enabled = value;
974
- value ? this._onEnable() : this._onDisable();
975
- }
976
- }
977
- }
978
- ]);
979
- return XRFeature;
980
- }();
981
-
982
- var XRFeatureType = /*#__PURE__*/ function(XRFeatureType) {
983
- XRFeatureType[XRFeatureType["AnchorTracking"] = 0] = "AnchorTracking";
984
- XRFeatureType[XRFeatureType["ImageTracking"] = 1] = "ImageTracking";
985
- XRFeatureType[XRFeatureType["PlaneTracking"] = 2] = "PlaneTracking";
986
- XRFeatureType[XRFeatureType["HitTest"] = 3] = "HitTest";
987
- return XRFeatureType;
988
- }({});
989
-
990
- var XRRequestTrackingState = /*#__PURE__*/ function(XRRequestTrackingState) {
991
- XRRequestTrackingState[XRRequestTrackingState["None"] = 0] = "None";
992
- XRRequestTrackingState[XRRequestTrackingState["Submitted"] = 1] = "Submitted";
993
- XRRequestTrackingState[XRRequestTrackingState["Resolved"] = 2] = "Resolved";
994
- XRRequestTrackingState[XRRequestTrackingState["Rejected"] = 3] = "Rejected";
995
- XRRequestTrackingState[XRRequestTrackingState["Destroyed"] = 4] = "Destroyed";
996
- XRRequestTrackingState[XRRequestTrackingState["WaitingDestroy"] = 5] = "WaitingDestroy";
997
- return XRRequestTrackingState;
998
- }({});
999
-
1000
- /**
1001
- * The base class of XR trackable manager.
1002
- */ var XRTrackableFeature = /*#__PURE__*/ function(XRFeature) {
1003
- _inherits(XRTrackableFeature, XRFeature);
1004
- function XRTrackableFeature() {
1005
- var _this;
1006
- _this = XRFeature.apply(this, arguments) || this, _this._requestTrackings = [], _this._tracked = [], _this._added = [], _this._updated = [], _this._removed = [], _this._statusSnapshot = {}, _this._listeners = new SafeLoopArray();
1007
- return _this;
1008
- }
1009
- var _proto = XRTrackableFeature.prototype;
1010
- /**
1011
- * Add a listening function for tracked object changes.
1012
- * @param listener - The listening function
1013
- */ _proto.addChangedListener = function addChangedListener(listener) {
1014
- this._listeners.push({
1015
- fn: listener
1016
- });
1017
- };
1018
- /**
1019
- * Remove a listening function of tracked object changes.
1020
- * @param listener - The listening function
1021
- */ _proto.removeChangedListener = function removeChangedListener(listener) {
1022
- this._listeners.findAndRemove(function(value) {
1023
- return value.fn === listener ? value.destroyed = true : false;
1024
- });
1025
- };
1026
- _proto._onUpdate = function _onUpdate() {
1027
- var _this__xrManager_sessionManager = this._xrManager.sessionManager, platformSession = _this__xrManager_sessionManager._platformSession;
1028
- var platformFrame = platformSession.frame;
1029
- var _this = this, platformFeature = _this._platformFeature, requestTrackings = _this._requestTrackings, statusSnapshot = _this._statusSnapshot, allTracked = _this._tracked, added = _this._added, updated = _this._updated, removed = _this._removed;
1030
- if (!platformFrame || !requestTrackings.length) {
1031
- return;
1032
- }
1033
- if (!platformFeature.checkAvailable(platformSession, platformFrame, requestTrackings)) {
1034
- return;
1035
- }
1036
- added.length = updated.length = removed.length = 0;
1037
- platformFeature.getTrackedResult(platformSession, platformFrame, requestTrackings, this._generateTracked);
1038
- for(var i = 0, n = requestTrackings.length; i < n; i++){
1039
- var requestTracking = requestTrackings[i];
1040
- switch(requestTracking.state){
1041
- case XRRequestTrackingState.Destroyed:
1042
- var destroyedTracked = requestTracking.tracked;
1043
- for(var j = 0, n1 = destroyedTracked.length; j < n1; j++){
1044
- var tracked = destroyedTracked[j];
1045
- var trackId = tracked.id;
1046
- if (statusSnapshot[trackId] === XRTrackingState.Tracking) {
1047
- removed.push(tracked);
1048
- allTracked.splice(allTracked.indexOf(tracked), 1);
1049
- }
1050
- statusSnapshot[trackId] = XRTrackingState.NotTracking;
1051
- }
1052
- break;
1053
- case XRRequestTrackingState.Resolved:
1054
- var tracked1 = requestTracking.tracked;
1055
- for(var j1 = 0, n2 = tracked1.length; j1 < n2; j1++){
1056
- var trackedObject = tracked1[j1];
1057
- var trackId1 = trackedObject.id;
1058
- if (trackedObject.state === XRTrackingState.Tracking) {
1059
- if (statusSnapshot[trackId1] === XRTrackingState.Tracking) {
1060
- updated.push(trackedObject);
1061
- } else {
1062
- added.push(trackedObject);
1063
- statusSnapshot[trackId1] = XRTrackingState.Tracking;
1064
- allTracked.push(trackedObject);
1065
- }
1066
- } else {
1067
- if (statusSnapshot[trackId1] === XRTrackingState.Tracking) {
1068
- removed.push(trackedObject);
1069
- allTracked.splice(allTracked.indexOf(trackedObject), 1);
1070
- }
1071
- statusSnapshot[trackId1] = trackedObject.state;
1072
- }
1073
- }
1074
- break;
1075
- }
1076
- }
1077
- for(var i1 = requestTrackings.length - 1; i1 >= 0; i1--){
1078
- requestTrackings[i1].state === XRRequestTrackingState.Destroyed && requestTrackings.splice(i1, 1);
1079
- }
1080
- if (added.length > 0 || updated.length > 0 || removed.length > 0) {
1081
- var listeners = this._listeners.getLoopArray();
1082
- for(var i2 = 0, n3 = listeners.length; i2 < n3; i2++){
1083
- var listener = listeners[i2];
1084
- !listener.destroyed && listener.fn(added, updated, removed);
1085
- }
1086
- }
1087
- };
1088
- _proto._onSessionStop = function _onSessionStop() {
1089
- this._added.length = this._updated.length = this._removed.length = 0;
1090
- };
1091
- _proto._onSessionExit = function _onSessionExit() {
1092
- // prettier-ignore
1093
- this._requestTrackings.length = this._tracked.length = this._added.length = this._updated.length = this._removed.length = 0;
1094
- this._listeners.findAndRemove(function(value) {
1095
- return value.destroyed = true;
1096
- });
1097
- };
1098
- _proto._addRequestTracking = function _addRequestTracking(requestTracking) {
1099
- var _this = this, platformFeature = _this._platformFeature;
1100
- if (this._xrManager.sessionManager._platformSession && !platformFeature.canModifyRequestTrackingAfterInit) {
1101
- throw new Error(XRFeatureType[this._type] + " request tracking cannot be modified after initialization.");
1102
- }
1103
- this._requestTrackings.push(requestTracking);
1104
- platformFeature.onAddRequestTracking(requestTracking);
1105
- };
1106
- _proto._removeRequestTracking = function _removeRequestTracking(requestTracking) {
1107
- var _this = this, platformFeature = _this._platformFeature;
1108
- if (this._xrManager.sessionManager._platformSession && !platformFeature.canModifyRequestTrackingAfterInit) {
1109
- throw new Error(XRFeatureType[this._type] + " request tracking cannot be modified after initialization.");
1110
- }
1111
- platformFeature.onDelRequestTracking(requestTracking);
1112
- };
1113
- _proto._removeAllRequestTrackings = function _removeAllRequestTrackings() {
1114
- var _this = this, platformFeature = _this._platformFeature;
1115
- if (this._xrManager.sessionManager._platformSession && !platformFeature.canModifyRequestTrackingAfterInit) {
1116
- throw new Error(XRFeatureType[this._type] + " request tracking cannot be modified after initialization.");
1117
- }
1118
- var _this1 = this, requestTrackings = _this1._requestTrackings;
1119
- for(var i = 0, n = requestTrackings.length; i < n; i++){
1120
- platformFeature.onDelRequestTracking(requestTrackings[i]);
1121
- }
1122
- };
1123
- return XRTrackableFeature;
1124
- }(XRFeature);
1125
- XRTrackableFeature._uuid = 0;
1126
-
1127
- /**
1128
- * The base class of XR tracked object.
1129
- */ var XRTracked = function XRTracked() {
1130
- /** The pose of the trackable in XR space. */ this.pose = new XRPose();
1131
- /** The tracking state of the trackable. */ this.state = XRTrackingState.NotTracking;
1132
- };
1133
-
1134
- /**
1135
- * Enum for the types of hit test that can be performed.
1136
- * Note: currently only supports plane.
1137
- */ var TrackableType = /*#__PURE__*/ function(TrackableType) {
1138
- /** Tracked plane. */ TrackableType[TrackableType["Plane"] = 1] = "Plane";
1139
- /** All tracked objects. */ TrackableType[TrackableType["All"] = 1] = "All";
1140
- return TrackableType;
1141
- }({});
1142
-
1143
- /**
1144
- * XR hit result.
1145
- * It is the detection result returned by using XR HitTest feature.
1146
- */ var XRHitResult = function XRHitResult() {
1147
- /** The position of the hit point. */ this.point = new Vector3();
1148
- /** The normal of the hit point. */ this.normal = new Vector3();
1149
- };
1150
-
1151
- /******************************************************************************
1152
- Copyright (c) Microsoft Corporation.
1153
-
1154
- Permission to use, copy, modify, and/or distribute this software for any
1155
- purpose with or without fee is hereby granted.
1156
-
1157
- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
1158
- REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
1159
- AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
1160
- INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
1161
- LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
1162
- OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
1163
- PERFORMANCE OF THIS SOFTWARE.
1164
- ***************************************************************************** */
1165
-
1166
- function __decorate(decorators, target, key, desc) {
1167
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
1168
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
1169
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
1170
- return c > 3 && r && Object.defineProperty(target, key, r), r;
1171
- }
1172
-
1173
- typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
1174
- var e = new Error(message);
1175
- return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
1176
- };
1177
-
1178
- /**
1179
- * Enumerates modes of plane in XR.
1180
- */ var XRPlaneMode = /*#__PURE__*/ function(XRPlaneMode) {
1181
- /** None. */ XRPlaneMode[XRPlaneMode["None"] = 0] = "None";
1182
- /** Horizontal */ XRPlaneMode[XRPlaneMode["Horizontal"] = 1] = "Horizontal";
1183
- /** Vertical */ XRPlaneMode[XRPlaneMode["Vertical"] = 2] = "Vertical";
1184
- /** Includes horizontal and vertical. */ XRPlaneMode[XRPlaneMode["EveryThing"] = 3] = "EveryThing";
1185
- return XRPlaneMode;
1186
- }({});
1187
-
1188
- /**
1189
- * @internal
1190
- */ var XRRequestTracking = function XRRequestTracking() {
1191
- /** The status of the current request tracking. */ this.state = XRRequestTrackingState.None;
1192
- /** Tracked instances, make up from the tracking data returned by Session. */ this.tracked = [];
1193
- };
1194
-
1195
- /**
1196
- * The request plane in XR space.
1197
- */ var XRRequestPlane = /*#__PURE__*/ function(XRRequestTracking) {
1198
- _inherits(XRRequestPlane, XRRequestTracking);
1199
- function XRRequestPlane(detectionMode) {
1200
- var _this;
1201
- _this = XRRequestTracking.call(this) || this, _this.detectionMode = detectionMode;
1202
- return _this;
1203
- }
1204
- return XRRequestPlane;
1205
- }(XRRequestTracking);
1206
-
1207
- /**
1208
- * The tracked plane in XR space.
1209
- */ var XRTrackedPlane = /*#__PURE__*/ function(XRTracked) {
1210
- _inherits(XRTrackedPlane, XRTracked);
1211
- function XRTrackedPlane() {
1212
- var _this;
1213
- _this = XRTracked.apply(this, arguments) || this, /** The points that make up this plane.
1214
- * Note: These points are in the plane coordinate system,
1215
- * and their Y coordinates are all zero.
1216
- */ _this.polygon = [], /**
1217
- * Whether this frame changes the attributes of the plane.
1218
- * Note: Includes `polygon` but no `pose`.
1219
- */ _this.attributesDirty = false;
1220
- return _this;
1221
- }
1222
- return XRTrackedPlane;
1223
- }(XRTracked);
1224
-
1225
- var XRPlaneTracking = /*#__PURE__*/ function(XRTrackableFeature1) {
1226
- _inherits(XRPlaneTracking, XRTrackableFeature1);
1227
- function XRPlaneTracking(xrManager, detectionMode) {
1228
- if (detectionMode === void 0) detectionMode = XRPlaneMode.EveryThing;
1229
- var _this;
1230
- _this = XRTrackableFeature1.call(this, xrManager, XRFeatureType.PlaneTracking, detectionMode) || this;
1231
- _this._addRequestTracking(new XRRequestPlane(detectionMode));
1232
- return _this;
1233
- }
1234
- var _proto = XRPlaneTracking.prototype;
1235
- _proto._generateTracked = function _generateTracked() {
1236
- var plane = new XRTrackedPlane();
1237
- plane.id = XRTrackableFeature._uuid++;
1238
- return plane;
1239
- };
1240
- _create_class(XRPlaneTracking, [
1241
- {
1242
- key: "detectionMode",
1243
- get: /**
1244
- * The plane detection mode.
1245
- */ function get() {
1246
- return this._requestTrackings[0].detectionMode;
1247
- }
1248
- },
1249
- {
1250
- key: "trackedPlanes",
1251
- get: /**
1252
- * The tracked planes.
1253
- */ function get() {
1254
- return this._tracked;
1255
- }
1256
- }
1257
- ]);
1258
- return XRPlaneTracking;
1259
- }(XRTrackableFeature);
1260
- XRPlaneTracking = __decorate([
1261
- registerXRFeature(XRFeatureType.PlaneTracking)
1262
- ], XRPlaneTracking);
1263
-
1264
- var XRHitTest = /*#__PURE__*/ function(XRFeature) {
1265
- _inherits(XRHitTest, XRFeature);
1266
- function XRHitTest(xrManager) {
1267
- var _this;
1268
- _this = XRFeature.call(this, xrManager, XRFeatureType.HitTest) || this, _this._tempRay = new Ray(), _this._tempPlane = new Plane(), _this._tempVec2 = new Vector2(), _this._tempVec30 = new Vector3(), _this._tempVec31 = new Vector3(), _this._tempVec32 = new Vector3(), _this._tempVec33 = new Vector3(), _this._tempVec34 = new Vector3(), _this._tempVec35 = new Vector3();
1269
- return _this;
1270
- }
1271
- var _proto = XRHitTest.prototype;
1272
- /**
1273
- * Hit test.
1274
- * @param ray - The ray to test
1275
- * @param type - The type of hit test
1276
- * @returns The hit result
1277
- */ _proto.hitTest = function hitTest(ray, type) {
1278
- var result = [];
1279
- if (type & TrackableType.Plane) {
1280
- this._hitTestPlane(ray, result);
1281
- }
1282
- return result;
1283
- };
1284
- /**
1285
- * Screen hit test.
1286
- * @param x - The x coordinate of the screen point
1287
- * @param y - The y coordinate of the screen point
1288
- * @param type - The type of hit test
1289
- * @returns The hit result
1290
- */ _proto.screenHitTest = function screenHitTest(x, y, type) {
1291
- var _this = this, xrManager = _this._xrManager;
1292
- if (xrManager.sessionManager.mode !== XRSessionMode.AR) {
1293
- throw new Error("Only AR mode supports using screen ray hit test.");
1294
- }
1295
- var _xrManager_inputManager_getTrackedDevice = xrManager.inputManager.getTrackedDevice(XRTrackedInputDevice.Camera), camera = _xrManager_inputManager_getTrackedDevice._camera;
1296
- if (!camera) {
1297
- throw new Error("No camera available.");
1298
- }
1299
- var ray = camera.screenPointToRay(this._tempVec2.set(x, y), this._tempRay);
1300
- return this.hitTest(ray, type);
1301
- };
1302
- _proto._hitTestPlane = function _hitTestPlane(ray, result) {
1303
- var planeManager = this._xrManager.getFeature(XRPlaneTracking);
1304
- if (!planeManager || !planeManager.enabled) {
1305
- throw new Error("The plane estimation function needs to be turned on for plane hit test.");
1306
- }
1307
- var _this = this, plane = _this._tempPlane, normal = _this._tempVec30, hitPoint = _this._tempVec31, hitPointInPlane = _this._tempVec32;
1308
- var trackedPlanes = planeManager.trackedPlanes;
1309
- for(var i = 0, n = trackedPlanes.length; i < n; i++){
1310
- var trackedPlane = trackedPlanes[i];
1311
- normal.set(0, 1, 0).transformNormal(trackedPlane.pose.matrix);
1312
- plane.normal.copyFrom(normal);
1313
- plane.distance = -Vector3.dot(normal, trackedPlane.pose.position);
1314
- var distance = ray.intersectPlane(plane);
1315
- if (distance >= 0) {
1316
- ray.getPoint(distance, hitPoint);
1317
- Vector3.transformToVec3(hitPoint, trackedPlane.pose.inverseMatrix, hitPointInPlane);
1318
- // Check if the hit position is within the plane boundary.
1319
- if (this._checkPointerWithinPlane(hitPointInPlane, trackedPlane)) {
1320
- var hitResult = new XRHitResult();
1321
- hitResult.point.copyFrom(hitPoint);
1322
- hitResult.normal.copyFrom(normal);
1323
- hitResult.distance = distance;
1324
- hitResult.trackedObject = trackedPlane;
1325
- hitResult.trackableType = TrackableType.Plane;
1326
- result.push(hitResult);
1327
- }
1328
- }
1329
- }
1330
- };
1331
- _proto._checkPointerWithinPlane = function _checkPointerWithinPlane(pointer, plane) {
1332
- var _this = this, preToCur = _this._tempVec33, preToPointer = _this._tempVec34, cross = _this._tempVec35;
1333
- var polygon = plane.polygon;
1334
- var length = polygon.length;
1335
- var prePoint = polygon[length - 1];
1336
- var side = 0;
1337
- for(var i = 0; i < length; i++){
1338
- var curPoint = polygon[i];
1339
- Vector3.subtract(curPoint, prePoint, preToCur);
1340
- Vector3.subtract(pointer, prePoint, preToPointer);
1341
- Vector3.cross(preToCur, preToPointer, cross);
1342
- var y = cross.y;
1343
- if (side === 0) {
1344
- if (y > 0) {
1345
- side = 1;
1346
- } else if (y < 0) {
1347
- side = -1;
1348
- }
1349
- } else {
1350
- if (y > 0 && side < 0 || y < 0 && side > 0) {
1351
- return false;
1352
- }
1353
- }
1354
- prePoint = curPoint;
1355
- }
1356
- return true;
1357
- };
1358
- return XRHitTest;
1359
- }(XRFeature);
1360
- XRHitTest = __decorate([
1361
- registerXRFeature(XRFeatureType.HitTest)
1362
- ], XRHitTest);
1363
-
1364
- /**
1365
- * The anchor in XR space.
1366
- */ var XRAnchor = /*#__PURE__*/ function(XRTracked) {
1367
- _inherits(XRAnchor, XRTracked);
1368
- function XRAnchor() {
1369
- return XRTracked.apply(this, arguments) || this;
1370
- }
1371
- return XRAnchor;
1372
- }(XRTracked);
1373
-
1374
- /**
1375
- * The request anchor in XR space.
1376
- */ var XRRequestAnchor = /*#__PURE__*/ function(XRRequestTracking) {
1377
- _inherits(XRRequestAnchor, XRRequestTracking);
1378
- function XRRequestAnchor(position, rotation) {
1379
- var _this;
1380
- _this = XRRequestTracking.call(this) || this, _this.position = position, _this.rotation = rotation;
1381
- return _this;
1382
- }
1383
- return XRRequestAnchor;
1384
- }(XRRequestTracking);
1385
-
1386
- var XRAnchorTracking = /*#__PURE__*/ function(XRTrackableFeature1) {
1387
- _inherits(XRAnchorTracking, XRTrackableFeature1);
1388
- function XRAnchorTracking(xrManager) {
1389
- var _this;
1390
- _this = XRTrackableFeature1.call(this, xrManager, XRFeatureType.AnchorTracking) || this, _this._anchors = [];
1391
- return _this;
1392
- }
1393
- var _proto = XRAnchorTracking.prototype;
1394
- /**
1395
- * Add a anchor in XR space.
1396
- * @param anchor - The anchor to be added
1397
- */ _proto.addAnchor = function addAnchor(position, rotation) {
1398
- if (!this._enabled) {
1399
- throw new Error("Cannot add an anchor from a disabled anchor manager.");
1400
- }
1401
- var _this = this, anchors = _this._anchors;
1402
- var requestAnchor = new XRRequestAnchor(position, rotation);
1403
- var xrAnchor = this._generateTracked();
1404
- requestAnchor.tracked[0] = xrAnchor;
1405
- this._addRequestTracking(requestAnchor);
1406
- anchors.push(xrAnchor);
1407
- return xrAnchor;
1408
- };
1409
- /**
1410
- * Remove a anchor in XR space.
1411
- * @param anchor - The anchor to be removed
1412
- */ _proto.removeAnchor = function removeAnchor(anchor) {
1413
- if (!this._enabled) {
1414
- throw new Error("Cannot remove an anchor from a disabled anchor manager.");
1415
- }
1416
- var _this = this, requestTrackings = _this._requestTrackings, anchors = _this._anchors;
1417
- for(var i = 0, n = requestTrackings.length; i < n; i++){
1418
- var requestAnchor = requestTrackings[i];
1419
- if (requestAnchor.tracked[0] === anchor) {
1420
- this._removeRequestTracking(requestAnchor);
1421
- break;
1422
- }
1423
- }
1424
- anchors.splice(anchors.indexOf(anchor), 1);
1425
- };
1426
- /**
1427
- * Remove all tracking anchors.
1428
- */ _proto.clearAnchors = function clearAnchors() {
1429
- if (!this._enabled) {
1430
- throw new Error("Cannot remove anchors from a disabled anchor manager.");
1431
- }
1432
- this._removeAllRequestTrackings();
1433
- };
1434
- _proto._generateTracked = function _generateTracked() {
1435
- var anchor = new XRAnchor();
1436
- anchor.id = XRTrackableFeature._uuid++;
1437
- return anchor;
1438
- };
1439
- _create_class(XRAnchorTracking, [
1440
- {
1441
- key: "trackingAnchors",
1442
- get: /**
1443
- * The anchors to tracking.
1444
- */ function get() {
1445
- return this._anchors;
1446
- }
1447
- },
1448
- {
1449
- key: "trackedAnchors",
1450
- get: /**
1451
- * The tracked anchors.
1452
- */ function get() {
1453
- return this._tracked;
1454
- }
1455
- }
1456
- ]);
1457
- return XRAnchorTracking;
1458
- }(XRTrackableFeature);
1459
- XRAnchorTracking = __decorate([
1460
- registerXRFeature(XRFeatureType.AnchorTracking)
1461
- ], XRAnchorTracking);
1462
-
1463
- /**
1464
- * The request image in XR space.
1465
- */ var XRRequestImage = /*#__PURE__*/ function(XRRequestTracking) {
1466
- _inherits(XRRequestImage, XRRequestTracking);
1467
- function XRRequestImage(image) {
1468
- var _this;
1469
- _this = XRRequestTracking.call(this) || this, _this.image = image;
1470
- return _this;
1471
- }
1472
- return XRRequestImage;
1473
- }(XRRequestTracking);
1474
-
1475
- /**
1476
- * A tracked image in XR space.
1477
- */ var XRTrackedImage = /*#__PURE__*/ function(XRTracked) {
1478
- _inherits(XRTrackedImage, XRTracked);
1479
- function XRTrackedImage() {
1480
- return XRTracked.apply(this, arguments) || this;
1481
- }
1482
- return XRTrackedImage;
1483
- }(XRTracked);
1484
-
1485
- var XRImageTracking = /*#__PURE__*/ function(XRTrackableFeature1) {
1486
- _inherits(XRImageTracking, XRTrackableFeature1);
1487
- function XRImageTracking(xrManager, trackingImages) {
1488
- var _this;
1489
- _this = XRTrackableFeature1.call(this, xrManager, XRFeatureType.ImageTracking, trackingImages) || this;
1490
- _this._trackingImages = trackingImages;
1491
- var imageLength = trackingImages ? trackingImages.length : 0;
1492
- if (imageLength > 0) {
1493
- for(var i = 0, n = trackingImages.length; i < n; i++){
1494
- _this._addRequestTracking(new XRRequestImage(trackingImages[i]));
1495
- }
1496
- } else {
1497
- console.warn("No image to be tracked.");
1498
- }
1499
- return _this;
1500
- }
1501
- var _proto = XRImageTracking.prototype;
1502
- _proto._generateTracked = function _generateTracked() {
1503
- var image = new XRTrackedImage();
1504
- image.id = XRTrackableFeature._uuid++;
1505
- return image;
1506
- };
1507
- _create_class(XRImageTracking, [
1508
- {
1509
- key: "trackingImages",
1510
- get: /**
1511
- * The image to tracking.
1512
- */ function get() {
1513
- return this._trackingImages;
1514
- }
1515
- },
1516
- {
1517
- key: "trackedImages",
1518
- get: /**
1519
- * The tracked images.
1520
- */ function get() {
1521
- return this._tracked;
1522
- }
1523
- }
1524
- ]);
1525
- return XRImageTracking;
1526
- }(XRTrackableFeature);
1527
- XRImageTracking = __decorate([
1528
- registerXRFeature(XRFeatureType.ImageTracking)
1529
- ], XRImageTracking);
1530
-
1531
- /**
1532
- * A reference image is an image to look for in the physical environment.
1533
- */ var XRReferenceImage = function XRReferenceImage(name, imageSource, physicalWidth) {
1534
- this.name = name;
1535
- this.imageSource = imageSource;
1536
- this.physicalWidth = physicalWidth;
1537
- };
1538
-
1539
- var XRReferenceImageDecoder = /*#__PURE__*/ function() {
1540
- function XRReferenceImageDecoder() {}
1541
- XRReferenceImageDecoder.decode = function decode(engine, bufferReader) {
1542
- return new AssetPromise(function(resolve, reject) {
1543
- var physicalWidth = bufferReader.nextFloat32();
1544
- bufferReader.nextUint8();
1545
- var img = new Image();
1546
- img.onload = function() {
1547
- resolve(new XRReferenceImage("", img, physicalWidth));
1548
- };
1549
- img.src = URL.createObjectURL(new window.Blob([
1550
- bufferReader.nextImagesData(1)[0]
1551
- ]));
1552
- });
1553
- };
1554
- return XRReferenceImageDecoder;
1555
- }();
1556
- XRReferenceImageDecoder = __decorate([
1557
- decoder("XRReferenceImage")
1558
- ], XRReferenceImageDecoder);
1559
-
1560
- function _extends() {
1561
- _extends = Object.assign || function assign(target) {
1562
- for (var i = 1; i < arguments.length; i++) {
1563
- var source = arguments[i];
1564
- for (var key in source) if (Object.prototype.hasOwnProperty.call(source, key)) target[key] = source[key];
1565
- }
1566
-
1567
- return target;
1568
- };
1569
-
1570
- return _extends.apply(this, arguments);
1571
- }
1572
-
1573
- var XRReferenceImageLoader = /*#__PURE__*/ function(Loader) {
1574
- _inherits(XRReferenceImageLoader, Loader);
1575
- function XRReferenceImageLoader() {
1576
- return Loader.apply(this, arguments) || this;
1577
- }
1578
- var _proto = XRReferenceImageLoader.prototype;
1579
- _proto.load = function load(item, resourceManager) {
1580
- return new AssetPromise(function(resolve, reject) {
1581
- resourceManager// @ts-ignore
1582
- ._request(item.url, _extends({}, item, {
1583
- type: "arraybuffer"
1584
- })).then(function(data) {
1585
- decode(data, resourceManager.engine).then(function(referenceImage) {
1586
- resolve(referenceImage);
1587
- });
1588
- }).catch(reject);
1589
- });
1590
- };
1591
- return XRReferenceImageLoader;
1592
- }(Loader);
1593
- XRReferenceImageLoader = __decorate([
1594
- resourceLoader("XRReferenceImage", [])
1595
- ], XRReferenceImageLoader);
1596
-
1597
- export { TrackableType, XRAnchor, XRAnchorTracking, XRCamera, XRCameraManager, XRController, XRFeature, XRFeatureType, XRHitResult, XRHitTest, XRImageTracking, XRInputButton, XRInputEventType, XRInputManager, XRPlaneMode, XRPlaneTracking, XRPose, XRReferenceImage, XRReferenceImageDecoder, XRReferenceImageLoader, XRRequestTrackingState, XRSessionManager, XRSessionMode, XRSessionState, XRTargetRayMode, XRTrackableFeature, XRTracked, XRTrackedImage, XRTrackedInputDevice, XRTrackedPlane, XRTrackingState };
1598
- //# sourceMappingURL=module.js.map