@webspatial/builder 0.0.9 → 0.0.11

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 (92) hide show
  1. package/dist/index.js +1 -1
  2. package/dist/lib/Cli.js +1 -1
  3. package/dist/lib/cmds/build.d.ts +1 -1
  4. package/dist/lib/cmds/build.js +1 -1
  5. package/dist/lib/cmds/check.js +1 -1
  6. package/dist/lib/cmds/help.js +1 -1
  7. package/dist/lib/cmds/version.js +1 -1
  8. package/dist/lib/pwa/config.js +1 -1
  9. package/dist/lib/pwa/index.js +1 -1
  10. package/dist/lib/pwa/validate.js +1 -1
  11. package/dist/lib/resource/file.js +1 -1
  12. package/dist/lib/resource/imageHelper.js +1 -1
  13. package/dist/lib/resource/index.d.ts +10 -3
  14. package/dist/lib/resource/index.js +1 -1
  15. package/dist/lib/resource/load.js +1 -1
  16. package/dist/lib/utils/CustomError.js +1 -1
  17. package/dist/lib/utils/FetchUtils-1.js +1 -1
  18. package/dist/lib/utils/Log.js +1 -1
  19. package/dist/lib/utils/fetch.js +1 -1
  20. package/dist/lib/utils/messages.js +1 -1
  21. package/dist/lib/utils/utils.d.ts +9 -0
  22. package/dist/lib/utils/utils.js +1 -1
  23. package/dist/lib/xcode/index.js +1 -1
  24. package/dist/lib/xcode/manifestSwiftTemplate.d.ts +1 -1
  25. package/dist/lib/xcode/manifestSwiftTemplate.js +1 -1
  26. package/dist/lib/xcode/xcodebuild.js +1 -1
  27. package/dist/lib/xcode/xcodeproject.js +1 -1
  28. package/dist/lib/xcode/xcrun.js +1 -1
  29. package/package.json +3 -2
  30. package/template/visionOSApp/Packages/RealityKitContent/.build/workspace-state.json +0 -7
  31. package/template/visionOSApp/Packages/RealityKitContent/.swiftpm/xcode/xcuserdata/bytedance.xcuserdatad/xcschemes/xcschememanagement.plist +0 -14
  32. package/template/visionOSApp/Packages/RealityKitContent/Package.realitycomposerpro/ProjectData/main.json +0 -11
  33. package/template/visionOSApp/Packages/RealityKitContent/Package.realitycomposerpro/WorkspaceData/SceneMetadataList.json +0 -112
  34. package/template/visionOSApp/Packages/RealityKitContent/Package.realitycomposerpro/WorkspaceData/Settings.rcprojectdata +0 -17
  35. package/template/visionOSApp/Packages/RealityKitContent/Package.swift +0 -27
  36. package/template/visionOSApp/Packages/RealityKitContent/README.md +0 -3
  37. package/template/visionOSApp/Packages/RealityKitContent/Sources/RealityKitContent/RealityKitContent.rkassets/Immersive.usda +0 -50
  38. package/template/visionOSApp/Packages/RealityKitContent/Sources/RealityKitContent/RealityKitContent.rkassets/Materials/GridMaterial.usda +0 -216
  39. package/template/visionOSApp/Packages/RealityKitContent/Sources/RealityKitContent/RealityKitContent.rkassets/Scene.usda +0 -59
  40. package/template/visionOSApp/Packages/RealityKitContent/Sources/RealityKitContent/RealityKitContent.swift +0 -4
  41. package/template/visionOSApp/web-spatial/Assets.xcassets/AppIcon.solidimagestack/Back.solidimagestacklayer/Content.imageset/Contents.json +0 -12
  42. package/template/visionOSApp/web-spatial/Assets.xcassets/AppIcon.solidimagestack/Back.solidimagestacklayer/Contents.json +0 -6
  43. package/template/visionOSApp/web-spatial/Assets.xcassets/AppIcon.solidimagestack/Contents.json +0 -17
  44. package/template/visionOSApp/web-spatial/Assets.xcassets/AppIcon.solidimagestack/Front.solidimagestacklayer/Content.imageset/Contents.json +0 -12
  45. package/template/visionOSApp/web-spatial/Assets.xcassets/AppIcon.solidimagestack/Front.solidimagestacklayer/Contents.json +0 -6
  46. package/template/visionOSApp/web-spatial/Assets.xcassets/AppIcon.solidimagestack/Middle.solidimagestacklayer/Content.imageset/Contents.json +0 -12
  47. package/template/visionOSApp/web-spatial/Assets.xcassets/AppIcon.solidimagestack/Middle.solidimagestacklayer/Contents.json +0 -6
  48. package/template/visionOSApp/web-spatial/Assets.xcassets/Contents.json +0 -6
  49. package/template/visionOSApp/web-spatial/Info.plist +0 -33
  50. package/template/visionOSApp/web-spatial/Preview Content/Preview Assets.xcassets/Contents.json +0 -6
  51. package/template/visionOSApp/web-spatial/libs/EventEmitter.swift +0 -32
  52. package/template/visionOSApp/web-spatial/libs/SpatialComponent.swift +0 -31
  53. package/template/visionOSApp/web-spatial/libs/SpatialEntity.swift +0 -179
  54. package/template/visionOSApp/web-spatial/libs/SpatialInputComponent.swift +0 -26
  55. package/template/visionOSApp/web-spatial/libs/SpatialMeshResource.swift +0 -19
  56. package/template/visionOSApp/web-spatial/libs/SpatialModel3DComponent.swift +0 -51
  57. package/template/visionOSApp/web-spatial/libs/SpatialModelComponent.swift +0 -32
  58. package/template/visionOSApp/web-spatial/libs/SpatialObject.swift +0 -144
  59. package/template/visionOSApp/web-spatial/libs/SpatialPhysicallyBasedMaterial.swift +0 -19
  60. package/template/visionOSApp/web-spatial/libs/SpatialViewComponent.swift +0 -15
  61. package/template/visionOSApp/web-spatial/libs/SpatialWindowComponent.swift +0 -420
  62. package/template/visionOSApp/web-spatial/libs/SpatialWindowContainer.swift +0 -149
  63. package/template/visionOSApp/web-spatial/libs/Utils/CommandManager.swift +0 -800
  64. package/template/visionOSApp/web-spatial/libs/Utils/Logger.swift +0 -36
  65. package/template/visionOSApp/web-spatial/libs/Utils/SceneManager.swift +0 -108
  66. package/template/visionOSApp/web-spatial/libs/Utils/WindowContainerMgr.swift +0 -113
  67. package/template/visionOSApp/web-spatial/libs/json/JsonParser.swift +0 -52
  68. package/template/visionOSApp/web-spatial/libs/uiKitDelegate/Window.swift +0 -34
  69. package/template/visionOSApp/web-spatial/libs/webView/UpdateSystem.swift +0 -33
  70. package/template/visionOSApp/web-spatial/libs/webView/backend/NativeWebView.swift +0 -319
  71. package/template/visionOSApp/web-spatial/libs/webView/manifest.swift +0 -92
  72. package/template/visionOSApp/web-spatial/static-web/index.html +0 -9
  73. package/template/visionOSApp/web-spatial/views/HideViewModifier.swift +0 -17
  74. package/template/visionOSApp/web-spatial/views/ImmersiveView.swift +0 -24
  75. package/template/visionOSApp/web-spatial/views/LoadingView.swift +0 -25
  76. package/template/visionOSApp/web-spatial/views/MaterialWithBorderCornerModifier.swift +0 -82
  77. package/template/visionOSApp/web-spatial/views/OpenDismissHandlerUI.swift +0 -52
  78. package/template/visionOSApp/web-spatial/views/PlainWindowContainerView.swift +0 -84
  79. package/template/visionOSApp/web-spatial/views/SpatialModel3DView.swift +0 -193
  80. package/template/visionOSApp/web-spatial/views/SpatialViewUI.swift +0 -168
  81. package/template/visionOSApp/web-spatial/views/SpatialWebViewUI.swift +0 -186
  82. package/template/visionOSApp/web-spatial/views/VolumetricWindowContainerView.swift +0 -38
  83. package/template/visionOSApp/web-spatial/views/ui/NavView.swift +0 -125
  84. package/template/visionOSApp/web-spatial/web_spatialApp.swift +0 -158
  85. package/template/visionOSApp/web-spatial.xcodeproj/project.pbxproj +0 -686
  86. package/template/visionOSApp/web-spatial.xcodeproj/project.xcworkspace/contents.xcworkspacedata +0 -7
  87. package/template/visionOSApp/web-spatial.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist +0 -8
  88. package/template/visionOSApp/web-spatial.xcodeproj/project.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings +0 -5
  89. package/template/visionOSApp/web-spatial.xcodeproj/project.xcworkspace/xcuserdata/bytedance.xcuserdatad/WorkspaceSettings.xcsettings +0 -14
  90. package/template/visionOSApp/web-spatial.xcodeproj/xcshareddata/xcschemes/web-spatial.xcscheme +0 -115
  91. package/template/visionOSApp/web-spatial.xcodeproj/xcuserdata/bytedance.xcuserdatad/xcschemes/xcschememanagement.plist +0 -27
  92. package/template/visionOSApp/web-spatialTests/web_spatialTests.swift +0 -34
@@ -1,800 +0,0 @@
1
- //
2
- // CommandManager.swift
3
- // web-spatial
4
- //
5
- // Created by ByteDance on 2024/10/15.
6
- //
7
- import Foundation
8
- import RealityKit
9
- import SwiftUI
10
-
11
- struct CommandInfo {
12
- var windowContainerID = "notFound"
13
- var entityID = "notFound"
14
- var resourceID = "notFound"
15
- var requestID = -1
16
- var cmd: JSBCommand
17
- }
18
-
19
- class CommandManager {
20
- static let Instance = CommandManager()
21
-
22
- private let decoder = JSONDecoder()
23
- private var commandList: [String: (_ target: SpatialWindowComponent, _ info: CommandInfo) -> Void] = [:]
24
- private init() {
25
- _ = registerCommand(name: "multiCommand", action: multiCommand)
26
- _ = registerCommand(name: "ping", action: ping)
27
- _ = registerCommand(name: "inspect", action: inspect)
28
- _ = registerCommand(name: "getStats", action: getStats)
29
- _ = registerCommand(name: "setComponent", action: setComponent)
30
- _ = registerCommand(name: "removeComponent", action: removeComponent)
31
- _ = registerCommand(name: "createResource", action: createResource)
32
- _ = registerCommand(name: "destroyResource", action: destroyResource)
33
- _ = registerCommand(name: "updateResource", action: updateResource)
34
- _ = registerCommand(name: "createWindowContainer", action: createWindowContainer)
35
- _ = registerCommand(name: "updateWindowContainer", action: updateWindowContainer)
36
- _ = registerCommand(name: "openImmersiveSpace", action: openImmersiveSpace)
37
- _ = registerCommand(name: "dismissImmersiveSpace", action: dismissImmersiveSpace)
38
- _ = registerCommand(name: "log", action: log)
39
- _ = registerCommand(name: "createScene", action: createScene)
40
- _ = registerCommand(name: "setLoading", action: setLoading)
41
- }
42
-
43
- private func getInfo(_ target: SpatialWindowComponent, _ jsb: RawJSBCommand) -> CommandInfo? {
44
- var ret = CommandInfo(cmd: JSBCommand(command: jsb.command, data: jsb.data, requestID: jsb.requestID))
45
- ret.requestID = jsb.requestID
46
- if let windowContainerID = jsb.data?.windowContainerID {
47
- ret.windowContainerID = target.readWindowContainerID(id: windowContainerID)
48
- }
49
- if let entityID = jsb.data?.entityID {
50
- ret.entityID = entityID
51
- }
52
- if let resourceID = jsb.data?.resourceID {
53
- ret.resourceID = resourceID
54
- }
55
- if ret.resourceID == "current" {
56
- ret.resourceID = target.id
57
- }
58
- return ret
59
- }
60
-
61
- private func registerCommand(name: String, action: @escaping (_ target: SpatialWindowComponent, _ info: CommandInfo) -> Void) -> Bool {
62
- if commandList[name] == nil {
63
- commandList[name] = action
64
- return true
65
- }
66
- return false
67
- }
68
-
69
- public func decode(jsonData: String) -> RawJSBCommand {
70
- var jsbCommand = RawJSBCommand(command: "", data: RawJSData(), requestID: 0)
71
- do {
72
- jsbCommand = try decoder.decode(RawJSBCommand.self, from: jsonData.data(using: .utf8)!)
73
- } catch {
74
- logger.error("\(error)")
75
- }
76
- return jsbCommand
77
- }
78
-
79
- public func doCommand(target: SpatialWindowComponent, jsb: RawJSBCommand) {
80
- if let action = commandList[jsb.command] {
81
- if let info = getInfo(target, jsb) {
82
- action(target, info)
83
- }
84
- }
85
- }
86
-
87
- private func multiCommand(target: SpatialWindowComponent, info: CommandInfo) {
88
- for subCommand in info.cmd.data!.commandList! {
89
- doCommand(target: target, jsb: subCommand)
90
- }
91
- target.completeEvent(requestID: info.requestID)
92
- }
93
-
94
- private func ping(target: SpatialWindowComponent, info: CommandInfo) {
95
- target.completeEvent(requestID: info.requestID, data: "{ping: 'Complete'}")
96
- }
97
-
98
- private func inspect(target: SpatialWindowComponent, info: CommandInfo) {
99
- if let spatialObject = SpatialObject.getRefObject(info.resourceID) {
100
- let inspectInfo = spatialObject.inspect()
101
- let isValidJSON = JSONSerialization.isValidJSONObject(inspectInfo)
102
- if isValidJSON {
103
- do {
104
- let jsonData = try JSONSerialization.data(withJSONObject: inspectInfo, options: [])
105
- let jsonString = String(data: jsonData, encoding: .utf8)
106
- target.completeEvent(requestID: info.requestID, data: jsonString ?? "Conver failed")
107
- } catch {
108
- logger.error("Error: \(error.localizedDescription)")
109
- target.completeEvent(requestID: info.requestID, data: """
110
- error: \(error.localizedDescription)
111
- """)
112
- }
113
- } else {
114
- logger.warning("\(inspectInfo)")
115
- }
116
- } else {
117
- logger.warning("Missing spatialObject resource")
118
- return
119
- }
120
- }
121
-
122
- private func getStats(target: SpatialWindowComponent, info: CommandInfo) {
123
- let statsInfo = SpatialObject.stats()
124
- do {
125
- let jsonData = try JSONEncoder().encode(statsInfo)
126
- let jsonString = String(data: jsonData, encoding: .utf8)
127
- target.completeEvent(requestID: info.requestID, data: jsonString ?? "Conver failed")
128
- } catch {
129
- logger.error("Error: \(error.localizedDescription)")
130
- target.completeEvent(requestID: info.requestID, data: """
131
- error: \(error.localizedDescription)
132
- """)
133
- }
134
- }
135
-
136
- private func setComponent(target: SpatialWindowComponent, info: CommandInfo) {
137
- if let component = target.getChildSpatialObject(name: info.resourceID) as? SpatialComponent,
138
- let entity = target.getChildSpatialObject(name: info.entityID) as? SpatialEntity
139
- {
140
- entity.addComponent(component)
141
- } else {
142
- logger.warning("missing resource, setComponent not processed")
143
- }
144
- }
145
-
146
- private func removeComponent(target: SpatialWindowComponent, info: CommandInfo) {
147
- if let component = target.getChildSpatialObject(name: info.resourceID) as? SpatialComponent,
148
- let entity = target.getChildSpatialObject(name: info.entityID) as? SpatialEntity
149
- {
150
- entity.removeComponent(component)
151
- } else {
152
- logger.warning("missing resource, removeComponent not processed")
153
- }
154
- }
155
-
156
- private func createResource(target: SpatialWindowComponent, info: CommandInfo) {
157
- let data = info.cmd.data!
158
- if let type = data.type {
159
- var sr: SpatialObject?
160
- switch type {
161
- case "Entity":
162
- sr = SpatialEntity()
163
- case "InputComponent":
164
- sr = SpatialInputComponent()
165
- (sr as! SpatialInputComponent).wv = target
166
- case "MeshResource":
167
- if let shape = data.params?.shape {
168
- let meshResource: MeshResource = shape == "sphere" ? .generateSphere(radius: 0.5) : .generateBox(size: 1.0)
169
- sr = SpatialMeshResource(meshResource)
170
- }
171
- case "PhysicallyBasedMaterial":
172
- sr = SpatialPhysicallyBasedMaterial(PhysicallyBasedMaterial())
173
- case "SpatialWebView":
174
- sr = SpatialWindowComponent(parentWindowContainerID: target.readWindowContainerID(id: info.windowContainerID))
175
- let spatialWindowComponent = sr as! SpatialWindowComponent
176
- spatialWindowComponent.parentWebviewID = target.id
177
- case "SpatialView":
178
- sr = SpatialViewComponent()
179
- case "Model3DComponent":
180
- let spatialModel3DComponent = SpatialModel3DComponent()
181
- if let modelURL: String = data.params?.modelURL {
182
- spatialModel3DComponent.setURL(modelURL)
183
- }
184
- sr = spatialModel3DComponent
185
- spatialModel3DComponent.wv = target
186
- case "ModelComponent":
187
- if var modelURL: String = data.params?.modelURL {
188
- modelURL = target.parseURL(url: modelURL)
189
- // Create download task for the url
190
- let url = URL(string: modelURL)!
191
- let downloadSession = URLSession(configuration: URLSession.shared.configuration, delegate: nil, delegateQueue: nil)
192
- let downloadTask = downloadSession.downloadTask(with: url, completionHandler: { a, _, _ in
193
- var fileURL = url
194
- if !pwaManager.isLocal {
195
- // Copy temp file to documentes directory
196
- let fileStr = modelURL.replacingOccurrences(of: ":", with: "__").replacingOccurrences(of: "/", with: "_x_")
197
- do {
198
- fileURL = getDocumentsDirectory().appendingPathComponent(fileStr)
199
- try FileManager.default.copyItem(at: a!, to: fileURL)
200
- logger.debug("Downloaded and copied model")
201
- } catch {
202
- logger.warning("Model already exists")
203
- }
204
- }
205
-
206
- Task {
207
- do {
208
- let m = try await ModelEntity(contentsOf: fileURL)
209
-
210
- let modelComponent = await m.model!
211
- let spatialModelComponent = SpatialModelComponent(modelComponent)
212
-
213
- Task.detached { @MainActor in
214
- // Update state on main thread
215
- target.completeEvent(requestID: info.requestID, data: "{createdID: '" + spatialModelComponent.id + "'}")
216
- target.addChildSpatialObject(spatialModelComponent)
217
- logger.debug("Model load success!")
218
- }
219
- } catch {
220
- logger.warning("failed to load model: " + error.localizedDescription)
221
- }
222
- }
223
- })
224
- downloadTask.resume()
225
- return
226
- } else {
227
- let modelComponent = ModelComponent(mesh: .generateBox(size: 0.0), materials: [])
228
- sr = SpatialModelComponent(modelComponent)
229
- }
230
- default: logger.warning("failed to create sr of type \(type)")
231
- }
232
- if let srObject = sr {
233
- target.completeEvent(requestID: info.requestID, data: "{createdID: '" + srObject.id + "'}")
234
- target.addChildSpatialObject(srObject)
235
- } else {
236
- logger.warning("failed to create sr of type: \(type)")
237
- }
238
- }
239
- }
240
-
241
- private func destroyResource(target: SpatialWindowComponent, info: CommandInfo) {
242
- let resourceID = info.resourceID
243
- target.destroyChild(name: resourceID)
244
- }
245
-
246
- private func updateResource(target: SpatialWindowComponent, info: CommandInfo) {
247
- let data = info.cmd.data!
248
- var delayComplete = false
249
- if SpatialObject.get(info.resourceID) == nil {
250
- logger.warning("Missing resource:" + info.resourceID)
251
- return
252
- }
253
- let sr = SpatialObject.get(info.resourceID)!
254
- if let entity = sr as? SpatialEntity {
255
- if let setParentID: String = data.update?.setParent {
256
- if setParentID.isEmpty {
257
- entity.setParent(parentEnt: nil)
258
- } else {
259
- if let parentEntity = SpatialObject.get(setParentID) as? SpatialEntity {
260
- entity.setParent(parentEnt: parentEntity)
261
- } else {
262
- logger.warning("Invalid setParentID: \(setParentID)")
263
- }
264
- }
265
- }
266
-
267
- if let _ = data.update?.getBoundingBox {
268
- let b = entity.modelEntity.visualBounds(relativeTo: nil)
269
- let exts = "x:" + String(b.extents.x) + "," + "y:" + String(b.extents.y) + "," + "z:" + String(b.extents.z)
270
- let center = "x:" + String(b.center.x) + "," + "y:" + String(b.center.y) + "," + "z:" + String(b.center.z)
271
- target.completeEvent(requestID: info.requestID, data: "{center: {" + center + "}, extents: {" + exts + "}}")
272
- return
273
- }
274
-
275
- if let name = data.update?.name {
276
- entity.name = name
277
- }
278
-
279
- if let space: String = data.update?.setCoordinateSpace {
280
- entity.coordinateSpace = .APP
281
- if space == "Root" {
282
- entity.coordinateSpace = .ROOT
283
- }
284
-
285
- if space == "Dom" {
286
- entity.coordinateSpace = .DOM
287
- }
288
- }
289
-
290
- if let zIndex: Double = data.update?.zIndex {
291
- entity.zIndex = zIndex
292
- }
293
-
294
- if let visible: Bool = data.update?.visible {
295
- entity.visible = visible
296
- }
297
-
298
- if var newParentID: String = data.update?.setParentWindowContainerID {
299
- newParentID = target.readWindowContainerID(id: newParentID)
300
- let wg = SpatialWindowContainer.getSpatialWindowContainer(newParentID)
301
- entity.setParentWindowContainer(wg: wg)
302
- }
303
-
304
- if let position: JSVector3F = data.update?.position,
305
- let scale: JSVector3F = data.update?.scale,
306
- let orientation: JSVector4 = data.update?.orientation
307
- {
308
- entity.modelEntity.position = SIMD3<Float>(position.x, position.y, position.z)
309
- entity.modelEntity.scale = SIMD3<Float>(scale.x, scale.y, scale.z)
310
- entity.modelEntity.orientation.vector = SIMD4<Float>(orientation.x, orientation.y, orientation.z, orientation.w)
311
- entity.forceUpdate = !entity.forceUpdate
312
- }
313
- } else if sr is SpatialMeshResource {
314
- } else if let spatialPhysicallyBasedMaterial = sr as? SpatialPhysicallyBasedMaterial {
315
- if let baseColor: JSColor = data.update?.baseColor {
316
- spatialPhysicallyBasedMaterial.physicallyBasedMaterial.baseColor = PhysicallyBasedMaterial.BaseColor(tint: UIColor(red: baseColor.r, green: baseColor.g, blue: baseColor.b, alpha: baseColor.a))
317
- spatialPhysicallyBasedMaterial.physicallyBasedMaterial.opacityThreshold = 0.0
318
- }
319
-
320
- if let roughness: Double = data.update?.roughness?.value {
321
- spatialPhysicallyBasedMaterial.physicallyBasedMaterial.roughness = PhysicallyBasedMaterial.Roughness(floatLiteral: Float(roughness))
322
- }
323
-
324
- if let metallic: Double = data.update?.metallic?.value {
325
- spatialPhysicallyBasedMaterial.physicallyBasedMaterial.metallic = PhysicallyBasedMaterial.Metallic(floatLiteral: Float(metallic))
326
- }
327
-
328
- } else if let spatialViewComponent = sr as? SpatialViewComponent {
329
- if let resolution = data.update?.resolution {
330
- spatialViewComponent.resolutionX = resolution.x
331
- spatialViewComponent.resolutionY = resolution.y
332
- }
333
-
334
- if let isPortal = data.update?.isPortal {
335
- spatialViewComponent.isPortal = isPortal
336
- }
337
-
338
- } else if let spatialModelComponent = sr as? SpatialModelComponent {
339
- if let meshResourceId: String = data.update?.meshResource {
340
- if let spatialMeshResource = target.getChildSpatialObject(name: meshResourceId) as? SpatialMeshResource {
341
- spatialModelComponent.modelComponent.mesh = spatialMeshResource.meshResource
342
- } else {
343
- logger.warning("invalid meshResource")
344
- }
345
- }
346
-
347
- if let materials: [String] = data.update?.materials {
348
- spatialModelComponent.modelComponent.materials = []
349
- for matID in materials {
350
- if let spatialMaterialComponent = target.getChildSpatialObject(name: matID) as? SpatialPhysicallyBasedMaterial {
351
- spatialModelComponent.modelComponent.materials.append(spatialMaterialComponent.physicallyBasedMaterial)
352
- }
353
- }
354
- }
355
- spatialModelComponent.onUpdate()
356
- } else if let spatialModel3DComponent = sr as? SpatialModel3DComponent {
357
- if let resolution: JSVector2 = data.update?.resolution {
358
- spatialModel3DComponent.resolutionX = resolution.x
359
- spatialModel3DComponent.resolutionY = resolution.y
360
- }
361
-
362
- if let rotationAnchor = data.update?.rotationAnchor {
363
- spatialModel3DComponent.rotationAnchor = UnitPoint3D(
364
- x: rotationAnchor.x,
365
- y: rotationAnchor.y,
366
- z: rotationAnchor.z
367
- )
368
- }
369
-
370
- if let opacity = data.update?.opacity {
371
- spatialModel3DComponent.opacity = opacity
372
- }
373
-
374
- if let contentMode: String = data.update?.contentMode {
375
- if contentMode == "fill" {
376
- spatialModel3DComponent.contentMode = .fill
377
-
378
- } else if contentMode == "fit" {
379
- spatialModel3DComponent.contentMode = .fit
380
- }
381
- }
382
-
383
- if let resizable: Bool = data.update?.resizable {
384
- spatialModel3DComponent.resizable = resizable
385
- }
386
-
387
- if let aspectRatio: Double = data.update?.aspectRatio {
388
- if aspectRatio > 0 {
389
- spatialModel3DComponent.aspectRatio = aspectRatio
390
-
391
- } else if aspectRatio == 0 {
392
- spatialModel3DComponent.aspectRatio = nil
393
- }
394
- }
395
-
396
- if let enableTapEvent: Bool = data.update?.enableTapEvent {
397
- spatialModel3DComponent.enableTapEvent = enableTapEvent
398
- }
399
-
400
- if let enableDoubleTapEvent: Bool = data.update?.enableDoubleTapEvent {
401
- spatialModel3DComponent.enableDoubleTapEvent = enableDoubleTapEvent
402
- }
403
-
404
- if let enableDragEvent: Bool = data.update?.enableDragEvent {
405
- spatialModel3DComponent.enableDragEvent = enableDragEvent
406
- }
407
-
408
- if let enableLongPressEvent: Bool = data.update?.enableLongPressEvent {
409
- spatialModel3DComponent.enableLongPressEvent = enableLongPressEvent
410
- }
411
-
412
- } else if let spatialWindowComponent = sr as? SpatialWindowComponent {
413
- if let _: String = data.update?.getEntityID {
414
- if let entity: SpatialEntity = spatialWindowComponent.entity {
415
- target.completeEvent(requestID: info.requestID, data: "{parentID:'" + entity.id + "'}")
416
-
417
- } else {
418
- target.completeEvent(requestID: info.requestID, data: "{parentID:''}")
419
- }
420
- return
421
- }
422
-
423
- if let _: String = data.update?.getParentID {
424
- target.completeEvent(requestID: info.requestID, data: "{parentID:'" + spatialWindowComponent.parentWebviewID + "'}")
425
- return
426
- }
427
-
428
- if let scrollEnabled: Bool = data.update?.scrollEnabled {
429
- if !scrollEnabled {
430
- spatialWindowComponent.getView()!.webViewHolder.appleWebView!.scrollView.contentOffset.y = 0
431
- spatialWindowComponent.getView()!.webViewHolder.appleWebView!.scrollView.isScrollEnabled = false
432
- } else {
433
- spatialWindowComponent.getView()!.webViewHolder.appleWebView!.scrollView.isScrollEnabled = true
434
- }
435
- }
436
-
437
- if let rect: JSRect = data.update?.setScrollEdgeInsets {
438
- spatialWindowComponent.getView()!.webViewHolder.appleWebView!.scrollView.contentInset = UIEdgeInsets(top: rect.top, left: rect.left, bottom: rect.bottom, right: rect.right)
439
- }
440
-
441
- if let scrollWithParent: Bool = data.update?.scrollWithParent {
442
- spatialWindowComponent.scrollWithParent = scrollWithParent
443
- }
444
-
445
- if let windowID: String = data.update?.windowID {
446
- if let spawnedWebView = target.spawnedNativeWebviews.removeValue(forKey: windowID) {
447
- spatialWindowComponent.getView()!.destroy()
448
- spatialWindowComponent.setView(wv: spawnedWebView)
449
- spatialWindowComponent.getView()!.webViewHolder.webViewCoordinator!.webViewRef = spatialWindowComponent
450
- }
451
- }
452
-
453
- if let url: String = data.update?.url {
454
- // Compute target url depending if the url is relative or not
455
- let targetUrl = target.parseURL(url: url)
456
-
457
- delayComplete = true
458
- if spatialWindowComponent.loadRequestID == -1 {
459
- spatialWindowComponent.loadRequestID = info.requestID
460
- spatialWindowComponent.loadRequestWV = target
461
- spatialWindowComponent.setURL(url: URL(string: targetUrl)!)
462
- spatialWindowComponent.getView()!.initialLoad()
463
- } else {
464
- target.failEvent(requestID: info.requestID)
465
- }
466
- }
467
-
468
- if let resolution: JSVector2 = data.update?.resolution {
469
- spatialWindowComponent.resolutionX = resolution.x
470
- spatialWindowComponent.resolutionY = resolution.y
471
- }
472
-
473
- if let rotationAnchor = data.update?.rotationAnchor {
474
- spatialWindowComponent.rotationAnchor = UnitPoint3D(
475
- x: rotationAnchor.x,
476
- y: rotationAnchor.y,
477
- z: rotationAnchor.z
478
- )
479
- }
480
-
481
- if let opacity = data.update?.opacity {
482
- spatialWindowComponent.opacity = opacity
483
- }
484
-
485
- if let backgroundMaterial: BackgroundMaterial = data.update?.style?.backgroundMaterial {
486
- if target.isLoading {
487
- target.loadingStyles.backgroundMaterial = backgroundMaterial
488
- }
489
- spatialWindowComponent.backgroundMaterial = backgroundMaterial
490
- }
491
- if let cornerRadius: CornerRadius = data.update?.style?.cornerRadius {
492
- if target.isLoading {
493
- target.loadingStyles.cornerRadius = cornerRadius
494
- }
495
- spatialWindowComponent.cornerRadius = cornerRadius
496
- }
497
- spatialWindowComponent.gotStyle = true
498
- }
499
- if !delayComplete {
500
- target.completeEvent(requestID: info.requestID)
501
- }
502
- }
503
-
504
- private func createWindowContainer(target: SpatialWindowComponent, info: CommandInfo) {
505
- if let windowStyle: String = info.cmd.data!.windowStyle {
506
- let uuid = UUID().uuidString
507
- let wgd = WindowContainerData(windowStyle: windowStyle, windowContainerID: uuid)
508
-
509
- // Force window container creation to happen now so it can be accessed after complete event returns
510
- _ = SpatialWindowContainer.getOrCreateSpatialWindowContainer(uuid, wgd)
511
-
512
- if let wg = SpatialWindowContainer.getOrCreateSpatialWindowContainer(target.parentWindowContainerID, wgd) {
513
- wg.openWindowData.send(wgd)
514
-
515
- // If the parent window component isn't set, the new container can continue to exist even after other window is closed
516
- if info.resourceID != "notFound" {
517
- var rid = info.resourceID
518
- let so = SpatialObject.get(rid)
519
- if let parentWindowComponent = so as? SpatialWindowComponent {
520
- parentWindowComponent.setWindowContainer(uuid: uuid, wgd: wgd)
521
- }
522
- }
523
-
524
- if info.windowContainerID != "notFound" {
525
- if var parentContainer = SpatialWindowContainer.getSpatialWindowContainer(info.windowContainerID) {
526
- parentContainer.childContainers[uuid] = wg
527
- }
528
- }
529
-
530
- target.completeEvent(requestID: info.requestID, data: "{createdID: '" + uuid + "'}")
531
- }
532
- }
533
- }
534
-
535
- private func createScene(target: SpatialWindowComponent, info: CommandInfo) {
536
- let data = info.cmd.data!
537
- if let _: String = data.windowStyle {
538
- // windowID exist in SWC
539
-
540
- // TODO: check url scope
541
- // in scope: the url is configured in manifest
542
- // if not in scope, open in safari
543
-
544
- if data.sceneData?.method == "createRoot" {
545
- if let windowID = data.sceneData?.windowID {
546
- // if windowID in spawned uuid, createRoot
547
-
548
- if target.spawnedNativeWebviews[windowID] != nil {
549
- // setup windowContainer defaultValues
550
- if let config = data.sceneData?.sceneConfig {
551
- SceneManager.Instance
552
- .createRoot(target: target, windowID: windowID, config: config)
553
- } else {
554
- SceneManager.Instance
555
- .createRoot(target: target, windowID: windowID)
556
- }
557
-
558
- } else {
559
- if let windowContainerID = data.sceneData?.windowContainerID {
560
- SceneManager.Instance.focusRoot(target: target, windowContainerID: windowContainerID)
561
- } else {
562
- logger.error("error: no windowContainerID")
563
- }
564
- }
565
-
566
- target.completeEvent(requestID: info.requestID, data: "{}")
567
- }
568
- } else if data.sceneData?.method == "showRoot" {
569
- if let config = data.sceneData?.sceneConfig {
570
- let parentWindowContainerID = info.windowContainerID
571
- SceneManager.Instance.showRoot(target: target, config: config, parentWindowContainerID: parentWindowContainerID)
572
- }
573
- target.completeEvent(requestID: info.requestID, data: "{}")
574
-
575
- } else {
576
- target.failEvent(requestID: info.requestID, data: "method not supported")
577
- }
578
- }
579
- }
580
-
581
- private func updateWindowContainer(target: SpatialWindowComponent, info: CommandInfo) {
582
- let data = info.cmd.data!
583
- if let _ = data.update?.getRootEntityID,
584
- let wg = SpatialWindowContainer.getSpatialWindowContainer(target.readWindowContainerID(id: info.windowContainerID))
585
- {
586
- let rootEntity = wg.getEntities().filter {
587
- $0.value.coordinateSpace == .ROOT
588
- }.first?.value
589
- if rootEntity != nil {
590
- target.completeEvent(requestID: info.requestID, data: "{rootEntId:'" + rootEntity!
591
- .id + "'}")
592
- } else {
593
- target.completeEvent(requestID: info.requestID, data: "{rootEntId:''}")
594
- }
595
- return
596
- }
597
-
598
- if let resolution = data.update?.nextOpenSettings?.resolution {
599
- sceneStateChangedCB = { _ in
600
- // Complete event after scene state change is completed
601
- target.completeEvent(requestID: info.requestID)
602
- sceneStateChangedCB = { _ in }
603
- }
604
-
605
- // Update scene state
606
- var cfg = WindowContainerPlainDefaultValues()
607
- cfg.defaultSize = CGSize(width: resolution.width, height: resolution.height)
608
- WindowContainerMgr.Instance.updateWindowContainerPlainDefaultValues(cfg)
609
- return
610
- }
611
- target.completeEvent(requestID: info.requestID)
612
- }
613
-
614
- private func openImmersiveSpace(target: SpatialWindowComponent, info: CommandInfo) {
615
- let wg = SpatialWindowContainer.getSpatialWindowContainer(target.parentWindowContainerID)
616
- wg?.toggleImmersiveSpace.send(true)
617
- }
618
-
619
- private func dismissImmersiveSpace(target: SpatialWindowComponent, info: CommandInfo) {
620
- let wg = SpatialWindowContainer.getSpatialWindowContainer(target.parentWindowContainerID)
621
- wg?.toggleImmersiveSpace.send(false)
622
- }
623
-
624
- private func log(target: SpatialWindowComponent, info: CommandInfo) {
625
- if let logStringArr: [String] = info.cmd.data!.logString {
626
- let logString = logStringArr.joined()
627
- logger.debug(logString)
628
- }
629
- }
630
-
631
- private func setLoading(target: SpatialWindowComponent, info: CommandInfo) {
632
- let data = info.cmd.data!
633
- switch data.loading?.method {
634
- case "show":
635
- SceneManager.Instance.setLoading(.show, windowContainerID: info.windowContainerID)
636
- case "hide":
637
- SceneManager.Instance.setLoading(.hide, windowContainerID: info.windowContainerID)
638
- case _:
639
- break
640
- }
641
- target.completeEvent(requestID: info.requestID)
642
- }
643
- }
644
-
645
- struct RawJSBCommand: Codable {
646
- var command: String
647
- var data: RawJSData?
648
- var requestID: Int
649
- }
650
-
651
- struct JSBCommand {
652
- var command: String
653
- var data: JSData?
654
- var requestID: Int
655
- }
656
-
657
- protocol JSData {
658
- var commandList: [RawJSBCommand]? { get } // multiCommand
659
- var type: String? { get } // createResource
660
- var params: JSParams? { get } // createResource
661
- var update: JSResourceData? { get }
662
- var windowStyle: String? { get }
663
- var windowContainerOptions: WindowContainerOptions? { get }
664
- var sceneData: SceneJSBData? { get }
665
- var logString: [String]? { get }
666
- var logLevel: String? { get }
667
- var loading: LoadingJSBData? { get }
668
- }
669
-
670
- struct RawJSData: Codable, JSData {
671
- var commandList: [RawJSBCommand]? // multiCommand
672
- var resourceID: String?
673
- var windowContainerID: String?
674
- var entityID: String? // setComponent
675
- var type: String? // createResource
676
- var params: JSParams? // createResource
677
- var update: JSResourceData?
678
- var windowStyle: String?
679
- var windowContainerOptions: WindowContainerOptions?
680
- var sceneData: SceneJSBData?
681
- var logString: [String]?
682
- var logLevel: String?
683
- var loading: LoadingJSBData?
684
- }
685
-
686
- struct JSParams: Codable {
687
- var shape: String?
688
- var modelURL: String?
689
- }
690
-
691
- struct JSResourceData: Codable {
692
- var setParent: String?
693
- var setCoordinateSpace: String?
694
- var setParentWindowContainerID: String?
695
- var position: JSVector3F?
696
- var orientation: JSVector4?
697
- var scale: JSVector3F?
698
- var baseColor: JSColor?
699
- var roughness: JSValue?
700
- var metallic: JSValue?
701
- var url: String?
702
- var aspectRatio: Double?
703
- var opacity: Double?
704
- var contentMode: String?
705
- var resizable: Bool?
706
- var resolution: JSVector2?
707
- var isPortal: Bool?
708
- var rotationAnchor: JSVector3?
709
- var meshResource: String?
710
- var materials: [String]?
711
- var getEntityID: String?
712
- var getParentID: String?
713
- var getRootEntityID: String?
714
- var scrollEnabled: Bool?
715
- var scrollWithParent: Bool?
716
- var setScrollEdgeInsets: JSRect?
717
- var windowID: String?
718
- var style: JSEntityStyle?
719
- var nextOpenSettings: JSNextOpen?
720
- var getBoundingBox: Bool?
721
- var zIndex: Double?
722
- var visible: Bool?
723
- var name: String?
724
-
725
- var enableTapEvent: Bool?
726
- var enableDoubleTapEvent: Bool?
727
- var enableDragEvent: Bool?
728
- var enableLongPressEvent: Bool?
729
- }
730
-
731
- struct JSColor: Codable {
732
- var r: Double
733
- var g: Double
734
- var b: Double
735
- var a: Double
736
- }
737
-
738
- struct JSVector2: Codable {
739
- var x: Double
740
- var y: Double
741
- }
742
-
743
- struct JSResolution: Codable {
744
- var width: Double
745
- var height: Double
746
- }
747
-
748
- struct JSVector3: Codable {
749
- var x: Double
750
- var y: Double
751
- var z: Double
752
- }
753
-
754
- struct JSVector3F: Codable {
755
- var x: Float
756
- var y: Float
757
- var z: Float
758
- }
759
-
760
- struct JSVector4: Codable {
761
- var x: Float
762
- var y: Float
763
- var z: Float
764
- var w: Float
765
- }
766
-
767
- struct JSValue: Codable {
768
- var value: Double
769
- }
770
-
771
- struct JSRect: Codable {
772
- var top: Double
773
- var bottom: Double
774
- var left: Double
775
- var right: Double
776
- }
777
-
778
- struct JSEntityStyle: Codable {
779
- var cornerRadius: CornerRadius?
780
- var backgroundMaterial: BackgroundMaterial?
781
- var dimensions: JSVector2?
782
- }
783
-
784
- struct JSNextOpen: Codable {
785
- var resolution: JSResolution?
786
- }
787
-
788
- struct SceneJSBData: Codable {
789
- var method: String?
790
- var sceneName: String?
791
- var sceneConfig: WindowContainerOptions?
792
- var url: String?
793
- var windowID: String?
794
- var windowContainerID: String?
795
- }
796
-
797
- struct LoadingJSBData: Codable {
798
- var method: String?
799
- var style: String?
800
- }