@webspatial/core-sdk 0.0.1

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 (98) hide show
  1. package/dist/core/Spatial.d.ts +27 -0
  2. package/dist/core/Spatial.d.ts.map +1 -0
  3. package/dist/core/Spatial.js +41 -0
  4. package/dist/core/SpatialEntity.d.ts +92 -0
  5. package/dist/core/SpatialEntity.d.ts.map +1 -0
  6. package/dist/core/SpatialEntity.js +121 -0
  7. package/dist/core/SpatialHelper.d.ts +39 -0
  8. package/dist/core/SpatialHelper.d.ts.map +1 -0
  9. package/dist/core/SpatialHelper.js +169 -0
  10. package/dist/core/SpatialObject.d.ts +19 -0
  11. package/dist/core/SpatialObject.d.ts.map +1 -0
  12. package/dist/core/SpatialObject.js +22 -0
  13. package/dist/core/SpatialSession.d.ts +166 -0
  14. package/dist/core/SpatialSession.d.ts.map +1 -0
  15. package/dist/core/SpatialSession.js +310 -0
  16. package/dist/core/SpatialTransform.d.ts +23 -0
  17. package/dist/core/SpatialTransform.d.ts.map +1 -0
  18. package/dist/core/SpatialTransform.js +31 -0
  19. package/dist/core/SpatialWindowContainer.d.ts +31 -0
  20. package/dist/core/SpatialWindowContainer.d.ts.map +1 -0
  21. package/dist/core/SpatialWindowContainer.js +48 -0
  22. package/dist/core/component/EventSpatialComponent.d.ts +22 -0
  23. package/dist/core/component/EventSpatialComponent.d.ts.map +1 -0
  24. package/dist/core/component/EventSpatialComponent.js +23 -0
  25. package/dist/core/component/SpatialComponent.d.ts +11 -0
  26. package/dist/core/component/SpatialComponent.d.ts.map +1 -0
  27. package/dist/core/component/SpatialComponent.js +23 -0
  28. package/dist/core/component/SpatialInputComponent.d.ts +22 -0
  29. package/dist/core/component/SpatialInputComponent.d.ts.map +1 -0
  30. package/dist/core/component/SpatialInputComponent.js +14 -0
  31. package/dist/core/component/SpatialModel3DComponent.d.ts +86 -0
  32. package/dist/core/component/SpatialModel3DComponent.d.ts.map +1 -0
  33. package/dist/core/component/SpatialModel3DComponent.js +176 -0
  34. package/dist/core/component/SpatialModelComponent.d.ts +22 -0
  35. package/dist/core/component/SpatialModelComponent.d.ts.map +1 -0
  36. package/dist/core/component/SpatialModelComponent.js +34 -0
  37. package/dist/core/component/SpatialViewComponent.d.ts +22 -0
  38. package/dist/core/component/SpatialViewComponent.d.ts.map +1 -0
  39. package/dist/core/component/SpatialViewComponent.js +30 -0
  40. package/dist/core/component/SpatialWindowComponent.d.ts +86 -0
  41. package/dist/core/component/SpatialWindowComponent.d.ts.map +1 -0
  42. package/dist/core/component/SpatialWindowComponent.js +114 -0
  43. package/dist/core/component/index.d.ts +7 -0
  44. package/dist/core/component/index.d.ts.map +1 -0
  45. package/dist/core/component/index.js +6 -0
  46. package/dist/core/index.d.ts +11 -0
  47. package/dist/core/index.d.ts.map +1 -0
  48. package/dist/core/index.js +9 -0
  49. package/dist/core/private/WebSpatial.d.ts +46 -0
  50. package/dist/core/private/WebSpatial.d.ts.map +1 -0
  51. package/dist/core/private/WebSpatial.js +282 -0
  52. package/dist/core/private/remote-command/RemoteCommand.d.ts +8 -0
  53. package/dist/core/private/remote-command/RemoteCommand.d.ts.map +1 -0
  54. package/dist/core/private/remote-command/RemoteCommand.js +11 -0
  55. package/dist/core/private/remote-command/index.d.ts +2 -0
  56. package/dist/core/private/remote-command/index.d.ts.map +1 -0
  57. package/dist/core/private/remote-command/index.js +1 -0
  58. package/dist/core/resource/SpatialMeshResource.d.ts +7 -0
  59. package/dist/core/resource/SpatialMeshResource.d.ts.map +1 -0
  60. package/dist/core/resource/SpatialMeshResource.js +6 -0
  61. package/dist/core/resource/SpatialPhysicallyBasedMaterialResource.d.ts +37 -0
  62. package/dist/core/resource/SpatialPhysicallyBasedMaterialResource.d.ts.map +1 -0
  63. package/dist/core/resource/SpatialPhysicallyBasedMaterialResource.js +37 -0
  64. package/dist/core/resource/index.d.ts +3 -0
  65. package/dist/core/resource/index.d.ts.map +1 -0
  66. package/dist/core/resource/index.js +2 -0
  67. package/dist/core/types.d.ts +23 -0
  68. package/dist/core/types.d.ts.map +1 -0
  69. package/dist/core/types.js +1 -0
  70. package/dist/index.d.ts +2 -0
  71. package/dist/index.d.ts.map +1 -0
  72. package/dist/index.js +1 -0
  73. package/package.json +35 -0
  74. package/src/core/Spatial.ts +48 -0
  75. package/src/core/SpatialEntity.ts +146 -0
  76. package/src/core/SpatialHelper.ts +197 -0
  77. package/src/core/SpatialObject.ts +24 -0
  78. package/src/core/SpatialSession.ts +434 -0
  79. package/src/core/SpatialTransform.ts +26 -0
  80. package/src/core/SpatialWindowContainer.ts +51 -0
  81. package/src/core/component/EventSpatialComponent.ts +32 -0
  82. package/src/core/component/SpatialComponent.ts +26 -0
  83. package/src/core/component/SpatialInputComponent.ts +24 -0
  84. package/src/core/component/SpatialModel3DComponent.ts +213 -0
  85. package/src/core/component/SpatialModelComponent.ts +39 -0
  86. package/src/core/component/SpatialViewComponent.ts +32 -0
  87. package/src/core/component/SpatialWindowComponent.ts +168 -0
  88. package/src/core/component/index.ts +14 -0
  89. package/src/core/index.ts +10 -0
  90. package/src/core/private/WebSpatial.ts +356 -0
  91. package/src/core/private/remote-command/RemoteCommand.ts +15 -0
  92. package/src/core/private/remote-command/index.ts +1 -0
  93. package/src/core/resource/SpatialMeshResource.ts +6 -0
  94. package/src/core/resource/SpatialPhysicallyBasedMaterialResource.ts +42 -0
  95. package/src/core/resource/index.ts +2 -0
  96. package/src/core/types.ts +27 -0
  97. package/src/index.ts +1 -0
  98. package/tsconfig.json +26 -0
@@ -0,0 +1,213 @@
1
+ import { EventSpatialComponent } from './EventSpatialComponent'
2
+ import { WebSpatial } from '../private/WebSpatial'
3
+ import { Vec3 } from '../SpatialTransform'
4
+
5
+ /**
6
+ * Translate event, matching similar behavior to https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/drag_event
7
+ */
8
+ export type ModelDragEvent = {
9
+ eventType: 'dragstart' | 'dragend' | 'drag'
10
+ translation3D: Vec3
11
+ startLocation3D: Vec3
12
+ }
13
+
14
+ export type TapEvent = {
15
+ eventType: 'tap'
16
+ }
17
+
18
+ /**
19
+ * Used to position a model3d in 3D space
20
+ */
21
+ export class SpatialModel3DComponent extends EventSpatialComponent {
22
+ protected override onRecvEvent(data: any): void {
23
+ const { eventType, value, error } = data
24
+ switch (eventType) {
25
+ case 'phase':
26
+ if (value === 'success') {
27
+ this.onSuccess?.()
28
+ } else {
29
+ this.onFailure?.(error as string)
30
+ }
31
+ break
32
+ case 'dragstart':
33
+ this._onDragStart?.(value)
34
+ break
35
+ case 'dragend':
36
+ this._onDragEnd?.(value)
37
+ break
38
+ case 'drag':
39
+ this._onDrag?.(value)
40
+ break
41
+ case 'tap':
42
+ this._onTap?.()
43
+ break
44
+ case 'doubletap':
45
+ this._onDoubleTap?.()
46
+ break
47
+ case 'longpress':
48
+ this._onLongPress?.()
49
+ break
50
+
51
+ default:
52
+ break
53
+ }
54
+ }
55
+ /**
56
+ * Sets the resolution of the spatial view in dom pixels
57
+ */
58
+ async setResolution(width: number, height: number) {
59
+ await WebSpatial.updateResource(this._resource, {
60
+ resolution: { x: width, y: height },
61
+ })
62
+ }
63
+
64
+ async setRotationAnchor(rotationAnchor: Vec3) {
65
+ await WebSpatial.updateResource(this._resource, {
66
+ rotationAnchor: rotationAnchor,
67
+ })
68
+ }
69
+
70
+ /**
71
+ * Sets the opacity of the model
72
+ * @param opacity
73
+ */
74
+ async setOpacity(opacity: number) {
75
+ await WebSpatial.updateResource(this._resource, {
76
+ opacity,
77
+ })
78
+ }
79
+
80
+ /**
81
+ * Sets how the model fill the rect
82
+ * @param contentMode
83
+ */
84
+ async setContentMode(contentMode: 'fill' | 'fit') {
85
+ await WebSpatial.updateResource(this._resource, {
86
+ contentMode,
87
+ })
88
+ }
89
+
90
+ /**
91
+ * Constrains this model dimensions to the specified aspect ratio.
92
+ * with a value of 0, the model will use the original aspect ratio.
93
+ *
94
+ * @param aspectRatio number
95
+ */
96
+ async setAspectRatio(aspectRatio: number) {
97
+ await WebSpatial.updateResource(this._resource, {
98
+ aspectRatio,
99
+ })
100
+ }
101
+
102
+ /**
103
+ * Sets whether the model appear in original size or fit the rect
104
+ * @param resizable
105
+ */
106
+ async setResizable(resizable: boolean) {
107
+ await WebSpatial.updateResource(this._resource, {
108
+ resizable,
109
+ })
110
+ }
111
+
112
+ /**
113
+ * Callback fired when model load success
114
+ */
115
+ public onSuccess?: () => void
116
+
117
+ /**
118
+ * Callback fired when model load failure
119
+ * @param errorReason
120
+ */
121
+ public onFailure?: (errorReason: string) => void
122
+
123
+ /**
124
+ * Callback fired when model was dragged at the beginning
125
+ * @param dragEvent
126
+ */
127
+ private _onDragStart?: (dragEvent: ModelDragEvent) => void
128
+ public set onDragStart(
129
+ callback: ((dragEvent: ModelDragEvent) => void) | undefined,
130
+ ) {
131
+ if (this._onDragStart !== callback) {
132
+ this._onDragStart = callback
133
+ WebSpatial.updateResource(this._resource, {
134
+ enableDragEvent: this.enableDragEvent,
135
+ })
136
+ }
137
+ }
138
+
139
+ /**
140
+ * Callback fired when model was dragged
141
+ * @param dragEvent
142
+ */
143
+ private _onDrag?: (dragEvent: ModelDragEvent) => void
144
+ public set onDrag(
145
+ callback: ((dragEvent: ModelDragEvent) => void) | undefined,
146
+ ) {
147
+ if (this._onDrag !== callback) {
148
+ this._onDrag = callback
149
+ WebSpatial.updateResource(this._resource, {
150
+ enableDragEvent: this.enableDragEvent,
151
+ })
152
+ }
153
+ }
154
+
155
+ /**
156
+ * Callback fired when model was dragged at the ending
157
+ * @param dragEvent
158
+ */
159
+ private _onDragEnd?: (dragEvent: ModelDragEvent) => void
160
+ public set onDragEnd(
161
+ callback: ((dragEvent: ModelDragEvent) => void) | undefined,
162
+ ) {
163
+ if (this._onDragEnd !== callback) {
164
+ this._onDragEnd = callback
165
+ WebSpatial.updateResource(this._resource, {
166
+ enableDragEvent: this.enableDragEvent,
167
+ })
168
+ }
169
+ }
170
+
171
+ private get enableDragEvent(): boolean {
172
+ return (
173
+ undefined !== this._onDrag ||
174
+ undefined !== this._onDragStart ||
175
+ undefined !== this._onDragEnd
176
+ )
177
+ }
178
+
179
+ /**
180
+ * Callback fired when model was tapped
181
+ */
182
+ private _onTap?: () => void
183
+ public set onTap(callback: (() => void) | undefined) {
184
+ if (this._onTap !== callback) {
185
+ this._onTap = callback
186
+ WebSpatial.updateResource(this._resource, {
187
+ enableTapEvent: undefined !== callback,
188
+ })
189
+ }
190
+ }
191
+
192
+ /** Callback fired when model was double tapped */
193
+ private _onDoubleTap?: () => void
194
+ public set onDoubleTap(callback: (() => void) | undefined) {
195
+ if (this._onDoubleTap !== callback) {
196
+ this._onDoubleTap = callback
197
+ WebSpatial.updateResource(this._resource, {
198
+ enableDoubleTapEvent: undefined !== callback,
199
+ })
200
+ }
201
+ }
202
+
203
+ /** Callback fired when model was long pressed */
204
+ private _onLongPress?: () => void
205
+ public set onLongPress(callback: (() => void) | undefined) {
206
+ if (this._onLongPress !== callback) {
207
+ this._onLongPress = callback
208
+ WebSpatial.updateResource(this._resource, {
209
+ enableLongPressEvent: undefined !== callback,
210
+ })
211
+ }
212
+ }
213
+ }
@@ -0,0 +1,39 @@
1
+ import { SpatialMeshResource } from '../resource/SpatialMeshResource'
2
+ import { SpatialPhysicallyBasedMaterialResource } from '../resource/SpatialPhysicallyBasedMaterialResource'
3
+ import { SpatialComponent } from './SpatialComponent'
4
+ import { WebSpatial } from '../private/WebSpatial'
5
+
6
+ /**
7
+ * Used to position a model in 3D space, made up of a mesh and materials to be applied to the mesh
8
+ */
9
+ export class SpatialModelComponent extends SpatialComponent {
10
+ private cachedMaterials = new Array<SpatialPhysicallyBasedMaterialResource>()
11
+ /**
12
+ * Sets the mesh to be displayed by the component
13
+ * @param mesh mesh to set
14
+ */
15
+ async setMesh(mesh: SpatialMeshResource) {
16
+ await WebSpatial.updateResource(this._resource, {
17
+ meshResource: mesh._resource.id,
18
+ })
19
+ }
20
+
21
+ /**
22
+ * Sets the materials that should be applied to the mesh
23
+ * @param materials array of materials to set
24
+ */
25
+ async setMaterials(materials: Array<SpatialPhysicallyBasedMaterialResource>) {
26
+ this.cachedMaterials = materials
27
+ await WebSpatial.updateResource(this._resource, {
28
+ materials: materials.map(m => {
29
+ m._addToComponent(this)
30
+ return m._resource.id
31
+ }),
32
+ })
33
+ }
34
+
35
+ /** @hidden */
36
+ async _syncMaterials() {
37
+ await this.setMaterials(this.cachedMaterials)
38
+ }
39
+ }
@@ -0,0 +1,32 @@
1
+ import { WebSpatial } from '../private/WebSpatial'
2
+ import { SpatialComponent } from './SpatialComponent'
3
+
4
+ /**
5
+ * Represenets a volume that can be added to the webpage
6
+ * Child entities will be added within this volume's space
7
+ * Defaults to having 1x1x1 meter dimensions
8
+ * Resolution defaults to 100x100 pixels
9
+ * Only will be displayed on entities in "ROOT" or "DOM" space
10
+ * If the resolution of the spatial view is not a square, the volume will be larger based on the ratio with the shortest side being 1 meter.
11
+ * (eg. 200x100 = 2m x 1m x 1m volume)
12
+ */
13
+ export class SpatialViewComponent extends SpatialComponent {
14
+ /**
15
+ * Sets the resolution of the spatial view in dom pixels
16
+ */
17
+ async setResolution(width: number, height: number) {
18
+ await WebSpatial.updateResource(this._resource, {
19
+ resolution: { x: width, y: height },
20
+ })
21
+ }
22
+
23
+ /**
24
+ * Sets if content of the spatialView should be within a portal
25
+ * If true, volume will be behind the page, if false, it will be in front of the page
26
+ */
27
+ async setIsPortal(isPortal: Boolean) {
28
+ await WebSpatial.updateResource(this._resource, {
29
+ isPortal: isPortal,
30
+ })
31
+ }
32
+ }
@@ -0,0 +1,168 @@
1
+ import { SpatialComponent } from './SpatialComponent'
2
+ import { WebSpatial } from '../private/WebSpatial'
3
+ import { Vec3 } from '../SpatialTransform'
4
+
5
+ /**
6
+ * Material type for SpatialDiv or HTML document.
7
+ *
8
+ * This type defines the background material options for both SpatialDiv elements and HTML documents.
9
+ *
10
+ * - `'none'`: This is the default value.
11
+ * - For HTML documents, the web page window will have the default native background.
12
+ * - For SpatialDiv, the window will have a transparent background.
13
+ * - `'translucent'`: Represents a glass-like material in AVP (Apple Vision Pro).
14
+ * - `'thick'`: Represents a thick material in AVP.
15
+ * - `'regular'`: Represents a regular material in AVP.
16
+ * - `'thin'`: Represents a thin material in AVP.
17
+ * - `'transparent'`: Represents a fully transparent background.
18
+ */
19
+ export type BackgroundMaterialType =
20
+ | 'none'
21
+ | 'translucent'
22
+ | 'thick'
23
+ | 'regular'
24
+ | 'thin'
25
+ | 'transparent'
26
+
27
+ export type CornerRadius = {
28
+ topLeading: number
29
+ bottomLeading: number
30
+ topTrailing: number
31
+ bottomTrailing: number
32
+ }
33
+
34
+ export type StyleParam = {
35
+ material?: {
36
+ type: BackgroundMaterialType
37
+ }
38
+ cornerRadius?: number | CornerRadius
39
+ }
40
+
41
+ /**
42
+ * Used to position an web window in 3D space
43
+ */
44
+ export class SpatialWindowComponent extends SpatialComponent {
45
+ /**
46
+ * Loads a url page in the window
47
+ * @param url url to load
48
+ */
49
+ async loadURL(url: string) {
50
+ await WebSpatial.updateResource(this._resource, { url: url })
51
+ }
52
+
53
+ async setFromWindow(window: any) {
54
+ if (window._webSpatialID) {
55
+ await WebSpatial.updateResource(this._resource, {
56
+ windowID: window._webSpatialID,
57
+ })
58
+ } else {
59
+ await console.warn(
60
+ 'failed to call setFromWindow, window provided is not valid',
61
+ )
62
+ }
63
+ }
64
+
65
+ /**
66
+ * Sets the resolution of the window, the resulting dimensions when rendered will be equal to 1/1360 units
67
+ * eg. if the resolution is set to 1360x1360 it will be a 1x1 plane
68
+ * See 1360 in spatialViewUI.swift for how this ratio works
69
+ * @param width width in pixels
70
+ * @param height height in pixels
71
+ */
72
+ async setResolution(width: number, height: number) {
73
+ await WebSpatial.updateResource(this._resource, {
74
+ resolution: { x: width, y: height },
75
+ })
76
+ }
77
+
78
+ /**
79
+ * [Experimental] Sets the anchor which the entity this is attached to will rotate around
80
+ * @param rotationAnchor
81
+ */
82
+ async setRotationAnchor(rotationAnchor: Vec3) {
83
+ await WebSpatial.updateResource(this._resource, {
84
+ rotationAnchor: rotationAnchor,
85
+ })
86
+ }
87
+
88
+ /**
89
+ * [Experimental] Sets the opacity of the window after apply material
90
+ * @param opacity
91
+ */
92
+ async setOpacity(opacity: number) {
93
+ await WebSpatial.updateResource(this._resource, {
94
+ opacity,
95
+ })
96
+ }
97
+
98
+ /**
99
+ * Sets the style that should be applied to the window
100
+ * @param options style options
101
+ */
102
+ async setStyle(styleParam: StyleParam) {
103
+ const { material, cornerRadius } = styleParam
104
+ const options: any = {}
105
+ if (material?.type) {
106
+ options.backgroundMaterial = material.type
107
+ }
108
+
109
+ if (cornerRadius !== undefined) {
110
+ if (typeof cornerRadius === 'number') {
111
+ options.cornerRadius = {
112
+ topLeading: cornerRadius,
113
+ bottomLeading: cornerRadius,
114
+ topTrailing: cornerRadius,
115
+ bottomTrailing: cornerRadius,
116
+ }
117
+ } else {
118
+ options.cornerRadius = { ...cornerRadius }
119
+ }
120
+ }
121
+
122
+ if (document && document.readyState == 'loading') {
123
+ // Avoid flash of unstyled content by sending style command via a link element
124
+ var encoded = encodeURIComponent(JSON.stringify(options))
125
+ var x = document.createElement('link')
126
+ x.rel = 'stylesheet'
127
+ x.href = 'forceStyle://mystyle.css?' + 'style=' + encoded
128
+ document.head.appendChild(x)
129
+ }
130
+
131
+ await WebSpatial.updateResource(this._resource, { style: options })
132
+ }
133
+
134
+ /**
135
+ * Modifies the amount the spatial window can be scrolled
136
+ * Should only be used internally
137
+ * See https://developer.apple.com/documentation/uikit/1624475-uiedgeinsetsmake?language=objc
138
+ * @param insets margin to modify scroll distances by
139
+ */
140
+ async setScrollEdgeInsets(insets: {
141
+ top: number
142
+ left: number
143
+ bottom: number
144
+ right: number
145
+ }) {
146
+ await WebSpatial.updateResource(this._resource, {
147
+ setScrollEdgeInsets: insets,
148
+ })
149
+ }
150
+
151
+ /**
152
+ * Enable/Disable scrolling in the window (defaults to enabled), if disabled, scrolling will be applied to the root page
153
+ * @param enabled value to set
154
+ */
155
+ async setScrollEnabled(enabled: boolean) {
156
+ await WebSpatial.updateResource(this._resource, { scrollEnabled: enabled })
157
+ }
158
+
159
+ /**
160
+ * Defaults to false. If set to true, scrolling the parent page will also scroll this window with it like other dom elements
161
+ * @param scrollWithParent value to set
162
+ */
163
+ async setScrollWithParent(scrollWithParent: boolean) {
164
+ await WebSpatial.updateResource(this._resource, {
165
+ scrollWithParent: scrollWithParent,
166
+ })
167
+ }
168
+ }
@@ -0,0 +1,14 @@
1
+ export { SpatialComponent } from './SpatialComponent'
2
+ export {
3
+ SpatialWindowComponent,
4
+ type BackgroundMaterialType,
5
+ type StyleParam,
6
+ type CornerRadius,
7
+ } from './SpatialWindowComponent'
8
+ export { SpatialInputComponent } from './SpatialInputComponent'
9
+ export { SpatialModelComponent } from './SpatialModelComponent'
10
+ export { SpatialViewComponent } from './SpatialViewComponent'
11
+ export {
12
+ SpatialModel3DComponent,
13
+ type ModelDragEvent,
14
+ } from './SpatialModel3DComponent'
@@ -0,0 +1,10 @@
1
+ export * from './component'
2
+ export * from './resource'
3
+ export { SpatialEntity } from './SpatialEntity'
4
+ export { SpatialSession } from './SpatialSession'
5
+ export { Spatial } from './Spatial'
6
+ export { SpatialWindowContainer } from './SpatialWindowContainer'
7
+ export { SpatialHelper } from './SpatialHelper'
8
+ export { type WindowStyle } from './types'
9
+ export { SpatialTransform, Vec3, Vec4 } from './SpatialTransform'
10
+ export * from './types'