-
Notifications
You must be signed in to change notification settings - Fork 10
/
FocusNode.swift
388 lines (320 loc) · 11.5 KB
/
FocusNode.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
//
// FocusNode.swift
// FocusSquare
//
// Created by Max Cobb on 11/28/18.
// Copyright © 2018 Max Cobb. All rights reserved.
//
import ARKit
import SmartHitTest
private extension UIView {
/// Center of the view
var screenCenter: CGPoint {
let bounds = self.bounds
return CGPoint(x: bounds.midX, y: bounds.midY)
}
}
/**
An `SCNNode` which is used to provide uses with visual cues about the status of ARKit world tracking.
- Tag: FocusSquare
*/
open class FocusNode: SCNNode {
weak public var viewDelegate: ARSmartHitTest?
// MARK: - Types
public enum State: Equatable {
case initializing
case detecting(hitTestResult: ARHitTestResult, camera: ARCamera?)
}
var screenCenter: CGPoint?
// MARK: - Properties
/// The most recent position of the focus square based on the current state.
var lastPosition: SIMD3<Float>? {
switch state {
case .initializing: return nil
case .detecting(let hitTestResult, _): return hitTestResult.worldTransform.translation
}
}
fileprivate func nodeOffPlane(_ hitTestResult: ARHitTestResult, _ camera: ARCamera?) {
self.onPlane = false
displayOffPlane(for: hitTestResult, camera: camera)
}
public var state: State = .initializing {
didSet {
guard state != oldValue else { return }
switch state {
case .initializing:
displayAsBillboard()
case let .detecting(hitTestResult, camera):
if let planeAnchor = hitTestResult.anchor as? ARPlaneAnchor {
nodeOnPlane(for: hitTestResult, planeAnchor: planeAnchor, camera: camera)
currentPlaneAnchor = planeAnchor
} else {
nodeOffPlane(hitTestResult, camera)
currentPlaneAnchor = nil
}
}
}
}
public var onPlane: Bool = false
/// Indicates if the square is currently being animated.
public var isAnimating = false
/// Indicates if the square is currently changing its alignment.
private var isChangingAlignment = false
/// The focus square's current alignment.
private var currentAlignment: ARPlaneAnchor.Alignment?
/// The current plane anchor if the focus square is on a plane.
private(set) var currentPlaneAnchor: ARPlaneAnchor?
/// The focus square's most recent positions.
private var recentFocusNodePositions: [SIMD3<Float>] = []
/// The focus square's most recent alignments.
private(set) var recentFocusNodeAlignments: [ARPlaneAnchor.Alignment] = []
/// Previously visited plane anchors.
private var anchorsOfVisitedPlanes: Set<ARAnchor> = []
/// The primary node that controls the position of other `FocusSquare` nodes.
public let positioningNode = SCNNode()
public var scaleNodeBasedOnDistance = true {
didSet {
if self.scaleNodeBasedOnDistance == false {
self.simdScale = SIMD3<Float>([1, 1, 1])
}
}
}
// MARK: - Initialization
public override init() {
super.init()
self.positioningNode.eulerAngles.x = .pi / 2 // Horizontal
// Always render focus square on top of other content.
self.displayNodeHierarchyOnTop(true)
addChildNode(self.positioningNode)
// Start the focus square as a billboard.
self.displayAsBillboard()
}
required public init?(coder aDecoder: NSCoder) {
fatalError("\(#function) has not been implemented")
}
// MARK: - Appearance
/// Hides the focus square.
func hide() {
guard action(forKey: "hide") == nil else { return }
displayNodeHierarchyOnTop(false)
runAction(.fadeOut(duration: 0.5), forKey: "hide")
}
/// Unhides the focus square.
func unhide() {
guard action(forKey: "unhide") == nil else { return }
displayNodeHierarchyOnTop(true)
runAction(.fadeIn(duration: 0.5), forKey: "unhide")
}
/// Displays the focus square parallel to the camera plane.
private func displayAsBillboard() {
self.onPlane = false
simdTransform = matrix_identity_float4x4
eulerAngles.x = .pi / 2
simdPosition = SIMD3<Float>(0, 0, -0.8)
unhide()
stateChangedSetup()
}
/// Called when a surface has been detected.
private func displayOffPlane(for hitTestResult: ARHitTestResult, camera: ARCamera?) {
self.stateChangedSetup()
let position = hitTestResult.worldTransform.translation
recentFocusNodePositions.append(position)
updateTransform(for: position, hitTestResult: hitTestResult, camera: camera)
}
/// Called when a plane has been detected.
private func nodeOnPlane(for hitTestResult: ARHitTestResult, planeAnchor: ARPlaneAnchor, camera: ARCamera?) {
self.onPlane = true
self.stateChangedSetup(newPlane: !anchorsOfVisitedPlanes.contains(planeAnchor))
anchorsOfVisitedPlanes.insert(planeAnchor)
let position = hitTestResult.worldTransform.translation
recentFocusNodePositions.append(position)
updateTransform(for: position, hitTestResult: hitTestResult, camera: camera)
}
// MARK: Helper Methods
/// Update the transform of the focus square to be aligned with the camera.
private func updateTransform(for position: SIMD3<Float>, hitTestResult: ARHitTestResult, camera: ARCamera?) {
// Average using several most recent positions.
recentFocusNodePositions = Array(recentFocusNodePositions.suffix(10))
// Move to average of recent positions to avoid jitter.
let average = recentFocusNodePositions.reduce(
SIMD3<Float>(repeating: 0), { $0 + $1 }
) / Float(recentFocusNodePositions.count)
self.simdPosition = average
if self.scaleNodeBasedOnDistance {
self.simdScale = SIMD3<Float>(repeating: scaleBasedOnDistance(camera: camera))
}
// Correct y rotation of camera square.
guard let camera = camera else { return }
let tilt = abs(camera.eulerAngles.x)
let threshold1: Float = .pi / 2 * 0.65
let threshold2: Float = .pi / 2 * 0.75
let yaw = atan2f(camera.transform.columns.0.x, camera.transform.columns.1.x)
var angle: Float = 0
switch tilt {
case 0..<threshold1:
angle = camera.eulerAngles.y
case threshold1..<threshold2:
let relativeInRange = abs((tilt - threshold1) / (threshold2 - threshold1))
let normalizedY = normalize(camera.eulerAngles.y, forMinimalRotationTo: yaw)
angle = normalizedY * (1 - relativeInRange) + yaw * relativeInRange
default:
angle = yaw
}
if state != .initializing {
updateAlignment(for: hitTestResult, yRotationAngle: angle)
}
}
private func updateAlignment(for hitTestResult: ARHitTestResult, yRotationAngle angle: Float) {
// Abort if an animation is currently in progress.
if isChangingAlignment {
return
}
var shouldAnimateAlignmentChange = false
let tempNode = SCNNode()
tempNode.simdRotation = SIMD4<Float>(0, 1, 0, angle)
// Determine current alignment
var alignment: ARPlaneAnchor.Alignment?
if let planeAnchor = hitTestResult.anchor as? ARPlaneAnchor {
alignment = planeAnchor.alignment
} else if hitTestResult.type == .estimatedHorizontalPlane {
alignment = .horizontal
} else if hitTestResult.type == .estimatedVerticalPlane {
alignment = .vertical
}
// add to list of recent alignments
if alignment != nil {
self.recentFocusNodeAlignments.append(alignment!)
}
// Average using several most recent alignments.
self.recentFocusNodeAlignments = Array(self.recentFocusNodeAlignments.suffix(20))
let horizontalHistory = recentFocusNodeAlignments.filter({ $0 == .horizontal }).count
let verticalHistory = recentFocusNodeAlignments.filter({ $0 == .vertical }).count
// Alignment is same as most of the history - change it
if alignment == .horizontal && horizontalHistory > 15 ||
alignment == .vertical && verticalHistory > 10 ||
hitTestResult.anchor is ARPlaneAnchor {
if alignment != self.currentAlignment {
shouldAnimateAlignmentChange = true
self.currentAlignment = alignment
self.recentFocusNodeAlignments.removeAll()
}
} else {
// Alignment is different than most of the history - ignore it
alignment = self.currentAlignment
return
}
if alignment == .vertical {
tempNode.simdOrientation = hitTestResult.worldTransform.orientation
shouldAnimateAlignmentChange = true
}
// Change the focus square's alignment
if shouldAnimateAlignmentChange {
performAlignmentAnimation(to: tempNode.simdOrientation)
} else {
simdOrientation = tempNode.simdOrientation
}
}
private func normalize(_ angle: Float, forMinimalRotationTo ref: Float) -> Float {
// Normalize angle in steps of 90 degrees such that the rotation to the other angle is minimal
var normalized = angle
while abs(normalized - ref) > .pi / 4 {
if angle > ref {
normalized -= .pi / 2
} else {
normalized += .pi / 2
}
}
return normalized
}
/**
Reduce visual size change with distance by scaling up when close and down when far away.
These adjustments result in a scale of 1.0x for a distance of 0.7 m or less
(estimated distance when looking at a table), and a scale of 1.2x
for a distance 1.5 m distance (estimated distance when looking at the floor).
*/
private func scaleBasedOnDistance(camera: ARCamera?) -> Float {
guard let camera = camera else { return 1.0 }
let distanceFromCamera = simd_length(simdWorldPosition - camera.transform.translation)
if distanceFromCamera < 0.7 {
return distanceFromCamera / 0.7
} else {
return 0.25 * distanceFromCamera + 0.825
}
}
// MARK: Animations
/// Called whenever the state of the focus node changes
///
/// - Parameter newPlane: If the node is directly on a plane, is it a new plane to track
public func stateChanged(newPlane: Bool = false) {
if self.onPlane {
/// Used when the node is tracking directly on a plane
} else {
/// Used when the node is tracking, but is estimating the plane
}
isAnimating = false
}
private func stateChangedSetup(newPlane: Bool = false) {
guard !isAnimating else { return }
isAnimating = true
self.stateChanged(newPlane: newPlane)
}
private func performAlignmentAnimation(to newOrientation: simd_quatf) {
self.isChangingAlignment = true
SCNTransaction.begin()
SCNTransaction.completionBlock = {
self.isChangingAlignment = false
}
SCNTransaction.animationDuration = 0.3
SCNTransaction.animationTimingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeInEaseOut)
simdOrientation = newOrientation
SCNTransaction.commit()
}
// MARK: Convenience Methods
/// Sets the rendering order of the `positioningNode` to show on top or under other scene content.
func displayNodeHierarchyOnTop(_ isOnTop: Bool) {
// Recursivley traverses the node's children to update the rendering order depending on the `isOnTop` parameter.
func updateRenderOrder(for node: SCNNode) {
node.renderingOrder = isOnTop ? 2 : 0
for material in node.geometry?.materials ?? [] {
material.readsFromDepthBuffer = !isOnTop
}
for child in node.childNodes {
updateRenderOrder(for: child)
}
}
updateRenderOrder(for: self.positioningNode)
}
public func updateFocusNode() {
guard let view = self.viewDelegate as? (ARSCNView & ARSmartHitTest) else {
print("FocusNode viewDelegate must be an ARSCNView for now")
return
}
// Perform hit testing only when ARKit tracking is in a good state.
guard let camera = view.session.currentFrame?.camera, case .normal = camera.trackingState else {
self.state = .initializing
view.pointOfView?.addChildNode(self)
return
}
var result: ARHitTestResult?
if !Thread.isMainThread {
if let center = self.screenCenter {
result = view.smartHitTest(center)
} else {
DispatchQueue.main.async {
self.screenCenter = view.screenCenter
self.updateFocusNode()
}
return
}
} else {
result = view.smartHitTest(view.screenCenter)
}
if let result = result {
view.scene.rootNode.addChildNode(self)
self.state = .detecting(hitTestResult: result, camera: camera)
} else {
self.state = .initializing
view.pointOfView?.addChildNode(self)
}
}
}