@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.
- package/CHANGELOG.md +2 -0
- package/README.md +112 -81
- package/dist/iife/index.d.ts +683 -561
- package/dist/iife/index.global.js +3 -4
- package/dist/iife/index.global.js.map +1 -1
- package/dist/index.d.ts +683 -561
- package/dist/index.js +2175 -1291
- package/dist/index.js.map +1 -1
- package/package.json +7 -4
- package/src/JSBCommand.ts +631 -0
- package/src/Spatial.ts +68 -0
- package/src/SpatialObject.ts +46 -0
- package/src/SpatialScene.ts +75 -0
- package/src/SpatialSession.ts +187 -0
- package/src/SpatialWebEvent.ts +23 -0
- package/src/SpatialWebEventCreator.ts +12 -0
- package/src/Spatialized2DElement.ts +51 -0
- package/src/SpatializedDynamic3DElement.ts +30 -0
- package/src/SpatializedElement.ts +331 -0
- package/src/SpatializedElementCreator.ts +45 -0
- package/src/SpatializedStatic3DElement.ts +111 -0
- package/src/WebMsgCommand.ts +88 -0
- package/src/index.ts +23 -1
- package/src/platform-adapter/CommandResultUtils.ts +22 -0
- package/src/platform-adapter/android/AndroidPlatform.ts +133 -0
- package/src/platform-adapter/index.ts +21 -0
- package/src/platform-adapter/interface.ts +36 -0
- package/src/platform-adapter/ssr/SSRPlatform.ts +43 -0
- package/src/platform-adapter/vision-os/VisionOSPlatform.ts +77 -0
- package/src/reality/component/ModelComponent.ts +11 -0
- package/src/reality/component/SpatialComponent.ts +17 -0
- package/src/reality/component/index.ts +2 -0
- package/src/reality/entity/SpatialEntity.ts +255 -0
- package/src/reality/entity/SpatialModelEntity.ts +15 -0
- package/src/reality/entity/index.ts +2 -0
- package/src/reality/geometry/SpatialBoxGeometry.ts +12 -0
- package/src/reality/geometry/SpatialConeGeometry.ts +15 -0
- package/src/reality/geometry/SpatialCylinderGeometry.ts +15 -0
- package/src/reality/geometry/SpatialGeometry.ts +12 -0
- package/src/reality/geometry/SpatialPlaneGeometry.ts +15 -0
- package/src/reality/geometry/SpatialSphereGeometry.ts +15 -0
- package/src/reality/geometry/index.ts +6 -0
- package/src/reality/index.ts +5 -0
- package/src/reality/material/SpatialMaterial.ts +14 -0
- package/src/reality/material/SpatialUnlitMaterial.ts +16 -0
- package/src/reality/material/index.ts +2 -0
- package/src/reality/realityCreator.ts +94 -0
- package/src/reality/resource/SpatialModelAsset.ts +11 -0
- package/src/reality/resource/index.ts +1 -0
- package/src/scene-polyfill.test.ts +376 -0
- package/src/scene-polyfill.ts +359 -0
- package/src/spatial-window-polyfill.ts +182 -0
- package/src/ssr-polyfill.ts +3 -0
- package/src/types/global.d.ts +33 -1
- package/src/types/internal.ts +13 -0
- package/src/types/types.ts +380 -0
- package/src/utils.ts +61 -0
- package/tsconfig.json +1 -1
- package/vitest.config.ts +8 -0
- package/src/core/Spatial.ts +0 -50
- package/src/core/SpatialEntity.ts +0 -147
- package/src/core/SpatialHelper.ts +0 -230
- package/src/core/SpatialObject.ts +0 -26
- package/src/core/SpatialSession.ts +0 -457
- package/src/core/SpatialTransform.ts +0 -26
- package/src/core/SpatialWindowContainer.ts +0 -59
- package/src/core/component/EventSpatialComponent.ts +0 -32
- package/src/core/component/SpatialComponent.ts +0 -26
- package/src/core/component/SpatialInputComponent.ts +0 -24
- package/src/core/component/SpatialModel3DComponent.ts +0 -223
- package/src/core/component/SpatialModelComponent.ts +0 -39
- package/src/core/component/SpatialViewComponent.ts +0 -32
- package/src/core/component/SpatialWindowComponent.ts +0 -177
- package/src/core/component/index.ts +0 -14
- package/src/core/index.ts +0 -10
- package/src/core/private/WebSpatial.ts +0 -383
- package/src/core/private/remote-command/RemoteCommand.ts +0 -15
- package/src/core/private/remote-command/index.ts +0 -1
- package/src/core/resource/SpatialMeshResource.ts +0 -6
- package/src/core/resource/SpatialPhysicallyBasedMaterialResource.ts +0 -42
- package/src/core/resource/index.ts +0 -2
- package/src/core/types.ts +0 -32
|
@@ -0,0 +1,331 @@
|
|
|
1
|
+
import { UpdateSpatializedElementTransform } from './JSBCommand'
|
|
2
|
+
import { WebSpatialProtocolResult } from './platform-adapter/interface'
|
|
3
|
+
import { SpatialObject } from './SpatialObject'
|
|
4
|
+
import { SpatialWebEvent } from './SpatialWebEvent'
|
|
5
|
+
import { createSpatialEvent } from './SpatialWebEventCreator'
|
|
6
|
+
import {
|
|
7
|
+
CubeInfo,
|
|
8
|
+
SpatialDragEndEvent,
|
|
9
|
+
SpatialDragEvent,
|
|
10
|
+
SpatializedElementProperties,
|
|
11
|
+
SpatialMagnifyEndEvent,
|
|
12
|
+
SpatialMagnifyEvent,
|
|
13
|
+
SpatialRotateEndEvent,
|
|
14
|
+
SpatialRotateEvent,
|
|
15
|
+
SpatialTapEvent,
|
|
16
|
+
} from './types/types'
|
|
17
|
+
import {
|
|
18
|
+
CubeInfoMsg,
|
|
19
|
+
ObjectDestroyMsg,
|
|
20
|
+
SpatialDragEndMsg,
|
|
21
|
+
SpatialDragMsg,
|
|
22
|
+
SpatialMagnifyEndMsg,
|
|
23
|
+
SpatialMagnifyMsg,
|
|
24
|
+
SpatialRotateEndMsg,
|
|
25
|
+
SpatialRotateMsg,
|
|
26
|
+
SpatialTapMsg,
|
|
27
|
+
SpatialWebMsgType,
|
|
28
|
+
TransformMsg,
|
|
29
|
+
} from './WebMsgCommand'
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Abstract base class for all spatialized elements in the WebSpatial environment.
|
|
33
|
+
* Provides common functionality for elements that can exist in 3D space,
|
|
34
|
+
* including transformation handling and gesture event processing.
|
|
35
|
+
*/
|
|
36
|
+
export abstract class SpatializedElement extends SpatialObject {
|
|
37
|
+
/**
|
|
38
|
+
* Creates a new spatialized element with the specified ID.
|
|
39
|
+
* Registers the element to receive spatial events.
|
|
40
|
+
* @param id Unique identifier for this element
|
|
41
|
+
*/
|
|
42
|
+
constructor(public readonly id: string) {
|
|
43
|
+
super(id)
|
|
44
|
+
|
|
45
|
+
SpatialWebEvent.addEventReceiver(id, this.onReceiveEvent.bind(this))
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Updates the properties of this spatialized element.
|
|
50
|
+
* Must be implemented by derived classes to handle specific property updates.
|
|
51
|
+
* @param properties Partial set of properties to update
|
|
52
|
+
* @returns Promise resolving to the result of the update operation
|
|
53
|
+
*/
|
|
54
|
+
abstract updateProperties(
|
|
55
|
+
properties: Partial<SpatializedElementProperties>,
|
|
56
|
+
): Promise<WebSpatialProtocolResult>
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Updates the transformation matrix of this element in 3D space.
|
|
60
|
+
* This affects the position, rotation, and scale of the element.
|
|
61
|
+
* @param matrix The new transformation matrix
|
|
62
|
+
* @returns Promise resolving when the transform is updated
|
|
63
|
+
*/
|
|
64
|
+
async updateTransform(matrix: DOMMatrix) {
|
|
65
|
+
return new UpdateSpatializedElementTransform(this, matrix).execute()
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Information about the element's bounding cube.
|
|
70
|
+
* Used for spatial calculations and hit testing.
|
|
71
|
+
*/
|
|
72
|
+
private _cubeInfo?: CubeInfo
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Gets the current cube information for this element.
|
|
76
|
+
* @returns The current CubeInfo or undefined if not set
|
|
77
|
+
*/
|
|
78
|
+
get cubeInfo() {
|
|
79
|
+
return this._cubeInfo
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* The current transformation matrix of this element.
|
|
84
|
+
*/
|
|
85
|
+
private _transform?: DOMMatrix
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* The inverse of the current transformation matrix.
|
|
89
|
+
* Used for converting world coordinates to local coordinates.
|
|
90
|
+
*/
|
|
91
|
+
private _transformInv?: DOMMatrix
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Gets the current transformation matrix.
|
|
95
|
+
* @returns The current transformation matrix or undefined if not set
|
|
96
|
+
*/
|
|
97
|
+
get transform() {
|
|
98
|
+
return this._transform
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Gets the inverse of the current transformation matrix.
|
|
103
|
+
* @returns The inverse transformation matrix or undefined if not set
|
|
104
|
+
*/
|
|
105
|
+
get transformInv() {
|
|
106
|
+
return this._transformInv
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Processes events received from the WebSpatial environment.
|
|
111
|
+
* Handles various spatial events like transforms, gestures, and interactions.
|
|
112
|
+
* @param data The event data received from the WebSpatial system
|
|
113
|
+
*/
|
|
114
|
+
protected onReceiveEvent(
|
|
115
|
+
data:
|
|
116
|
+
| CubeInfoMsg
|
|
117
|
+
| TransformMsg
|
|
118
|
+
| SpatialTapMsg
|
|
119
|
+
| SpatialDragMsg
|
|
120
|
+
| SpatialDragEndMsg
|
|
121
|
+
| SpatialRotateMsg
|
|
122
|
+
| SpatialRotateEndMsg
|
|
123
|
+
| ObjectDestroyMsg,
|
|
124
|
+
) {
|
|
125
|
+
const { type } = data
|
|
126
|
+
if (type === SpatialWebMsgType.objectdestroy) {
|
|
127
|
+
this.isDestroyed = true
|
|
128
|
+
} else if (type === SpatialWebMsgType.cubeInfo) {
|
|
129
|
+
// Handle cube info updates (bounding box information)
|
|
130
|
+
const cubeInfoMsg = data as CubeInfoMsg
|
|
131
|
+
this._cubeInfo = new CubeInfo(cubeInfoMsg.size, cubeInfoMsg.origin)
|
|
132
|
+
} else if (type === SpatialWebMsgType.transform) {
|
|
133
|
+
// Handle transformation matrix updates
|
|
134
|
+
this._transform = new DOMMatrix([
|
|
135
|
+
data.detail.column0[0],
|
|
136
|
+
data.detail.column0[1],
|
|
137
|
+
data.detail.column0[2],
|
|
138
|
+
0,
|
|
139
|
+
data.detail.column1[0],
|
|
140
|
+
data.detail.column1[1],
|
|
141
|
+
data.detail.column1[2],
|
|
142
|
+
0,
|
|
143
|
+
data.detail.column2[0],
|
|
144
|
+
data.detail.column2[1],
|
|
145
|
+
data.detail.column2[2],
|
|
146
|
+
0,
|
|
147
|
+
data.detail.column3[0],
|
|
148
|
+
data.detail.column3[1],
|
|
149
|
+
data.detail.column3[2],
|
|
150
|
+
1,
|
|
151
|
+
])
|
|
152
|
+
this._transformInv = this._transform.inverse()
|
|
153
|
+
} else if (type === SpatialWebMsgType.spatialtap) {
|
|
154
|
+
// Handle tap gestures
|
|
155
|
+
const event = createSpatialEvent(
|
|
156
|
+
SpatialWebMsgType.spatialtap,
|
|
157
|
+
(data as SpatialTapMsg).detail,
|
|
158
|
+
)
|
|
159
|
+
this._onSpatialTap?.(event)
|
|
160
|
+
} else if (type === SpatialWebMsgType.spatialdrag) {
|
|
161
|
+
// Handle drag gestures, with special handling for drag start
|
|
162
|
+
if (!this._isDragging) {
|
|
163
|
+
const dragStartEvent = createSpatialEvent(
|
|
164
|
+
SpatialWebMsgType.spatialdragstart,
|
|
165
|
+
(data as SpatialDragMsg).detail,
|
|
166
|
+
)
|
|
167
|
+
this._onSpatialDragStart?.(dragStartEvent)
|
|
168
|
+
}
|
|
169
|
+
this._isDragging = true
|
|
170
|
+
const event = createSpatialEvent(
|
|
171
|
+
SpatialWebMsgType.spatialdrag,
|
|
172
|
+
(data as SpatialDragMsg).detail,
|
|
173
|
+
)
|
|
174
|
+
this._onSpatialDrag?.(event)
|
|
175
|
+
} else if (type === SpatialWebMsgType.spatialdragend) {
|
|
176
|
+
this._isDragging = false
|
|
177
|
+
const event = createSpatialEvent(
|
|
178
|
+
SpatialWebMsgType.spatialdragend,
|
|
179
|
+
(data as SpatialDragEndMsg).detail,
|
|
180
|
+
)
|
|
181
|
+
this._onSpatialDragEnd?.(event)
|
|
182
|
+
} else if (type === SpatialWebMsgType.spatialrotate) {
|
|
183
|
+
if (!this._isRotating) {
|
|
184
|
+
const rotationStartEvent = createSpatialEvent(
|
|
185
|
+
SpatialWebMsgType.spatialrotatestart,
|
|
186
|
+
(data as SpatialRotateMsg).detail,
|
|
187
|
+
)
|
|
188
|
+
this._onSpatialRotateStart?.(rotationStartEvent)
|
|
189
|
+
}
|
|
190
|
+
this._isRotating = true
|
|
191
|
+
const event = createSpatialEvent(
|
|
192
|
+
SpatialWebMsgType.spatialrotate,
|
|
193
|
+
(data as SpatialRotateMsg).detail,
|
|
194
|
+
)
|
|
195
|
+
this._onSpatialRotate?.(event)
|
|
196
|
+
} else if (type === SpatialWebMsgType.spatialrotateend) {
|
|
197
|
+
this._isRotating = false
|
|
198
|
+
const event = createSpatialEvent(
|
|
199
|
+
SpatialWebMsgType.spatialrotateend,
|
|
200
|
+
(data as SpatialRotateEndMsg).detail,
|
|
201
|
+
)
|
|
202
|
+
this._onSpatialRotateEnd?.(event)
|
|
203
|
+
} else if (type === SpatialWebMsgType.spatialmagnify) {
|
|
204
|
+
if (!this._isMagnify) {
|
|
205
|
+
const magnifyStartEvent = createSpatialEvent(
|
|
206
|
+
SpatialWebMsgType.spatialmagnifystart,
|
|
207
|
+
(data as SpatialMagnifyMsg).detail,
|
|
208
|
+
)
|
|
209
|
+
this._onSpatialMagnifyStart?.(magnifyStartEvent)
|
|
210
|
+
}
|
|
211
|
+
this._isMagnify = true
|
|
212
|
+
const event = createSpatialEvent(
|
|
213
|
+
SpatialWebMsgType.spatialmagnify,
|
|
214
|
+
(data as SpatialMagnifyMsg).detail,
|
|
215
|
+
)
|
|
216
|
+
this._onSpatialMagnify?.(event)
|
|
217
|
+
} else if (type === SpatialWebMsgType.spatialmagnifyend) {
|
|
218
|
+
this._isMagnify = false
|
|
219
|
+
const event = createSpatialEvent(
|
|
220
|
+
SpatialWebMsgType.spatialmagnifyend,
|
|
221
|
+
(data as SpatialMagnifyEndMsg).detail,
|
|
222
|
+
)
|
|
223
|
+
this._onSpatialMagnifyEnd?.(event)
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
private _onSpatialTap?: (event: SpatialTapEvent) => void
|
|
228
|
+
set onSpatialTap(value: (event: SpatialTapEvent) => void | undefined) {
|
|
229
|
+
this._onSpatialTap = value
|
|
230
|
+
this.updateProperties({
|
|
231
|
+
enableTapGesture: value !== undefined,
|
|
232
|
+
})
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
private _isDragging = false
|
|
236
|
+
private _onSpatialDragStart?: (event: SpatialDragEvent) => void
|
|
237
|
+
set onSpatialDragStart(value: (event: SpatialDragEvent) => void | undefined) {
|
|
238
|
+
this._onSpatialDragStart = value
|
|
239
|
+
this.updateProperties({
|
|
240
|
+
enableDragStartGesture: this._onSpatialDragStart !== undefined,
|
|
241
|
+
})
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
private _onSpatialDrag?: (event: SpatialDragEvent) => void
|
|
245
|
+
set onSpatialDrag(value: (event: SpatialDragEvent) => void | undefined) {
|
|
246
|
+
this._onSpatialDrag = value
|
|
247
|
+
this.updateProperties({
|
|
248
|
+
enableDragGesture: this._onSpatialDrag !== undefined,
|
|
249
|
+
})
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
private _onSpatialDragEnd?: (event: SpatialDragEndEvent) => void
|
|
253
|
+
set onSpatialDragEnd(
|
|
254
|
+
value: ((event: SpatialDragEndEvent) => void) | undefined,
|
|
255
|
+
) {
|
|
256
|
+
this._onSpatialDragEnd = value
|
|
257
|
+
this.updateProperties({
|
|
258
|
+
enableDragEndGesture: value !== undefined,
|
|
259
|
+
})
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
private _isRotating = false
|
|
263
|
+
private _onSpatialRotateStart?: (event: SpatialRotateEvent) => void
|
|
264
|
+
set onSpatialRotateStart(
|
|
265
|
+
value: ((event: SpatialRotateEvent) => void) | undefined,
|
|
266
|
+
) {
|
|
267
|
+
this._onSpatialRotateStart = value
|
|
268
|
+
this.updateProperties({
|
|
269
|
+
enableRotateStartGesture: this._onSpatialRotateStart !== undefined,
|
|
270
|
+
})
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
private _onSpatialRotate?: (event: SpatialRotateEvent) => void
|
|
274
|
+
set onSpatialRotate(
|
|
275
|
+
value: ((event: SpatialRotateEvent) => void) | undefined,
|
|
276
|
+
) {
|
|
277
|
+
this._onSpatialRotate = value
|
|
278
|
+
this.updateProperties({
|
|
279
|
+
enableRotateGesture: this._onSpatialRotate !== undefined,
|
|
280
|
+
})
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
private _onSpatialRotateEnd?: (event: SpatialRotateEndEvent) => void
|
|
284
|
+
set onSpatialRotateEnd(
|
|
285
|
+
value: ((event: SpatialRotateEndEvent) => void) | undefined,
|
|
286
|
+
) {
|
|
287
|
+
this._onSpatialRotateEnd = value
|
|
288
|
+
this.updateProperties({
|
|
289
|
+
enableRotateEndGesture: value !== undefined,
|
|
290
|
+
})
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
private _isMagnify = false
|
|
294
|
+
private _onSpatialMagnifyStart?: (event: SpatialMagnifyEvent) => void
|
|
295
|
+
set onSpatialMagnifyStart(
|
|
296
|
+
value: ((event: SpatialMagnifyEvent) => void) | undefined,
|
|
297
|
+
) {
|
|
298
|
+
this._onSpatialMagnifyStart = value
|
|
299
|
+
this.updateProperties({
|
|
300
|
+
enableMagnifyStartGesture: value !== undefined,
|
|
301
|
+
})
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
private _onSpatialMagnify?: (event: SpatialMagnifyEvent) => void
|
|
305
|
+
set onSpatialMagnify(
|
|
306
|
+
value: ((event: SpatialMagnifyEvent) => void) | undefined,
|
|
307
|
+
) {
|
|
308
|
+
this._onSpatialMagnify = value
|
|
309
|
+
this.updateProperties({
|
|
310
|
+
enableMagnifyGesture: value !== undefined,
|
|
311
|
+
})
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
private _onSpatialMagnifyEnd?: (event: SpatialMagnifyEndEvent) => void
|
|
315
|
+
set onSpatialMagnifyEnd(
|
|
316
|
+
value: ((event: SpatialMagnifyEndEvent) => void) | undefined,
|
|
317
|
+
) {
|
|
318
|
+
this._onSpatialMagnifyEnd = value
|
|
319
|
+
this.updateProperties({
|
|
320
|
+
enableMagnifyEndGesture: value !== undefined,
|
|
321
|
+
})
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
/**
|
|
325
|
+
* Cleans up resources when this element is destroyed.
|
|
326
|
+
* Removes event receivers to prevent memory leaks.
|
|
327
|
+
*/
|
|
328
|
+
override onDestroy() {
|
|
329
|
+
SpatialWebEvent.removeEventReceiver(this.id)
|
|
330
|
+
}
|
|
331
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createSpatialized2DElementCommand,
|
|
3
|
+
CreateSpatializedDynamic3DElementCommand,
|
|
4
|
+
CreateSpatializedStatic3DElementCommand,
|
|
5
|
+
} from './JSBCommand'
|
|
6
|
+
import { Spatialized2DElement } from './Spatialized2DElement'
|
|
7
|
+
import { SpatializedStatic3DElement } from './SpatializedStatic3DElement'
|
|
8
|
+
import { SpatializedDynamic3DElement } from './SpatializedDynamic3DElement'
|
|
9
|
+
|
|
10
|
+
export async function createSpatialized2DElement(): Promise<Spatialized2DElement> {
|
|
11
|
+
const result = await new createSpatialized2DElementCommand().execute()
|
|
12
|
+
if (!result.success) {
|
|
13
|
+
throw new Error('createSpatialized2DElement failed')
|
|
14
|
+
} else {
|
|
15
|
+
const { id, windowProxy } = result.data!
|
|
16
|
+
// set base href to make sure the relative url is correct
|
|
17
|
+
windowProxy.document.head.innerHTML = `<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
18
|
+
<base href="${document.baseURI}">`
|
|
19
|
+
return new Spatialized2DElement(id, windowProxy)
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export async function createSpatializedStatic3DElement(
|
|
24
|
+
modelURL: string,
|
|
25
|
+
): Promise<SpatializedStatic3DElement> {
|
|
26
|
+
const result = await new CreateSpatializedStatic3DElementCommand(
|
|
27
|
+
modelURL,
|
|
28
|
+
).execute()
|
|
29
|
+
if (!result.success) {
|
|
30
|
+
throw new Error('createSpatializedStatic3DElement failed')
|
|
31
|
+
} else {
|
|
32
|
+
const { id } = result.data
|
|
33
|
+
return new SpatializedStatic3DElement(id)
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export async function createSpatializedDynamic3DElement(): Promise<SpatializedDynamic3DElement> {
|
|
38
|
+
const result = await new CreateSpatializedDynamic3DElementCommand().execute()
|
|
39
|
+
if (!result.success) {
|
|
40
|
+
throw new Error('createSpatializedDynamic3DElement failed')
|
|
41
|
+
} else {
|
|
42
|
+
const { id } = result.data
|
|
43
|
+
return new SpatializedDynamic3DElement(id)
|
|
44
|
+
}
|
|
45
|
+
}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { UpdateSpatializedStatic3DElementProperties } from './JSBCommand'
|
|
2
|
+
import { SpatializedElement } from './SpatializedElement'
|
|
3
|
+
import { SpatializedStatic3DElementProperties } from './types/types'
|
|
4
|
+
import { SpatialWebMsgType } from './WebMsgCommand'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Represents a static 3D model element in the spatial environment.
|
|
8
|
+
* This class handles loading and displaying pre-built 3D models from URLs,
|
|
9
|
+
* and provides events for load success and failure.
|
|
10
|
+
*/
|
|
11
|
+
export class SpatializedStatic3DElement extends SpatializedElement {
|
|
12
|
+
/**
|
|
13
|
+
* Promise resolver for the ready state.
|
|
14
|
+
* Used to resolve the ready promise when the model is loaded.
|
|
15
|
+
*/
|
|
16
|
+
private _readyResolve?: (success: boolean) => void
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Caches the last model URL to detect changes.
|
|
20
|
+
* Used to reset the ready promise when the model URL changes.
|
|
21
|
+
*/
|
|
22
|
+
private modelURL: string = ''
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Creates a new promise for tracking the ready state of the model.
|
|
26
|
+
* @returns Promise that resolves when the model is loaded (true) or fails to load (false)
|
|
27
|
+
*/
|
|
28
|
+
private createReadyPromise() {
|
|
29
|
+
return new Promise<boolean>(resolve => {
|
|
30
|
+
this._readyResolve = resolve
|
|
31
|
+
})
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Promise that resolves when the model is loaded.
|
|
36
|
+
* Resolves to true on successful load, false on failure.
|
|
37
|
+
*/
|
|
38
|
+
ready: Promise<boolean> = this.createReadyPromise()
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Updates the properties of this static 3D element.
|
|
42
|
+
* Handles special case for modelURL changes by resetting the ready promise.
|
|
43
|
+
* @param properties Partial set of properties to update
|
|
44
|
+
* @returns Promise resolving when the update is complete
|
|
45
|
+
*/
|
|
46
|
+
async updateProperties(
|
|
47
|
+
properties: Partial<SpatializedStatic3DElementProperties>,
|
|
48
|
+
) {
|
|
49
|
+
if (properties.modelURL !== undefined) {
|
|
50
|
+
if (this.modelURL !== properties.modelURL) {
|
|
51
|
+
this.modelURL = properties.modelURL
|
|
52
|
+
this.ready = this.createReadyPromise()
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return new UpdateSpatializedStatic3DElementProperties(
|
|
56
|
+
this,
|
|
57
|
+
properties,
|
|
58
|
+
).execute()
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Processes events received from the WebSpatial environment.
|
|
63
|
+
* Handles model loading events in addition to base spatial events.
|
|
64
|
+
* @param data The event data received from the WebSpatial system
|
|
65
|
+
*/
|
|
66
|
+
override onReceiveEvent(data: { type: SpatialWebMsgType }) {
|
|
67
|
+
if (data.type === SpatialWebMsgType.modelloaded) {
|
|
68
|
+
// Handle successful model loading
|
|
69
|
+
this._onLoadCallback?.()
|
|
70
|
+
this._readyResolve?.(true)
|
|
71
|
+
} else if (data.type === SpatialWebMsgType.modelloadfailed) {
|
|
72
|
+
// Handle model loading failure
|
|
73
|
+
this._onLoadFailureCallback?.()
|
|
74
|
+
this._readyResolve?.(false)
|
|
75
|
+
} else {
|
|
76
|
+
// Handle other spatial events using the base class implementation
|
|
77
|
+
super.onReceiveEvent(data as any)
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Callback function for successful model loading.
|
|
83
|
+
*/
|
|
84
|
+
private _onLoadCallback?: () => void
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Sets the callback function for successful model loading.
|
|
88
|
+
* @param callback Function to call when the model is loaded successfully
|
|
89
|
+
*/
|
|
90
|
+
set onLoadCallback(callback: undefined | (() => void)) {
|
|
91
|
+
this._onLoadCallback = callback
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Callback function for model loading failure.
|
|
96
|
+
*/
|
|
97
|
+
private _onLoadFailureCallback?: undefined | (() => void)
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Sets the callback function for model loading failure.
|
|
101
|
+
* @param callback Function to call when the model fails to load
|
|
102
|
+
*/
|
|
103
|
+
set onLoadFailureCallback(callback: undefined | (() => void)) {
|
|
104
|
+
this._onLoadFailureCallback = callback
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
updateModelTransform(transform: DOMMatrix) {
|
|
108
|
+
const modelTransform = Array.from(transform.toFloat64Array())
|
|
109
|
+
this.updateProperties({ modelTransform })
|
|
110
|
+
}
|
|
111
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Vec3,
|
|
3
|
+
Size3D,
|
|
4
|
+
SpatialDragEventDetail,
|
|
5
|
+
SpatialTapEventDetail,
|
|
6
|
+
SpatialRotateEventDetail,
|
|
7
|
+
SpatialMagnifyEventDetail,
|
|
8
|
+
} from './types/types'
|
|
9
|
+
|
|
10
|
+
export enum SpatialWebMsgType {
|
|
11
|
+
cubeInfo = 'cubeInfo',
|
|
12
|
+
transform = 'transform',
|
|
13
|
+
modelloaded = 'modelloaded',
|
|
14
|
+
modelloadfailed = 'modelloadfailed',
|
|
15
|
+
spatialtap = 'spatialtap',
|
|
16
|
+
spatialdragstart = 'spatialdragstart',
|
|
17
|
+
spatialdrag = 'spatialdrag',
|
|
18
|
+
spatialdragend = 'spatialdragend',
|
|
19
|
+
spatialrotatestart = 'spatialrotatestart',
|
|
20
|
+
spatialrotate = 'spatialrotate',
|
|
21
|
+
spatialrotateend = 'spatialrotateend',
|
|
22
|
+
spatialmagnifystart = 'spatialmagnifystart',
|
|
23
|
+
spatialmagnify = 'spatialmagnify',
|
|
24
|
+
spatialmagnifyend = 'spatialmagnifyend',
|
|
25
|
+
|
|
26
|
+
objectdestroy = 'objectdestroy',
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface ObjectDestroyMsg {
|
|
30
|
+
type: SpatialWebMsgType.objectdestroy
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface CubeInfoMsg {
|
|
34
|
+
type: SpatialWebMsgType.cubeInfo
|
|
35
|
+
origin: Vec3
|
|
36
|
+
size: Size3D
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface CubeInfoMsg {
|
|
40
|
+
type: SpatialWebMsgType.cubeInfo
|
|
41
|
+
origin: Vec3
|
|
42
|
+
size: Size3D
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export interface TransformMsg {
|
|
46
|
+
type: SpatialWebMsgType.transform
|
|
47
|
+
detail: {
|
|
48
|
+
column0: [number, number, number]
|
|
49
|
+
column1: [number, number, number]
|
|
50
|
+
column2: [number, number, number]
|
|
51
|
+
column3: [number, number, number]
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export interface SpatialTapMsg {
|
|
56
|
+
type: SpatialWebMsgType.spatialtap
|
|
57
|
+
detail: SpatialTapEventDetail
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export interface SpatialDragMsg {
|
|
61
|
+
type: SpatialWebMsgType.spatialdrag
|
|
62
|
+
detail: SpatialDragEventDetail
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export interface SpatialDragEndMsg {
|
|
66
|
+
type: SpatialWebMsgType.spatialdragend
|
|
67
|
+
detail: SpatialDragEventDetail
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export interface SpatialRotateMsg {
|
|
71
|
+
type: SpatialWebMsgType.spatialrotate
|
|
72
|
+
detail: SpatialRotateEventDetail
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export interface SpatialRotateEndMsg {
|
|
76
|
+
type: SpatialWebMsgType.spatialrotateend
|
|
77
|
+
detail: SpatialRotateEventDetail
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export interface SpatialMagnifyMsg {
|
|
81
|
+
type: SpatialWebMsgType.spatialmagnify
|
|
82
|
+
detail: SpatialMagnifyEventDetail
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export interface SpatialMagnifyEndMsg {
|
|
86
|
+
type: SpatialWebMsgType.spatialmagnifyend
|
|
87
|
+
detail: SpatialMagnifyEventDetail
|
|
88
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1 +1,23 @@
|
|
|
1
|
-
export
|
|
1
|
+
export { SpatialObject } from './SpatialObject'
|
|
2
|
+
export { Spatial } from './Spatial'
|
|
3
|
+
export { SpatialSession } from './SpatialSession'
|
|
4
|
+
export { SpatialScene } from './SpatialScene'
|
|
5
|
+
export { SpatializedElement } from './SpatializedElement'
|
|
6
|
+
export { Spatialized2DElement } from './Spatialized2DElement'
|
|
7
|
+
export { SpatializedStatic3DElement } from './SpatializedStatic3DElement'
|
|
8
|
+
export { SpatializedDynamic3DElement } from './SpatializedDynamic3DElement'
|
|
9
|
+
export * from './reality'
|
|
10
|
+
export * from './types/types'
|
|
11
|
+
export * from './types/global.d'
|
|
12
|
+
|
|
13
|
+
// side effects
|
|
14
|
+
import { injectSceneHook } from './scene-polyfill'
|
|
15
|
+
import { isSSREnv } from './ssr-polyfill'
|
|
16
|
+
import { spatialWindowPolyfill } from './spatial-window-polyfill'
|
|
17
|
+
|
|
18
|
+
export { isSSREnv }
|
|
19
|
+
|
|
20
|
+
if (!isSSREnv() && navigator.userAgent.indexOf('WebSpatial/') > 0) {
|
|
21
|
+
injectSceneHook()
|
|
22
|
+
spatialWindowPolyfill()
|
|
23
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { CommandResult } from './interface'
|
|
2
|
+
|
|
3
|
+
export function CommandResultSuccess(data: any): CommandResult {
|
|
4
|
+
return {
|
|
5
|
+
success: true,
|
|
6
|
+
data,
|
|
7
|
+
errorCode: '',
|
|
8
|
+
errorMessage: '',
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function CommandResultFailure(
|
|
13
|
+
errorCode: string,
|
|
14
|
+
errorMessage = '',
|
|
15
|
+
): CommandResult {
|
|
16
|
+
return {
|
|
17
|
+
success: false,
|
|
18
|
+
data: undefined,
|
|
19
|
+
errorCode,
|
|
20
|
+
errorMessage,
|
|
21
|
+
}
|
|
22
|
+
}
|