@webspatial/core-sdk 1.0.4 → 1.0.5

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 (82) hide show
  1. package/CHANGELOG.md +2 -0
  2. package/README.md +112 -81
  3. package/dist/iife/index.d.ts +683 -561
  4. package/dist/iife/index.global.js +3 -4
  5. package/dist/iife/index.global.js.map +1 -1
  6. package/dist/index.d.ts +683 -561
  7. package/dist/index.js +2175 -1291
  8. package/dist/index.js.map +1 -1
  9. package/package.json +7 -4
  10. package/src/JSBCommand.ts +631 -0
  11. package/src/Spatial.ts +68 -0
  12. package/src/SpatialObject.ts +46 -0
  13. package/src/SpatialScene.ts +75 -0
  14. package/src/SpatialSession.ts +187 -0
  15. package/src/SpatialWebEvent.ts +23 -0
  16. package/src/SpatialWebEventCreator.ts +12 -0
  17. package/src/Spatialized2DElement.ts +51 -0
  18. package/src/SpatializedDynamic3DElement.ts +30 -0
  19. package/src/SpatializedElement.ts +331 -0
  20. package/src/SpatializedElementCreator.ts +45 -0
  21. package/src/SpatializedStatic3DElement.ts +111 -0
  22. package/src/WebMsgCommand.ts +88 -0
  23. package/src/index.ts +23 -1
  24. package/src/platform-adapter/CommandResultUtils.ts +22 -0
  25. package/src/platform-adapter/android/AndroidPlatform.ts +133 -0
  26. package/src/platform-adapter/index.ts +21 -0
  27. package/src/platform-adapter/interface.ts +36 -0
  28. package/src/platform-adapter/ssr/SSRPlatform.ts +43 -0
  29. package/src/platform-adapter/vision-os/VisionOSPlatform.ts +77 -0
  30. package/src/reality/component/ModelComponent.ts +11 -0
  31. package/src/reality/component/SpatialComponent.ts +17 -0
  32. package/src/reality/component/index.ts +2 -0
  33. package/src/reality/entity/SpatialEntity.ts +255 -0
  34. package/src/reality/entity/SpatialModelEntity.ts +15 -0
  35. package/src/reality/entity/index.ts +2 -0
  36. package/src/reality/geometry/SpatialBoxGeometry.ts +12 -0
  37. package/src/reality/geometry/SpatialConeGeometry.ts +15 -0
  38. package/src/reality/geometry/SpatialCylinderGeometry.ts +15 -0
  39. package/src/reality/geometry/SpatialGeometry.ts +12 -0
  40. package/src/reality/geometry/SpatialPlaneGeometry.ts +15 -0
  41. package/src/reality/geometry/SpatialSphereGeometry.ts +15 -0
  42. package/src/reality/geometry/index.ts +6 -0
  43. package/src/reality/index.ts +5 -0
  44. package/src/reality/material/SpatialMaterial.ts +14 -0
  45. package/src/reality/material/SpatialUnlitMaterial.ts +16 -0
  46. package/src/reality/material/index.ts +2 -0
  47. package/src/reality/realityCreator.ts +94 -0
  48. package/src/reality/resource/SpatialModelAsset.ts +11 -0
  49. package/src/reality/resource/index.ts +1 -0
  50. package/src/scene-polyfill.test.ts +376 -0
  51. package/src/scene-polyfill.ts +359 -0
  52. package/src/spatial-window-polyfill.ts +182 -0
  53. package/src/ssr-polyfill.ts +3 -0
  54. package/src/types/global.d.ts +33 -1
  55. package/src/types/internal.ts +13 -0
  56. package/src/types/types.ts +380 -0
  57. package/src/utils.ts +61 -0
  58. package/tsconfig.json +1 -1
  59. package/vitest.config.ts +8 -0
  60. package/src/core/Spatial.ts +0 -50
  61. package/src/core/SpatialEntity.ts +0 -147
  62. package/src/core/SpatialHelper.ts +0 -230
  63. package/src/core/SpatialObject.ts +0 -26
  64. package/src/core/SpatialSession.ts +0 -457
  65. package/src/core/SpatialTransform.ts +0 -26
  66. package/src/core/SpatialWindowContainer.ts +0 -59
  67. package/src/core/component/EventSpatialComponent.ts +0 -32
  68. package/src/core/component/SpatialComponent.ts +0 -26
  69. package/src/core/component/SpatialInputComponent.ts +0 -24
  70. package/src/core/component/SpatialModel3DComponent.ts +0 -223
  71. package/src/core/component/SpatialModelComponent.ts +0 -39
  72. package/src/core/component/SpatialViewComponent.ts +0 -32
  73. package/src/core/component/SpatialWindowComponent.ts +0 -177
  74. package/src/core/component/index.ts +0 -14
  75. package/src/core/index.ts +0 -10
  76. package/src/core/private/WebSpatial.ts +0 -383
  77. package/src/core/private/remote-command/RemoteCommand.ts +0 -15
  78. package/src/core/private/remote-command/index.ts +0 -1
  79. package/src/core/resource/SpatialMeshResource.ts +0 -6
  80. package/src/core/resource/SpatialPhysicallyBasedMaterialResource.ts +0 -42
  81. package/src/core/resource/index.ts +0 -2
  82. package/src/core/types.ts +0 -32
@@ -0,0 +1,631 @@
1
+ import { createPlatform } from './platform-adapter'
2
+ import { WebSpatialProtocolResult } from './platform-adapter/interface'
3
+ import { SpatialComponent } from './reality/component/SpatialComponent'
4
+ import { SpatialEntity } from './reality/entity/SpatialEntity'
5
+ import { SpatializedDynamic3DElement } from './SpatializedDynamic3DElement'
6
+ import { SpatializedElement } from './SpatializedElement'
7
+ import { SpatialObject } from './SpatialObject'
8
+
9
+ import {
10
+ Spatialized2DElementProperties,
11
+ SpatializedElementProperties,
12
+ SpatializedStatic3DElementProperties,
13
+ SpatialSceneProperties,
14
+ SpatialSceneCreationOptions,
15
+ SpatialUnlitMaterialOptions,
16
+ SpatialGeometryOptions,
17
+ SpatialGeometryType,
18
+ ModelComponentOptions,
19
+ SpatialEntityProperties,
20
+ ModelAssetOptions,
21
+ SpatialModelEntityCreationOptions,
22
+ SpatialEntityEventType,
23
+ Vec3,
24
+ } from './types/types'
25
+ import { SpatialSceneCreationOptionsInternal } from './types/internal'
26
+ import { composeSRT } from './utils'
27
+
28
+ const platform = createPlatform()
29
+
30
+ abstract class JSBCommand {
31
+ commandType: string = ''
32
+ protected abstract getParams(): Record<string, any> | undefined
33
+
34
+ async execute() {
35
+ const param = this.getParams()
36
+ const msg = param ? JSON.stringify(param) : ''
37
+ return platform.callJSB(this.commandType, msg)
38
+ }
39
+ }
40
+
41
+ export class UpdateEntityPropertiesCommand extends JSBCommand {
42
+ commandType = 'UpdateEntityProperties'
43
+
44
+ constructor(
45
+ public entity: SpatialEntity,
46
+ public properties: Partial<SpatialEntityProperties>,
47
+ ) {
48
+ super()
49
+ }
50
+
51
+ protected getParams() {
52
+ const transform = composeSRT(
53
+ this.properties.position ?? this.entity.position,
54
+ this.properties.rotation ?? this.entity.rotation,
55
+ this.properties.scale ?? this.entity.scale,
56
+ ).toFloat64Array()
57
+ return {
58
+ entityId: this.entity.id,
59
+ transform,
60
+ }
61
+ }
62
+ }
63
+
64
+ export class UpdateEntityEventCommand extends JSBCommand {
65
+ commandType = 'UpdateEntityEvent'
66
+
67
+ constructor(
68
+ public entity: SpatialEntity,
69
+ public type: SpatialEntityEventType,
70
+ public isEnable: boolean,
71
+ ) {
72
+ super()
73
+ }
74
+
75
+ protected getParams() {
76
+ return {
77
+ type: this.type,
78
+ entityId: this.entity.id,
79
+ isEnable: this.isEnable,
80
+ }
81
+ }
82
+ }
83
+
84
+ // todo: to be used in SpatialEntity
85
+ export class UpdateEntityEventsCommand extends JSBCommand {
86
+ // let types:[String:Bool]
87
+ // let entityId:String
88
+ constructor(
89
+ public entity: SpatialEntity,
90
+ public types: Record<SpatialEntityEventType, boolean>,
91
+ ) {
92
+ super()
93
+ }
94
+
95
+ protected getParams() {
96
+ return {
97
+ entityId: this.entity.id,
98
+ types: this.types,
99
+ }
100
+ }
101
+ }
102
+
103
+ export class UpdateSpatialSceneProperties extends JSBCommand {
104
+ properties: Partial<SpatialSceneProperties>
105
+ commandType = 'UpdateSpatialSceneProperties'
106
+
107
+ constructor(properties: Partial<SpatialSceneProperties>) {
108
+ super()
109
+ this.properties = properties
110
+ }
111
+
112
+ protected getParams() {
113
+ return this.properties
114
+ }
115
+ }
116
+
117
+ export class UpdateSceneConfig extends JSBCommand {
118
+ config: SpatialSceneCreationOptions
119
+ commandType = 'UpdateSceneConfig'
120
+
121
+ constructor(config: SpatialSceneCreationOptions) {
122
+ super()
123
+ this.config = config
124
+ }
125
+
126
+ protected getParams(): Record<string, any> | undefined {
127
+ return { config: this.config }
128
+ }
129
+ }
130
+
131
+ export class FocusScene extends JSBCommand {
132
+ commandType = 'FocusScene'
133
+
134
+ constructor(public id: string) {
135
+ super()
136
+ }
137
+
138
+ protected getParams(): Record<string, any> | undefined {
139
+ return { id: this.id }
140
+ }
141
+ }
142
+
143
+ export class GetSpatialSceneState extends JSBCommand {
144
+ commandType = 'GetSpatialSceneState'
145
+
146
+ constructor() {
147
+ super()
148
+ }
149
+
150
+ protected getParams(): Record<string, any> | undefined {
151
+ return {}
152
+ }
153
+ }
154
+
155
+ export abstract class SpatializedElementCommand extends JSBCommand {
156
+ constructor(readonly spatialObject: SpatialObject) {
157
+ super()
158
+ }
159
+
160
+ protected getParams() {
161
+ const extraParams = this.getExtraParams()
162
+ return { id: this.spatialObject.id, ...extraParams }
163
+ }
164
+
165
+ protected abstract getExtraParams(): Record<string, any> | undefined
166
+ }
167
+
168
+ export class UpdateSpatialized2DElementProperties extends SpatializedElementCommand {
169
+ properties: Partial<Spatialized2DElementProperties>
170
+ commandType = 'UpdateSpatialized2DElementProperties'
171
+
172
+ constructor(
173
+ spatialObject: SpatialObject,
174
+ properties: Partial<SpatializedElementProperties>,
175
+ ) {
176
+ super(spatialObject)
177
+ this.properties = properties
178
+ }
179
+
180
+ protected getExtraParams() {
181
+ return this.properties
182
+ }
183
+ }
184
+
185
+ export class UpdateSpatializedDynamic3DElementProperties extends SpatializedElementCommand {
186
+ properties: Partial<Spatialized2DElementProperties>
187
+ commandType = 'UpdateSpatializedDynamic3DElementProperties'
188
+
189
+ constructor(
190
+ spatialObject: SpatialObject,
191
+ properties: Partial<SpatializedElementProperties>,
192
+ ) {
193
+ super(spatialObject)
194
+ this.properties = properties
195
+ }
196
+
197
+ protected getExtraParams() {
198
+ return {
199
+ id: this.spatialObject.id,
200
+ ...this.properties,
201
+ }
202
+ }
203
+ }
204
+
205
+ export class UpdateUnlitMaterialProperties extends SpatializedElementCommand {
206
+ properties: Partial<SpatialUnlitMaterialOptions>
207
+ commandType = 'UpdateUnlitMaterialProperties'
208
+
209
+ constructor(
210
+ spatialObject: SpatialObject,
211
+ properties: Partial<SpatialUnlitMaterialOptions>,
212
+ ) {
213
+ super(spatialObject)
214
+ this.properties = properties
215
+ }
216
+
217
+ protected getExtraParams() {
218
+ return this.properties
219
+ }
220
+ }
221
+
222
+ export class UpdateSpatializedElementTransform extends SpatializedElementCommand {
223
+ matrix: DOMMatrix
224
+ commandType = 'UpdateSpatializedElementTransform'
225
+
226
+ constructor(spatialObject: SpatialObject, matrix: DOMMatrix) {
227
+ super(spatialObject)
228
+ this.matrix = matrix
229
+ }
230
+
231
+ protected getExtraParams() {
232
+ return { matrix: Array.from(this.matrix.toFloat64Array()) }
233
+ }
234
+ }
235
+
236
+ export class UpdateSpatializedStatic3DElementProperties extends SpatializedElementCommand {
237
+ properties: Partial<SpatializedStatic3DElementProperties>
238
+ commandType = 'UpdateSpatializedStatic3DElementProperties'
239
+
240
+ constructor(
241
+ spatialObject: SpatialObject,
242
+ properties: Partial<SpatializedStatic3DElementProperties>,
243
+ ) {
244
+ super(spatialObject)
245
+ this.properties = properties
246
+ }
247
+
248
+ protected getExtraParams() {
249
+ return this.properties
250
+ }
251
+ }
252
+
253
+ export class AddSpatializedElementToSpatialized2DElement extends SpatializedElementCommand {
254
+ commandType = 'AddSpatializedElementToSpatialized2DElement'
255
+ spatializedElement: SpatializedElement
256
+
257
+ constructor(
258
+ spatialObject: SpatialObject,
259
+ spatializedElement: SpatializedElement,
260
+ ) {
261
+ super(spatialObject)
262
+ this.spatializedElement = spatializedElement
263
+ }
264
+
265
+ protected getExtraParams() {
266
+ return { spatializedElementId: this.spatializedElement.id }
267
+ }
268
+ }
269
+
270
+ export class AddSpatializedElementToSpatialScene extends JSBCommand {
271
+ commandType = 'AddSpatializedElementToSpatialScene'
272
+ spatializedElement: SpatializedElement
273
+
274
+ constructor(spatializedElement: SpatializedElement) {
275
+ super()
276
+ this.spatializedElement = spatializedElement
277
+ }
278
+
279
+ protected getParams() {
280
+ return {
281
+ spatializedElementId: this.spatializedElement.id,
282
+ }
283
+ }
284
+ }
285
+
286
+ export class CreateSpatializedStatic3DElementCommand extends JSBCommand {
287
+ commandType = 'CreateSpatializedStatic3DElement'
288
+
289
+ constructor(readonly modelURL: string) {
290
+ super()
291
+ this.modelURL = modelURL
292
+ }
293
+
294
+ protected getParams() {
295
+ return { modelURL: this.modelURL }
296
+ }
297
+ }
298
+
299
+ export class CreateSpatializedDynamic3DElementCommand extends JSBCommand {
300
+ protected getParams(): Record<string, any> | undefined {
301
+ return { test: true }
302
+ }
303
+ commandType = 'CreateSpatializedDynamic3DElement'
304
+ }
305
+
306
+ export class CreateSpatialEntityCommand extends JSBCommand {
307
+ constructor(private name?: string) {
308
+ super()
309
+ }
310
+ protected getParams(): Record<string, any> | undefined {
311
+ return { name: this.name }
312
+ }
313
+ commandType = 'CreateSpatialEntity'
314
+ }
315
+
316
+ export class CreateModelComponentCommand extends JSBCommand {
317
+ constructor(private options: ModelComponentOptions) {
318
+ super()
319
+ }
320
+ protected getParams(): Record<string, any> | undefined {
321
+ let geometryId = this.options.mesh.id
322
+ let materialIds = this.options.materials.map(material => material.id)
323
+ return { geometryId, materialIds }
324
+ }
325
+ commandType = 'CreateModelComponent'
326
+ }
327
+
328
+ export class CreateSpatialModelEntityCommand extends JSBCommand {
329
+ constructor(private options: SpatialModelEntityCreationOptions) {
330
+ super()
331
+ }
332
+ protected getParams(): Record<string, any> | undefined {
333
+ return this.options
334
+ }
335
+ commandType = 'CreateSpatialModelEntity'
336
+ }
337
+
338
+ export class CreateModelAssetCommand extends JSBCommand {
339
+ constructor(private options: ModelAssetOptions) {
340
+ super()
341
+ }
342
+ protected getParams(): Record<string, any> | undefined {
343
+ return { url: this.options.url }
344
+ }
345
+ commandType = 'CreateModelAsset'
346
+ }
347
+
348
+ export class CreateSpatialGeometryCommand extends JSBCommand {
349
+ constructor(
350
+ private type: SpatialGeometryType,
351
+ private options: SpatialGeometryOptions = {},
352
+ ) {
353
+ super()
354
+ }
355
+ protected getParams(): Record<string, any> | undefined {
356
+ return { type: this.type, ...this.options }
357
+ }
358
+ commandType = 'CreateGeometry'
359
+ }
360
+
361
+ export class CreateSpatialUnlitMaterialCommand extends JSBCommand {
362
+ constructor(private options: SpatialUnlitMaterialOptions) {
363
+ super()
364
+ }
365
+ protected getParams(): Record<string, any> | undefined {
366
+ return this.options
367
+ }
368
+ commandType = 'CreateUnlitMaterial'
369
+ }
370
+
371
+ export class AddComponentToEntityCommand extends JSBCommand {
372
+ constructor(
373
+ public entity: SpatialEntity,
374
+ public comp: SpatialComponent,
375
+ ) {
376
+ super()
377
+ }
378
+ protected getParams(): Record<string, any> | undefined {
379
+ return {
380
+ entityId: this.entity.id,
381
+ componentId: this.comp.id,
382
+ }
383
+ }
384
+ commandType = 'AddComponentToEntity'
385
+ }
386
+
387
+ export class AddEntityToDynamic3DCommand extends JSBCommand {
388
+ constructor(
389
+ public d3dEle: SpatializedDynamic3DElement,
390
+ public entity: SpatialEntity,
391
+ ) {
392
+ super()
393
+ }
394
+ protected getParams(): Record<string, any> | undefined {
395
+ return {
396
+ entityId: this.entity.id,
397
+ dynamic3dId: this.d3dEle.id,
398
+ }
399
+ }
400
+
401
+ commandType = 'AddEntityToDynamic3D'
402
+ }
403
+
404
+ export class AddEntityToEntityCommand extends JSBCommand {
405
+ constructor(
406
+ public parent: SpatialEntity,
407
+ public child: SpatialEntity,
408
+ ) {
409
+ super()
410
+ }
411
+ protected getParams(): Record<string, any> | undefined {
412
+ return {
413
+ parentId: this.parent.id,
414
+ childId: this.child.id,
415
+ }
416
+ }
417
+ commandType = 'AddEntityToEntity'
418
+ }
419
+
420
+ export class RemoveEntityFromParentCommand extends JSBCommand {
421
+ constructor(public entity: SpatialEntity) {
422
+ super()
423
+ }
424
+ protected getParams(): Record<string, any> | undefined {
425
+ return {
426
+ entityId: this.entity.id,
427
+ }
428
+ }
429
+ commandType = 'RemoveEntityFromParent'
430
+ }
431
+
432
+ export class SetParentForEntityCommand extends JSBCommand {
433
+ // childId, parentId
434
+ constructor(
435
+ public childId: string,
436
+ public parentId?: string,
437
+ ) {
438
+ super()
439
+ }
440
+ protected getParams(): Record<string, any> | undefined {
441
+ return {
442
+ childId: this.childId,
443
+ parentId: this.parentId,
444
+ }
445
+ }
446
+ commandType = 'SetParentToEntity'
447
+ }
448
+
449
+ export class ConvertFromEntityToEntityCommand extends JSBCommand {
450
+ constructor(
451
+ public fromEntityId: string,
452
+ public toEntityId: string,
453
+ public fromPosition: Vec3,
454
+ ) {
455
+ super()
456
+ }
457
+ protected getParams(): Record<string, any> | undefined {
458
+ return {
459
+ fromEntityId: this.fromEntityId,
460
+ toEntityId: this.toEntityId,
461
+ position: this.fromPosition,
462
+ }
463
+ }
464
+ commandType = 'ConvertFromEntityToEntity'
465
+ }
466
+
467
+ export class ConvertFromEntityToSceneCommand extends JSBCommand {
468
+ constructor(
469
+ public fromEntityId: string,
470
+ public position: Vec3,
471
+ ) {
472
+ super()
473
+ }
474
+
475
+ protected getParams(): Record<string, any> | undefined {
476
+ return {
477
+ fromEntityId: this.fromEntityId,
478
+ position: this.position,
479
+ }
480
+ }
481
+
482
+ commandType = 'ConvertFromEntityToScene'
483
+ }
484
+
485
+ export class ConvertFromSceneToEntityCommand extends JSBCommand {
486
+ // let entityId: String
487
+ // let position:Vec3
488
+ constructor(
489
+ public entityId: string,
490
+ public position: Vec3,
491
+ ) {
492
+ super()
493
+ }
494
+ protected getParams(): Record<string, any> | undefined {
495
+ return {
496
+ entityId: this.entityId,
497
+ position: this.position,
498
+ }
499
+ }
500
+ commandType = 'ConvertFromSceneToEntity'
501
+ }
502
+
503
+ export class CreateTextureResourceCommand extends JSBCommand {
504
+ constructor(private url: string) {
505
+ super()
506
+ }
507
+ protected getParams(): Record<string, any> | undefined {
508
+ return {
509
+ url: this.url,
510
+ }
511
+ }
512
+ commandType = 'CreateTextureResource'
513
+ }
514
+
515
+ export class InspectCommand extends JSBCommand {
516
+ commandType = 'Inspect'
517
+
518
+ constructor(readonly id: string = '') {
519
+ super()
520
+ }
521
+
522
+ protected getParams() {
523
+ return this.id ? { id: this.id } : { id: '' }
524
+ }
525
+ }
526
+
527
+ export class DestroyCommand extends JSBCommand {
528
+ commandType = 'Destroy'
529
+
530
+ constructor(readonly id: string) {
531
+ super()
532
+ }
533
+
534
+ protected getParams() {
535
+ return { id: this.id }
536
+ }
537
+ }
538
+
539
+ export class CheckWebViewCanCreateCommand extends JSBCommand {
540
+ commandType = 'CheckWebViewCanCreate'
541
+
542
+ constructor(readonly id: string = "") {
543
+ super()
544
+ }
545
+
546
+ protected getParams() {
547
+ return { id: this.id }
548
+ }
549
+ }
550
+
551
+ /* WebSpatial Protocol Begin */
552
+ abstract class WebSpatialProtocolCommand extends JSBCommand {
553
+ target?: string
554
+ features?: string
555
+
556
+ async execute(): Promise<WebSpatialProtocolResult> {
557
+ const query = this.getQuery()
558
+ return platform.callWebSpatialProtocol(
559
+ this.commandType,
560
+ query,
561
+ this.target,
562
+ this.features,
563
+ )
564
+ }
565
+
566
+ executeSync(): WebSpatialProtocolResult {
567
+ const query = this.getQuery()
568
+ return platform.callWebSpatialProtocolSync(
569
+ this.commandType,
570
+ query,
571
+ this.target,
572
+ this.features,
573
+ )
574
+ }
575
+
576
+ private getQuery() {
577
+ let query = undefined
578
+ const params = this.getParams()
579
+ if (params) {
580
+ query = Object.keys(params)
581
+ .map(key => {
582
+ const value = params[key]
583
+ const finalValue =
584
+ typeof value === 'object' ? JSON.stringify(value) : value
585
+ return `${key}=${encodeURIComponent(finalValue)}`
586
+ })
587
+ .join('&')
588
+ }
589
+
590
+ return query
591
+ }
592
+ }
593
+
594
+ export class createSpatialized2DElementCommand extends WebSpatialProtocolCommand {
595
+ commandType = 'createSpatialized2DElement'
596
+ constructor() {
597
+ super()
598
+ }
599
+ protected getParams() {
600
+ return {}
601
+ }
602
+ }
603
+
604
+ export class createSpatialSceneCommand extends WebSpatialProtocolCommand {
605
+ commandType = 'createSpatialScene'
606
+
607
+ constructor(
608
+ private url: string,
609
+ private config: SpatialSceneCreationOptionsInternal | undefined,
610
+ public target?: string,
611
+ public features?: string,
612
+ ) {
613
+ super()
614
+ }
615
+ protected getParams() {
616
+ return {
617
+ url: this.url,
618
+ config: this.config,
619
+ }
620
+ }
621
+ }
622
+
623
+ // TODO: Can crypto.randomUUID be used instead including in dev environments without https
624
+ function uuid(): string {
625
+ return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {
626
+ const r = (Math.random() * 16) | 0
627
+ return (c === 'x' ? r : (r & 0x3) | 0x8).toString(16)
628
+ })
629
+ }
630
+
631
+ /* WebSpatial Protocol End */
package/src/Spatial.ts ADDED
@@ -0,0 +1,68 @@
1
+ import { SpatialSession } from './SpatialSession'
2
+ import { SpatialWebEvent } from './SpatialWebEvent'
3
+
4
+ /**
5
+ * Base object designed to be placed on navigator.spatial to mirror navigator.xr for webxr.
6
+ * This is the main entry point for the WebSpatial SDK, providing access to spatial capabilities.
7
+ */
8
+ export class Spatial {
9
+ /**
10
+ * Requests a spatial session object from the browser.
11
+ * This is the primary method to initialize spatial functionality.
12
+ * @returns The SpatialSession instance or null if not available in the current browser
13
+ * [TODO] discuss implications of this not being async
14
+ */
15
+ requestSession() {
16
+ if (this.runInSpatialWeb()) {
17
+ SpatialWebEvent.init()
18
+ return new SpatialSession()
19
+ } else {
20
+ return null
21
+ }
22
+ }
23
+
24
+ /**
25
+ * Checks if the current page is running in a spatial web environment.
26
+ * This method detects if the application is running in a WebSpatial-compatible browser.
27
+ * @returns True if running in a spatial web environment, false otherwise
28
+ */
29
+ runInSpatialWeb() {
30
+ if (navigator.userAgent.indexOf('WebSpatial/') > 0) {
31
+ return true
32
+ }
33
+ return false
34
+ }
35
+
36
+ /** @deprecated
37
+ * Checks if WebSpatial is supported in the current environment.
38
+ * Verifies compatibility between native and client versions.
39
+ * @returns True if web spatial is supported by this webpage
40
+ */
41
+ isSupported() {
42
+ return true
43
+ }
44
+
45
+ /** @deprecated
46
+ * Gets the native WebSpatial version from the browser environment.
47
+ * The version format follows semantic versioning (x.x.x).
48
+ * @returns Native version string in format "x.x.x"
49
+ */
50
+ getNativeVersion() {
51
+ if (window.__WebSpatialData && window.__WebSpatialData.getNativeVersion) {
52
+ return window.__WebSpatialData.getNativeVersion()
53
+ }
54
+ return window.WebSpatailNativeVersion === 'PACKAGE_VERSION'
55
+ ? this.getClientVersion()
56
+ : window.WebSpatailNativeVersion
57
+ }
58
+
59
+ /** @deprecated
60
+ * Gets the client SDK version.
61
+ * The version format follows semantic versioning (x.x.x).
62
+ * @returns Client SDK version string in format "x.x.x"
63
+ */
64
+ getClientVersion() {
65
+ // @ts-ignore
66
+ return __WEBSPATIAL_CORE_SDK_VERSION__
67
+ }
68
+ }