@webspatial/builder 0.0.1 → 0.0.3

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 (113) hide show
  1. package/dist/index.d.ts +1 -0
  2. package/dist/index.js +1 -0
  3. package/dist/lib/Cli.d.ts +3 -0
  4. package/dist/lib/Cli.js +1 -0
  5. package/dist/lib/cmds/build.d.ts +4 -0
  6. package/dist/lib/cmds/build.js +1 -0
  7. package/dist/lib/cmds/check.d.ts +3 -0
  8. package/dist/lib/cmds/check.js +1 -0
  9. package/dist/lib/cmds/help.d.ts +3 -0
  10. package/dist/lib/cmds/help.js +1 -0
  11. package/dist/lib/cmds/version.d.ts +2 -0
  12. package/dist/lib/cmds/version.js +1 -0
  13. package/dist/lib/pwa/config.d.ts +7 -0
  14. package/dist/lib/pwa/config.js +1 -0
  15. package/dist/lib/pwa/index.d.ts +16 -0
  16. package/dist/lib/pwa/index.js +1 -0
  17. package/dist/lib/pwa/validate.d.ts +4 -0
  18. package/dist/lib/pwa/validate.js +1 -0
  19. package/dist/lib/resource/file.d.ts +2 -0
  20. package/dist/lib/resource/file.js +1 -0
  21. package/dist/lib/resource/imageHelper.d.ts +7 -0
  22. package/dist/lib/resource/imageHelper.js +1 -0
  23. package/dist/lib/resource/index.d.ts +13 -0
  24. package/dist/lib/resource/index.js +1 -0
  25. package/dist/lib/resource/load.d.ts +6 -0
  26. package/dist/lib/resource/load.js +1 -0
  27. package/dist/lib/utils/CustomError.d.ts +13 -0
  28. package/dist/lib/utils/CustomError.js +1 -0
  29. package/dist/lib/utils/FetchUtils-1.d.ts +12 -0
  30. package/dist/lib/utils/FetchUtils-1.js +1 -0
  31. package/dist/lib/utils/Log.d.ts +88 -0
  32. package/dist/lib/utils/Log.js +1 -0
  33. package/dist/lib/utils/fetch.d.ts +5 -0
  34. package/dist/lib/utils/fetch.js +1 -0
  35. package/dist/lib/utils/messages.d.ts +50 -0
  36. package/dist/lib/utils/messages.js +1 -0
  37. package/dist/lib/utils/utils.d.ts +1 -0
  38. package/dist/lib/utils/utils.js +1 -0
  39. package/dist/lib/xcode/index.d.ts +5 -0
  40. package/dist/lib/xcode/index.js +1 -0
  41. package/dist/lib/xcode/manifestSwiftTemplate.d.ts +1 -0
  42. package/dist/lib/xcode/manifestSwiftTemplate.js +1 -0
  43. package/dist/lib/xcode/xcodebuild.d.ts +16 -0
  44. package/dist/lib/xcode/xcodebuild.js +1 -0
  45. package/dist/lib/xcode/xcodeproject.d.ts +11 -0
  46. package/dist/lib/xcode/xcodeproject.js +1 -0
  47. package/dist/lib/xcode/xcrun.d.ts +7 -0
  48. package/dist/lib/xcode/xcrun.js +1 -0
  49. package/package.json +2 -1
  50. package/template/visionOSApp/Packages/RealityKitContent/.build/workspace-state.json +7 -0
  51. package/template/visionOSApp/Packages/RealityKitContent/.swiftpm/xcode/xcuserdata/bytedance.xcuserdatad/xcschemes/xcschememanagement.plist +14 -0
  52. package/template/visionOSApp/Packages/RealityKitContent/Package.realitycomposerpro/ProjectData/main.json +11 -0
  53. package/template/visionOSApp/Packages/RealityKitContent/Package.realitycomposerpro/WorkspaceData/SceneMetadataList.json +112 -0
  54. package/template/visionOSApp/Packages/RealityKitContent/Package.realitycomposerpro/WorkspaceData/Settings.rcprojectdata +17 -0
  55. package/template/visionOSApp/Packages/RealityKitContent/Package.swift +27 -0
  56. package/template/visionOSApp/Packages/RealityKitContent/README.md +3 -0
  57. package/template/visionOSApp/Packages/RealityKitContent/Sources/RealityKitContent/RealityKitContent.rkassets/Immersive.usda +50 -0
  58. package/template/visionOSApp/Packages/RealityKitContent/Sources/RealityKitContent/RealityKitContent.rkassets/Materials/GridMaterial.usda +216 -0
  59. package/template/visionOSApp/Packages/RealityKitContent/Sources/RealityKitContent/RealityKitContent.rkassets/Scene.usda +59 -0
  60. package/template/visionOSApp/Packages/RealityKitContent/Sources/RealityKitContent/RealityKitContent.swift +4 -0
  61. package/template/visionOSApp/web-spatial/Assets.xcassets/AppIcon.solidimagestack/Back.solidimagestacklayer/Content.imageset/Contents.json +12 -0
  62. package/template/visionOSApp/web-spatial/Assets.xcassets/AppIcon.solidimagestack/Back.solidimagestacklayer/Contents.json +6 -0
  63. package/template/visionOSApp/web-spatial/Assets.xcassets/AppIcon.solidimagestack/Contents.json +17 -0
  64. package/template/visionOSApp/web-spatial/Assets.xcassets/AppIcon.solidimagestack/Front.solidimagestacklayer/Content.imageset/Contents.json +12 -0
  65. package/template/visionOSApp/web-spatial/Assets.xcassets/AppIcon.solidimagestack/Front.solidimagestacklayer/Contents.json +6 -0
  66. package/template/visionOSApp/web-spatial/Assets.xcassets/AppIcon.solidimagestack/Middle.solidimagestacklayer/Content.imageset/Contents.json +12 -0
  67. package/template/visionOSApp/web-spatial/Assets.xcassets/AppIcon.solidimagestack/Middle.solidimagestacklayer/Contents.json +6 -0
  68. package/template/visionOSApp/web-spatial/Assets.xcassets/Contents.json +6 -0
  69. package/template/visionOSApp/web-spatial/Info.plist +33 -0
  70. package/template/visionOSApp/web-spatial/Preview Content/Preview Assets.xcassets/Contents.json +6 -0
  71. package/template/visionOSApp/web-spatial/libs/EventEmitter.swift +32 -0
  72. package/template/visionOSApp/web-spatial/libs/SpatialComponent.swift +31 -0
  73. package/template/visionOSApp/web-spatial/libs/SpatialEntity.swift +179 -0
  74. package/template/visionOSApp/web-spatial/libs/SpatialInputComponent.swift +26 -0
  75. package/template/visionOSApp/web-spatial/libs/SpatialMeshResource.swift +19 -0
  76. package/template/visionOSApp/web-spatial/libs/SpatialModel3DComponent.swift +51 -0
  77. package/template/visionOSApp/web-spatial/libs/SpatialModelComponent.swift +32 -0
  78. package/template/visionOSApp/web-spatial/libs/SpatialObject.swift +144 -0
  79. package/template/visionOSApp/web-spatial/libs/SpatialPhysicallyBasedMaterial.swift +19 -0
  80. package/template/visionOSApp/web-spatial/libs/SpatialViewComponent.swift +15 -0
  81. package/template/visionOSApp/web-spatial/libs/SpatialWindowComponent.swift +420 -0
  82. package/template/visionOSApp/web-spatial/libs/SpatialWindowContainer.swift +132 -0
  83. package/template/visionOSApp/web-spatial/libs/Utils/CommandManager.swift +799 -0
  84. package/template/visionOSApp/web-spatial/libs/Utils/Logger.swift +36 -0
  85. package/template/visionOSApp/web-spatial/libs/Utils/SceneManager.swift +108 -0
  86. package/template/visionOSApp/web-spatial/libs/Utils/WindowContainerMgr.swift +113 -0
  87. package/template/visionOSApp/web-spatial/libs/json/JsonParser.swift +52 -0
  88. package/template/visionOSApp/web-spatial/libs/uiKitDelegate/Window.swift +34 -0
  89. package/template/visionOSApp/web-spatial/libs/webView/UpdateSystem.swift +33 -0
  90. package/template/visionOSApp/web-spatial/libs/webView/backend/NativeWebView.swift +319 -0
  91. package/template/visionOSApp/web-spatial/libs/webView/manifest.swift +91 -0
  92. package/template/visionOSApp/web-spatial/static-web/index.html +9 -0
  93. package/template/visionOSApp/web-spatial/views/HideViewModifier.swift +17 -0
  94. package/template/visionOSApp/web-spatial/views/ImmersiveView.swift +24 -0
  95. package/template/visionOSApp/web-spatial/views/LoadingView.swift +25 -0
  96. package/template/visionOSApp/web-spatial/views/MaterialWithBorderCornerModifier.swift +82 -0
  97. package/template/visionOSApp/web-spatial/views/OpenDismissHandlerUI.swift +52 -0
  98. package/template/visionOSApp/web-spatial/views/PlainWindowContainerView.swift +84 -0
  99. package/template/visionOSApp/web-spatial/views/SpatialModel3DView.swift +193 -0
  100. package/template/visionOSApp/web-spatial/views/SpatialViewUI.swift +168 -0
  101. package/template/visionOSApp/web-spatial/views/SpatialWebViewUI.swift +187 -0
  102. package/template/visionOSApp/web-spatial/views/VolumetricWindowContainerView.swift +34 -0
  103. package/template/visionOSApp/web-spatial/views/ui/NavView.swift +88 -0
  104. package/template/visionOSApp/web-spatial/web_spatialApp.swift +134 -0
  105. package/template/visionOSApp/web-spatial.xcodeproj/project.pbxproj +686 -0
  106. package/template/visionOSApp/web-spatial.xcodeproj/project.xcworkspace/contents.xcworkspacedata +7 -0
  107. package/template/visionOSApp/web-spatial.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist +8 -0
  108. package/template/visionOSApp/web-spatial.xcodeproj/project.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings +5 -0
  109. package/template/visionOSApp/web-spatial.xcodeproj/project.xcworkspace/xcuserdata/bytedance.xcuserdatad/UserInterfaceState.xcuserstate +0 -0
  110. package/template/visionOSApp/web-spatial.xcodeproj/project.xcworkspace/xcuserdata/bytedance.xcuserdatad/WorkspaceSettings.xcsettings +14 -0
  111. package/template/visionOSApp/web-spatial.xcodeproj/xcshareddata/xcschemes/web-spatial.xcscheme +115 -0
  112. package/template/visionOSApp/web-spatial.xcodeproj/xcuserdata/bytedance.xcuserdatad/xcschemes/xcschememanagement.plist +27 -0
  113. package/template/visionOSApp/web-spatialTests/web_spatialTests.swift +34 -0
@@ -0,0 +1,168 @@
1
+ //
2
+ // SpatialViewUI.swift
3
+ // web-spatial
4
+ //
5
+ // Created by ByteDance on 11/6/24.
6
+ //
7
+ import RealityKit
8
+ import SwiftUI
9
+
10
+ extension View {
11
+ @ViewBuilder
12
+ func `if`<Content: View>(_ condition: Bool, transform: (Self) -> Content) -> some View {
13
+ if condition {
14
+ transform(self)
15
+ } else {
16
+ self
17
+ }
18
+ }
19
+ }
20
+
21
+ struct SpatialViewUI: View {
22
+ @Environment(SpatialEntity.self) var ent: SpatialEntity
23
+ @State var isRoot = false
24
+
25
+ // Entity which will contain all the content of this realityView and scale to fit frame
26
+ @State var world = Entity()
27
+ @State var portal = Entity()
28
+ @State var light = PointLight()
29
+ @State var portalModel = ModelComponent(
30
+ mesh: .generatePlane(width: 1.0, height: 1.0, cornerRadius: 0.0),
31
+ materials: [PortalMaterial()]
32
+ )
33
+ @State var worldComponent = WorldComponent()
34
+
35
+ private func toJson(val: SIMD3<Float>) -> String {
36
+ return "{x: " + String(val.x) + ",y: " + String(val.y) + ",z: " + String(val.z) + "}"
37
+ }
38
+
39
+ var dragGesture: some Gesture {
40
+ DragGesture(minimumDistance: 0).handActivationBehavior(.automatic)
41
+ .targetedToAnyEntity()
42
+ .onChanged { value in
43
+ let startPos = value.convert(value.startLocation3D, from: .local, to: .scene)
44
+ let translate = value.convert(value.location3D, from: .local, to: .scene)
45
+ let spatialEntity = value.entity.components[SpatialBridgeComponent.self]!.spatialEntity
46
+ let ic = spatialEntity.getComponent(SpatialInputComponent.self)!
47
+
48
+ if !ic.isDragging {
49
+ ic.isDragging = true
50
+ ic.trackedPosition = startPos
51
+ let delta = translate - ic.trackedPosition
52
+ ic.trackedPosition = translate
53
+
54
+ ic.wv!.fireComponentEvent(componentId: ic.id, data: "{eventType: 'dragstart', translate: " + toJson(val: delta) + "}")
55
+ } else {
56
+ let delta = translate - ic.trackedPosition
57
+ ic.trackedPosition = translate
58
+ ic.wv!.fireComponentEvent(componentId: ic.id, data: "{eventType: 'drag', translate: " + toJson(val: delta) + "}")
59
+ }
60
+ }
61
+ .onEnded { value in
62
+ let spatialEntity = value.entity.components[SpatialBridgeComponent.self]!.spatialEntity
63
+ let ic = spatialEntity.getComponent(SpatialInputComponent.self)!
64
+ ic.wv!.fireComponentEvent(componentId: ic.id, data: "{eventType: 'dragend'}")
65
+ ic.isDragging = false
66
+ }
67
+ }
68
+
69
+ var body: some View {
70
+ if let viewComponent = ent.getComponent(SpatialViewComponent.self) {
71
+ GeometryReader3D { proxy in
72
+ // Get dimensions of the frame
73
+ let proxySize3d = proxy.frame(in: .local)
74
+
75
+ RealityView { _, _ in
76
+ } update: { content, attachments in
77
+ // Scale content so it will be a 1x1x1 space and not exceed the frame
78
+ let viewSpaceDimensions = content.convert(proxySize3d, from: .local, to: content)
79
+ let newScale = min(viewSpaceDimensions.extents.x, viewSpaceDimensions.extents.y)
80
+
81
+ world.transform.scale.x = newScale
82
+ world.transform.scale.y = newScale
83
+ world.transform.scale.z = newScale
84
+ portal.transform.scale.x = newScale
85
+ portal.transform.scale.y = newScale
86
+ portal.transform.scale.z = newScale
87
+
88
+ if !isRoot {
89
+ // Pull out content so volume sits in front of the page
90
+ world.transform.translation.z = world.transform.scale.z / 2
91
+ }
92
+
93
+ for (_, entity) in ent.getEntities() {
94
+ world.addChild(entity.modelEntity)
95
+ }
96
+
97
+ // Add attachments for window entities
98
+ let entities = ent.getEntities().filter { _, entity in
99
+ entity.coordinateSpace == .APP && entity.hasComponent(SpatialWindowComponent.self)
100
+ }
101
+ for key in Array(entities.keys) {
102
+ let e = entities[key]!
103
+ let windowComponent = e.getComponent(SpatialWindowComponent.self)
104
+ if windowComponent != nil && e.coordinateSpace == .APP {
105
+ if let windowAttachment = attachments.entity(for: key) {
106
+ if e.modelEntity.children.count == 0 {
107
+ e.modelEntity.addChild(windowAttachment, preservingWorldTransform: false)
108
+
109
+ // Scale the window to fit the resolution to unit ratio as defined by setResolution API
110
+ let b = windowAttachment.attachment.bounds
111
+ let wv = e.getComponent(SpatialWindowComponent.self)!
112
+ let scaleFact = (Float(wv.resolutionX) / 1360.0) / (b.max.x - b.min.x)
113
+ windowAttachment.scale.x = scaleFact
114
+ windowAttachment.scale.y = scaleFact
115
+ windowAttachment.scale.z = scaleFact
116
+ }
117
+ }
118
+ }
119
+ }
120
+ if viewComponent.isPortal {
121
+ // Setup portal
122
+ portal.components.set(portalModel)
123
+ portal.transform.translation.z = 0.0001 // avoid z fighting
124
+ if !portal.components.has(PortalComponent.self) {
125
+ portal.components.set(PortalComponent(target: world))
126
+ }
127
+
128
+ // Setup default light
129
+ light.light.intensity = 5000
130
+ light.position.z = 2
131
+ light.position.y = 1
132
+ light.position.x = 0.5
133
+ world.addChild(light)
134
+
135
+ // Position volume behind portal instead of in front
136
+ world.transform.translation.z *= -1
137
+
138
+ // Add portal to scene
139
+ world.components.set(worldComponent)
140
+ content.add(portal)
141
+ } else {
142
+ // Remove portal elements/components
143
+ content.remove(portal)
144
+ world.components.remove(WorldComponent.self)
145
+ world.removeChild(light)
146
+ }
147
+
148
+ content.add(world)
149
+ }
150
+ attachments: {
151
+ // Create an attachment for each window component
152
+ let entities = ent.getEntities().filter { _, entity in
153
+ entity.coordinateSpace == .APP && entity.hasComponent(SpatialWindowComponent.self)
154
+ }
155
+ ForEach(Array(entities.keys), id: \.self) { key in
156
+ let entity = entities[key]!
157
+ let wv = entity.getComponent(SpatialWindowComponent.self)!
158
+ Attachment(id: key) {
159
+ SpatialWebViewUI().environment(entity).frame(width: wv.resolutionX, height: wv.resolutionY)
160
+ }
161
+ }
162
+ }.gesture(dragGesture).if(!isRoot) { view in
163
+ view.clipped()
164
+ }
165
+ }
166
+ }
167
+ }
168
+ }
@@ -0,0 +1,187 @@
1
+ //
2
+ // SpatialWebViewUI.swift
3
+ // web-spatial
4
+ //
5
+ // Created by ByteDance on 5/9/24.
6
+ //
7
+
8
+ import RealityKit
9
+ import SwiftUI
10
+
11
+ // Using scrollview has some side effects so only use it on elements we want to clip the edges of
12
+ // Seems only scrollview has this clipping property so far on visionOS otherwise we would use ZStack
13
+ struct OptionalClip<Content: View>: View {
14
+ var clipEnabled = true
15
+ let viewBuilder: () -> Content
16
+
17
+ var body: some View {
18
+ if clipEnabled {
19
+ ScrollView {
20
+ viewBuilder()
21
+ }.offset(z: CGFloat(0)).frame(maxWidth: .infinity, maxHeight: .infinity).scrollDisabled(true)
22
+ } else {
23
+ viewBuilder()
24
+ }
25
+ }
26
+ }
27
+
28
+ struct SpatialWebViewUI: View {
29
+ @Environment(SpatialEntity.self) var ent: SpatialEntity
30
+ var body: some View {
31
+ if let wv = ent.getComponent(SpatialWindowComponent.self) {
32
+ let parentYOffset = Float(wv.scrollOffset.y)
33
+
34
+ let childEntities = ent.getEntities()
35
+
36
+ // Display child entities of the webview
37
+ VStack(alignment: .trailing, spacing: 20) {
38
+ if wv.isRootWebview() {
39
+ NavView(swc: wv)
40
+ }
41
+
42
+ ZStack {
43
+ OptionalClip(clipEnabled: ent.coordinateSpace != .ROOT && wv.isScrollEnabled()) {
44
+ ZStack {
45
+ ForEach(Array(childEntities.keys), id: \.self) { key in
46
+ if let e = childEntities[key] {
47
+ let _ = e.forceUpdate ? 0 : 0
48
+ if let childWindowcomponent = e.getComponent(SpatialWindowComponent.self) {
49
+ if e.coordinateSpace == .DOM {
50
+ let view = childWindowcomponent
51
+ let x = CGFloat(e.modelEntity.position.x)
52
+ let y = CGFloat(e.modelEntity.position.y - (view.scrollWithParent ? parentYOffset : 0))
53
+ let z = CGFloat(e.modelEntity.position.z)
54
+ let width = CGFloat(view.resolutionX)
55
+ let height = CGFloat(view.resolutionY)
56
+ let anchor = view.rotationAnchor
57
+
58
+ // Matrix = MTranslate X MRotate X MScale
59
+ SpatialWebViewUI().environment(e)
60
+ .frame(width: width, height: height)
61
+ // use .offset(smallVal) to workaround for glassEffect not working and small width/height spatialDiv not working
62
+ .offset(z: 0.0001)
63
+ .scaleEffect(
64
+ x: CGFloat(e.modelEntity.scale.x),
65
+ y: CGFloat(e.modelEntity.scale.y),
66
+ z: CGFloat(e.modelEntity.scale.z),
67
+ anchor: anchor
68
+ )
69
+ .rotation3DEffect(
70
+ Rotation3D(simd_quatf(
71
+ ix: e.modelEntity.orientation.vector.x,
72
+ iy: e.modelEntity.orientation.vector.y,
73
+ iz: e.modelEntity.orientation.vector.z,
74
+ r: e.modelEntity.orientation.vector.w
75
+ )),
76
+ anchor: anchor
77
+ )
78
+
79
+ .position(x: x, y: y)
80
+ .offset(z: z)
81
+ .zIndex(e.zIndex)
82
+ .gesture(
83
+ DragGesture()
84
+ .onChanged { gesture in
85
+ let scrollEnabled = view.isScrollEnabled()
86
+ if !scrollEnabled, wv.isScrollEnabled() {
87
+ if !view.dragStarted {
88
+ view.dragStarted = true
89
+ view.dragStart = (gesture.translation.height)
90
+ }
91
+
92
+ // TODO: this should have velocity
93
+ let delta = view.dragStart - gesture.translation.height
94
+ view.dragStart = gesture.translation.height
95
+ wv.updateScrollOffset(delta: delta)
96
+ }
97
+ }
98
+ .onEnded { _ in
99
+ let scrollEnabled = view.isScrollEnabled()
100
+ if !scrollEnabled, wv.isScrollEnabled() {
101
+ view.dragStarted = false
102
+ view.dragStart = 0
103
+
104
+ wv.stopScrolling()
105
+ }
106
+ }
107
+ )
108
+ }
109
+ }
110
+ }
111
+ }
112
+
113
+ // Model3D content
114
+ ForEach(Array(childEntities.keys), id: \.self) { key in
115
+ if let e = childEntities[key] {
116
+ let _ = e.forceUpdate ? 0 : 0
117
+ SpatialModel3DView(parentYOffset: parentYOffset)
118
+ .environment(e)
119
+ }
120
+ }
121
+
122
+ // SpatialView content
123
+ ForEach(Array(childEntities.keys), id: \.self) { key in
124
+ if let e = childEntities[key] {
125
+ if e.coordinateSpace == .DOM {
126
+ if let viewComponent = e.getComponent(SpatialViewComponent.self) {
127
+ let _ = e.forceUpdate ? 0 : 0
128
+ let x = CGFloat(e.modelEntity.position.x)
129
+ let y = CGFloat(e.modelEntity.position.y - parentYOffset)
130
+ let z = CGFloat(e.modelEntity.position.z)
131
+
132
+ let width = CGFloat(viewComponent.resolutionX)
133
+ let height = CGFloat(viewComponent.resolutionY)
134
+
135
+ SpatialViewUI().environment(e).frame(width: width, height: height).scaleEffect(
136
+ x: CGFloat(e.modelEntity.scale.x),
137
+ y: CGFloat(e.modelEntity.scale.y),
138
+ z: CGFloat(e.modelEntity.scale.z)
139
+ )
140
+ .rotation3DEffect(
141
+ Rotation3D(simd_quatf(
142
+ ix: e.modelEntity.orientation.vector.x,
143
+ iy: e.modelEntity.orientation.vector.y,
144
+ iz: e.modelEntity.orientation.vector.z,
145
+ r: e.modelEntity.orientation.vector.w
146
+ ))
147
+ ).position(x: x, y: y)
148
+ .offset(z: z)
149
+ }
150
+ }
151
+ }
152
+ }
153
+ }.frame(maxWidth: .infinity, maxHeight: .infinity).frame(maxDepth: 0, alignment: .back).offset(z: 0)
154
+ }
155
+
156
+ // Display the main webview
157
+ if wv.didFailLoad {
158
+ VStack {
159
+ Text("Failed to load webpage. Is the server running?")
160
+ .foregroundColor(.white)
161
+ Button("Reload") {
162
+ if let url = wv.getURL() {
163
+ wv.navigateToURL(url: url)
164
+ } else {
165
+ logger.warning("Unable to reload URL")
166
+ }
167
+ }
168
+ .foregroundColor(.white)
169
+ }
170
+ .frame(maxWidth: .infinity, maxHeight: .infinity).glassBackgroundEffect()
171
+
172
+ } else {
173
+ wv.getView()
174
+ .materialWithBorderCorner(
175
+ wv.backgroundMaterial,
176
+ wv.cornerRadius
177
+ )
178
+
179
+ .frame(maxWidth: .infinity, maxHeight: .infinity)
180
+ }
181
+ }
182
+ .opacity(wv.opacity)
183
+ .hidden(!ent.visible)
184
+ }
185
+ }
186
+ }
187
+ }
@@ -0,0 +1,34 @@
1
+ //
2
+ // VolumetricWindowContainerView.swift
3
+ // web-spatial
4
+ //
5
+ // Created by ByteDance on 5/9/24.
6
+ //
7
+
8
+ import typealias RealityKit.Attachment
9
+ import typealias RealityKit.Entity
10
+ import typealias RealityKit.MeshResource
11
+ import typealias RealityKit.Model3D
12
+ import typealias RealityKit.ModelEntity
13
+ import typealias RealityKit.RealityView
14
+ import typealias RealityKit.SimpleMaterial
15
+ import SwiftUI
16
+
17
+ struct VolumetricWindowContainerView: View {
18
+ @Environment(SpatialWindowContainer.self) var windowContainerContent: SpatialWindowContainer
19
+
20
+ var body: some View {
21
+ OpenDismissHandlerUI().environment(windowContainerContent).onDisappear {
22
+ windowContainerContent.destroy()
23
+ }
24
+
25
+ let entities = windowContainerContent.getEntities().filter { _, entity in
26
+ entity.coordinateSpace == .ROOT && entity.hasComponent(SpatialViewComponent.self)
27
+ }
28
+
29
+ ForEach(Array(entities.keys), id: \.self) { key in
30
+ let entity = entities[key]!
31
+ SpatialViewUI(isRoot: true).environment(entity)
32
+ }
33
+ }
34
+ }
@@ -0,0 +1,88 @@
1
+ //
2
+ // NavView.swift
3
+ // web-spatial
4
+ //
5
+ // Created by ByteDance on 2025/1/8.
6
+ //
7
+
8
+ import SwiftUI
9
+ import WebKit
10
+
11
+ struct NavView: View {
12
+ @State var swc: SpatialWindowComponent?
13
+ @State var showUrl: Bool = false
14
+ @State private var showCopyTip = false
15
+
16
+ var body: some View {
17
+ ZStack(alignment: .bottomTrailing) {
18
+ VStack(alignment: .trailing, spacing: 0) {
19
+ HStack(spacing: 10) {
20
+ Text(pwaManager.name).padding(.trailing, 10)
21
+ if pwaManager.display == .minimal {
22
+ Button(action: {
23
+ swc?.goBack()
24
+ }, label: {
25
+ Image(systemName: "arrow.left")
26
+ })
27
+ .disabled(!swc!.canGoBack)
28
+ Button(action: {
29
+ swc?.goForward()
30
+ }, label: {
31
+ Image(systemName: "arrow.right")
32
+ })
33
+ .disabled(!swc!.canGoForward)
34
+ Button(action: {
35
+ swc?.reload()
36
+ }, label: {
37
+ Image(systemName: "arrow.clockwise")
38
+ })
39
+ Button(
40
+ action: {
41
+ swc?
42
+ .navigateToURL(
43
+ url: URL(string: pwaManager.start_url)!
44
+ )
45
+ },
46
+ label: {
47
+ Image(systemName: "house.fill")
48
+ }
49
+ )
50
+ }
51
+ Button(action: {
52
+ showUrl.toggle()
53
+ }, label: {
54
+ Image(systemName: "info.circle")
55
+ })
56
+ }
57
+ }.padding().glassBackgroundEffect(in: .rect).cornerRadius(15)
58
+ HStack(spacing: 0) {
59
+ Text(
60
+ swc?.getURL()?.absoluteString ?? ""
61
+ )
62
+ .padding()
63
+ Button(action: {
64
+ UIPasteboard.general.string = swc?.getURL()?.absoluteString ?? ""
65
+ showCopyTip = true
66
+ DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
67
+ showCopyTip = false
68
+ }
69
+ }, label: {
70
+ Text("copy")
71
+ }).padding(.trailing, 5)
72
+
73
+ Button(action: {
74
+ showUrl = false
75
+ }, label: {
76
+ Text("X")
77
+ })
78
+ }.glassBackgroundEffect(in: .rect).cornerRadius(15).offset(y: 35).opacity(showUrl ? 1 : 0).animation(.easeOut, value: 0.2)
79
+ }
80
+ .zIndex(10) // closer to user
81
+ .popover(isPresented: $showCopyTip) {
82
+ Text("copied!")
83
+ .padding()
84
+ .background(.ultraThinMaterial)
85
+ .cornerRadius(10)
86
+ }
87
+ }
88
+ }
@@ -0,0 +1,134 @@
1
+ //
2
+ // web_spatialApp.swift
3
+ // web-spatial
4
+ //
5
+ // Created by ByteDance on 5/8/24.
6
+ //
7
+
8
+ import typealias RealityKit.Attachment
9
+ import typealias RealityKit.Entity
10
+ import typealias RealityKit.MeshResource
11
+ import typealias RealityKit.Model3D
12
+ import typealias RealityKit.ModelEntity
13
+ import typealias RealityKit.RealityView
14
+ import typealias RealityKit.SimpleMaterial
15
+ import SwiftUI
16
+
17
+ let logger = Logger()
18
+
19
+ // To load a local path, remove http:// eg. "static-web/"
20
+ let nativeAPIVersion = "0.0.1"
21
+
22
+ // detect when app properties like defaultSize change so we can avoid race condition of setting default values and then opening window container
23
+ var sceneStateChangedCB: ((Any) -> Void) = { _ in
24
+ }
25
+
26
+ @main
27
+ struct web_spatialApp: App {
28
+ @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
29
+ @State var root: SpatialWindowComponent? = nil
30
+ @State var rootWGD: SpatialWindowContainer
31
+ @State var initialLaunch = true
32
+
33
+ @ObservedObject var wgm = WindowContainerMgr.Instance
34
+
35
+ @Environment(\.scenePhase) private var scenePhase
36
+
37
+ init() {
38
+ logger.debug("WebSpatial App Started --------")
39
+
40
+ // init global logger
41
+ Logger.initLogger()
42
+
43
+ // init pwa manager
44
+ pwaManager._init()
45
+
46
+ // create root SpatialWindowContainer and Immersive SpatialWindowContainer
47
+ rootWGD = SpatialWindowContainer.createRootWindowContainer()
48
+ let _ = SpatialWindowContainer.createImmersiveWindowContainer()
49
+ }
50
+
51
+ func getFileUrl() -> URL {
52
+ return URL(string: pwaManager.start_url)!
53
+ }
54
+
55
+ // There seems to be a bug in WKWebView where it needs to be initialized after the app has loaded so we do this here instead of init()
56
+ // https://forums.developer.apple.com/forums/thread/61432
57
+ func initAppOnViewMount() {
58
+ if root == nil {
59
+ let fileUrl = getFileUrl()
60
+
61
+ // Create a default entity with webview resource
62
+ let rootEntity = SpatialEntity()
63
+ rootEntity.coordinateSpace = CoordinateSpaceMode.ROOT
64
+ let windowComponent = SpatialWindowComponent(parentWindowContainerID: rootWGD.id, url: fileUrl)
65
+ rootEntity.addComponent(windowComponent)
66
+ rootEntity.setParentWindowContainer(wg: rootWGD)
67
+
68
+ root = windowComponent
69
+ }
70
+ }
71
+
72
+ func getDefaultSize() -> CGSize {
73
+ sceneStateChangedCB("")
74
+ return wgm.getValue().defaultSize!
75
+ }
76
+
77
+ var body: some Scene {
78
+ WindowGroup(id: "Plain", for: WindowContainerData.self) { $windowData in
79
+ if windowData.windowContainerID == SpatialWindowContainer.getRootID() {
80
+ VStack {}.onAppear { initAppOnViewMount() }
81
+
82
+ PlainWindowContainerView().environment(rootWGD).background(Color.clear.opacity(0)).onOpenURL { myURL in
83
+ initAppOnViewMount()
84
+ let urlToLoad = pwaManager.checkInDeeplink(url: myURL.absoluteString)
85
+
86
+ if let url = URL(string: urlToLoad) {
87
+ root!.navigateToURL(url: url)
88
+ }
89
+ }
90
+ } else {
91
+ let wg = SpatialWindowContainer.getOrCreateSpatialWindowContainer(
92
+ windowData.windowContainerID, windowData
93
+ )
94
+ PlainWindowContainerView().environment(wg)
95
+ // https://stackoverflow.com/questions/78567737/how-to-get-initial-windowgroup-to-reopen-on-launch-visionos
96
+ .handlesExternalEvents(preferring: [], allowing: [])
97
+ }
98
+ } defaultValue: {
99
+ WindowContainerData(windowStyle: "Plain", windowContainerID: SpatialWindowContainer.getRootID())
100
+
101
+ }.windowStyle(.plain).onChange(of: scenePhase) { oldPhase, newPhase in
102
+ if oldPhase == .background && newPhase == .inactive {
103
+ if initialLaunch {
104
+ // App initial open
105
+ initialLaunch = false
106
+ } else {
107
+ // App reopened
108
+ let fileUrl = getFileUrl()
109
+ root?.navigateToURL(url: fileUrl)
110
+ rootWGD.setSize.send(DefaultPlainWindowContainerSize)
111
+ }
112
+ }
113
+ }.defaultSize(
114
+ getDefaultSize()
115
+ ).windowResizability(
116
+ wgm.getValue().windowResizability!
117
+ )
118
+
119
+ WindowGroup(id: "Volumetric", for: WindowContainerData.self) { $windowData in
120
+ let wg = SpatialWindowContainer.getOrCreateSpatialWindowContainer(windowData!.windowContainerID, windowData!)
121
+ VolumetricWindowContainerView().environment(wg).handlesExternalEvents(preferring: [], allowing: [])
122
+
123
+ }.windowStyle(.volumetric).defaultSize(width: 1, height: 1, depth: 1, in: .meters)
124
+
125
+ ImmersiveSpace(id: "ImmersiveSpace") {
126
+ let wg = SpatialWindowContainer.getImmersiveWindowContainer()
127
+ VolumetricWindowContainerView().environment(wg).handlesExternalEvents(preferring: [], allowing: [])
128
+ }
129
+
130
+ WindowGroup(id: "loading") {
131
+ LoadingView()
132
+ }
133
+ }
134
+ }