-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathFlyoverSceneView.swift
198 lines (185 loc) · 7.91 KB
/
FlyoverSceneView.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// Copyright 2023 Esri
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import ARKit
import SwiftUI
import ArcGIS
/// A scene view that provides an augmented reality fly over experience.
@available(macCatalyst, unavailable)
@available(visionOS, unavailable)
public struct FlyoverSceneView: View {
#if os(iOS)
/// The AR session.
@StateObject private var session = ObservableARSession()
#endif
/// The initial camera.
let initialCamera: Camera
/// The translation factor.
let translationFactor: Double
/// A Boolean value indicating whether to orient the scene view's initial heading to compass heading.
let shouldOrientToCompass: Bool
/// The closure that builds the scene view.
private let sceneViewBuilder: (SceneViewProxy) -> SceneView
/// The camera controller that we will set on the scene view.
@State private var cameraController: TransformationMatrixCameraController
/// The current interface orientation.
@State private var interfaceOrientation: InterfaceOrientation?
/// Creates a fly over scene view.
/// - Parameters:
/// - initialLatitude: The initial latitude of the scene view's camera.
/// - initialLongitude: The initial longitude of the scene view's camera.
/// - initialAltitude: The initial altitude of the scene view's camera.
/// - translationFactor: The translation factor that defines how much the scene view translates
/// as the device moves.
/// - initialHeading: The initial heading of the scene view's camera. A value of `nil` means
/// the scene view's heading will be initially oriented to compass heading.
/// - sceneView: A closure that builds the scene view to be overlayed on top of the
/// augmented reality video feed.
/// - Remark: The provided scene view will have certain properties overridden in order to
/// be effectively viewed in augmented reality. One such property is the camera controller.
public init(
initialLatitude: Double,
initialLongitude: Double,
initialAltitude: Double,
translationFactor: Double,
initialHeading: Double? = nil,
@ViewBuilder sceneView: @escaping (SceneViewProxy) -> SceneView
) {
let camera = Camera(
latitude: initialLatitude,
longitude: initialLongitude,
altitude: initialAltitude,
heading: initialHeading ?? 0,
pitch: 90,
roll: 0
)
self.init(
initialCamera: camera,
translationFactor: translationFactor,
shouldOrientToCompass: initialHeading == nil,
sceneView: sceneView
)
}
/// Creates a fly over scene view.
/// - Parameters:
/// - initialLocation: The initial location of the scene view's camera.
/// - translationFactor: The translation factor that defines how much the scene view translates
/// as the device moves.
/// - initialHeading: The initial heading of the scene view's camera. A value of `nil` means
/// the scene view's heading will be initially oriented to compass heading.
/// - sceneView: A closure that builds the scene view to be overlayed on top of the
/// augmented reality video feed.
/// - Remark: The provided scene view will have certain properties overridden in order to
/// be effectively viewed in augmented reality. One such property is the camera controller.
public init(
initialLocation: Point,
translationFactor: Double,
initialHeading: Double? = nil,
@ViewBuilder sceneView: @escaping (SceneViewProxy) -> SceneView
) {
let camera = Camera(
location: initialLocation,
heading: initialHeading ?? 0,
pitch: 90,
roll: 0
)
self.init(
initialCamera: camera,
translationFactor: translationFactor,
shouldOrientToCompass: initialHeading == nil,
sceneView: sceneView
)
}
/// Creates a fly over scene view.
/// - Parameters:
/// - initialCamera: The initial camera.
/// - translationFactor: The translation factor that defines how much the scene view translates
/// as the device moves.
/// - shouldOrientToCompass: A Boolean value indicating whether to orient the scene view's
/// initial heading to compass heading.
/// - sceneView: A closure that builds the scene view to be overlayed on top of the
/// augmented reality video feed.
/// - Remark: The provided scene view will have certain properties overridden in order to
/// be effectively viewed in augmented reality. One such property is the camera controller.
private init(
initialCamera: Camera,
translationFactor: Double,
shouldOrientToCompass: Bool,
@ViewBuilder sceneView: @escaping (SceneViewProxy) -> SceneView
) {
self.sceneViewBuilder = sceneView
self.shouldOrientToCompass = shouldOrientToCompass
self.translationFactor = translationFactor
self.initialCamera = initialCamera
let cameraController = TransformationMatrixCameraController(originCamera: initialCamera)
cameraController.translationFactor = translationFactor
_cameraController = .init(initialValue: cameraController)
}
public var body: some View {
SceneViewReader { sceneViewProxy in
sceneViewBuilder(sceneViewProxy)
.cameraController(cameraController)
#if os(iOS)
.onAppear {
let configuration = ARPositionalTrackingConfiguration()
if shouldOrientToCompass {
configuration.worldAlignment = .gravityAndHeading
}
session.start(configuration: configuration)
}
.onDisappear { session.pause() }
.onChange(session.currentFrame) { frame in
guard let frame, let interfaceOrientation else { return }
sceneViewProxy.updateCamera(
frame: frame,
cameraController: cameraController,
orientation: interfaceOrientation
)
}
#endif
.onChange(initialCamera) { initialCamera in
cameraController.originCamera = initialCamera
}
.onChange(translationFactor) { translationFactor in
cameraController.translationFactor = translationFactor
}
.observingInterfaceOrientation($interfaceOrientation)
}
}
}
#if os(iOS)
/// An observable object that wraps an `ARSession` and provides the current frame.
private class ObservableARSession: NSObject, ObservableObject, ARSessionDelegate {
/// The backing AR session.
private let session = ARSession()
override init() {
super.init()
session.delegate = self
}
/// Starts the AR session by running a given configuration.
/// - Parameter configuration: The AR configuration to run.
func start(configuration: ARConfiguration) {
session.run(configuration)
}
/// Pauses the AR session.
func pause() {
session.pause()
}
/// The latest AR frame.
@Published
var currentFrame: ARFrame?
func session(_ session: ARSession, didUpdate frame: ARFrame) {
currentFrame = frame
}
}
#endif