|
5 | 5 | * LICENSE file in the root directory of this source tree. |
6 | 6 | */ |
7 | 7 |
|
8 | | -/** |
9 | | - * Type declarations for WebXR Anchors API persistence features. |
10 | | - * |
11 | | - * These extensions to the WebXR Device API allow anchors to persist across sessions, |
12 | | - * enabling virtual objects to maintain their real-world positions even after the |
13 | | - * application is closed and reopened. |
14 | | - * |
15 | | - * @see {@link https://github.com/immersive-web/anchors/blob/main/explainer.md WebXR Anchors Module} |
16 | | - */ |
17 | | -declare global { |
18 | | - interface XRAnchor { |
19 | | - /** |
20 | | - * Requests a persistent handle (UUID) for this anchor. |
21 | | - * The UUID can be used with restorePersistentAnchor() to restore the anchor in future sessions. |
22 | | - */ |
23 | | - requestPersistentHandle(): Promise<string>; |
24 | | - /** |
25 | | - * Deletes this anchor, removing it from tracking. |
26 | | - */ |
27 | | - delete(): void; |
28 | | - } |
29 | | - |
30 | | - interface XRSession { |
31 | | - /** |
32 | | - * Array of UUIDs for all persistent anchors available in this session. |
33 | | - */ |
34 | | - readonly persistentAnchors: readonly string[]; |
35 | | - /** |
36 | | - * Restores a previously created persistent anchor using its UUID. |
37 | | - * @param uuid - The persistent handle returned from requestPersistentHandle() |
38 | | - * @returns The restored anchor, or rejects if the anchor cannot be found |
39 | | - */ |
40 | | - restorePersistentAnchor(uuid: string): Promise<XRAnchor>; |
41 | | - /** |
42 | | - * Permanently deletes a persistent anchor. |
43 | | - * @param uuid - The persistent handle of the anchor to delete |
44 | | - */ |
45 | | - deletePersistentAnchor(uuid: string): Promise<void>; |
46 | | - } |
47 | | -} |
48 | | - |
49 | 8 | import { createSystem, Entity, Types } from '../ecs/index.js'; |
50 | 9 | import { |
51 | 10 | BoxGeometry, |
@@ -471,6 +430,13 @@ export class SceneUnderstandingSystem extends createSystem( |
471 | 430 |
|
472 | 431 | // Attempt to restore the anchor using the saved UUID |
473 | 432 | this.anchorRequested = true; |
| 433 | + |
| 434 | + if (!session.restorePersistentAnchor) { |
| 435 | + console.warn('XRSession.restorePersistentAnchor not supported'); |
| 436 | + this.anchorRequested = false; |
| 437 | + return; |
| 438 | + } |
| 439 | + |
474 | 440 | this.xrAnchor = await session.restorePersistentAnchor(savedUuid); |
475 | 441 |
|
476 | 442 | if (!this.xrAnchor) { |
@@ -513,11 +479,13 @@ export class SceneUnderstandingSystem extends createSystem( |
513 | 479 |
|
514 | 480 | // Request a persistent handle for the anchor so it can be restored in future sessions |
515 | 481 | try { |
516 | | - const uuid = await this.xrAnchor.requestPersistentHandle(); |
517 | | - localStorage.setItem( |
518 | | - SceneUnderstandingSystem.ANCHOR_UUID_STORAGE_KEY, |
519 | | - uuid, |
520 | | - ); |
| 482 | + if (this.xrAnchor.requestPersistentHandle) { |
| 483 | + const uuid = await this.xrAnchor.requestPersistentHandle(); |
| 484 | + localStorage.setItem( |
| 485 | + SceneUnderstandingSystem.ANCHOR_UUID_STORAGE_KEY, |
| 486 | + uuid, |
| 487 | + ); |
| 488 | + } |
521 | 489 | } catch (_error) { |
522 | 490 | // Persistence not supported or failed - anchor will work for this session only |
523 | 491 | } |
|
0 commit comments