@webspatial/core-sdk 1.0.4 → 1.1.0

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 +8 -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 +2193 -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 +259 -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 +379 -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,46 @@
1
+ import { DestroyCommand, InspectCommand } from './JSBCommand'
2
+
3
+ /**
4
+ * @hidden
5
+ * Parent class of spatial objects, should not be used directly
6
+ */
7
+ export class SpatialObject {
8
+ /** @hidden */
9
+ constructor(
10
+ /** @hidden */
11
+ public readonly id: string,
12
+ ) {}
13
+
14
+ name?: string
15
+
16
+ isDestroyed = false
17
+
18
+ async inspect() {
19
+ const ret = await new InspectCommand(this.id).execute()
20
+ if (ret.success) {
21
+ return ret.data
22
+ }
23
+ throw new Error(ret.errorMessage)
24
+ }
25
+
26
+ async destroy() {
27
+ if (this.isDestroyed) {
28
+ return
29
+ }
30
+
31
+ const ret = await new DestroyCommand(this.id).execute()
32
+ if (ret.success) {
33
+ this.onDestroy()
34
+ this.isDestroyed = true
35
+ return ret.data
36
+ } else if (this.isDestroyed) {
37
+ // already destroyed
38
+ return
39
+ }
40
+
41
+ throw new Error(ret.errorMessage)
42
+ }
43
+
44
+ // override this method to do some cleanup
45
+ protected onDestroy() {}
46
+ }
@@ -0,0 +1,75 @@
1
+ import {
2
+ SpatialSceneCreationOptions,
3
+ SpatialSceneProperties,
4
+ } from './types/types'
5
+ import { SpatialSceneCreationOptionsInternal } from "./types/internal"
6
+ import {
7
+ AddSpatializedElementToSpatialScene,
8
+ GetSpatialSceneState,
9
+ UpdateSceneConfig,
10
+ UpdateSpatialSceneProperties,
11
+ } from './JSBCommand'
12
+
13
+ import { SpatializedElement } from './SpatializedElement'
14
+ import { SpatialObject } from './SpatialObject'
15
+ import { SpatialSceneState } from './types/internal'
16
+
17
+ let instance: SpatialScene
18
+
19
+ /**
20
+ * Represents the spatial scene that contains all spatialized elements.
21
+ * This class follows the singleton pattern - only one instance exists per application.
22
+ * The scene manages the overall spatial environment properties and contains all spatial elements.
23
+ */
24
+ export class SpatialScene extends SpatialObject {
25
+ /**
26
+ * Gets the singleton instance of the SpatialScene.
27
+ * Creates a new instance if one doesn't exist yet.
28
+ * @returns The singleton SpatialScene instance
29
+ */
30
+ static getInstance(): SpatialScene {
31
+ if (!instance) {
32
+ instance = new SpatialScene('')
33
+ }
34
+ return instance
35
+ }
36
+
37
+ /**
38
+ * Updates the properties of the spatial scene.
39
+ * This can include background settings, lighting, and other scene-wide properties.
40
+ * @param properties Partial set of properties to update
41
+ * @returns Promise resolving when the update is complete
42
+ */
43
+ async updateSpatialProperties(properties: Partial<SpatialSceneProperties>) {
44
+ return new UpdateSpatialSceneProperties(properties).execute()
45
+ }
46
+
47
+ /**
48
+ * Adds a spatialized element to the scene.
49
+ * This makes the element visible and interactive in the spatial environment.
50
+ * @param element The SpatializedElement to add to the scene
51
+ * @returns Promise resolving when the element is added
52
+ */
53
+ async addSpatializedElement(element: SpatializedElement) {
54
+ return new AddSpatializedElementToSpatialScene(element).execute()
55
+ }
56
+
57
+ /**
58
+ * Updates the scene creation configuration.
59
+ * This allows changing scene parameters after initial creation.
60
+ * @param config The new scene creation configuration
61
+ * @returns Promise resolving when the update is complete
62
+ */
63
+ async updateSceneCreationConfig(config: SpatialSceneCreationOptionsInternal) {
64
+ return new UpdateSceneConfig(config).execute()
65
+ }
66
+
67
+ /**
68
+ * Gets the current state of the spatial scene.
69
+ * This includes information about active elements and scene configuration.
70
+ * @returns Promise resolving to the current SpatialSceneState
71
+ */
72
+ async getState(): Promise<SpatialSceneState> {
73
+ return (await new GetSpatialSceneState().execute()).data.name
74
+ }
75
+ }
@@ -0,0 +1,187 @@
1
+ import { initScene } from './scene-polyfill'
2
+ import { SpatialScene } from './SpatialScene'
3
+ import { Spatialized2DElement } from './Spatialized2DElement'
4
+ import {
5
+ createSpatialized2DElement,
6
+ createSpatializedDynamic3DElement,
7
+ } from './SpatializedElementCreator'
8
+ import { createSpatializedStatic3DElement } from './SpatializedElementCreator'
9
+ import { SpatializedStatic3DElement } from './SpatializedStatic3DElement'
10
+ import {
11
+ ModelComponentOptions,
12
+ ModelAssetOptions,
13
+ SpatialBoxGeometryOptions,
14
+ SpatialConeGeometryOptions,
15
+ SpatialCylinderGeometryOptions,
16
+ SpatialGeometryOptions,
17
+ SpatialModelEntityCreationOptions,
18
+ SpatialPlaneGeometryOptions,
19
+ SpatialSceneCreationOptions,
20
+ SpatialSphereGeometryOptions,
21
+ SpatialUnlitMaterialOptions,
22
+ SpatialEntityUserData,
23
+ } from './types/types'
24
+ import { SpatializedDynamic3DElement } from './SpatializedDynamic3DElement'
25
+ import { SpatialEntity } from './reality/entity/SpatialEntity'
26
+ import {
27
+ createModelAsset,
28
+ createModelComponent,
29
+ createSpatialEntity,
30
+ createSpatialGeometry,
31
+ createSpatialModelEntity,
32
+ createSpatialUnlitMaterial,
33
+ } from './reality/realityCreator'
34
+ import {
35
+ SpatialBoxGeometry,
36
+ SpatialPlaneGeometry,
37
+ SpatialSphereGeometry,
38
+ SpatialConeGeometry,
39
+ SpatialCylinderGeometry,
40
+ } from './reality'
41
+
42
+ /**
43
+ * Session used to establish a connection to the spatial renderer of the system.
44
+ * All spatial resources must be created through this session object.
45
+ * This class serves as the main factory for creating spatial elements and geometries.
46
+ */
47
+ export class SpatialSession {
48
+ /**
49
+ * Gets the singleton instance of the spatial scene.
50
+ * The spatial scene is the root container for all spatial elements.
51
+ * @returns The SpatialScene singleton instance
52
+ */
53
+ getSpatialScene(): SpatialScene {
54
+ return SpatialScene.getInstance()
55
+ }
56
+
57
+ /**
58
+ * Creates a new 2D element that can be spatialized in the 3D environment.
59
+ * 2D elements represent HTML content that can be positioned in 3D space.
60
+ * @returns Promise resolving to a new Spatialized2DElement instance
61
+ */
62
+ createSpatialized2DElement(): Promise<Spatialized2DElement> {
63
+ return createSpatialized2DElement()
64
+ }
65
+
66
+ /**
67
+ * Creates a new static 3D element with an optional model URL.
68
+ * Static 3D elements represent pre-built 3D models that can be loaded from a URL.
69
+ * @param modelURL Optional URL to the 3D model to load
70
+ * @returns Promise resolving to a new SpatializedStatic3DElement instance
71
+ */
72
+ createSpatializedStatic3DElement(
73
+ modelURL: string = '',
74
+ ): Promise<SpatializedStatic3DElement> {
75
+ return createSpatializedStatic3DElement(modelURL)
76
+ }
77
+
78
+ /**
79
+ * Initializes the spatial scene with custom configuration.
80
+ * This is a reference to the initScene function from scene-polyfill.
81
+ */
82
+ initScene = initScene
83
+
84
+ /**
85
+ * Creates a new dynamic 3D element that can be manipulated at runtime.
86
+ * Dynamic 3D elements allow for programmatic creation and modification of 3D content.
87
+ * @returns Promise resolving to a new SpatializedDynamic3DElement instance
88
+ */
89
+ createSpatializedDynamic3DElement(): Promise<SpatializedDynamic3DElement> {
90
+ return createSpatializedDynamic3DElement()
91
+ }
92
+
93
+ /**
94
+ * Creates a new spatial entity with an optional name.
95
+ * Entities are the basic building blocks for creating custom 3D content.
96
+ * @param name Optional name for the entity
97
+ * @returns Promise resolving to a new SpatialEntity instance
98
+ */
99
+ createEntity(userData?: SpatialEntityUserData,): Promise<SpatialEntity> {
100
+ return createSpatialEntity(userData)
101
+ }
102
+
103
+ /**
104
+ * Creates a box geometry with optional configuration.
105
+ * @param options Configuration options for the box geometry
106
+ * @returns Promise resolving to a new SpatialBoxGeometry instance
107
+ */
108
+ createBoxGeometry(options: SpatialBoxGeometryOptions = {}) {
109
+ return createSpatialGeometry(SpatialBoxGeometry, options)
110
+ }
111
+
112
+ /**
113
+ * Creates a plane geometry with optional configuration.
114
+ * @param options Configuration options for the plane geometry
115
+ * @returns Promise resolving to a new SpatialPlaneGeometry instance
116
+ */
117
+ createPlaneGeometry(options: SpatialPlaneGeometryOptions = {}) {
118
+ return createSpatialGeometry(SpatialPlaneGeometry, options)
119
+ }
120
+
121
+ /**
122
+ * Creates a sphere geometry with optional configuration.
123
+ * @param options Configuration options for the sphere geometry
124
+ * @returns Promise resolving to a new SpatialSphereGeometry instance
125
+ */
126
+ createSphereGeometry(options: SpatialSphereGeometryOptions = {}) {
127
+ return createSpatialGeometry(SpatialSphereGeometry, options)
128
+ }
129
+
130
+ /**
131
+ * Creates a cone geometry with the specified configuration.
132
+ * @param options Configuration options for the cone geometry
133
+ * @returns Promise resolving to a new SpatialConeGeometry instance
134
+ */
135
+ createConeGeometry(options: SpatialConeGeometryOptions) {
136
+ return createSpatialGeometry(SpatialConeGeometry, options)
137
+ }
138
+
139
+ /**
140
+ * Creates a cylinder geometry with the specified configuration.
141
+ * @param options Configuration options for the cylinder geometry
142
+ * @returns Promise resolving to a new SpatialCylinderGeometry instance
143
+ */
144
+ createCylinderGeometry(options: SpatialCylinderGeometryOptions) {
145
+ return createSpatialGeometry(SpatialCylinderGeometry, options)
146
+ }
147
+
148
+ /**
149
+ * Creates a model component with the specified configuration.
150
+ * Model components are used to add 3D model rendering capabilities to entities.
151
+ * @param options Configuration options for the model component
152
+ * @returns Promise resolving to a new ModelComponent instance
153
+ */
154
+ createModelComponent(options: ModelComponentOptions) {
155
+ return createModelComponent(options)
156
+ }
157
+
158
+ /**
159
+ * Creates an unlit material with the specified configuration.
160
+ * Unlit materials don't respond to lighting in the scene.
161
+ * @param options Configuration options for the unlit material
162
+ * @returns Promise resolving to a new SpatialUnlitMaterial instance
163
+ */
164
+ createUnlitMaterial(options: SpatialUnlitMaterialOptions) {
165
+ return createSpatialUnlitMaterial(options)
166
+ }
167
+
168
+ /**
169
+ * Creates a model asset with the specified configuration.
170
+ * Model assets represent 3D model resources that can be used by entities.
171
+ * @param options Configuration options for the model asset
172
+ * @returns Promise resolving to a new SpatialModelAsset instance
173
+ */
174
+ createModelAsset(options: ModelAssetOptions) {
175
+ return createModelAsset(options)
176
+ }
177
+
178
+ /**
179
+ * Creates a spatial model entity with the specified configuration.
180
+ * This is a convenience method for creating an entity with a model component.
181
+ * @param options Configuration options for the spatial model entity
182
+ * @returns Promise resolving to a new SpatialModelEntity instance
183
+ */
184
+ createSpatialModelEntity(options: SpatialModelEntityCreationOptions, userData?: SpatialEntityUserData) {
185
+ return createSpatialModelEntity(options, userData)
186
+ }
187
+ }
@@ -0,0 +1,23 @@
1
+ interface SpatialWebEventData {
2
+ id: string
3
+ data: any
4
+ }
5
+
6
+ export class SpatialWebEvent {
7
+ static eventReceiver: Record<string, (data: any) => void> = {}
8
+ static init() {
9
+ // inject __SpatialWebEvent
10
+ window.__SpatialWebEvent = ({ id, data }: SpatialWebEventData) => {
11
+ // console.log('__SpatialWebEvent', id, data)
12
+ SpatialWebEvent.eventReceiver[id]?.(data)
13
+ }
14
+ }
15
+
16
+ static addEventReceiver(id: string, callback: (data: any) => void) {
17
+ SpatialWebEvent.eventReceiver[id] = callback
18
+ }
19
+
20
+ static removeEventReceiver(id: string) {
21
+ delete SpatialWebEvent.eventReceiver[id]
22
+ }
23
+ }
@@ -0,0 +1,12 @@
1
+ import { SpatialWebMsgType } from './WebMsgCommand'
2
+
3
+ export function createSpatialEvent<T>(
4
+ type: SpatialWebMsgType,
5
+ detail: T,
6
+ ): CustomEvent<T> {
7
+ return new CustomEvent<T>(type, {
8
+ bubbles: true,
9
+ cancelable: false,
10
+ detail,
11
+ })
12
+ }
@@ -0,0 +1,51 @@
1
+ import {
2
+ AddSpatializedElementToSpatialized2DElement,
3
+ UpdateSpatialized2DElementProperties,
4
+ } from './JSBCommand'
5
+ import { hijackWindowATag } from './scene-polyfill'
6
+ import { SpatializedElement } from './SpatializedElement'
7
+ import { Spatialized2DElementProperties } from './types/types'
8
+
9
+ /**
10
+ * Represents a 2D HTML element that has been spatialized in 3D space.
11
+ * This class handles the integration between 2D web content and the 3D spatial environment,
12
+ * allowing HTML elements to be positioned and interacted with in spatial applications.
13
+ */
14
+ export class Spatialized2DElement extends SpatializedElement {
15
+ /**
16
+ * Creates a new spatialized 2D element.
17
+ * @param id Unique identifier for this element
18
+ * @param windowProxy Reference to the window object containing the 2D content
19
+ */
20
+ constructor(
21
+ id: string,
22
+ readonly windowProxy: WindowProxy,
23
+ ) {
24
+ super(id)
25
+ // Hijack anchor tag events to handle navigation within the spatial context
26
+ hijackWindowATag(windowProxy)
27
+ }
28
+
29
+ /**
30
+ * Updates the properties of this 2D element.
31
+ * This can include size, position, background, and other visual properties.
32
+ * @param properties Partial set of properties to update
33
+ * @returns Promise resolving when the update is complete
34
+ */
35
+ async updateProperties(properties: Partial<Spatialized2DElementProperties>) {
36
+ return new UpdateSpatialized2DElementProperties(this, properties).execute()
37
+ }
38
+
39
+ /**
40
+ * Adds a child spatialized element to this 2D element.
41
+ * This allows for creating hierarchical structures of spatial elements.
42
+ * @param element The child element to add
43
+ * @returns Promise resolving when the element is added
44
+ */
45
+ async addSpatializedElement(element: SpatializedElement) {
46
+ return new AddSpatializedElementToSpatialized2DElement(
47
+ this,
48
+ element,
49
+ ).execute()
50
+ }
51
+ }
@@ -0,0 +1,30 @@
1
+ import {
2
+ AddEntityToDynamic3DCommand,
3
+ SetParentForEntityCommand,
4
+ UpdateSpatializedDynamic3DElementProperties,
5
+ } from './JSBCommand'
6
+ import { SpatialEntity } from './reality'
7
+ import { SpatializedElement } from './SpatializedElement'
8
+ import {
9
+ SpatialEntityOrReality,
10
+ SpatializedElementProperties,
11
+ } from './types/types'
12
+ export class SpatializedDynamic3DElement extends SpatializedElement {
13
+ children: SpatialEntityOrReality[] = []
14
+ constructor(id: string) {
15
+ super(id)
16
+ }
17
+
18
+ async addEntity(entity: SpatialEntity) {
19
+ const ans = new SetParentForEntityCommand(entity.id, this.id).execute()
20
+ this.children.push(entity)
21
+ entity.parent = this
22
+ return ans
23
+ }
24
+ async updateProperties(properties: Partial<SpatializedElementProperties>) {
25
+ return new UpdateSpatializedDynamic3DElementProperties(
26
+ this,
27
+ properties,
28
+ ).execute()
29
+ }
30
+ }