-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAugmentedRealityView.swift
253 lines (220 loc) · 8.69 KB
/
AugmentedRealityView.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
//
// AugmentedRealityView.swift
// SwiftPlaygrounds
//
// Created by A. Zheng (github.com/aheze) on 1/10/22.
// Copyright © 2022 A. Zheng. All rights reserved.
//
import ARKit
import SwiftUI
class ARViewModel: ObservableObject {
var addText: (() -> Void)?
var addSphere: (() -> Void)?
var addSprinkler: (() -> Void)?
var clear: (() -> Void)?
var resume: ((Bool) -> Void)?
}
struct AugmentedRealityButton: View {
var image: String
var text: String
let action: () -> Void
var body: some View {
Button(action: action) {
VStack(spacing: 10) {
Image(systemName: image)
.foregroundColor(.purple)
.font(.title2)
Text(text)
.foregroundColor(Color(uiColor: .label))
}
.font(.title3)
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(Color(uiColor: .systemBackground))
.cornerRadius(16)
}
}
}
struct AugmentedRealityView: View {
let focused: Bool
@StateObject var model = ARViewModel()
var body: some View {
VStack(spacing: 0) {
ARViewControllerRepresentable(model: model)
.ignoresSafeArea()
.overlay(alignment: .topTrailing) {
Button {
model.clear?()
} label: {
Image(systemName: "xmark")
.foregroundColor(.white)
.font(.largeTitle)
.frame(width: 64, height: 64)
.background(.white.opacity(0.3))
.cornerRadius(32)
.padding()
}
}
HStack {
AugmentedRealityButton(image: "textformat", text: "Text") {
model.addText?()
}
AugmentedRealityButton(image: "circle.fill", text: "Sphere") {
model.addSphere?()
}
AugmentedRealityButton(image: "capsule.righthalf.filled", text: "Sprinkler") {
model.addSprinkler?()
}
}
.frame(height: 80)
.padding()
.background(
Color(uiColor: .secondarySystemBackground)
)
}
.onChange(of: focused) { newValue in
model.resume?(newValue)
}
}
}
struct ARViewControllerRepresentable: UIViewControllerRepresentable {
@ObservedObject var model: ARViewModel
func makeUIViewController(context _: Context) -> ARViewController {
return ARViewController(model: model)
}
func updateUIViewController(_: ARViewController, context _: Context) {}
}
class ARViewController: UIViewController, ARSCNViewDelegate {
var model: ARViewModel
var sceneView: ARSCNView!
var nodes = [SCNNode]()
let configuration = ARWorldTrackingConfiguration()
@available(*, unavailable)
public required init?(coder _: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
init(model: ARViewModel) {
self.model = model
configuration.planeDetection = .horizontal
super.init(nibName: nil, bundle: nil)
model.clear = { [weak self] in
guard let self = self else { return }
for node in self.nodes {
node.removeFromParentNode()
}
}
model.resume = { [weak self] resume in
guard let self = self else { return }
if resume {
self.sceneView.session.run(self.configuration)
} else {
self.sceneView.session.pause()
}
}
model.addText = { [weak self] in
guard let self = self else { return }
if let position = self.getPosition() {
let textGeometry = SCNText(string: "Coding Club", extrusionDepth: 0.8)
textGeometry.font = UIFont.systemFont(ofSize: 18, weight: .medium)
textGeometry.firstMaterial?.diffuse.contents = UIColor.systemBlue
let node = SCNNode(geometry: textGeometry)
let lookAtConstraint = SCNBillboardConstraint()
node.constraints = [lookAtConstraint]
node.position = SCNVector3(
x: position.x,
y: position.y,
z: position.z
)
node.scale = SCNVector3(0.006, 0.006, 0.006)
self.nodes.append(node)
self.sceneView.scene.rootNode.addChildNode(node)
}
}
model.addSphere = { [weak self] in
guard let self = self else { return }
if let position = self.getPosition() {
let sphereGeometry = SCNSphere(radius: 0.06)
sphereGeometry.firstMaterial?.diffuse.contents = UIColor.green
let node = SCNNode(geometry: sphereGeometry)
node.position = SCNVector3(
x: position.x,
y: position.y,
z: position.z
)
self.nodes.append(node)
self.sceneView.scene.rootNode.addChildNode(node)
}
}
model.addSprinkler = { [weak self] in
guard let self = self else { return }
if let position = self.getPosition() {
let cylinderGeometry = SCNCylinder(radius: 0.06, height: 0.1)
cylinderGeometry.firstMaterial?.diffuse.contents = UIColor.gray
let cylinderNode = SCNNode(geometry: cylinderGeometry)
cylinderNode.position = SCNVector3(
x: position.x,
y: position.y,
z: position.z
)
let particleSystem = SCNParticleSystem()
particleSystem.birthRate = 80
particleSystem.particleSize = 0.01
particleSystem.particleLifeSpan = 2
particleSystem.particleColor = .green
particleSystem.particleVelocity = 5
particleSystem.spreadingAngle = 10
particleSystem.isAffectedByGravity = true
particleSystem.particleColorVariation = SCNVector4(0.25, 0, 0, 0)
let particlesNode = SCNNode()
particlesNode.position = SCNVector3(
x: position.x,
y: position.y + 0.1,
z: position.z
)
particlesNode.addParticleSystem(particleSystem)
self.nodes.append(cylinderNode)
self.nodes.append(particlesNode)
self.sceneView.scene.rootNode.addChildNode(cylinderNode)
self.sceneView.scene.rootNode.addChildNode(particlesNode)
}
}
}
var crosshairImageView: UIImageView!
override func loadView() {
super.loadView()
view = UIView()
view.backgroundColor = .black
sceneView = ARSCNView()
view.addSubview(sceneView)
sceneView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
sceneView.topAnchor.constraint(equalTo: view.topAnchor),
sceneView.rightAnchor.constraint(equalTo: view.rightAnchor),
sceneView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
sceneView.leftAnchor.constraint(equalTo: view.leftAnchor),
])
let configuration = UIImage.SymbolConfiguration(pointSize: 36)
let image = UIImage(systemName: "plus", withConfiguration: configuration)?.withTintColor(.white, renderingMode: .alwaysOriginal)
crosshairImageView = UIImageView(image: image)
view.addSubview(crosshairImageView)
crosshairImageView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
crosshairImageView.centerXAnchor.constraint(equalTo: view.centerXAnchor),
crosshairImageView.centerYAnchor.constraint(equalTo: view.centerYAnchor),
])
sceneView.debugOptions = [ARSCNDebugOptions.showFeaturePoints]
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
sceneView.autoenablesDefaultLighting = true
sceneView.session.run(configuration)
}
func getPosition() -> simd_float4? {
let touchLocation = crosshairImageView.center
let results = sceneView.hitTest(touchLocation, types: .existingPlaneUsingExtent)
if let hitResult = results.first {
let position = hitResult.worldTransform.columns.3
return position
}
return nil
}
}