基于ARKit的AR尺子Demo代码分析

Last updated on May 7, 2023 pm

  • Code: https://github.com/levantAJ/Measure

  • UIViewController

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    final class ViewController: UIViewController {
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    resetValues()
    isMeasuring = true
    targetImageView.image = UIImage(named: "targetGreen")
    }

    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    isMeasuring = false
    targetImageView.image = UIImage(named: "targetWhite")
    if let line = currentLine {
    lines.append(line)
    currentLine = nil
    resetButton.isHidden = false
    resetImageView.isHidden = false
    }
    }
    }

  • compute distance

    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
    extension ViewController: ARSCNViewDelegate {
    func renderer(_ renderer: SCNSceneRenderer, updateAtTime time: TimeInterval) {
    DispatchQueue.main.async { [weak self] in
    self?.detectObjects()
    }
    }
    }

    extension ViewController {
    fileprivate func detectObjects() {
    guard let worldPosition =
    sceneView.realWorldVector(screenPosition: view.center) else { return }
    targetImageView.isHidden = false
    meterImageView.isHidden = false
    if lines.isEmpty {
    messageLabel.text = "Hold screen & move your phone…"
    }
    loadingView.stopAnimating()
    if isMeasuring {
    if startValue == vectorZero {
    startValue = worldPosition
    currentLine = Line(sceneView: sceneView, startVector: startValue, unit: unit)
    }
    endValue = worldPosition
    currentLine?.update(to: endValue)
    messageLabel.text = currentLine?.distance(to: endValue) ?? "Calculating…"
    }
    }
    }

    extension ARSCNView {
    func realWorldVector(screenPosition: CGPoint) -> SCNVector3? {
    let results = self.hitTest(screenPosition, types: [.featurePoint])
    guard let result = results.first else { return nil }
    return SCNVector3.positionFromTransform(result.worldTransform)
    }
    }

    final class Line {
    func distance(to vector: SCNVector3) -> String {
    return String(format: "%.2f%@", startVector.distance(from: vector) * unit.fator, unit.unit)
    }
    }


基于ARKit的AR尺子Demo代码分析
https://cgabc.xyz/posts/f4d3f6b7/
Author
Gavin Gao
Posted on
December 17, 2021
Licensed under