@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
@@ -1,383 +0,0 @@
1
- import { RemoteCommand } from './remote-command'
2
- import {
3
- WindowStyle,
4
- WindowContainerOptions,
5
- LoadingMethodKind,
6
- sceneDataShape,
7
- sceneDataJSBShape,
8
- } from '../types'
9
-
10
- declare global {
11
- interface Window {
12
- // Location for webspatial custom functions
13
- __WebSpatialData: {
14
- androidNativeMessage: Function
15
- getNativeVersion: Function
16
- }
17
-
18
- // Location for webspatial internal callbacks (eg. completion events)
19
- __SpatialWebEvent: Function
20
-
21
- // Used to access webkit specific api
22
- webkit: any
23
-
24
- // Marks the page as unloaded so it doesn't send additional events
25
- __WebSpatialUnloaded: boolean
26
-
27
- // Internal id information mapping to internal state about the native window
28
- _webSpatialID: string
29
- _webSpatialGroupID: string
30
- _webSpatialParentGroupID: string
31
-
32
- // Will be removed in favor of __WebSpatialData
33
- WebSpatailNativeVersion: string
34
- }
35
- }
36
-
37
- export class WindowContainer {
38
- id = ''
39
- }
40
-
41
- export class WebSpatialResource {
42
- id = ''
43
- windowContainerId = ''
44
- data = {} as any
45
-
46
- receiveEvent() {}
47
- }
48
-
49
- export class WebSpatial {
50
- public static eventPromises: any = {}
51
-
52
- public static transactionStarted = false
53
- public static transactionCommands = Array<RemoteCommand>()
54
-
55
- // store event receivers
56
- private static eventReceivers: { [resourceId: string]: (data: any) => void } =
57
- {}
58
-
59
- public static registerEventReceiver(
60
- resourceId: string,
61
- callback: (data: any) => void,
62
- ) {
63
- this.eventReceivers[resourceId] = callback
64
- }
65
-
66
- public static unregisterEventReceiver(resourceId: string) {
67
- delete this.eventReceivers[resourceId]
68
- }
69
-
70
- static init() {
71
- window.__SpatialWebEvent = (e: any) => {
72
- if (e.resourceId) {
73
- var callback = WebSpatial.eventReceivers[e.resourceId]
74
- callback(e.data)
75
- } else {
76
- var p = WebSpatial.eventPromises[e.requestID]
77
- if (p) {
78
- if (e.success) {
79
- p.res(e)
80
- } else {
81
- p.rej(e)
82
- }
83
- }
84
- }
85
- }
86
- }
87
-
88
- static startTransaction() {
89
- WebSpatial.transactionStarted = true
90
- WebSpatial.transactionCommands = []
91
- }
92
-
93
- static async sendTransaction() {
94
- WebSpatial.transactionStarted = false
95
- var cmd = new RemoteCommand('multiCommand', {
96
- commandList: WebSpatial.transactionCommands,
97
- })
98
-
99
- var result = await new Promise((res, rej) => {
100
- WebSpatial.eventPromises[cmd.requestID] = { res: res, rej: rej }
101
- WebSpatial.sendCommand(cmd)
102
- })
103
- return result
104
- }
105
-
106
- static getBackend() {
107
- if (window.webkit) {
108
- return 'AVP'
109
- } else {
110
- return 'UNKNOWN'
111
- }
112
- }
113
-
114
- static async sendCommand(cmd: RemoteCommand) {
115
- if (window.__WebSpatialUnloaded) {
116
- return
117
- }
118
- if (WebSpatial.transactionStarted) {
119
- WebSpatial.transactionCommands.push(cmd as any)
120
- return
121
- }
122
-
123
- var msg = JSON.stringify(cmd)
124
-
125
- if (WebSpatial.getBackend() == 'AVP') {
126
- window.webkit.messageHandlers.bridge.postMessage(msg)
127
- return
128
- } else {
129
- window.__WebSpatialData.androidNativeMessage(msg)
130
- return
131
- }
132
- }
133
-
134
- static getImmersiveWindowContainer() {
135
- var wg = new WindowContainer()
136
- wg.id = 'Immersive'
137
- return wg
138
- }
139
-
140
- static getCurrentWindowContainer() {
141
- var wg = new WindowContainer()
142
- wg.id = 'current'
143
- return wg
144
- }
145
-
146
- static getCurrentWebPanel() {
147
- var wg = new WebSpatialResource()
148
- wg.id = 'current'
149
- wg.windowContainerId = WebSpatial.getCurrentWindowContainer().id
150
- return wg
151
- }
152
-
153
- static async createScene(
154
- style: WindowStyle = 'Plain',
155
- cfg: {
156
- sceneData: sceneDataShape
157
- },
158
- ) {
159
- const { window: newWindow, ...sceneData } = cfg.sceneData
160
- const jsbSceneData: sceneDataJSBShape = {
161
- ...sceneData,
162
- windowID: newWindow._webSpatialID,
163
- windowContainerID: newWindow._webSpatialGroupID,
164
- }
165
- var cmd = new RemoteCommand('createScene', {
166
- windowStyle: style,
167
- sceneData: jsbSceneData,
168
- windowContainerID: window._webSpatialParentGroupID, // parent WindowContainerID
169
- })
170
-
171
- try {
172
- await new Promise((res, rej) => {
173
- WebSpatial.eventPromises[cmd.requestID] = { res: res, rej: rej }
174
- WebSpatial.sendCommand(cmd)
175
- })
176
- return true
177
- } catch (error) {
178
- return false
179
- }
180
- }
181
-
182
- static async createWindowContainer(
183
- style: WindowStyle = 'Plain',
184
- windowContainer: WindowContainer | null,
185
- parentWebView: WebSpatialResource | null,
186
- ) {
187
- var cmd = new RemoteCommand('createWindowContainer', {
188
- windowStyle: style,
189
- windowContainerID: windowContainer ? windowContainer.id : undefined,
190
- resourceID: parentWebView ? parentWebView.id : undefined,
191
- })
192
-
193
- var result = await new Promise((res, rej) => {
194
- WebSpatial.eventPromises[cmd.requestID] = { res: res, rej: rej }
195
- WebSpatial.sendCommand(cmd)
196
- })
197
- var res = new WindowContainer()
198
- res.id = (result as any).data.createdID
199
- return res
200
- }
201
-
202
- static async destroyResource(resource: WebSpatialResource) {
203
- const data = {}
204
- var cmd = new RemoteCommand('destroyResource', {
205
- windowContainerID: resource.windowContainerId,
206
- resourceID: resource.id,
207
- })
208
-
209
- WebSpatial.sendCommand(cmd)
210
- }
211
-
212
- static async ping(msg: string) {
213
- var cmd = new RemoteCommand('ping', {
214
- windowContainerID: this.getCurrentWindowContainer().id,
215
- resourceID: this.getCurrentWebPanel().id,
216
- message: msg,
217
- })
218
-
219
- if (WebSpatial.transactionStarted) {
220
- WebSpatial.sendCommand(cmd)
221
- return null
222
- } else {
223
- var result = await new Promise((res, rej) => {
224
- WebSpatial.eventPromises[cmd.requestID] = { res: res, rej: rej }
225
- WebSpatial.sendCommand(cmd)
226
- })
227
- return result
228
- }
229
- }
230
-
231
- static async getStats() {
232
- var cmd = new RemoteCommand('getStats', {
233
- windowContainerID: this.getCurrentWindowContainer().id,
234
- resourceID: this.getCurrentWebPanel().id,
235
- })
236
-
237
- var result = await new Promise((res, rej) => {
238
- WebSpatial.eventPromises[cmd.requestID] = { res: res, rej: rej }
239
- WebSpatial.sendCommand(cmd)
240
- })
241
- return (result as any).data
242
- }
243
-
244
- static async inspect(spatialObjectId: string) {
245
- var cmd = new RemoteCommand('inspect', {
246
- resourceID: spatialObjectId,
247
- })
248
-
249
- var result = await new Promise((res, rej) => {
250
- WebSpatial.eventPromises[cmd.requestID] = { res: res, rej: rej }
251
- WebSpatial.sendCommand(cmd)
252
- })
253
-
254
- return (result as any).data
255
- }
256
-
257
- static async inspectRootWindowContainer() {
258
- return this.inspect('root')
259
- }
260
-
261
- static async setComponent(
262
- entity: WebSpatialResource,
263
- resource: WebSpatialResource,
264
- ) {
265
- var cmd = new RemoteCommand('setComponent', {
266
- windowContainerID: entity.windowContainerId,
267
- resourceID: resource.id,
268
- entityID: entity.id,
269
- })
270
-
271
- WebSpatial.sendCommand(cmd)
272
- }
273
-
274
- static async removeComponent(
275
- entity: WebSpatialResource,
276
- resource: WebSpatialResource,
277
- ) {
278
- var cmd = new RemoteCommand('removeComponent', {
279
- windowContainerID: entity.windowContainerId,
280
- resourceID: resource.id,
281
- entityID: entity.id,
282
- })
283
-
284
- WebSpatial.sendCommand(cmd)
285
- }
286
-
287
- // windowContainer is the group the resource will be tied to (if not provided it will use the current window grou)
288
- // parentWebView is the SpatialWebView that the resource will be tied to (if not provided, resource will continue to exist even if this page is unloaded)
289
- static async createResource(
290
- type: string,
291
- windowContainer: WindowContainer | null,
292
- parentWebView: WebSpatialResource | null,
293
- params = {} as any,
294
- ) {
295
- var cmd = new RemoteCommand('createResource', {
296
- windowContainerID: windowContainer ? windowContainer.id : undefined,
297
- resourceID: parentWebView ? parentWebView.id : undefined,
298
- type: type,
299
- params: params,
300
- })
301
-
302
- var result = await new Promise((res, rej) => {
303
- WebSpatial.eventPromises[cmd.requestID] = { res: res, rej: rej }
304
- WebSpatial.sendCommand(cmd)
305
- })
306
- var res = new WebSpatialResource()
307
- res.id = (result as any).data.createdID
308
- res.windowContainerId = cmd.data.windowContainerID
309
- return res
310
- }
311
-
312
- static async updateWindowContainer(wg: WindowContainer, data: any) {
313
- var cmd = new RemoteCommand('updateWindowContainer', {
314
- windowContainerID: wg.id,
315
- update: data,
316
- })
317
-
318
- var result = await new Promise((res, rej) => {
319
- WebSpatial.eventPromises[cmd.requestID] = { res: res, rej: rej }
320
- WebSpatial.sendCommand(cmd)
321
- })
322
- return result
323
- }
324
-
325
- static async updateResource(resource: WebSpatialResource, data: any = null) {
326
- var cmd = new RemoteCommand('updateResource', {
327
- windowContainerID: resource.windowContainerId,
328
- resourceID: resource.id,
329
- update: data || resource.data,
330
- })
331
-
332
- var result = await new Promise((res, rej) => {
333
- WebSpatial.eventPromises[cmd.requestID] = { res: res, rej: rej }
334
- WebSpatial.sendCommand(cmd)
335
- })
336
- return result
337
- }
338
-
339
- static async setLoading(method: LoadingMethodKind, style?: string) {
340
- var cmd = new RemoteCommand('setLoading', {
341
- windowContainerID: window._webSpatialParentGroupID, // parent WindowContainerID
342
- loading: {
343
- method,
344
- style,
345
- },
346
- })
347
-
348
- var result = await new Promise((res, rej) => {
349
- WebSpatial.eventPromises[cmd.requestID] = { res: res, rej: rej }
350
- WebSpatial.sendCommand(cmd)
351
- })
352
- return result
353
- }
354
-
355
- static async openImmersiveSpace() {
356
- var cmd = new RemoteCommand('openImmersiveSpace')
357
- await WebSpatial.sendCommand(cmd)
358
- }
359
-
360
- static async dismissImmersiveSpace() {
361
- var cmd = new RemoteCommand('dismissImmersiveSpace')
362
- await WebSpatial.sendCommand(cmd)
363
- }
364
-
365
- static onFrame(fn: (curTime: number) => Promise<any>) {
366
- var dt = 0
367
- var loop = async () => {
368
- var curTime = window.performance.now()
369
- await fn(curTime)
370
- var updateTime = window.performance.now() - curTime
371
-
372
- // Call update loop targetting 60 fps
373
- setTimeout(
374
- () => {
375
- loop()
376
- },
377
- Math.max(1000 / 60 - updateTime, 0),
378
- )
379
- }
380
- loop()
381
- }
382
- }
383
- WebSpatial.init()
@@ -1,15 +0,0 @@
1
- export class RemoteCommand {
2
- private static requestCounter = 0
3
-
4
- command: string
5
-
6
- data: any
7
-
8
- requestID: number
9
-
10
- constructor(cmd: string, data?: any) {
11
- this.command = cmd
12
- this.data = data
13
- this.requestID = ++RemoteCommand.requestCounter
14
- }
15
- }
@@ -1 +0,0 @@
1
- export { RemoteCommand } from './RemoteCommand'
@@ -1,6 +0,0 @@
1
- import { SpatialObject } from '../SpatialObject'
2
-
3
- /**
4
- * Mesh asset containing geometry
5
- */
6
- export class SpatialMeshResource extends SpatialObject {}
@@ -1,42 +0,0 @@
1
- import { SpatialModelComponent } from '../component'
2
- import { WebSpatial } from '../private/WebSpatial'
3
- import { SpatialObject } from '../SpatialObject'
4
-
5
- /**
6
- * PBR material which can be set on a SpatialModelComponent
7
- */
8
- export class SpatialPhysicallyBasedMaterialResource extends SpatialObject {
9
- /**
10
- * Base color of the material containing rgba between 0 and 1
11
- */
12
- baseColor = { r: 0.0, g: 0.7, b: 0.7, a: 1.0 }
13
- /**
14
- * PBR metalic value between 0 and 1
15
- */
16
- metallic = { value: 0.5 }
17
- /**
18
- * PBR roughness value between 0 and 1
19
- */
20
- roughness = { value: 0.5 }
21
-
22
- _modelComponentAttachedTo: { [key: string]: SpatialModelComponent } = {}
23
- _addToComponent(c: SpatialModelComponent) {
24
- this._modelComponentAttachedTo[c._resource.id] = c
25
- }
26
-
27
- /**
28
- * Syncs state of color, metallic, roupghness to the renderer
29
- */
30
- async update() {
31
- await WebSpatial.updateResource(this._resource, {
32
- baseColor: this.baseColor,
33
- metallic: this.metallic,
34
- roughness: this.roughness,
35
- })
36
-
37
- // Since realitykit's materials are structs and not references, every time we change a material, we must copy it to all of the components its attached to to observe the update
38
- for (var key in this._modelComponentAttachedTo) {
39
- await this._modelComponentAttachedTo[key]._syncMaterials()
40
- }
41
- }
42
- }
@@ -1,2 +0,0 @@
1
- export { SpatialMeshResource } from './SpatialMeshResource'
2
- export { SpatialPhysicallyBasedMaterialResource } from './SpatialPhysicallyBasedMaterialResource'
package/src/core/types.ts DELETED
@@ -1,32 +0,0 @@
1
- export type WindowStyle = 'Plain' | 'Volumetric'
2
-
3
- export interface WindowContainerOptions {
4
- defaultSize?: {
5
- width: number // Initial width of the window
6
- height: number // Initial height of the window
7
- }
8
-
9
- resizability?: {
10
- minWidth?: number
11
- minHeight?: number
12
- maxWidth?: number
13
- maxHeight?: number
14
- }
15
- }
16
-
17
- export type LoadingMethodKind = 'show' | 'hide'
18
-
19
- export interface sceneDataShape {
20
- method?: 'createRoot' | 'showRoot'
21
- sceneConfig?: WindowContainerOptions
22
- url?: string
23
- window: Window
24
- }
25
-
26
- export interface sceneDataJSBShape {
27
- method?: 'createRoot' | 'showRoot'
28
- sceneConfig?: WindowContainerOptions
29
- url?: string
30
- windowID?: string
31
- windowContainerID?: string
32
- }